blob: 01e0dac33ee3c0631f8f16567dc9594f383727c7 [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 Jakovljevice114da22016-12-26 14:21:43 +01001136void CodeGeneratorMIPS::MarkGCCard(Register object,
1137 Register value,
1138 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001139 MipsLabel done;
1140 Register card = AT;
1141 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001142 if (value_can_be_null) {
1143 __ Beqz(value, &done);
1144 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001145 __ LoadFromOffset(kLoadWord,
1146 card,
1147 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001148 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001149 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1150 __ Addu(temp, card, temp);
1151 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001152 if (value_can_be_null) {
1153 __ Bind(&done);
1154 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001155}
1156
David Brazdil58282f42016-01-14 12:45:10 +00001157void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001158 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1159 blocked_core_registers_[ZERO] = true;
1160 blocked_core_registers_[K0] = true;
1161 blocked_core_registers_[K1] = true;
1162 blocked_core_registers_[GP] = true;
1163 blocked_core_registers_[SP] = true;
1164 blocked_core_registers_[RA] = true;
1165
1166 // AT and TMP(T8) are used as temporary/scratch registers
1167 // (similar to how AT is used by MIPS assemblers).
1168 blocked_core_registers_[AT] = true;
1169 blocked_core_registers_[TMP] = true;
1170 blocked_fpu_registers_[FTMP] = true;
1171
1172 // Reserve suspend and thread registers.
1173 blocked_core_registers_[S0] = true;
1174 blocked_core_registers_[TR] = true;
1175
1176 // Reserve T9 for function calls
1177 blocked_core_registers_[T9] = true;
1178
1179 // Reserve odd-numbered FPU registers.
1180 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1181 blocked_fpu_registers_[i] = true;
1182 }
1183
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001184 if (GetGraph()->IsDebuggable()) {
1185 // Stubs do not save callee-save floating point registers. If the graph
1186 // is debuggable, we need to deal with these registers differently. For
1187 // now, just block them.
1188 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1189 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1190 }
1191 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001192}
1193
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1195 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1196 return kMipsWordSize;
1197}
1198
1199size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1200 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1201 return kMipsWordSize;
1202}
1203
1204size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1205 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1206 return kMipsDoublewordSize;
1207}
1208
1209size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1210 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1211 return kMipsDoublewordSize;
1212}
1213
1214void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001215 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001216}
1217
1218void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001219 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001220}
1221
Serban Constantinescufca16662016-07-14 09:21:59 +01001222constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1223
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001224void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1225 HInstruction* instruction,
1226 uint32_t dex_pc,
1227 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001228 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001229 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001230 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001231 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001232 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233 // Reserve argument space on stack (for $a0-$a3) for
1234 // entrypoints that directly reference native implementations.
1235 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001236 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001237 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001238 } else {
1239 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001241 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001242 if (EntrypointRequiresStackMap(entrypoint)) {
1243 RecordPcInfo(instruction, dex_pc, slow_path);
1244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245}
1246
1247void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1248 Register class_reg) {
1249 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1250 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1251 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1252 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1253 __ Sync(0);
1254 __ Bind(slow_path->GetExitLabel());
1255}
1256
1257void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1258 __ Sync(0); // Only stype 0 is supported.
1259}
1260
1261void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1262 HBasicBlock* successor) {
1263 SuspendCheckSlowPathMIPS* slow_path =
1264 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1265 codegen_->AddSlowPath(slow_path);
1266
1267 __ LoadFromOffset(kLoadUnsignedHalfword,
1268 TMP,
1269 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001270 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271 if (successor == nullptr) {
1272 __ Bnez(TMP, slow_path->GetEntryLabel());
1273 __ Bind(slow_path->GetReturnLabel());
1274 } else {
1275 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1276 __ B(slow_path->GetEntryLabel());
1277 // slow_path will return to GetLabelOf(successor).
1278 }
1279}
1280
1281InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1282 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001283 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284 assembler_(codegen->GetAssembler()),
1285 codegen_(codegen) {}
1286
1287void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1288 DCHECK_EQ(instruction->InputCount(), 2U);
1289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1290 Primitive::Type type = instruction->GetResultType();
1291 switch (type) {
1292 case Primitive::kPrimInt: {
1293 locations->SetInAt(0, Location::RequiresRegister());
1294 HInstruction* right = instruction->InputAt(1);
1295 bool can_use_imm = false;
1296 if (right->IsConstant()) {
1297 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1298 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1299 can_use_imm = IsUint<16>(imm);
1300 } else if (instruction->IsAdd()) {
1301 can_use_imm = IsInt<16>(imm);
1302 } else {
1303 DCHECK(instruction->IsSub());
1304 can_use_imm = IsInt<16>(-imm);
1305 }
1306 }
1307 if (can_use_imm)
1308 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1309 else
1310 locations->SetInAt(1, Location::RequiresRegister());
1311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1312 break;
1313 }
1314
1315 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001317 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319 break;
1320 }
1321
1322 case Primitive::kPrimFloat:
1323 case Primitive::kPrimDouble:
1324 DCHECK(instruction->IsAdd() || instruction->IsSub());
1325 locations->SetInAt(0, Location::RequiresFpuRegister());
1326 locations->SetInAt(1, Location::RequiresFpuRegister());
1327 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1328 break;
1329
1330 default:
1331 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1332 }
1333}
1334
1335void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1336 Primitive::Type type = instruction->GetType();
1337 LocationSummary* locations = instruction->GetLocations();
1338
1339 switch (type) {
1340 case Primitive::kPrimInt: {
1341 Register dst = locations->Out().AsRegister<Register>();
1342 Register lhs = locations->InAt(0).AsRegister<Register>();
1343 Location rhs_location = locations->InAt(1);
1344
1345 Register rhs_reg = ZERO;
1346 int32_t rhs_imm = 0;
1347 bool use_imm = rhs_location.IsConstant();
1348 if (use_imm) {
1349 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1350 } else {
1351 rhs_reg = rhs_location.AsRegister<Register>();
1352 }
1353
1354 if (instruction->IsAnd()) {
1355 if (use_imm)
1356 __ Andi(dst, lhs, rhs_imm);
1357 else
1358 __ And(dst, lhs, rhs_reg);
1359 } else if (instruction->IsOr()) {
1360 if (use_imm)
1361 __ Ori(dst, lhs, rhs_imm);
1362 else
1363 __ Or(dst, lhs, rhs_reg);
1364 } else if (instruction->IsXor()) {
1365 if (use_imm)
1366 __ Xori(dst, lhs, rhs_imm);
1367 else
1368 __ Xor(dst, lhs, rhs_reg);
1369 } else if (instruction->IsAdd()) {
1370 if (use_imm)
1371 __ Addiu(dst, lhs, rhs_imm);
1372 else
1373 __ Addu(dst, lhs, rhs_reg);
1374 } else {
1375 DCHECK(instruction->IsSub());
1376 if (use_imm)
1377 __ Addiu(dst, lhs, -rhs_imm);
1378 else
1379 __ Subu(dst, lhs, rhs_reg);
1380 }
1381 break;
1382 }
1383
1384 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001385 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1386 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1387 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1388 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001389 Location rhs_location = locations->InAt(1);
1390 bool use_imm = rhs_location.IsConstant();
1391 if (!use_imm) {
1392 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1393 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1394 if (instruction->IsAnd()) {
1395 __ And(dst_low, lhs_low, rhs_low);
1396 __ And(dst_high, lhs_high, rhs_high);
1397 } else if (instruction->IsOr()) {
1398 __ Or(dst_low, lhs_low, rhs_low);
1399 __ Or(dst_high, lhs_high, rhs_high);
1400 } else if (instruction->IsXor()) {
1401 __ Xor(dst_low, lhs_low, rhs_low);
1402 __ Xor(dst_high, lhs_high, rhs_high);
1403 } else if (instruction->IsAdd()) {
1404 if (lhs_low == rhs_low) {
1405 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1406 __ Slt(TMP, lhs_low, ZERO);
1407 __ Addu(dst_low, lhs_low, rhs_low);
1408 } else {
1409 __ Addu(dst_low, lhs_low, rhs_low);
1410 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1411 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1412 }
1413 __ Addu(dst_high, lhs_high, rhs_high);
1414 __ Addu(dst_high, dst_high, TMP);
1415 } else {
1416 DCHECK(instruction->IsSub());
1417 __ Sltu(TMP, lhs_low, rhs_low);
1418 __ Subu(dst_low, lhs_low, rhs_low);
1419 __ Subu(dst_high, lhs_high, rhs_high);
1420 __ Subu(dst_high, dst_high, TMP);
1421 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001422 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001423 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1424 if (instruction->IsOr()) {
1425 uint32_t low = Low32Bits(value);
1426 uint32_t high = High32Bits(value);
1427 if (IsUint<16>(low)) {
1428 if (dst_low != lhs_low || low != 0) {
1429 __ Ori(dst_low, lhs_low, low);
1430 }
1431 } else {
1432 __ LoadConst32(TMP, low);
1433 __ Or(dst_low, lhs_low, TMP);
1434 }
1435 if (IsUint<16>(high)) {
1436 if (dst_high != lhs_high || high != 0) {
1437 __ Ori(dst_high, lhs_high, high);
1438 }
1439 } else {
1440 if (high != low) {
1441 __ LoadConst32(TMP, high);
1442 }
1443 __ Or(dst_high, lhs_high, TMP);
1444 }
1445 } else if (instruction->IsXor()) {
1446 uint32_t low = Low32Bits(value);
1447 uint32_t high = High32Bits(value);
1448 if (IsUint<16>(low)) {
1449 if (dst_low != lhs_low || low != 0) {
1450 __ Xori(dst_low, lhs_low, low);
1451 }
1452 } else {
1453 __ LoadConst32(TMP, low);
1454 __ Xor(dst_low, lhs_low, TMP);
1455 }
1456 if (IsUint<16>(high)) {
1457 if (dst_high != lhs_high || high != 0) {
1458 __ Xori(dst_high, lhs_high, high);
1459 }
1460 } else {
1461 if (high != low) {
1462 __ LoadConst32(TMP, high);
1463 }
1464 __ Xor(dst_high, lhs_high, TMP);
1465 }
1466 } else if (instruction->IsAnd()) {
1467 uint32_t low = Low32Bits(value);
1468 uint32_t high = High32Bits(value);
1469 if (IsUint<16>(low)) {
1470 __ Andi(dst_low, lhs_low, low);
1471 } else if (low != 0xFFFFFFFF) {
1472 __ LoadConst32(TMP, low);
1473 __ And(dst_low, lhs_low, TMP);
1474 } else if (dst_low != lhs_low) {
1475 __ Move(dst_low, lhs_low);
1476 }
1477 if (IsUint<16>(high)) {
1478 __ Andi(dst_high, lhs_high, high);
1479 } else if (high != 0xFFFFFFFF) {
1480 if (high != low) {
1481 __ LoadConst32(TMP, high);
1482 }
1483 __ And(dst_high, lhs_high, TMP);
1484 } else if (dst_high != lhs_high) {
1485 __ Move(dst_high, lhs_high);
1486 }
1487 } else {
1488 if (instruction->IsSub()) {
1489 value = -value;
1490 } else {
1491 DCHECK(instruction->IsAdd());
1492 }
1493 int32_t low = Low32Bits(value);
1494 int32_t high = High32Bits(value);
1495 if (IsInt<16>(low)) {
1496 if (dst_low != lhs_low || low != 0) {
1497 __ Addiu(dst_low, lhs_low, low);
1498 }
1499 if (low != 0) {
1500 __ Sltiu(AT, dst_low, low);
1501 }
1502 } else {
1503 __ LoadConst32(TMP, low);
1504 __ Addu(dst_low, lhs_low, TMP);
1505 __ Sltu(AT, dst_low, TMP);
1506 }
1507 if (IsInt<16>(high)) {
1508 if (dst_high != lhs_high || high != 0) {
1509 __ Addiu(dst_high, lhs_high, high);
1510 }
1511 } else {
1512 if (high != low) {
1513 __ LoadConst32(TMP, high);
1514 }
1515 __ Addu(dst_high, lhs_high, TMP);
1516 }
1517 if (low != 0) {
1518 __ Addu(dst_high, dst_high, AT);
1519 }
1520 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001521 }
1522 break;
1523 }
1524
1525 case Primitive::kPrimFloat:
1526 case Primitive::kPrimDouble: {
1527 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1528 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1529 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1530 if (instruction->IsAdd()) {
1531 if (type == Primitive::kPrimFloat) {
1532 __ AddS(dst, lhs, rhs);
1533 } else {
1534 __ AddD(dst, lhs, rhs);
1535 }
1536 } else {
1537 DCHECK(instruction->IsSub());
1538 if (type == Primitive::kPrimFloat) {
1539 __ SubS(dst, lhs, rhs);
1540 } else {
1541 __ SubD(dst, lhs, rhs);
1542 }
1543 }
1544 break;
1545 }
1546
1547 default:
1548 LOG(FATAL) << "Unexpected binary operation type " << type;
1549 }
1550}
1551
1552void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001553 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001554
1555 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1556 Primitive::Type type = instr->GetResultType();
1557 switch (type) {
1558 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001559 locations->SetInAt(0, Location::RequiresRegister());
1560 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1562 break;
1563 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001564 locations->SetInAt(0, Location::RequiresRegister());
1565 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1566 locations->SetOut(Location::RequiresRegister());
1567 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568 default:
1569 LOG(FATAL) << "Unexpected shift type " << type;
1570 }
1571}
1572
1573static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1574
1575void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001576 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001577 LocationSummary* locations = instr->GetLocations();
1578 Primitive::Type type = instr->GetType();
1579
1580 Location rhs_location = locations->InAt(1);
1581 bool use_imm = rhs_location.IsConstant();
1582 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1583 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001584 const uint32_t shift_mask =
1585 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001586 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001587 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1588 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001589
1590 switch (type) {
1591 case Primitive::kPrimInt: {
1592 Register dst = locations->Out().AsRegister<Register>();
1593 Register lhs = locations->InAt(0).AsRegister<Register>();
1594 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001595 if (shift_value == 0) {
1596 if (dst != lhs) {
1597 __ Move(dst, lhs);
1598 }
1599 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001600 __ Sll(dst, lhs, shift_value);
1601 } else if (instr->IsShr()) {
1602 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001603 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001604 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001605 } else {
1606 if (has_ins_rotr) {
1607 __ Rotr(dst, lhs, shift_value);
1608 } else {
1609 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1610 __ Srl(dst, lhs, shift_value);
1611 __ Or(dst, dst, TMP);
1612 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001613 }
1614 } else {
1615 if (instr->IsShl()) {
1616 __ Sllv(dst, lhs, rhs_reg);
1617 } else if (instr->IsShr()) {
1618 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001619 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001620 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 } else {
1622 if (has_ins_rotr) {
1623 __ Rotrv(dst, lhs, rhs_reg);
1624 } else {
1625 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001626 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1627 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1628 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1629 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1630 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 __ Sllv(TMP, lhs, TMP);
1632 __ Srlv(dst, lhs, rhs_reg);
1633 __ Or(dst, dst, TMP);
1634 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001635 }
1636 }
1637 break;
1638 }
1639
1640 case Primitive::kPrimLong: {
1641 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1642 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1643 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1644 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1645 if (use_imm) {
1646 if (shift_value == 0) {
1647 codegen_->Move64(locations->Out(), locations->InAt(0));
1648 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001649 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001650 if (instr->IsShl()) {
1651 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1652 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1653 __ Sll(dst_low, lhs_low, shift_value);
1654 } else if (instr->IsShr()) {
1655 __ Srl(dst_low, lhs_low, shift_value);
1656 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1657 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001658 } else if (instr->IsUShr()) {
1659 __ Srl(dst_low, lhs_low, shift_value);
1660 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1661 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001662 } else {
1663 __ Srl(dst_low, lhs_low, shift_value);
1664 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1665 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001666 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001667 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001668 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001669 if (instr->IsShl()) {
1670 __ Sll(dst_low, lhs_low, shift_value);
1671 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1672 __ Sll(dst_high, lhs_high, shift_value);
1673 __ Or(dst_high, dst_high, TMP);
1674 } else if (instr->IsShr()) {
1675 __ Sra(dst_high, lhs_high, shift_value);
1676 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1677 __ Srl(dst_low, lhs_low, shift_value);
1678 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001680 __ Srl(dst_high, lhs_high, shift_value);
1681 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1682 __ Srl(dst_low, lhs_low, shift_value);
1683 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001684 } else {
1685 __ Srl(TMP, lhs_low, shift_value);
1686 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1687 __ Or(dst_low, dst_low, TMP);
1688 __ Srl(TMP, lhs_high, shift_value);
1689 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1690 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001691 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001692 }
1693 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001694 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001695 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001696 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001697 __ Move(dst_low, ZERO);
1698 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001699 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001700 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001701 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001702 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001703 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001704 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001705 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001706 // 64-bit rotation by 32 is just a swap.
1707 __ Move(dst_low, lhs_high);
1708 __ Move(dst_high, lhs_low);
1709 } else {
1710 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001711 __ Srl(dst_low, lhs_high, shift_value_high);
1712 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1713 __ Srl(dst_high, lhs_low, shift_value_high);
1714 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001715 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001716 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1717 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001718 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001719 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1720 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001721 __ Or(dst_high, dst_high, TMP);
1722 }
1723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 }
1725 }
1726 } else {
1727 MipsLabel done;
1728 if (instr->IsShl()) {
1729 __ Sllv(dst_low, lhs_low, rhs_reg);
1730 __ Nor(AT, ZERO, rhs_reg);
1731 __ Srl(TMP, lhs_low, 1);
1732 __ Srlv(TMP, TMP, AT);
1733 __ Sllv(dst_high, lhs_high, rhs_reg);
1734 __ Or(dst_high, dst_high, TMP);
1735 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1736 __ Beqz(TMP, &done);
1737 __ Move(dst_high, dst_low);
1738 __ Move(dst_low, ZERO);
1739 } else if (instr->IsShr()) {
1740 __ Srav(dst_high, lhs_high, rhs_reg);
1741 __ Nor(AT, ZERO, rhs_reg);
1742 __ Sll(TMP, lhs_high, 1);
1743 __ Sllv(TMP, TMP, AT);
1744 __ Srlv(dst_low, lhs_low, rhs_reg);
1745 __ Or(dst_low, dst_low, TMP);
1746 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1747 __ Beqz(TMP, &done);
1748 __ Move(dst_low, dst_high);
1749 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001750 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001751 __ Srlv(dst_high, lhs_high, rhs_reg);
1752 __ Nor(AT, ZERO, rhs_reg);
1753 __ Sll(TMP, lhs_high, 1);
1754 __ Sllv(TMP, TMP, AT);
1755 __ Srlv(dst_low, lhs_low, rhs_reg);
1756 __ Or(dst_low, dst_low, TMP);
1757 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1758 __ Beqz(TMP, &done);
1759 __ Move(dst_low, dst_high);
1760 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001761 } else {
1762 __ Nor(AT, ZERO, rhs_reg);
1763 __ Srlv(TMP, lhs_low, rhs_reg);
1764 __ Sll(dst_low, lhs_high, 1);
1765 __ Sllv(dst_low, dst_low, AT);
1766 __ Or(dst_low, dst_low, TMP);
1767 __ Srlv(TMP, lhs_high, rhs_reg);
1768 __ Sll(dst_high, lhs_low, 1);
1769 __ Sllv(dst_high, dst_high, AT);
1770 __ Or(dst_high, dst_high, TMP);
1771 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1772 __ Beqz(TMP, &done);
1773 __ Move(TMP, dst_high);
1774 __ Move(dst_high, dst_low);
1775 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001776 }
1777 __ Bind(&done);
1778 }
1779 break;
1780 }
1781
1782 default:
1783 LOG(FATAL) << "Unexpected shift operation type " << type;
1784 }
1785}
1786
1787void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1788 HandleBinaryOp(instruction);
1789}
1790
1791void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1792 HandleBinaryOp(instruction);
1793}
1794
1795void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1796 HandleBinaryOp(instruction);
1797}
1798
1799void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1800 HandleBinaryOp(instruction);
1801}
1802
1803void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1806 locations->SetInAt(0, Location::RequiresRegister());
1807 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1808 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1809 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1810 } else {
1811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1812 }
1813}
1814
Alexey Frunze2923db72016-08-20 01:55:47 -07001815auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1816 auto null_checker = [this, instruction]() {
1817 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1818 };
1819 return null_checker;
1820}
1821
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1823 LocationSummary* locations = instruction->GetLocations();
1824 Register obj = locations->InAt(0).AsRegister<Register>();
1825 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001826 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001827 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001828
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001829 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001830 switch (type) {
1831 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001832 Register out = locations->Out().AsRegister<Register>();
1833 if (index.IsConstant()) {
1834 size_t offset =
1835 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001836 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001837 } else {
1838 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001839 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840 }
1841 break;
1842 }
1843
1844 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845 Register out = locations->Out().AsRegister<Register>();
1846 if (index.IsConstant()) {
1847 size_t offset =
1848 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001849 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850 } else {
1851 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001852 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 }
1854 break;
1855 }
1856
1857 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 Register out = locations->Out().AsRegister<Register>();
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001862 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 } else {
1864 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1865 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001866 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 }
1868 break;
1869 }
1870
1871 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872 Register out = locations->Out().AsRegister<Register>();
1873 if (index.IsConstant()) {
1874 size_t offset =
1875 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001876 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001877 } else {
1878 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1879 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001880 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001881 }
1882 break;
1883 }
1884
1885 case Primitive::kPrimInt:
1886 case Primitive::kPrimNot: {
1887 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001888 Register out = locations->Out().AsRegister<Register>();
1889 if (index.IsConstant()) {
1890 size_t offset =
1891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001892 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 } else {
1894 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1895 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001896 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001897 }
1898 break;
1899 }
1900
1901 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001902 Register out = locations->Out().AsRegisterPairLow<Register>();
1903 if (index.IsConstant()) {
1904 size_t offset =
1905 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 } else {
1908 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1909 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001910 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 }
1912 break;
1913 }
1914
1915 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1917 if (index.IsConstant()) {
1918 size_t offset =
1919 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001920 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 } else {
1922 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1923 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001924 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 }
1926 break;
1927 }
1928
1929 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001930 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1931 if (index.IsConstant()) {
1932 size_t offset =
1933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001934 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935 } else {
1936 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1937 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 }
1940 break;
1941 }
1942
1943 case Primitive::kPrimVoid:
1944 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1945 UNREACHABLE();
1946 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947}
1948
1949void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1950 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1951 locations->SetInAt(0, Location::RequiresRegister());
1952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1953}
1954
1955void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1956 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001957 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001958 Register obj = locations->InAt(0).AsRegister<Register>();
1959 Register out = locations->Out().AsRegister<Register>();
1960 __ LoadFromOffset(kLoadWord, out, obj, offset);
1961 codegen_->MaybeRecordImplicitNullCheck(instruction);
1962}
1963
Alexey Frunzef58b2482016-09-02 22:14:06 -07001964Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1965 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1966 ? Location::ConstantLocation(instruction->AsConstant())
1967 : Location::RequiresRegister();
1968}
1969
1970Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1971 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1972 // We can store a non-zero float or double constant without first loading it into the FPU,
1973 // but we should only prefer this if the constant has a single use.
1974 if (instruction->IsConstant() &&
1975 (instruction->AsConstant()->IsZeroBitPattern() ||
1976 instruction->GetUses().HasExactlyOneElement())) {
1977 return Location::ConstantLocation(instruction->AsConstant());
1978 // Otherwise fall through and require an FPU register for the constant.
1979 }
1980 return Location::RequiresFpuRegister();
1981}
1982
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001983void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001984 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1986 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001987 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001988 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001989 InvokeRuntimeCallingConvention calling_convention;
1990 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1991 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1992 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1993 } else {
1994 locations->SetInAt(0, Location::RequiresRegister());
1995 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1996 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07001997 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07001999 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002000 }
2001 }
2002}
2003
2004void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2005 LocationSummary* locations = instruction->GetLocations();
2006 Register obj = locations->InAt(0).AsRegister<Register>();
2007 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002008 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009 Primitive::Type value_type = instruction->GetComponentType();
2010 bool needs_runtime_call = locations->WillCall();
2011 bool needs_write_barrier =
2012 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002013 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002014 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015
2016 switch (value_type) {
2017 case Primitive::kPrimBoolean:
2018 case Primitive::kPrimByte: {
2019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002020 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002021 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002023 __ Addu(base_reg, obj, index.AsRegister<Register>());
2024 }
2025 if (value_location.IsConstant()) {
2026 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2027 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2028 } else {
2029 Register value = value_location.AsRegister<Register>();
2030 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031 }
2032 break;
2033 }
2034
2035 case Primitive::kPrimShort:
2036 case Primitive::kPrimChar: {
2037 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002038 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002039 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002041 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2042 __ Addu(base_reg, obj, base_reg);
2043 }
2044 if (value_location.IsConstant()) {
2045 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2046 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2047 } else {
2048 Register value = value_location.AsRegister<Register>();
2049 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 }
2051 break;
2052 }
2053
2054 case Primitive::kPrimInt:
2055 case Primitive::kPrimNot: {
2056 if (!needs_runtime_call) {
2057 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002059 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002061 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2062 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002064 if (value_location.IsConstant()) {
2065 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2066 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2067 DCHECK(!needs_write_barrier);
2068 } else {
2069 Register value = value_location.AsRegister<Register>();
2070 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2071 if (needs_write_barrier) {
2072 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002073 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002074 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002075 }
2076 } else {
2077 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002078 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002079 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2080 }
2081 break;
2082 }
2083
2084 case Primitive::kPrimLong: {
2085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002087 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002088 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002089 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2090 __ Addu(base_reg, obj, base_reg);
2091 }
2092 if (value_location.IsConstant()) {
2093 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2094 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2095 } else {
2096 Register value = value_location.AsRegisterPairLow<Register>();
2097 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002098 }
2099 break;
2100 }
2101
2102 case Primitive::kPrimFloat: {
2103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002104 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002105 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002107 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2108 __ Addu(base_reg, obj, base_reg);
2109 }
2110 if (value_location.IsConstant()) {
2111 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2112 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2113 } else {
2114 FRegister value = value_location.AsFpuRegister<FRegister>();
2115 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002116 }
2117 break;
2118 }
2119
2120 case Primitive::kPrimDouble: {
2121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002123 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002125 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2126 __ Addu(base_reg, obj, base_reg);
2127 }
2128 if (value_location.IsConstant()) {
2129 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2130 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2131 } else {
2132 FRegister value = value_location.AsFpuRegister<FRegister>();
2133 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002134 }
2135 break;
2136 }
2137
2138 case Primitive::kPrimVoid:
2139 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2140 UNREACHABLE();
2141 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142}
2143
2144void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002145 RegisterSet caller_saves = RegisterSet::Empty();
2146 InvokeRuntimeCallingConvention calling_convention;
2147 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2148 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2149 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 locations->SetInAt(0, Location::RequiresRegister());
2151 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002152}
2153
2154void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2155 LocationSummary* locations = instruction->GetLocations();
2156 BoundsCheckSlowPathMIPS* slow_path =
2157 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2158 codegen_->AddSlowPath(slow_path);
2159
2160 Register index = locations->InAt(0).AsRegister<Register>();
2161 Register length = locations->InAt(1).AsRegister<Register>();
2162
2163 // length is limited by the maximum positive signed 32-bit integer.
2164 // Unsigned comparison of length and index checks for index < 0
2165 // and for length <= index simultaneously.
2166 __ Bgeu(index, length, slow_path->GetEntryLabel());
2167}
2168
2169void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2170 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2171 instruction,
2172 LocationSummary::kCallOnSlowPath);
2173 locations->SetInAt(0, Location::RequiresRegister());
2174 locations->SetInAt(1, Location::RequiresRegister());
2175 // Note that TypeCheckSlowPathMIPS uses this register too.
2176 locations->AddTemp(Location::RequiresRegister());
2177}
2178
2179void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2180 LocationSummary* locations = instruction->GetLocations();
2181 Register obj = locations->InAt(0).AsRegister<Register>();
2182 Register cls = locations->InAt(1).AsRegister<Register>();
2183 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2184
2185 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2186 codegen_->AddSlowPath(slow_path);
2187
2188 // TODO: avoid this check if we know obj is not null.
2189 __ Beqz(obj, slow_path->GetExitLabel());
2190 // Compare the class of `obj` with `cls`.
2191 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2192 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2193 __ Bind(slow_path->GetExitLabel());
2194}
2195
2196void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2197 LocationSummary* locations =
2198 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2199 locations->SetInAt(0, Location::RequiresRegister());
2200 if (check->HasUses()) {
2201 locations->SetOut(Location::SameAsFirstInput());
2202 }
2203}
2204
2205void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2206 // We assume the class is not null.
2207 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2208 check->GetLoadClass(),
2209 check,
2210 check->GetDexPc(),
2211 true);
2212 codegen_->AddSlowPath(slow_path);
2213 GenerateClassInitializationCheck(slow_path,
2214 check->GetLocations()->InAt(0).AsRegister<Register>());
2215}
2216
2217void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2218 Primitive::Type in_type = compare->InputAt(0)->GetType();
2219
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002222
2223 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002224 case Primitive::kPrimBoolean:
2225 case Primitive::kPrimByte:
2226 case Primitive::kPrimShort:
2227 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002228 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002229 locations->SetInAt(0, Location::RequiresRegister());
2230 locations->SetInAt(1, Location::RequiresRegister());
2231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 break;
2233
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002234 case Primitive::kPrimLong:
2235 locations->SetInAt(0, Location::RequiresRegister());
2236 locations->SetInAt(1, Location::RequiresRegister());
2237 // Output overlaps because it is written before doing the low comparison.
2238 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2239 break;
2240
2241 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002242 case Primitive::kPrimDouble:
2243 locations->SetInAt(0, Location::RequiresFpuRegister());
2244 locations->SetInAt(1, Location::RequiresFpuRegister());
2245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002246 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247
2248 default:
2249 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2250 }
2251}
2252
2253void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2254 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002255 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002256 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002257 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258
2259 // 0 if: left == right
2260 // 1 if: left > right
2261 // -1 if: left < right
2262 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002263 case Primitive::kPrimBoolean:
2264 case Primitive::kPrimByte:
2265 case Primitive::kPrimShort:
2266 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002267 case Primitive::kPrimInt: {
2268 Register lhs = locations->InAt(0).AsRegister<Register>();
2269 Register rhs = locations->InAt(1).AsRegister<Register>();
2270 __ Slt(TMP, lhs, rhs);
2271 __ Slt(res, rhs, lhs);
2272 __ Subu(res, res, TMP);
2273 break;
2274 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275 case Primitive::kPrimLong: {
2276 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2278 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2279 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2280 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2281 // TODO: more efficient (direct) comparison with a constant.
2282 __ Slt(TMP, lhs_high, rhs_high);
2283 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2284 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2285 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2286 __ Sltu(TMP, lhs_low, rhs_low);
2287 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2288 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2289 __ Bind(&done);
2290 break;
2291 }
2292
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002293 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002294 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002295 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2296 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2297 MipsLabel done;
2298 if (isR6) {
2299 __ CmpEqS(FTMP, lhs, rhs);
2300 __ LoadConst32(res, 0);
2301 __ Bc1nez(FTMP, &done);
2302 if (gt_bias) {
2303 __ CmpLtS(FTMP, lhs, rhs);
2304 __ LoadConst32(res, -1);
2305 __ Bc1nez(FTMP, &done);
2306 __ LoadConst32(res, 1);
2307 } else {
2308 __ CmpLtS(FTMP, rhs, lhs);
2309 __ LoadConst32(res, 1);
2310 __ Bc1nez(FTMP, &done);
2311 __ LoadConst32(res, -1);
2312 }
2313 } else {
2314 if (gt_bias) {
2315 __ ColtS(0, lhs, rhs);
2316 __ LoadConst32(res, -1);
2317 __ Bc1t(0, &done);
2318 __ CeqS(0, lhs, rhs);
2319 __ LoadConst32(res, 1);
2320 __ Movt(res, ZERO, 0);
2321 } else {
2322 __ ColtS(0, rhs, lhs);
2323 __ LoadConst32(res, 1);
2324 __ Bc1t(0, &done);
2325 __ CeqS(0, lhs, rhs);
2326 __ LoadConst32(res, -1);
2327 __ Movt(res, ZERO, 0);
2328 }
2329 }
2330 __ Bind(&done);
2331 break;
2332 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002333 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002334 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002335 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2336 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2337 MipsLabel done;
2338 if (isR6) {
2339 __ CmpEqD(FTMP, lhs, rhs);
2340 __ LoadConst32(res, 0);
2341 __ Bc1nez(FTMP, &done);
2342 if (gt_bias) {
2343 __ CmpLtD(FTMP, lhs, rhs);
2344 __ LoadConst32(res, -1);
2345 __ Bc1nez(FTMP, &done);
2346 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002347 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002348 __ CmpLtD(FTMP, rhs, lhs);
2349 __ LoadConst32(res, 1);
2350 __ Bc1nez(FTMP, &done);
2351 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002352 }
2353 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002354 if (gt_bias) {
2355 __ ColtD(0, lhs, rhs);
2356 __ LoadConst32(res, -1);
2357 __ Bc1t(0, &done);
2358 __ CeqD(0, lhs, rhs);
2359 __ LoadConst32(res, 1);
2360 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002361 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002362 __ ColtD(0, rhs, lhs);
2363 __ LoadConst32(res, 1);
2364 __ Bc1t(0, &done);
2365 __ CeqD(0, lhs, rhs);
2366 __ LoadConst32(res, -1);
2367 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 }
2369 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002370 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002371 break;
2372 }
2373
2374 default:
2375 LOG(FATAL) << "Unimplemented compare type " << in_type;
2376 }
2377}
2378
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002379void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002381 switch (instruction->InputAt(0)->GetType()) {
2382 default:
2383 case Primitive::kPrimLong:
2384 locations->SetInAt(0, Location::RequiresRegister());
2385 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2386 break;
2387
2388 case Primitive::kPrimFloat:
2389 case Primitive::kPrimDouble:
2390 locations->SetInAt(0, Location::RequiresFpuRegister());
2391 locations->SetInAt(1, Location::RequiresFpuRegister());
2392 break;
2393 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002394 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2396 }
2397}
2398
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002399void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002400 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 return;
2402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002404 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 LocationSummary* locations = instruction->GetLocations();
2406 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002407 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002409 switch (type) {
2410 default:
2411 // Integer case.
2412 GenerateIntCompare(instruction->GetCondition(), locations);
2413 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002414
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002415 case Primitive::kPrimLong:
2416 // TODO: don't use branches.
2417 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 break;
2419
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002420 case Primitive::kPrimFloat:
2421 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002422 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2423 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002425
2426 // Convert the branches into the result.
2427 MipsLabel done;
2428
2429 // False case: result = 0.
2430 __ LoadConst32(dst, 0);
2431 __ B(&done);
2432
2433 // True case: result = 1.
2434 __ Bind(&true_label);
2435 __ LoadConst32(dst, 1);
2436 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437}
2438
Alexey Frunze7e99e052015-11-24 19:28:01 -08002439void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2440 DCHECK(instruction->IsDiv() || instruction->IsRem());
2441 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2442
2443 LocationSummary* locations = instruction->GetLocations();
2444 Location second = locations->InAt(1);
2445 DCHECK(second.IsConstant());
2446
2447 Register out = locations->Out().AsRegister<Register>();
2448 Register dividend = locations->InAt(0).AsRegister<Register>();
2449 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2450 DCHECK(imm == 1 || imm == -1);
2451
2452 if (instruction->IsRem()) {
2453 __ Move(out, ZERO);
2454 } else {
2455 if (imm == -1) {
2456 __ Subu(out, ZERO, dividend);
2457 } else if (out != dividend) {
2458 __ Move(out, dividend);
2459 }
2460 }
2461}
2462
2463void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2464 DCHECK(instruction->IsDiv() || instruction->IsRem());
2465 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2466
2467 LocationSummary* locations = instruction->GetLocations();
2468 Location second = locations->InAt(1);
2469 DCHECK(second.IsConstant());
2470
2471 Register out = locations->Out().AsRegister<Register>();
2472 Register dividend = locations->InAt(0).AsRegister<Register>();
2473 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002474 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002475 int ctz_imm = CTZ(abs_imm);
2476
2477 if (instruction->IsDiv()) {
2478 if (ctz_imm == 1) {
2479 // Fast path for division by +/-2, which is very common.
2480 __ Srl(TMP, dividend, 31);
2481 } else {
2482 __ Sra(TMP, dividend, 31);
2483 __ Srl(TMP, TMP, 32 - ctz_imm);
2484 }
2485 __ Addu(out, dividend, TMP);
2486 __ Sra(out, out, ctz_imm);
2487 if (imm < 0) {
2488 __ Subu(out, ZERO, out);
2489 }
2490 } else {
2491 if (ctz_imm == 1) {
2492 // Fast path for modulo +/-2, which is very common.
2493 __ Sra(TMP, dividend, 31);
2494 __ Subu(out, dividend, TMP);
2495 __ Andi(out, out, 1);
2496 __ Addu(out, out, TMP);
2497 } else {
2498 __ Sra(TMP, dividend, 31);
2499 __ Srl(TMP, TMP, 32 - ctz_imm);
2500 __ Addu(out, dividend, TMP);
2501 if (IsUint<16>(abs_imm - 1)) {
2502 __ Andi(out, out, abs_imm - 1);
2503 } else {
2504 __ Sll(out, out, 32 - ctz_imm);
2505 __ Srl(out, out, 32 - ctz_imm);
2506 }
2507 __ Subu(out, out, TMP);
2508 }
2509 }
2510}
2511
2512void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2513 DCHECK(instruction->IsDiv() || instruction->IsRem());
2514 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2515
2516 LocationSummary* locations = instruction->GetLocations();
2517 Location second = locations->InAt(1);
2518 DCHECK(second.IsConstant());
2519
2520 Register out = locations->Out().AsRegister<Register>();
2521 Register dividend = locations->InAt(0).AsRegister<Register>();
2522 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2523
2524 int64_t magic;
2525 int shift;
2526 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2527
2528 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2529
2530 __ LoadConst32(TMP, magic);
2531 if (isR6) {
2532 __ MuhR6(TMP, dividend, TMP);
2533 } else {
2534 __ MultR2(dividend, TMP);
2535 __ Mfhi(TMP);
2536 }
2537 if (imm > 0 && magic < 0) {
2538 __ Addu(TMP, TMP, dividend);
2539 } else if (imm < 0 && magic > 0) {
2540 __ Subu(TMP, TMP, dividend);
2541 }
2542
2543 if (shift != 0) {
2544 __ Sra(TMP, TMP, shift);
2545 }
2546
2547 if (instruction->IsDiv()) {
2548 __ Sra(out, TMP, 31);
2549 __ Subu(out, TMP, out);
2550 } else {
2551 __ Sra(AT, TMP, 31);
2552 __ Subu(AT, TMP, AT);
2553 __ LoadConst32(TMP, imm);
2554 if (isR6) {
2555 __ MulR6(TMP, AT, TMP);
2556 } else {
2557 __ MulR2(TMP, AT, TMP);
2558 }
2559 __ Subu(out, dividend, TMP);
2560 }
2561}
2562
2563void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2564 DCHECK(instruction->IsDiv() || instruction->IsRem());
2565 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2566
2567 LocationSummary* locations = instruction->GetLocations();
2568 Register out = locations->Out().AsRegister<Register>();
2569 Location second = locations->InAt(1);
2570
2571 if (second.IsConstant()) {
2572 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2573 if (imm == 0) {
2574 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2575 } else if (imm == 1 || imm == -1) {
2576 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002577 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002578 DivRemByPowerOfTwo(instruction);
2579 } else {
2580 DCHECK(imm <= -2 || imm >= 2);
2581 GenerateDivRemWithAnyConstant(instruction);
2582 }
2583 } else {
2584 Register dividend = locations->InAt(0).AsRegister<Register>();
2585 Register divisor = second.AsRegister<Register>();
2586 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2587 if (instruction->IsDiv()) {
2588 if (isR6) {
2589 __ DivR6(out, dividend, divisor);
2590 } else {
2591 __ DivR2(out, dividend, divisor);
2592 }
2593 } else {
2594 if (isR6) {
2595 __ ModR6(out, dividend, divisor);
2596 } else {
2597 __ ModR2(out, dividend, divisor);
2598 }
2599 }
2600 }
2601}
2602
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2604 Primitive::Type type = div->GetResultType();
2605 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002606 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 : LocationSummary::kNoCall;
2608
2609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2610
2611 switch (type) {
2612 case Primitive::kPrimInt:
2613 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002614 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002615 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2616 break;
2617
2618 case Primitive::kPrimLong: {
2619 InvokeRuntimeCallingConvention calling_convention;
2620 locations->SetInAt(0, Location::RegisterPairLocation(
2621 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2622 locations->SetInAt(1, Location::RegisterPairLocation(
2623 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2624 locations->SetOut(calling_convention.GetReturnLocation(type));
2625 break;
2626 }
2627
2628 case Primitive::kPrimFloat:
2629 case Primitive::kPrimDouble:
2630 locations->SetInAt(0, Location::RequiresFpuRegister());
2631 locations->SetInAt(1, Location::RequiresFpuRegister());
2632 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2633 break;
2634
2635 default:
2636 LOG(FATAL) << "Unexpected div type " << type;
2637 }
2638}
2639
2640void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2641 Primitive::Type type = instruction->GetType();
2642 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643
2644 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002645 case Primitive::kPrimInt:
2646 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002647 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002648 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002649 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002650 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2651 break;
2652 }
2653 case Primitive::kPrimFloat:
2654 case Primitive::kPrimDouble: {
2655 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2656 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2657 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2658 if (type == Primitive::kPrimFloat) {
2659 __ DivS(dst, lhs, rhs);
2660 } else {
2661 __ DivD(dst, lhs, rhs);
2662 }
2663 break;
2664 }
2665 default:
2666 LOG(FATAL) << "Unexpected div type " << type;
2667 }
2668}
2669
2670void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002671 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002673}
2674
2675void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2676 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2677 codegen_->AddSlowPath(slow_path);
2678 Location value = instruction->GetLocations()->InAt(0);
2679 Primitive::Type type = instruction->GetType();
2680
2681 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002682 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002683 case Primitive::kPrimByte:
2684 case Primitive::kPrimChar:
2685 case Primitive::kPrimShort:
2686 case Primitive::kPrimInt: {
2687 if (value.IsConstant()) {
2688 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2689 __ B(slow_path->GetEntryLabel());
2690 } else {
2691 // A division by a non-null constant is valid. We don't need to perform
2692 // any check, so simply fall through.
2693 }
2694 } else {
2695 DCHECK(value.IsRegister()) << value;
2696 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2697 }
2698 break;
2699 }
2700 case Primitive::kPrimLong: {
2701 if (value.IsConstant()) {
2702 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2703 __ B(slow_path->GetEntryLabel());
2704 } else {
2705 // A division by a non-null constant is valid. We don't need to perform
2706 // any check, so simply fall through.
2707 }
2708 } else {
2709 DCHECK(value.IsRegisterPair()) << value;
2710 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2711 __ Beqz(TMP, slow_path->GetEntryLabel());
2712 }
2713 break;
2714 }
2715 default:
2716 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2717 }
2718}
2719
2720void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2723 locations->SetOut(Location::ConstantLocation(constant));
2724}
2725
2726void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2727 // Will be generated at use site.
2728}
2729
2730void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2731 exit->SetLocations(nullptr);
2732}
2733
2734void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2735}
2736
2737void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2740 locations->SetOut(Location::ConstantLocation(constant));
2741}
2742
2743void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2744 // Will be generated at use site.
2745}
2746
2747void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2748 got->SetLocations(nullptr);
2749}
2750
2751void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2752 DCHECK(!successor->IsExitBlock());
2753 HBasicBlock* block = got->GetBlock();
2754 HInstruction* previous = got->GetPrevious();
2755 HLoopInformation* info = block->GetLoopInformation();
2756
2757 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2758 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2759 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2760 return;
2761 }
2762 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2763 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2764 }
2765 if (!codegen_->GoesToNextBlock(block, successor)) {
2766 __ B(codegen_->GetLabelOf(successor));
2767 }
2768}
2769
2770void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2771 HandleGoto(got, got->GetSuccessor());
2772}
2773
2774void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2775 try_boundary->SetLocations(nullptr);
2776}
2777
2778void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2779 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2780 if (!successor->IsExitBlock()) {
2781 HandleGoto(try_boundary, successor);
2782 }
2783}
2784
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002785void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2786 LocationSummary* locations) {
2787 Register dst = locations->Out().AsRegister<Register>();
2788 Register lhs = locations->InAt(0).AsRegister<Register>();
2789 Location rhs_location = locations->InAt(1);
2790 Register rhs_reg = ZERO;
2791 int64_t rhs_imm = 0;
2792 bool use_imm = rhs_location.IsConstant();
2793 if (use_imm) {
2794 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2795 } else {
2796 rhs_reg = rhs_location.AsRegister<Register>();
2797 }
2798
2799 switch (cond) {
2800 case kCondEQ:
2801 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002802 if (use_imm && IsInt<16>(-rhs_imm)) {
2803 if (rhs_imm == 0) {
2804 if (cond == kCondEQ) {
2805 __ Sltiu(dst, lhs, 1);
2806 } else {
2807 __ Sltu(dst, ZERO, lhs);
2808 }
2809 } else {
2810 __ Addiu(dst, lhs, -rhs_imm);
2811 if (cond == kCondEQ) {
2812 __ Sltiu(dst, dst, 1);
2813 } else {
2814 __ Sltu(dst, ZERO, dst);
2815 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002816 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002817 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002818 if (use_imm && IsUint<16>(rhs_imm)) {
2819 __ Xori(dst, lhs, rhs_imm);
2820 } else {
2821 if (use_imm) {
2822 rhs_reg = TMP;
2823 __ LoadConst32(rhs_reg, rhs_imm);
2824 }
2825 __ Xor(dst, lhs, rhs_reg);
2826 }
2827 if (cond == kCondEQ) {
2828 __ Sltiu(dst, dst, 1);
2829 } else {
2830 __ Sltu(dst, ZERO, dst);
2831 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002832 }
2833 break;
2834
2835 case kCondLT:
2836 case kCondGE:
2837 if (use_imm && IsInt<16>(rhs_imm)) {
2838 __ Slti(dst, lhs, rhs_imm);
2839 } else {
2840 if (use_imm) {
2841 rhs_reg = TMP;
2842 __ LoadConst32(rhs_reg, rhs_imm);
2843 }
2844 __ Slt(dst, lhs, rhs_reg);
2845 }
2846 if (cond == kCondGE) {
2847 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2848 // only the slt instruction but no sge.
2849 __ Xori(dst, dst, 1);
2850 }
2851 break;
2852
2853 case kCondLE:
2854 case kCondGT:
2855 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2856 // Simulate lhs <= rhs via lhs < rhs + 1.
2857 __ Slti(dst, lhs, rhs_imm + 1);
2858 if (cond == kCondGT) {
2859 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2860 // only the slti instruction but no sgti.
2861 __ Xori(dst, dst, 1);
2862 }
2863 } else {
2864 if (use_imm) {
2865 rhs_reg = TMP;
2866 __ LoadConst32(rhs_reg, rhs_imm);
2867 }
2868 __ Slt(dst, rhs_reg, lhs);
2869 if (cond == kCondLE) {
2870 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2871 // only the slt instruction but no sle.
2872 __ Xori(dst, dst, 1);
2873 }
2874 }
2875 break;
2876
2877 case kCondB:
2878 case kCondAE:
2879 if (use_imm && IsInt<16>(rhs_imm)) {
2880 // Sltiu sign-extends its 16-bit immediate operand before
2881 // the comparison and thus lets us compare directly with
2882 // unsigned values in the ranges [0, 0x7fff] and
2883 // [0xffff8000, 0xffffffff].
2884 __ Sltiu(dst, lhs, rhs_imm);
2885 } else {
2886 if (use_imm) {
2887 rhs_reg = TMP;
2888 __ LoadConst32(rhs_reg, rhs_imm);
2889 }
2890 __ Sltu(dst, lhs, rhs_reg);
2891 }
2892 if (cond == kCondAE) {
2893 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2894 // only the sltu instruction but no sgeu.
2895 __ Xori(dst, dst, 1);
2896 }
2897 break;
2898
2899 case kCondBE:
2900 case kCondA:
2901 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2902 // Simulate lhs <= rhs via lhs < rhs + 1.
2903 // Note that this only works if rhs + 1 does not overflow
2904 // to 0, hence the check above.
2905 // Sltiu sign-extends its 16-bit immediate operand before
2906 // the comparison and thus lets us compare directly with
2907 // unsigned values in the ranges [0, 0x7fff] and
2908 // [0xffff8000, 0xffffffff].
2909 __ Sltiu(dst, lhs, rhs_imm + 1);
2910 if (cond == kCondA) {
2911 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2912 // only the sltiu instruction but no sgtiu.
2913 __ Xori(dst, dst, 1);
2914 }
2915 } else {
2916 if (use_imm) {
2917 rhs_reg = TMP;
2918 __ LoadConst32(rhs_reg, rhs_imm);
2919 }
2920 __ Sltu(dst, rhs_reg, lhs);
2921 if (cond == kCondBE) {
2922 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2923 // only the sltu instruction but no sleu.
2924 __ Xori(dst, dst, 1);
2925 }
2926 }
2927 break;
2928 }
2929}
2930
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002931bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2932 LocationSummary* input_locations,
2933 Register dst) {
2934 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2935 Location rhs_location = input_locations->InAt(1);
2936 Register rhs_reg = ZERO;
2937 int64_t rhs_imm = 0;
2938 bool use_imm = rhs_location.IsConstant();
2939 if (use_imm) {
2940 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2941 } else {
2942 rhs_reg = rhs_location.AsRegister<Register>();
2943 }
2944
2945 switch (cond) {
2946 case kCondEQ:
2947 case kCondNE:
2948 if (use_imm && IsInt<16>(-rhs_imm)) {
2949 __ Addiu(dst, lhs, -rhs_imm);
2950 } else if (use_imm && IsUint<16>(rhs_imm)) {
2951 __ Xori(dst, lhs, rhs_imm);
2952 } else {
2953 if (use_imm) {
2954 rhs_reg = TMP;
2955 __ LoadConst32(rhs_reg, rhs_imm);
2956 }
2957 __ Xor(dst, lhs, rhs_reg);
2958 }
2959 return (cond == kCondEQ);
2960
2961 case kCondLT:
2962 case kCondGE:
2963 if (use_imm && IsInt<16>(rhs_imm)) {
2964 __ Slti(dst, lhs, rhs_imm);
2965 } else {
2966 if (use_imm) {
2967 rhs_reg = TMP;
2968 __ LoadConst32(rhs_reg, rhs_imm);
2969 }
2970 __ Slt(dst, lhs, rhs_reg);
2971 }
2972 return (cond == kCondGE);
2973
2974 case kCondLE:
2975 case kCondGT:
2976 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2977 // Simulate lhs <= rhs via lhs < rhs + 1.
2978 __ Slti(dst, lhs, rhs_imm + 1);
2979 return (cond == kCondGT);
2980 } else {
2981 if (use_imm) {
2982 rhs_reg = TMP;
2983 __ LoadConst32(rhs_reg, rhs_imm);
2984 }
2985 __ Slt(dst, rhs_reg, lhs);
2986 return (cond == kCondLE);
2987 }
2988
2989 case kCondB:
2990 case kCondAE:
2991 if (use_imm && IsInt<16>(rhs_imm)) {
2992 // Sltiu sign-extends its 16-bit immediate operand before
2993 // the comparison and thus lets us compare directly with
2994 // unsigned values in the ranges [0, 0x7fff] and
2995 // [0xffff8000, 0xffffffff].
2996 __ Sltiu(dst, lhs, rhs_imm);
2997 } else {
2998 if (use_imm) {
2999 rhs_reg = TMP;
3000 __ LoadConst32(rhs_reg, rhs_imm);
3001 }
3002 __ Sltu(dst, lhs, rhs_reg);
3003 }
3004 return (cond == kCondAE);
3005
3006 case kCondBE:
3007 case kCondA:
3008 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3009 // Simulate lhs <= rhs via lhs < rhs + 1.
3010 // Note that this only works if rhs + 1 does not overflow
3011 // to 0, hence the check above.
3012 // Sltiu sign-extends its 16-bit immediate operand before
3013 // the comparison and thus lets us compare directly with
3014 // unsigned values in the ranges [0, 0x7fff] and
3015 // [0xffff8000, 0xffffffff].
3016 __ Sltiu(dst, lhs, rhs_imm + 1);
3017 return (cond == kCondA);
3018 } else {
3019 if (use_imm) {
3020 rhs_reg = TMP;
3021 __ LoadConst32(rhs_reg, rhs_imm);
3022 }
3023 __ Sltu(dst, rhs_reg, lhs);
3024 return (cond == kCondBE);
3025 }
3026 }
3027}
3028
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003029void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3030 LocationSummary* locations,
3031 MipsLabel* label) {
3032 Register lhs = locations->InAt(0).AsRegister<Register>();
3033 Location rhs_location = locations->InAt(1);
3034 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003035 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003036 bool use_imm = rhs_location.IsConstant();
3037 if (use_imm) {
3038 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3039 } else {
3040 rhs_reg = rhs_location.AsRegister<Register>();
3041 }
3042
3043 if (use_imm && rhs_imm == 0) {
3044 switch (cond) {
3045 case kCondEQ:
3046 case kCondBE: // <= 0 if zero
3047 __ Beqz(lhs, label);
3048 break;
3049 case kCondNE:
3050 case kCondA: // > 0 if non-zero
3051 __ Bnez(lhs, label);
3052 break;
3053 case kCondLT:
3054 __ Bltz(lhs, label);
3055 break;
3056 case kCondGE:
3057 __ Bgez(lhs, label);
3058 break;
3059 case kCondLE:
3060 __ Blez(lhs, label);
3061 break;
3062 case kCondGT:
3063 __ Bgtz(lhs, label);
3064 break;
3065 case kCondB: // always false
3066 break;
3067 case kCondAE: // always true
3068 __ B(label);
3069 break;
3070 }
3071 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003072 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3073 if (isR6 || !use_imm) {
3074 if (use_imm) {
3075 rhs_reg = TMP;
3076 __ LoadConst32(rhs_reg, rhs_imm);
3077 }
3078 switch (cond) {
3079 case kCondEQ:
3080 __ Beq(lhs, rhs_reg, label);
3081 break;
3082 case kCondNE:
3083 __ Bne(lhs, rhs_reg, label);
3084 break;
3085 case kCondLT:
3086 __ Blt(lhs, rhs_reg, label);
3087 break;
3088 case kCondGE:
3089 __ Bge(lhs, rhs_reg, label);
3090 break;
3091 case kCondLE:
3092 __ Bge(rhs_reg, lhs, label);
3093 break;
3094 case kCondGT:
3095 __ Blt(rhs_reg, lhs, label);
3096 break;
3097 case kCondB:
3098 __ Bltu(lhs, rhs_reg, label);
3099 break;
3100 case kCondAE:
3101 __ Bgeu(lhs, rhs_reg, label);
3102 break;
3103 case kCondBE:
3104 __ Bgeu(rhs_reg, lhs, label);
3105 break;
3106 case kCondA:
3107 __ Bltu(rhs_reg, lhs, label);
3108 break;
3109 }
3110 } else {
3111 // Special cases for more efficient comparison with constants on R2.
3112 switch (cond) {
3113 case kCondEQ:
3114 __ LoadConst32(TMP, rhs_imm);
3115 __ Beq(lhs, TMP, label);
3116 break;
3117 case kCondNE:
3118 __ LoadConst32(TMP, rhs_imm);
3119 __ Bne(lhs, TMP, label);
3120 break;
3121 case kCondLT:
3122 if (IsInt<16>(rhs_imm)) {
3123 __ Slti(TMP, lhs, rhs_imm);
3124 __ Bnez(TMP, label);
3125 } else {
3126 __ LoadConst32(TMP, rhs_imm);
3127 __ Blt(lhs, TMP, label);
3128 }
3129 break;
3130 case kCondGE:
3131 if (IsInt<16>(rhs_imm)) {
3132 __ Slti(TMP, lhs, rhs_imm);
3133 __ Beqz(TMP, label);
3134 } else {
3135 __ LoadConst32(TMP, rhs_imm);
3136 __ Bge(lhs, TMP, label);
3137 }
3138 break;
3139 case kCondLE:
3140 if (IsInt<16>(rhs_imm + 1)) {
3141 // Simulate lhs <= rhs via lhs < rhs + 1.
3142 __ Slti(TMP, lhs, rhs_imm + 1);
3143 __ Bnez(TMP, label);
3144 } else {
3145 __ LoadConst32(TMP, rhs_imm);
3146 __ Bge(TMP, lhs, label);
3147 }
3148 break;
3149 case kCondGT:
3150 if (IsInt<16>(rhs_imm + 1)) {
3151 // Simulate lhs > rhs via !(lhs < rhs + 1).
3152 __ Slti(TMP, lhs, rhs_imm + 1);
3153 __ Beqz(TMP, label);
3154 } else {
3155 __ LoadConst32(TMP, rhs_imm);
3156 __ Blt(TMP, lhs, label);
3157 }
3158 break;
3159 case kCondB:
3160 if (IsInt<16>(rhs_imm)) {
3161 __ Sltiu(TMP, lhs, rhs_imm);
3162 __ Bnez(TMP, label);
3163 } else {
3164 __ LoadConst32(TMP, rhs_imm);
3165 __ Bltu(lhs, TMP, label);
3166 }
3167 break;
3168 case kCondAE:
3169 if (IsInt<16>(rhs_imm)) {
3170 __ Sltiu(TMP, lhs, rhs_imm);
3171 __ Beqz(TMP, label);
3172 } else {
3173 __ LoadConst32(TMP, rhs_imm);
3174 __ Bgeu(lhs, TMP, label);
3175 }
3176 break;
3177 case kCondBE:
3178 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3179 // Simulate lhs <= rhs via lhs < rhs + 1.
3180 // Note that this only works if rhs + 1 does not overflow
3181 // to 0, hence the check above.
3182 __ Sltiu(TMP, lhs, rhs_imm + 1);
3183 __ Bnez(TMP, label);
3184 } else {
3185 __ LoadConst32(TMP, rhs_imm);
3186 __ Bgeu(TMP, lhs, label);
3187 }
3188 break;
3189 case kCondA:
3190 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3191 // Simulate lhs > rhs via !(lhs < rhs + 1).
3192 // Note that this only works if rhs + 1 does not overflow
3193 // to 0, hence the check above.
3194 __ Sltiu(TMP, lhs, rhs_imm + 1);
3195 __ Beqz(TMP, label);
3196 } else {
3197 __ LoadConst32(TMP, rhs_imm);
3198 __ Bltu(TMP, lhs, label);
3199 }
3200 break;
3201 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003202 }
3203 }
3204}
3205
3206void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3207 LocationSummary* locations,
3208 MipsLabel* label) {
3209 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3210 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3211 Location rhs_location = locations->InAt(1);
3212 Register rhs_high = ZERO;
3213 Register rhs_low = ZERO;
3214 int64_t imm = 0;
3215 uint32_t imm_high = 0;
3216 uint32_t imm_low = 0;
3217 bool use_imm = rhs_location.IsConstant();
3218 if (use_imm) {
3219 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3220 imm_high = High32Bits(imm);
3221 imm_low = Low32Bits(imm);
3222 } else {
3223 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3224 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3225 }
3226
3227 if (use_imm && imm == 0) {
3228 switch (cond) {
3229 case kCondEQ:
3230 case kCondBE: // <= 0 if zero
3231 __ Or(TMP, lhs_high, lhs_low);
3232 __ Beqz(TMP, label);
3233 break;
3234 case kCondNE:
3235 case kCondA: // > 0 if non-zero
3236 __ Or(TMP, lhs_high, lhs_low);
3237 __ Bnez(TMP, label);
3238 break;
3239 case kCondLT:
3240 __ Bltz(lhs_high, label);
3241 break;
3242 case kCondGE:
3243 __ Bgez(lhs_high, label);
3244 break;
3245 case kCondLE:
3246 __ Or(TMP, lhs_high, lhs_low);
3247 __ Sra(AT, lhs_high, 31);
3248 __ Bgeu(AT, TMP, label);
3249 break;
3250 case kCondGT:
3251 __ Or(TMP, lhs_high, lhs_low);
3252 __ Sra(AT, lhs_high, 31);
3253 __ Bltu(AT, TMP, label);
3254 break;
3255 case kCondB: // always false
3256 break;
3257 case kCondAE: // always true
3258 __ B(label);
3259 break;
3260 }
3261 } else if (use_imm) {
3262 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3263 switch (cond) {
3264 case kCondEQ:
3265 __ LoadConst32(TMP, imm_high);
3266 __ Xor(TMP, TMP, lhs_high);
3267 __ LoadConst32(AT, imm_low);
3268 __ Xor(AT, AT, lhs_low);
3269 __ Or(TMP, TMP, AT);
3270 __ Beqz(TMP, label);
3271 break;
3272 case kCondNE:
3273 __ LoadConst32(TMP, imm_high);
3274 __ Xor(TMP, TMP, lhs_high);
3275 __ LoadConst32(AT, imm_low);
3276 __ Xor(AT, AT, lhs_low);
3277 __ Or(TMP, TMP, AT);
3278 __ Bnez(TMP, label);
3279 break;
3280 case kCondLT:
3281 __ LoadConst32(TMP, imm_high);
3282 __ Blt(lhs_high, TMP, label);
3283 __ Slt(TMP, TMP, lhs_high);
3284 __ LoadConst32(AT, imm_low);
3285 __ Sltu(AT, lhs_low, AT);
3286 __ Blt(TMP, AT, label);
3287 break;
3288 case kCondGE:
3289 __ LoadConst32(TMP, imm_high);
3290 __ Blt(TMP, lhs_high, label);
3291 __ Slt(TMP, lhs_high, TMP);
3292 __ LoadConst32(AT, imm_low);
3293 __ Sltu(AT, lhs_low, AT);
3294 __ Or(TMP, TMP, AT);
3295 __ Beqz(TMP, label);
3296 break;
3297 case kCondLE:
3298 __ LoadConst32(TMP, imm_high);
3299 __ Blt(lhs_high, TMP, label);
3300 __ Slt(TMP, TMP, lhs_high);
3301 __ LoadConst32(AT, imm_low);
3302 __ Sltu(AT, AT, lhs_low);
3303 __ Or(TMP, TMP, AT);
3304 __ Beqz(TMP, label);
3305 break;
3306 case kCondGT:
3307 __ LoadConst32(TMP, imm_high);
3308 __ Blt(TMP, lhs_high, label);
3309 __ Slt(TMP, lhs_high, TMP);
3310 __ LoadConst32(AT, imm_low);
3311 __ Sltu(AT, AT, lhs_low);
3312 __ Blt(TMP, AT, label);
3313 break;
3314 case kCondB:
3315 __ LoadConst32(TMP, imm_high);
3316 __ Bltu(lhs_high, TMP, label);
3317 __ Sltu(TMP, TMP, lhs_high);
3318 __ LoadConst32(AT, imm_low);
3319 __ Sltu(AT, lhs_low, AT);
3320 __ Blt(TMP, AT, label);
3321 break;
3322 case kCondAE:
3323 __ LoadConst32(TMP, imm_high);
3324 __ Bltu(TMP, lhs_high, label);
3325 __ Sltu(TMP, lhs_high, TMP);
3326 __ LoadConst32(AT, imm_low);
3327 __ Sltu(AT, lhs_low, AT);
3328 __ Or(TMP, TMP, AT);
3329 __ Beqz(TMP, label);
3330 break;
3331 case kCondBE:
3332 __ LoadConst32(TMP, imm_high);
3333 __ Bltu(lhs_high, TMP, label);
3334 __ Sltu(TMP, TMP, lhs_high);
3335 __ LoadConst32(AT, imm_low);
3336 __ Sltu(AT, AT, lhs_low);
3337 __ Or(TMP, TMP, AT);
3338 __ Beqz(TMP, label);
3339 break;
3340 case kCondA:
3341 __ LoadConst32(TMP, imm_high);
3342 __ Bltu(TMP, lhs_high, label);
3343 __ Sltu(TMP, lhs_high, TMP);
3344 __ LoadConst32(AT, imm_low);
3345 __ Sltu(AT, AT, lhs_low);
3346 __ Blt(TMP, AT, label);
3347 break;
3348 }
3349 } else {
3350 switch (cond) {
3351 case kCondEQ:
3352 __ Xor(TMP, lhs_high, rhs_high);
3353 __ Xor(AT, lhs_low, rhs_low);
3354 __ Or(TMP, TMP, AT);
3355 __ Beqz(TMP, label);
3356 break;
3357 case kCondNE:
3358 __ Xor(TMP, lhs_high, rhs_high);
3359 __ Xor(AT, lhs_low, rhs_low);
3360 __ Or(TMP, TMP, AT);
3361 __ Bnez(TMP, label);
3362 break;
3363 case kCondLT:
3364 __ Blt(lhs_high, rhs_high, label);
3365 __ Slt(TMP, rhs_high, lhs_high);
3366 __ Sltu(AT, lhs_low, rhs_low);
3367 __ Blt(TMP, AT, label);
3368 break;
3369 case kCondGE:
3370 __ Blt(rhs_high, lhs_high, label);
3371 __ Slt(TMP, lhs_high, rhs_high);
3372 __ Sltu(AT, lhs_low, rhs_low);
3373 __ Or(TMP, TMP, AT);
3374 __ Beqz(TMP, label);
3375 break;
3376 case kCondLE:
3377 __ Blt(lhs_high, rhs_high, label);
3378 __ Slt(TMP, rhs_high, lhs_high);
3379 __ Sltu(AT, rhs_low, lhs_low);
3380 __ Or(TMP, TMP, AT);
3381 __ Beqz(TMP, label);
3382 break;
3383 case kCondGT:
3384 __ Blt(rhs_high, lhs_high, label);
3385 __ Slt(TMP, lhs_high, rhs_high);
3386 __ Sltu(AT, rhs_low, lhs_low);
3387 __ Blt(TMP, AT, label);
3388 break;
3389 case kCondB:
3390 __ Bltu(lhs_high, rhs_high, label);
3391 __ Sltu(TMP, rhs_high, lhs_high);
3392 __ Sltu(AT, lhs_low, rhs_low);
3393 __ Blt(TMP, AT, label);
3394 break;
3395 case kCondAE:
3396 __ Bltu(rhs_high, lhs_high, label);
3397 __ Sltu(TMP, lhs_high, rhs_high);
3398 __ Sltu(AT, lhs_low, rhs_low);
3399 __ Or(TMP, TMP, AT);
3400 __ Beqz(TMP, label);
3401 break;
3402 case kCondBE:
3403 __ Bltu(lhs_high, rhs_high, label);
3404 __ Sltu(TMP, rhs_high, lhs_high);
3405 __ Sltu(AT, rhs_low, lhs_low);
3406 __ Or(TMP, TMP, AT);
3407 __ Beqz(TMP, label);
3408 break;
3409 case kCondA:
3410 __ Bltu(rhs_high, lhs_high, label);
3411 __ Sltu(TMP, lhs_high, rhs_high);
3412 __ Sltu(AT, rhs_low, lhs_low);
3413 __ Blt(TMP, AT, label);
3414 break;
3415 }
3416 }
3417}
3418
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003419void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3420 bool gt_bias,
3421 Primitive::Type type,
3422 LocationSummary* locations) {
3423 Register dst = locations->Out().AsRegister<Register>();
3424 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3425 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3426 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3427 if (type == Primitive::kPrimFloat) {
3428 if (isR6) {
3429 switch (cond) {
3430 case kCondEQ:
3431 __ CmpEqS(FTMP, lhs, rhs);
3432 __ Mfc1(dst, FTMP);
3433 __ Andi(dst, dst, 1);
3434 break;
3435 case kCondNE:
3436 __ CmpEqS(FTMP, lhs, rhs);
3437 __ Mfc1(dst, FTMP);
3438 __ Addiu(dst, dst, 1);
3439 break;
3440 case kCondLT:
3441 if (gt_bias) {
3442 __ CmpLtS(FTMP, lhs, rhs);
3443 } else {
3444 __ CmpUltS(FTMP, lhs, rhs);
3445 }
3446 __ Mfc1(dst, FTMP);
3447 __ Andi(dst, dst, 1);
3448 break;
3449 case kCondLE:
3450 if (gt_bias) {
3451 __ CmpLeS(FTMP, lhs, rhs);
3452 } else {
3453 __ CmpUleS(FTMP, lhs, rhs);
3454 }
3455 __ Mfc1(dst, FTMP);
3456 __ Andi(dst, dst, 1);
3457 break;
3458 case kCondGT:
3459 if (gt_bias) {
3460 __ CmpUltS(FTMP, rhs, lhs);
3461 } else {
3462 __ CmpLtS(FTMP, rhs, lhs);
3463 }
3464 __ Mfc1(dst, FTMP);
3465 __ Andi(dst, dst, 1);
3466 break;
3467 case kCondGE:
3468 if (gt_bias) {
3469 __ CmpUleS(FTMP, rhs, lhs);
3470 } else {
3471 __ CmpLeS(FTMP, rhs, lhs);
3472 }
3473 __ Mfc1(dst, FTMP);
3474 __ Andi(dst, dst, 1);
3475 break;
3476 default:
3477 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3478 UNREACHABLE();
3479 }
3480 } else {
3481 switch (cond) {
3482 case kCondEQ:
3483 __ CeqS(0, lhs, rhs);
3484 __ LoadConst32(dst, 1);
3485 __ Movf(dst, ZERO, 0);
3486 break;
3487 case kCondNE:
3488 __ CeqS(0, lhs, rhs);
3489 __ LoadConst32(dst, 1);
3490 __ Movt(dst, ZERO, 0);
3491 break;
3492 case kCondLT:
3493 if (gt_bias) {
3494 __ ColtS(0, lhs, rhs);
3495 } else {
3496 __ CultS(0, lhs, rhs);
3497 }
3498 __ LoadConst32(dst, 1);
3499 __ Movf(dst, ZERO, 0);
3500 break;
3501 case kCondLE:
3502 if (gt_bias) {
3503 __ ColeS(0, lhs, rhs);
3504 } else {
3505 __ CuleS(0, lhs, rhs);
3506 }
3507 __ LoadConst32(dst, 1);
3508 __ Movf(dst, ZERO, 0);
3509 break;
3510 case kCondGT:
3511 if (gt_bias) {
3512 __ CultS(0, rhs, lhs);
3513 } else {
3514 __ ColtS(0, rhs, lhs);
3515 }
3516 __ LoadConst32(dst, 1);
3517 __ Movf(dst, ZERO, 0);
3518 break;
3519 case kCondGE:
3520 if (gt_bias) {
3521 __ CuleS(0, rhs, lhs);
3522 } else {
3523 __ ColeS(0, rhs, lhs);
3524 }
3525 __ LoadConst32(dst, 1);
3526 __ Movf(dst, ZERO, 0);
3527 break;
3528 default:
3529 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3530 UNREACHABLE();
3531 }
3532 }
3533 } else {
3534 DCHECK_EQ(type, Primitive::kPrimDouble);
3535 if (isR6) {
3536 switch (cond) {
3537 case kCondEQ:
3538 __ CmpEqD(FTMP, lhs, rhs);
3539 __ Mfc1(dst, FTMP);
3540 __ Andi(dst, dst, 1);
3541 break;
3542 case kCondNE:
3543 __ CmpEqD(FTMP, lhs, rhs);
3544 __ Mfc1(dst, FTMP);
3545 __ Addiu(dst, dst, 1);
3546 break;
3547 case kCondLT:
3548 if (gt_bias) {
3549 __ CmpLtD(FTMP, lhs, rhs);
3550 } else {
3551 __ CmpUltD(FTMP, lhs, rhs);
3552 }
3553 __ Mfc1(dst, FTMP);
3554 __ Andi(dst, dst, 1);
3555 break;
3556 case kCondLE:
3557 if (gt_bias) {
3558 __ CmpLeD(FTMP, lhs, rhs);
3559 } else {
3560 __ CmpUleD(FTMP, lhs, rhs);
3561 }
3562 __ Mfc1(dst, FTMP);
3563 __ Andi(dst, dst, 1);
3564 break;
3565 case kCondGT:
3566 if (gt_bias) {
3567 __ CmpUltD(FTMP, rhs, lhs);
3568 } else {
3569 __ CmpLtD(FTMP, rhs, lhs);
3570 }
3571 __ Mfc1(dst, FTMP);
3572 __ Andi(dst, dst, 1);
3573 break;
3574 case kCondGE:
3575 if (gt_bias) {
3576 __ CmpUleD(FTMP, rhs, lhs);
3577 } else {
3578 __ CmpLeD(FTMP, rhs, lhs);
3579 }
3580 __ Mfc1(dst, FTMP);
3581 __ Andi(dst, dst, 1);
3582 break;
3583 default:
3584 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3585 UNREACHABLE();
3586 }
3587 } else {
3588 switch (cond) {
3589 case kCondEQ:
3590 __ CeqD(0, lhs, rhs);
3591 __ LoadConst32(dst, 1);
3592 __ Movf(dst, ZERO, 0);
3593 break;
3594 case kCondNE:
3595 __ CeqD(0, lhs, rhs);
3596 __ LoadConst32(dst, 1);
3597 __ Movt(dst, ZERO, 0);
3598 break;
3599 case kCondLT:
3600 if (gt_bias) {
3601 __ ColtD(0, lhs, rhs);
3602 } else {
3603 __ CultD(0, lhs, rhs);
3604 }
3605 __ LoadConst32(dst, 1);
3606 __ Movf(dst, ZERO, 0);
3607 break;
3608 case kCondLE:
3609 if (gt_bias) {
3610 __ ColeD(0, lhs, rhs);
3611 } else {
3612 __ CuleD(0, lhs, rhs);
3613 }
3614 __ LoadConst32(dst, 1);
3615 __ Movf(dst, ZERO, 0);
3616 break;
3617 case kCondGT:
3618 if (gt_bias) {
3619 __ CultD(0, rhs, lhs);
3620 } else {
3621 __ ColtD(0, rhs, lhs);
3622 }
3623 __ LoadConst32(dst, 1);
3624 __ Movf(dst, ZERO, 0);
3625 break;
3626 case kCondGE:
3627 if (gt_bias) {
3628 __ CuleD(0, rhs, lhs);
3629 } else {
3630 __ ColeD(0, rhs, lhs);
3631 }
3632 __ LoadConst32(dst, 1);
3633 __ Movf(dst, ZERO, 0);
3634 break;
3635 default:
3636 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3637 UNREACHABLE();
3638 }
3639 }
3640 }
3641}
3642
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003643bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3644 bool gt_bias,
3645 Primitive::Type type,
3646 LocationSummary* input_locations,
3647 int cc) {
3648 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3649 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3650 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3651 if (type == Primitive::kPrimFloat) {
3652 switch (cond) {
3653 case kCondEQ:
3654 __ CeqS(cc, lhs, rhs);
3655 return false;
3656 case kCondNE:
3657 __ CeqS(cc, lhs, rhs);
3658 return true;
3659 case kCondLT:
3660 if (gt_bias) {
3661 __ ColtS(cc, lhs, rhs);
3662 } else {
3663 __ CultS(cc, lhs, rhs);
3664 }
3665 return false;
3666 case kCondLE:
3667 if (gt_bias) {
3668 __ ColeS(cc, lhs, rhs);
3669 } else {
3670 __ CuleS(cc, lhs, rhs);
3671 }
3672 return false;
3673 case kCondGT:
3674 if (gt_bias) {
3675 __ CultS(cc, rhs, lhs);
3676 } else {
3677 __ ColtS(cc, rhs, lhs);
3678 }
3679 return false;
3680 case kCondGE:
3681 if (gt_bias) {
3682 __ CuleS(cc, rhs, lhs);
3683 } else {
3684 __ ColeS(cc, rhs, lhs);
3685 }
3686 return false;
3687 default:
3688 LOG(FATAL) << "Unexpected non-floating-point condition";
3689 UNREACHABLE();
3690 }
3691 } else {
3692 DCHECK_EQ(type, Primitive::kPrimDouble);
3693 switch (cond) {
3694 case kCondEQ:
3695 __ CeqD(cc, lhs, rhs);
3696 return false;
3697 case kCondNE:
3698 __ CeqD(cc, lhs, rhs);
3699 return true;
3700 case kCondLT:
3701 if (gt_bias) {
3702 __ ColtD(cc, lhs, rhs);
3703 } else {
3704 __ CultD(cc, lhs, rhs);
3705 }
3706 return false;
3707 case kCondLE:
3708 if (gt_bias) {
3709 __ ColeD(cc, lhs, rhs);
3710 } else {
3711 __ CuleD(cc, lhs, rhs);
3712 }
3713 return false;
3714 case kCondGT:
3715 if (gt_bias) {
3716 __ CultD(cc, rhs, lhs);
3717 } else {
3718 __ ColtD(cc, rhs, lhs);
3719 }
3720 return false;
3721 case kCondGE:
3722 if (gt_bias) {
3723 __ CuleD(cc, rhs, lhs);
3724 } else {
3725 __ ColeD(cc, rhs, lhs);
3726 }
3727 return false;
3728 default:
3729 LOG(FATAL) << "Unexpected non-floating-point condition";
3730 UNREACHABLE();
3731 }
3732 }
3733}
3734
3735bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3736 bool gt_bias,
3737 Primitive::Type type,
3738 LocationSummary* input_locations,
3739 FRegister dst) {
3740 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3741 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3742 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3743 if (type == Primitive::kPrimFloat) {
3744 switch (cond) {
3745 case kCondEQ:
3746 __ CmpEqS(dst, lhs, rhs);
3747 return false;
3748 case kCondNE:
3749 __ CmpEqS(dst, lhs, rhs);
3750 return true;
3751 case kCondLT:
3752 if (gt_bias) {
3753 __ CmpLtS(dst, lhs, rhs);
3754 } else {
3755 __ CmpUltS(dst, lhs, rhs);
3756 }
3757 return false;
3758 case kCondLE:
3759 if (gt_bias) {
3760 __ CmpLeS(dst, lhs, rhs);
3761 } else {
3762 __ CmpUleS(dst, lhs, rhs);
3763 }
3764 return false;
3765 case kCondGT:
3766 if (gt_bias) {
3767 __ CmpUltS(dst, rhs, lhs);
3768 } else {
3769 __ CmpLtS(dst, rhs, lhs);
3770 }
3771 return false;
3772 case kCondGE:
3773 if (gt_bias) {
3774 __ CmpUleS(dst, rhs, lhs);
3775 } else {
3776 __ CmpLeS(dst, rhs, lhs);
3777 }
3778 return false;
3779 default:
3780 LOG(FATAL) << "Unexpected non-floating-point condition";
3781 UNREACHABLE();
3782 }
3783 } else {
3784 DCHECK_EQ(type, Primitive::kPrimDouble);
3785 switch (cond) {
3786 case kCondEQ:
3787 __ CmpEqD(dst, lhs, rhs);
3788 return false;
3789 case kCondNE:
3790 __ CmpEqD(dst, lhs, rhs);
3791 return true;
3792 case kCondLT:
3793 if (gt_bias) {
3794 __ CmpLtD(dst, lhs, rhs);
3795 } else {
3796 __ CmpUltD(dst, lhs, rhs);
3797 }
3798 return false;
3799 case kCondLE:
3800 if (gt_bias) {
3801 __ CmpLeD(dst, lhs, rhs);
3802 } else {
3803 __ CmpUleD(dst, lhs, rhs);
3804 }
3805 return false;
3806 case kCondGT:
3807 if (gt_bias) {
3808 __ CmpUltD(dst, rhs, lhs);
3809 } else {
3810 __ CmpLtD(dst, rhs, lhs);
3811 }
3812 return false;
3813 case kCondGE:
3814 if (gt_bias) {
3815 __ CmpUleD(dst, rhs, lhs);
3816 } else {
3817 __ CmpLeD(dst, rhs, lhs);
3818 }
3819 return false;
3820 default:
3821 LOG(FATAL) << "Unexpected non-floating-point condition";
3822 UNREACHABLE();
3823 }
3824 }
3825}
3826
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003827void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3828 bool gt_bias,
3829 Primitive::Type type,
3830 LocationSummary* locations,
3831 MipsLabel* label) {
3832 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3833 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3834 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3835 if (type == Primitive::kPrimFloat) {
3836 if (isR6) {
3837 switch (cond) {
3838 case kCondEQ:
3839 __ CmpEqS(FTMP, lhs, rhs);
3840 __ Bc1nez(FTMP, label);
3841 break;
3842 case kCondNE:
3843 __ CmpEqS(FTMP, lhs, rhs);
3844 __ Bc1eqz(FTMP, label);
3845 break;
3846 case kCondLT:
3847 if (gt_bias) {
3848 __ CmpLtS(FTMP, lhs, rhs);
3849 } else {
3850 __ CmpUltS(FTMP, lhs, rhs);
3851 }
3852 __ Bc1nez(FTMP, label);
3853 break;
3854 case kCondLE:
3855 if (gt_bias) {
3856 __ CmpLeS(FTMP, lhs, rhs);
3857 } else {
3858 __ CmpUleS(FTMP, lhs, rhs);
3859 }
3860 __ Bc1nez(FTMP, label);
3861 break;
3862 case kCondGT:
3863 if (gt_bias) {
3864 __ CmpUltS(FTMP, rhs, lhs);
3865 } else {
3866 __ CmpLtS(FTMP, rhs, lhs);
3867 }
3868 __ Bc1nez(FTMP, label);
3869 break;
3870 case kCondGE:
3871 if (gt_bias) {
3872 __ CmpUleS(FTMP, rhs, lhs);
3873 } else {
3874 __ CmpLeS(FTMP, rhs, lhs);
3875 }
3876 __ Bc1nez(FTMP, label);
3877 break;
3878 default:
3879 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003880 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003881 }
3882 } else {
3883 switch (cond) {
3884 case kCondEQ:
3885 __ CeqS(0, lhs, rhs);
3886 __ Bc1t(0, label);
3887 break;
3888 case kCondNE:
3889 __ CeqS(0, lhs, rhs);
3890 __ Bc1f(0, label);
3891 break;
3892 case kCondLT:
3893 if (gt_bias) {
3894 __ ColtS(0, lhs, rhs);
3895 } else {
3896 __ CultS(0, lhs, rhs);
3897 }
3898 __ Bc1t(0, label);
3899 break;
3900 case kCondLE:
3901 if (gt_bias) {
3902 __ ColeS(0, lhs, rhs);
3903 } else {
3904 __ CuleS(0, lhs, rhs);
3905 }
3906 __ Bc1t(0, label);
3907 break;
3908 case kCondGT:
3909 if (gt_bias) {
3910 __ CultS(0, rhs, lhs);
3911 } else {
3912 __ ColtS(0, rhs, lhs);
3913 }
3914 __ Bc1t(0, label);
3915 break;
3916 case kCondGE:
3917 if (gt_bias) {
3918 __ CuleS(0, rhs, lhs);
3919 } else {
3920 __ ColeS(0, rhs, lhs);
3921 }
3922 __ Bc1t(0, label);
3923 break;
3924 default:
3925 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003926 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003927 }
3928 }
3929 } else {
3930 DCHECK_EQ(type, Primitive::kPrimDouble);
3931 if (isR6) {
3932 switch (cond) {
3933 case kCondEQ:
3934 __ CmpEqD(FTMP, lhs, rhs);
3935 __ Bc1nez(FTMP, label);
3936 break;
3937 case kCondNE:
3938 __ CmpEqD(FTMP, lhs, rhs);
3939 __ Bc1eqz(FTMP, label);
3940 break;
3941 case kCondLT:
3942 if (gt_bias) {
3943 __ CmpLtD(FTMP, lhs, rhs);
3944 } else {
3945 __ CmpUltD(FTMP, lhs, rhs);
3946 }
3947 __ Bc1nez(FTMP, label);
3948 break;
3949 case kCondLE:
3950 if (gt_bias) {
3951 __ CmpLeD(FTMP, lhs, rhs);
3952 } else {
3953 __ CmpUleD(FTMP, lhs, rhs);
3954 }
3955 __ Bc1nez(FTMP, label);
3956 break;
3957 case kCondGT:
3958 if (gt_bias) {
3959 __ CmpUltD(FTMP, rhs, lhs);
3960 } else {
3961 __ CmpLtD(FTMP, rhs, lhs);
3962 }
3963 __ Bc1nez(FTMP, label);
3964 break;
3965 case kCondGE:
3966 if (gt_bias) {
3967 __ CmpUleD(FTMP, rhs, lhs);
3968 } else {
3969 __ CmpLeD(FTMP, rhs, lhs);
3970 }
3971 __ Bc1nez(FTMP, label);
3972 break;
3973 default:
3974 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003975 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003976 }
3977 } else {
3978 switch (cond) {
3979 case kCondEQ:
3980 __ CeqD(0, lhs, rhs);
3981 __ Bc1t(0, label);
3982 break;
3983 case kCondNE:
3984 __ CeqD(0, lhs, rhs);
3985 __ Bc1f(0, label);
3986 break;
3987 case kCondLT:
3988 if (gt_bias) {
3989 __ ColtD(0, lhs, rhs);
3990 } else {
3991 __ CultD(0, lhs, rhs);
3992 }
3993 __ Bc1t(0, label);
3994 break;
3995 case kCondLE:
3996 if (gt_bias) {
3997 __ ColeD(0, lhs, rhs);
3998 } else {
3999 __ CuleD(0, lhs, rhs);
4000 }
4001 __ Bc1t(0, label);
4002 break;
4003 case kCondGT:
4004 if (gt_bias) {
4005 __ CultD(0, rhs, lhs);
4006 } else {
4007 __ ColtD(0, rhs, lhs);
4008 }
4009 __ Bc1t(0, label);
4010 break;
4011 case kCondGE:
4012 if (gt_bias) {
4013 __ CuleD(0, rhs, lhs);
4014 } else {
4015 __ ColeD(0, rhs, lhs);
4016 }
4017 __ Bc1t(0, label);
4018 break;
4019 default:
4020 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004021 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004022 }
4023 }
4024 }
4025}
4026
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004027void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004028 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004029 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004030 MipsLabel* false_target) {
4031 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004032
David Brazdil0debae72015-11-12 18:37:00 +00004033 if (true_target == nullptr && false_target == nullptr) {
4034 // Nothing to do. The code always falls through.
4035 return;
4036 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004037 // Constant condition, statically compared against "true" (integer value 1).
4038 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004039 if (true_target != nullptr) {
4040 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004041 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004043 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004044 if (false_target != nullptr) {
4045 __ B(false_target);
4046 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004047 }
David Brazdil0debae72015-11-12 18:37:00 +00004048 return;
4049 }
4050
4051 // The following code generates these patterns:
4052 // (1) true_target == nullptr && false_target != nullptr
4053 // - opposite condition true => branch to false_target
4054 // (2) true_target != nullptr && false_target == nullptr
4055 // - condition true => branch to true_target
4056 // (3) true_target != nullptr && false_target != nullptr
4057 // - condition true => branch to true_target
4058 // - branch to false_target
4059 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004060 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004061 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004062 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004063 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004064 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4065 } else {
4066 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4067 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004068 } else {
4069 // The condition instruction has not been materialized, use its inputs as
4070 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004071 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004072 Primitive::Type type = condition->InputAt(0)->GetType();
4073 LocationSummary* locations = cond->GetLocations();
4074 IfCondition if_cond = condition->GetCondition();
4075 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004076
David Brazdil0debae72015-11-12 18:37:00 +00004077 if (true_target == nullptr) {
4078 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004079 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004080 }
4081
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004082 switch (type) {
4083 default:
4084 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4085 break;
4086 case Primitive::kPrimLong:
4087 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4088 break;
4089 case Primitive::kPrimFloat:
4090 case Primitive::kPrimDouble:
4091 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4092 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004093 }
4094 }
David Brazdil0debae72015-11-12 18:37:00 +00004095
4096 // If neither branch falls through (case 3), the conditional branch to `true_target`
4097 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4098 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004099 __ B(false_target);
4100 }
4101}
4102
4103void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004105 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004106 locations->SetInAt(0, Location::RequiresRegister());
4107 }
4108}
4109
4110void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004111 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4112 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4113 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4114 nullptr : codegen_->GetLabelOf(true_successor);
4115 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4116 nullptr : codegen_->GetLabelOf(false_successor);
4117 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004118}
4119
4120void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4121 LocationSummary* locations = new (GetGraph()->GetArena())
4122 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004123 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004124 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004125 locations->SetInAt(0, Location::RequiresRegister());
4126 }
4127}
4128
4129void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004130 SlowPathCodeMIPS* slow_path =
4131 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004132 GenerateTestAndBranch(deoptimize,
4133 /* condition_input_index */ 0,
4134 slow_path->GetEntryLabel(),
4135 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004136}
4137
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004138// This function returns true if a conditional move can be generated for HSelect.
4139// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4140// branches and regular moves.
4141//
4142// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4143//
4144// While determining feasibility of a conditional move and setting inputs/outputs
4145// are two distinct tasks, this function does both because they share quite a bit
4146// of common logic.
4147static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4148 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4149 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4150 HCondition* condition = cond->AsCondition();
4151
4152 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4153 Primitive::Type dst_type = select->GetType();
4154
4155 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4156 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4157 bool is_true_value_zero_constant =
4158 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4159 bool is_false_value_zero_constant =
4160 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4161
4162 bool can_move_conditionally = false;
4163 bool use_const_for_false_in = false;
4164 bool use_const_for_true_in = false;
4165
4166 if (!cond->IsConstant()) {
4167 switch (cond_type) {
4168 default:
4169 switch (dst_type) {
4170 default:
4171 // Moving int on int condition.
4172 if (is_r6) {
4173 if (is_true_value_zero_constant) {
4174 // seleqz out_reg, false_reg, cond_reg
4175 can_move_conditionally = true;
4176 use_const_for_true_in = true;
4177 } else if (is_false_value_zero_constant) {
4178 // selnez out_reg, true_reg, cond_reg
4179 can_move_conditionally = true;
4180 use_const_for_false_in = true;
4181 } else if (materialized) {
4182 // Not materializing unmaterialized int conditions
4183 // to keep the instruction count low.
4184 // selnez AT, true_reg, cond_reg
4185 // seleqz TMP, false_reg, cond_reg
4186 // or out_reg, AT, TMP
4187 can_move_conditionally = true;
4188 }
4189 } else {
4190 // movn out_reg, true_reg/ZERO, cond_reg
4191 can_move_conditionally = true;
4192 use_const_for_true_in = is_true_value_zero_constant;
4193 }
4194 break;
4195 case Primitive::kPrimLong:
4196 // Moving long on int condition.
4197 if (is_r6) {
4198 if (is_true_value_zero_constant) {
4199 // seleqz out_reg_lo, false_reg_lo, cond_reg
4200 // seleqz out_reg_hi, false_reg_hi, cond_reg
4201 can_move_conditionally = true;
4202 use_const_for_true_in = true;
4203 } else if (is_false_value_zero_constant) {
4204 // selnez out_reg_lo, true_reg_lo, cond_reg
4205 // selnez out_reg_hi, true_reg_hi, cond_reg
4206 can_move_conditionally = true;
4207 use_const_for_false_in = true;
4208 }
4209 // Other long conditional moves would generate 6+ instructions,
4210 // which is too many.
4211 } else {
4212 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4213 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4214 can_move_conditionally = true;
4215 use_const_for_true_in = is_true_value_zero_constant;
4216 }
4217 break;
4218 case Primitive::kPrimFloat:
4219 case Primitive::kPrimDouble:
4220 // Moving float/double on int condition.
4221 if (is_r6) {
4222 if (materialized) {
4223 // Not materializing unmaterialized int conditions
4224 // to keep the instruction count low.
4225 can_move_conditionally = true;
4226 if (is_true_value_zero_constant) {
4227 // sltu TMP, ZERO, cond_reg
4228 // mtc1 TMP, temp_cond_reg
4229 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4230 use_const_for_true_in = true;
4231 } else if (is_false_value_zero_constant) {
4232 // sltu TMP, ZERO, cond_reg
4233 // mtc1 TMP, temp_cond_reg
4234 // selnez.fmt out_reg, true_reg, temp_cond_reg
4235 use_const_for_false_in = true;
4236 } else {
4237 // sltu TMP, ZERO, cond_reg
4238 // mtc1 TMP, temp_cond_reg
4239 // sel.fmt temp_cond_reg, false_reg, true_reg
4240 // mov.fmt out_reg, temp_cond_reg
4241 }
4242 }
4243 } else {
4244 // movn.fmt out_reg, true_reg, cond_reg
4245 can_move_conditionally = true;
4246 }
4247 break;
4248 }
4249 break;
4250 case Primitive::kPrimLong:
4251 // We don't materialize long comparison now
4252 // and use conditional branches instead.
4253 break;
4254 case Primitive::kPrimFloat:
4255 case Primitive::kPrimDouble:
4256 switch (dst_type) {
4257 default:
4258 // Moving int on float/double condition.
4259 if (is_r6) {
4260 if (is_true_value_zero_constant) {
4261 // mfc1 TMP, temp_cond_reg
4262 // seleqz out_reg, false_reg, TMP
4263 can_move_conditionally = true;
4264 use_const_for_true_in = true;
4265 } else if (is_false_value_zero_constant) {
4266 // mfc1 TMP, temp_cond_reg
4267 // selnez out_reg, true_reg, TMP
4268 can_move_conditionally = true;
4269 use_const_for_false_in = true;
4270 } else {
4271 // mfc1 TMP, temp_cond_reg
4272 // selnez AT, true_reg, TMP
4273 // seleqz TMP, false_reg, TMP
4274 // or out_reg, AT, TMP
4275 can_move_conditionally = true;
4276 }
4277 } else {
4278 // movt out_reg, true_reg/ZERO, cc
4279 can_move_conditionally = true;
4280 use_const_for_true_in = is_true_value_zero_constant;
4281 }
4282 break;
4283 case Primitive::kPrimLong:
4284 // Moving long on float/double condition.
4285 if (is_r6) {
4286 if (is_true_value_zero_constant) {
4287 // mfc1 TMP, temp_cond_reg
4288 // seleqz out_reg_lo, false_reg_lo, TMP
4289 // seleqz out_reg_hi, false_reg_hi, TMP
4290 can_move_conditionally = true;
4291 use_const_for_true_in = true;
4292 } else if (is_false_value_zero_constant) {
4293 // mfc1 TMP, temp_cond_reg
4294 // selnez out_reg_lo, true_reg_lo, TMP
4295 // selnez out_reg_hi, true_reg_hi, TMP
4296 can_move_conditionally = true;
4297 use_const_for_false_in = true;
4298 }
4299 // Other long conditional moves would generate 6+ instructions,
4300 // which is too many.
4301 } else {
4302 // movt out_reg_lo, true_reg_lo/ZERO, cc
4303 // movt out_reg_hi, true_reg_hi/ZERO, cc
4304 can_move_conditionally = true;
4305 use_const_for_true_in = is_true_value_zero_constant;
4306 }
4307 break;
4308 case Primitive::kPrimFloat:
4309 case Primitive::kPrimDouble:
4310 // Moving float/double on float/double condition.
4311 if (is_r6) {
4312 can_move_conditionally = true;
4313 if (is_true_value_zero_constant) {
4314 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4315 use_const_for_true_in = true;
4316 } else if (is_false_value_zero_constant) {
4317 // selnez.fmt out_reg, true_reg, temp_cond_reg
4318 use_const_for_false_in = true;
4319 } else {
4320 // sel.fmt temp_cond_reg, false_reg, true_reg
4321 // mov.fmt out_reg, temp_cond_reg
4322 }
4323 } else {
4324 // movt.fmt out_reg, true_reg, cc
4325 can_move_conditionally = true;
4326 }
4327 break;
4328 }
4329 break;
4330 }
4331 }
4332
4333 if (can_move_conditionally) {
4334 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4335 } else {
4336 DCHECK(!use_const_for_false_in);
4337 DCHECK(!use_const_for_true_in);
4338 }
4339
4340 if (locations_to_set != nullptr) {
4341 if (use_const_for_false_in) {
4342 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4343 } else {
4344 locations_to_set->SetInAt(0,
4345 Primitive::IsFloatingPointType(dst_type)
4346 ? Location::RequiresFpuRegister()
4347 : Location::RequiresRegister());
4348 }
4349 if (use_const_for_true_in) {
4350 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4351 } else {
4352 locations_to_set->SetInAt(1,
4353 Primitive::IsFloatingPointType(dst_type)
4354 ? Location::RequiresFpuRegister()
4355 : Location::RequiresRegister());
4356 }
4357 if (materialized) {
4358 locations_to_set->SetInAt(2, Location::RequiresRegister());
4359 }
4360 // On R6 we don't require the output to be the same as the
4361 // first input for conditional moves unlike on R2.
4362 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4363 if (is_out_same_as_first_in) {
4364 locations_to_set->SetOut(Location::SameAsFirstInput());
4365 } else {
4366 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4367 ? Location::RequiresFpuRegister()
4368 : Location::RequiresRegister());
4369 }
4370 }
4371
4372 return can_move_conditionally;
4373}
4374
4375void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4376 LocationSummary* locations = select->GetLocations();
4377 Location dst = locations->Out();
4378 Location src = locations->InAt(1);
4379 Register src_reg = ZERO;
4380 Register src_reg_high = ZERO;
4381 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4382 Register cond_reg = TMP;
4383 int cond_cc = 0;
4384 Primitive::Type cond_type = Primitive::kPrimInt;
4385 bool cond_inverted = false;
4386 Primitive::Type dst_type = select->GetType();
4387
4388 if (IsBooleanValueOrMaterializedCondition(cond)) {
4389 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4390 } else {
4391 HCondition* condition = cond->AsCondition();
4392 LocationSummary* cond_locations = cond->GetLocations();
4393 IfCondition if_cond = condition->GetCondition();
4394 cond_type = condition->InputAt(0)->GetType();
4395 switch (cond_type) {
4396 default:
4397 DCHECK_NE(cond_type, Primitive::kPrimLong);
4398 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4399 break;
4400 case Primitive::kPrimFloat:
4401 case Primitive::kPrimDouble:
4402 cond_inverted = MaterializeFpCompareR2(if_cond,
4403 condition->IsGtBias(),
4404 cond_type,
4405 cond_locations,
4406 cond_cc);
4407 break;
4408 }
4409 }
4410
4411 DCHECK(dst.Equals(locations->InAt(0)));
4412 if (src.IsRegister()) {
4413 src_reg = src.AsRegister<Register>();
4414 } else if (src.IsRegisterPair()) {
4415 src_reg = src.AsRegisterPairLow<Register>();
4416 src_reg_high = src.AsRegisterPairHigh<Register>();
4417 } else if (src.IsConstant()) {
4418 DCHECK(src.GetConstant()->IsZeroBitPattern());
4419 }
4420
4421 switch (cond_type) {
4422 default:
4423 switch (dst_type) {
4424 default:
4425 if (cond_inverted) {
4426 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4427 } else {
4428 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4429 }
4430 break;
4431 case Primitive::kPrimLong:
4432 if (cond_inverted) {
4433 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4434 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4435 } else {
4436 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4437 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4438 }
4439 break;
4440 case Primitive::kPrimFloat:
4441 if (cond_inverted) {
4442 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4443 } else {
4444 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4445 }
4446 break;
4447 case Primitive::kPrimDouble:
4448 if (cond_inverted) {
4449 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4450 } else {
4451 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4452 }
4453 break;
4454 }
4455 break;
4456 case Primitive::kPrimLong:
4457 LOG(FATAL) << "Unreachable";
4458 UNREACHABLE();
4459 case Primitive::kPrimFloat:
4460 case Primitive::kPrimDouble:
4461 switch (dst_type) {
4462 default:
4463 if (cond_inverted) {
4464 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4465 } else {
4466 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4467 }
4468 break;
4469 case Primitive::kPrimLong:
4470 if (cond_inverted) {
4471 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4472 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4473 } else {
4474 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4475 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4476 }
4477 break;
4478 case Primitive::kPrimFloat:
4479 if (cond_inverted) {
4480 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4481 } else {
4482 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4483 }
4484 break;
4485 case Primitive::kPrimDouble:
4486 if (cond_inverted) {
4487 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4488 } else {
4489 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4490 }
4491 break;
4492 }
4493 break;
4494 }
4495}
4496
4497void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4498 LocationSummary* locations = select->GetLocations();
4499 Location dst = locations->Out();
4500 Location false_src = locations->InAt(0);
4501 Location true_src = locations->InAt(1);
4502 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4503 Register cond_reg = TMP;
4504 FRegister fcond_reg = FTMP;
4505 Primitive::Type cond_type = Primitive::kPrimInt;
4506 bool cond_inverted = false;
4507 Primitive::Type dst_type = select->GetType();
4508
4509 if (IsBooleanValueOrMaterializedCondition(cond)) {
4510 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4511 } else {
4512 HCondition* condition = cond->AsCondition();
4513 LocationSummary* cond_locations = cond->GetLocations();
4514 IfCondition if_cond = condition->GetCondition();
4515 cond_type = condition->InputAt(0)->GetType();
4516 switch (cond_type) {
4517 default:
4518 DCHECK_NE(cond_type, Primitive::kPrimLong);
4519 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4520 break;
4521 case Primitive::kPrimFloat:
4522 case Primitive::kPrimDouble:
4523 cond_inverted = MaterializeFpCompareR6(if_cond,
4524 condition->IsGtBias(),
4525 cond_type,
4526 cond_locations,
4527 fcond_reg);
4528 break;
4529 }
4530 }
4531
4532 if (true_src.IsConstant()) {
4533 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4534 }
4535 if (false_src.IsConstant()) {
4536 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4537 }
4538
4539 switch (dst_type) {
4540 default:
4541 if (Primitive::IsFloatingPointType(cond_type)) {
4542 __ Mfc1(cond_reg, fcond_reg);
4543 }
4544 if (true_src.IsConstant()) {
4545 if (cond_inverted) {
4546 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4547 } else {
4548 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4549 }
4550 } else if (false_src.IsConstant()) {
4551 if (cond_inverted) {
4552 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4553 } else {
4554 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4555 }
4556 } else {
4557 DCHECK_NE(cond_reg, AT);
4558 if (cond_inverted) {
4559 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4560 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4561 } else {
4562 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4563 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4564 }
4565 __ Or(dst.AsRegister<Register>(), AT, TMP);
4566 }
4567 break;
4568 case Primitive::kPrimLong: {
4569 if (Primitive::IsFloatingPointType(cond_type)) {
4570 __ Mfc1(cond_reg, fcond_reg);
4571 }
4572 Register dst_lo = dst.AsRegisterPairLow<Register>();
4573 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4574 if (true_src.IsConstant()) {
4575 Register src_lo = false_src.AsRegisterPairLow<Register>();
4576 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4577 if (cond_inverted) {
4578 __ Selnez(dst_lo, src_lo, cond_reg);
4579 __ Selnez(dst_hi, src_hi, cond_reg);
4580 } else {
4581 __ Seleqz(dst_lo, src_lo, cond_reg);
4582 __ Seleqz(dst_hi, src_hi, cond_reg);
4583 }
4584 } else {
4585 DCHECK(false_src.IsConstant());
4586 Register src_lo = true_src.AsRegisterPairLow<Register>();
4587 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4588 if (cond_inverted) {
4589 __ Seleqz(dst_lo, src_lo, cond_reg);
4590 __ Seleqz(dst_hi, src_hi, cond_reg);
4591 } else {
4592 __ Selnez(dst_lo, src_lo, cond_reg);
4593 __ Selnez(dst_hi, src_hi, cond_reg);
4594 }
4595 }
4596 break;
4597 }
4598 case Primitive::kPrimFloat: {
4599 if (!Primitive::IsFloatingPointType(cond_type)) {
4600 // sel*.fmt tests bit 0 of the condition register, account for that.
4601 __ Sltu(TMP, ZERO, cond_reg);
4602 __ Mtc1(TMP, fcond_reg);
4603 }
4604 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4605 if (true_src.IsConstant()) {
4606 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4607 if (cond_inverted) {
4608 __ SelnezS(dst_reg, src_reg, fcond_reg);
4609 } else {
4610 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4611 }
4612 } else if (false_src.IsConstant()) {
4613 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4614 if (cond_inverted) {
4615 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4616 } else {
4617 __ SelnezS(dst_reg, src_reg, fcond_reg);
4618 }
4619 } else {
4620 if (cond_inverted) {
4621 __ SelS(fcond_reg,
4622 true_src.AsFpuRegister<FRegister>(),
4623 false_src.AsFpuRegister<FRegister>());
4624 } else {
4625 __ SelS(fcond_reg,
4626 false_src.AsFpuRegister<FRegister>(),
4627 true_src.AsFpuRegister<FRegister>());
4628 }
4629 __ MovS(dst_reg, fcond_reg);
4630 }
4631 break;
4632 }
4633 case Primitive::kPrimDouble: {
4634 if (!Primitive::IsFloatingPointType(cond_type)) {
4635 // sel*.fmt tests bit 0 of the condition register, account for that.
4636 __ Sltu(TMP, ZERO, cond_reg);
4637 __ Mtc1(TMP, fcond_reg);
4638 }
4639 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4640 if (true_src.IsConstant()) {
4641 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4642 if (cond_inverted) {
4643 __ SelnezD(dst_reg, src_reg, fcond_reg);
4644 } else {
4645 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4646 }
4647 } else if (false_src.IsConstant()) {
4648 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4649 if (cond_inverted) {
4650 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4651 } else {
4652 __ SelnezD(dst_reg, src_reg, fcond_reg);
4653 }
4654 } else {
4655 if (cond_inverted) {
4656 __ SelD(fcond_reg,
4657 true_src.AsFpuRegister<FRegister>(),
4658 false_src.AsFpuRegister<FRegister>());
4659 } else {
4660 __ SelD(fcond_reg,
4661 false_src.AsFpuRegister<FRegister>(),
4662 true_src.AsFpuRegister<FRegister>());
4663 }
4664 __ MovD(dst_reg, fcond_reg);
4665 }
4666 break;
4667 }
4668 }
4669}
4670
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004671void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4672 LocationSummary* locations = new (GetGraph()->GetArena())
4673 LocationSummary(flag, LocationSummary::kNoCall);
4674 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004675}
4676
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004677void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4678 __ LoadFromOffset(kLoadWord,
4679 flag->GetLocations()->Out().AsRegister<Register>(),
4680 SP,
4681 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004682}
4683
David Brazdil74eb1b22015-12-14 11:44:01 +00004684void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004686 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004687}
4688
4689void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004690 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4691 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4692 if (is_r6) {
4693 GenConditionalMoveR6(select);
4694 } else {
4695 GenConditionalMoveR2(select);
4696 }
4697 } else {
4698 LocationSummary* locations = select->GetLocations();
4699 MipsLabel false_target;
4700 GenerateTestAndBranch(select,
4701 /* condition_input_index */ 2,
4702 /* true_target */ nullptr,
4703 &false_target);
4704 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4705 __ Bind(&false_target);
4706 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004707}
4708
David Srbecky0cf44932015-12-09 14:09:59 +00004709void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4710 new (GetGraph()->GetArena()) LocationSummary(info);
4711}
4712
David Srbeckyd28f4a02016-03-14 17:14:24 +00004713void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4714 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004715}
4716
4717void CodeGeneratorMIPS::GenerateNop() {
4718 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004719}
4720
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004721void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4722 Primitive::Type field_type = field_info.GetFieldType();
4723 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4724 bool generate_volatile = field_info.IsVolatile() && is_wide;
4725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004726 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004727
4728 locations->SetInAt(0, Location::RequiresRegister());
4729 if (generate_volatile) {
4730 InvokeRuntimeCallingConvention calling_convention;
4731 // need A0 to hold base + offset
4732 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4733 if (field_type == Primitive::kPrimLong) {
4734 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4735 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004736 // Use Location::Any() to prevent situations when running out of available fp registers.
4737 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004738 // Need some temp core regs since FP results are returned in core registers
4739 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4740 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4741 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4742 }
4743 } else {
4744 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4745 locations->SetOut(Location::RequiresFpuRegister());
4746 } else {
4747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4748 }
4749 }
4750}
4751
4752void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4753 const FieldInfo& field_info,
4754 uint32_t dex_pc) {
4755 Primitive::Type type = field_info.GetFieldType();
4756 LocationSummary* locations = instruction->GetLocations();
4757 Register obj = locations->InAt(0).AsRegister<Register>();
4758 LoadOperandType load_type = kLoadUnsignedByte;
4759 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004760 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004761 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004762
4763 switch (type) {
4764 case Primitive::kPrimBoolean:
4765 load_type = kLoadUnsignedByte;
4766 break;
4767 case Primitive::kPrimByte:
4768 load_type = kLoadSignedByte;
4769 break;
4770 case Primitive::kPrimShort:
4771 load_type = kLoadSignedHalfword;
4772 break;
4773 case Primitive::kPrimChar:
4774 load_type = kLoadUnsignedHalfword;
4775 break;
4776 case Primitive::kPrimInt:
4777 case Primitive::kPrimFloat:
4778 case Primitive::kPrimNot:
4779 load_type = kLoadWord;
4780 break;
4781 case Primitive::kPrimLong:
4782 case Primitive::kPrimDouble:
4783 load_type = kLoadDoubleword;
4784 break;
4785 case Primitive::kPrimVoid:
4786 LOG(FATAL) << "Unreachable type " << type;
4787 UNREACHABLE();
4788 }
4789
4790 if (is_volatile && load_type == kLoadDoubleword) {
4791 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004792 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004793 // Do implicit Null check
4794 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4795 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004796 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004797 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4798 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004799 // FP results are returned in core registers. Need to move them.
4800 Location out = locations->Out();
4801 if (out.IsFpuRegister()) {
4802 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4803 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4804 out.AsFpuRegister<FRegister>());
4805 } else {
4806 DCHECK(out.IsDoubleStackSlot());
4807 __ StoreToOffset(kStoreWord,
4808 locations->GetTemp(1).AsRegister<Register>(),
4809 SP,
4810 out.GetStackIndex());
4811 __ StoreToOffset(kStoreWord,
4812 locations->GetTemp(2).AsRegister<Register>(),
4813 SP,
4814 out.GetStackIndex() + 4);
4815 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004816 }
4817 } else {
4818 if (!Primitive::IsFloatingPointType(type)) {
4819 Register dst;
4820 if (type == Primitive::kPrimLong) {
4821 DCHECK(locations->Out().IsRegisterPair());
4822 dst = locations->Out().AsRegisterPairLow<Register>();
4823 } else {
4824 DCHECK(locations->Out().IsRegister());
4825 dst = locations->Out().AsRegister<Register>();
4826 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004827 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004828 } else {
4829 DCHECK(locations->Out().IsFpuRegister());
4830 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4831 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004832 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004833 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004834 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004835 }
4836 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004837 }
4838
4839 if (is_volatile) {
4840 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4841 }
4842}
4843
4844void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4845 Primitive::Type field_type = field_info.GetFieldType();
4846 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4847 bool generate_volatile = field_info.IsVolatile() && is_wide;
4848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004849 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850
4851 locations->SetInAt(0, Location::RequiresRegister());
4852 if (generate_volatile) {
4853 InvokeRuntimeCallingConvention calling_convention;
4854 // need A0 to hold base + offset
4855 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4856 if (field_type == Primitive::kPrimLong) {
4857 locations->SetInAt(1, Location::RegisterPairLocation(
4858 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4859 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004860 // Use Location::Any() to prevent situations when running out of available fp registers.
4861 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004862 // Pass FP parameters in core registers.
4863 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4864 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4865 }
4866 } else {
4867 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004868 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004869 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004870 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871 }
4872 }
4873}
4874
4875void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4876 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01004877 uint32_t dex_pc,
4878 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004879 Primitive::Type type = field_info.GetFieldType();
4880 LocationSummary* locations = instruction->GetLocations();
4881 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004882 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004883 StoreOperandType store_type = kStoreByte;
4884 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004885 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004886 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004887
4888 switch (type) {
4889 case Primitive::kPrimBoolean:
4890 case Primitive::kPrimByte:
4891 store_type = kStoreByte;
4892 break;
4893 case Primitive::kPrimShort:
4894 case Primitive::kPrimChar:
4895 store_type = kStoreHalfword;
4896 break;
4897 case Primitive::kPrimInt:
4898 case Primitive::kPrimFloat:
4899 case Primitive::kPrimNot:
4900 store_type = kStoreWord;
4901 break;
4902 case Primitive::kPrimLong:
4903 case Primitive::kPrimDouble:
4904 store_type = kStoreDoubleword;
4905 break;
4906 case Primitive::kPrimVoid:
4907 LOG(FATAL) << "Unreachable type " << type;
4908 UNREACHABLE();
4909 }
4910
4911 if (is_volatile) {
4912 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4913 }
4914
4915 if (is_volatile && store_type == kStoreDoubleword) {
4916 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004917 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004918 // Do implicit Null check.
4919 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4920 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4921 if (type == Primitive::kPrimDouble) {
4922 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004923 if (value_location.IsFpuRegister()) {
4924 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4925 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004926 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004927 value_location.AsFpuRegister<FRegister>());
4928 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004929 __ LoadFromOffset(kLoadWord,
4930 locations->GetTemp(1).AsRegister<Register>(),
4931 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004932 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004933 __ LoadFromOffset(kLoadWord,
4934 locations->GetTemp(2).AsRegister<Register>(),
4935 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004936 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004937 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004938 DCHECK(value_location.IsConstant());
4939 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4940 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004941 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4942 locations->GetTemp(1).AsRegister<Register>(),
4943 value);
4944 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004945 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004946 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004947 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4948 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004949 if (value_location.IsConstant()) {
4950 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4951 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4952 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004953 Register src;
4954 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004955 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004956 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004957 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004958 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004959 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004960 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004961 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004962 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004963 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004964 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004965 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966 }
4967 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004968 }
4969
4970 // TODO: memory barriers?
4971 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004972 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01004973 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004974 }
4975
4976 if (is_volatile) {
4977 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4978 }
4979}
4980
4981void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4982 HandleFieldGet(instruction, instruction->GetFieldInfo());
4983}
4984
4985void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4986 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4987}
4988
4989void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4990 HandleFieldSet(instruction, instruction->GetFieldInfo());
4991}
4992
4993void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01004994 HandleFieldSet(instruction,
4995 instruction->GetFieldInfo(),
4996 instruction->GetDexPc(),
4997 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004998}
4999
Alexey Frunze06a46c42016-07-19 15:00:40 -07005000void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5001 HInstruction* instruction ATTRIBUTE_UNUSED,
5002 Location root,
5003 Register obj,
5004 uint32_t offset) {
5005 Register root_reg = root.AsRegister<Register>();
5006 if (kEmitCompilerReadBarrier) {
5007 UNIMPLEMENTED(FATAL) << "for read barrier";
5008 } else {
5009 // Plain GC root load with no read barrier.
5010 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5011 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5012 // Note that GC roots are not affected by heap poisoning, thus we
5013 // do not have to unpoison `root_reg` here.
5014 }
5015}
5016
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005017void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5018 LocationSummary::CallKind call_kind =
5019 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5021 locations->SetInAt(0, Location::RequiresRegister());
5022 locations->SetInAt(1, Location::RequiresRegister());
5023 // The output does overlap inputs.
5024 // Note that TypeCheckSlowPathMIPS uses this register too.
5025 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5026}
5027
5028void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5029 LocationSummary* locations = instruction->GetLocations();
5030 Register obj = locations->InAt(0).AsRegister<Register>();
5031 Register cls = locations->InAt(1).AsRegister<Register>();
5032 Register out = locations->Out().AsRegister<Register>();
5033
5034 MipsLabel done;
5035
5036 // Return 0 if `obj` is null.
5037 // TODO: Avoid this check if we know `obj` is not null.
5038 __ Move(out, ZERO);
5039 __ Beqz(obj, &done);
5040
5041 // Compare the class of `obj` with `cls`.
5042 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5043 if (instruction->IsExactCheck()) {
5044 // Classes must be equal for the instanceof to succeed.
5045 __ Xor(out, out, cls);
5046 __ Sltiu(out, out, 1);
5047 } else {
5048 // If the classes are not equal, we go into a slow path.
5049 DCHECK(locations->OnlyCallsOnSlowPath());
5050 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5051 codegen_->AddSlowPath(slow_path);
5052 __ Bne(out, cls, slow_path->GetEntryLabel());
5053 __ LoadConst32(out, 1);
5054 __ Bind(slow_path->GetExitLabel());
5055 }
5056
5057 __ Bind(&done);
5058}
5059
5060void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5062 locations->SetOut(Location::ConstantLocation(constant));
5063}
5064
5065void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5066 // Will be generated at use site.
5067}
5068
5069void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5070 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5071 locations->SetOut(Location::ConstantLocation(constant));
5072}
5073
5074void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5075 // Will be generated at use site.
5076}
5077
5078void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5079 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5080 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5081}
5082
5083void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5084 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005085 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005086 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005087 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005088}
5089
5090void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5091 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5092 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005093 Location receiver = invoke->GetLocations()->InAt(0);
5094 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005095 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005096
5097 // Set the hidden argument.
5098 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5099 invoke->GetDexMethodIndex());
5100
5101 // temp = object->GetClass();
5102 if (receiver.IsStackSlot()) {
5103 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5104 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5105 } else {
5106 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5107 }
5108 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005109 __ LoadFromOffset(kLoadWord, temp, temp,
5110 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5111 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005112 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005113 // temp = temp->GetImtEntryAt(method_offset);
5114 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5115 // T9 = temp->GetEntryPoint();
5116 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5117 // T9();
5118 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005119 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120 DCHECK(!codegen_->IsLeafMethod());
5121 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5122}
5123
5124void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005125 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5126 if (intrinsic.TryDispatch(invoke)) {
5127 return;
5128 }
5129
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005130 HandleInvoke(invoke);
5131}
5132
5133void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005134 // Explicit clinit checks triggered by static invokes must have been pruned by
5135 // art::PrepareForRegisterAllocation.
5136 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005137
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005138 bool has_extra_input = invoke->HasPcRelativeDexCache();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005139
Chris Larsen701566a2015-10-27 15:29:13 -07005140 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5141 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005142 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5143 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5144 }
Chris Larsen701566a2015-10-27 15:29:13 -07005145 return;
5146 }
5147
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005148 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005149
5150 // Add the extra input register if either the dex cache array base register
5151 // or the PC-relative base register for accessing literals is needed.
5152 if (has_extra_input) {
5153 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5154 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005155}
5156
Chris Larsen701566a2015-10-27 15:29:13 -07005157static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005158 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005159 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5160 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005161 return true;
5162 }
5163 return false;
5164}
5165
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005166HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005167 HLoadString::LoadKind desired_string_load_kind) {
5168 if (kEmitCompilerReadBarrier) {
5169 UNIMPLEMENTED(FATAL) << "for read barrier";
5170 }
5171 // We disable PC-relative load when there is an irreducible loop, as the optimization
5172 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005173 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5174 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005175 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5176 bool fallback_load = has_irreducible_loops;
5177 switch (desired_string_load_kind) {
5178 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5179 DCHECK(!GetCompilerOptions().GetCompilePic());
5180 break;
5181 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5182 DCHECK(GetCompilerOptions().GetCompilePic());
5183 break;
5184 case HLoadString::LoadKind::kBootImageAddress:
5185 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005186 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005187 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005188 break;
5189 case HLoadString::LoadKind::kDexCacheViaMethod:
5190 fallback_load = false;
5191 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005192 case HLoadString::LoadKind::kJitTableAddress:
5193 DCHECK(Runtime::Current()->UseJitCompilation());
5194 // TODO: implement.
5195 fallback_load = true;
5196 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005197 }
5198 if (fallback_load) {
5199 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5200 }
5201 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005202}
5203
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005204HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5205 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005206 if (kEmitCompilerReadBarrier) {
5207 UNIMPLEMENTED(FATAL) << "for read barrier";
5208 }
5209 // We disable pc-relative load when there is an irreducible loop, as the optimization
5210 // is incompatible with it.
5211 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5212 bool fallback_load = has_irreducible_loops;
5213 switch (desired_class_load_kind) {
5214 case HLoadClass::LoadKind::kReferrersClass:
5215 fallback_load = false;
5216 break;
5217 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5218 DCHECK(!GetCompilerOptions().GetCompilePic());
5219 break;
5220 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5221 DCHECK(GetCompilerOptions().GetCompilePic());
5222 break;
5223 case HLoadClass::LoadKind::kBootImageAddress:
5224 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005225 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005226 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005227 fallback_load = true;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005228 break;
5229 case HLoadClass::LoadKind::kDexCachePcRelative:
5230 DCHECK(!Runtime::Current()->UseJitCompilation());
5231 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5232 // with irreducible loops.
5233 break;
5234 case HLoadClass::LoadKind::kDexCacheViaMethod:
5235 fallback_load = false;
5236 break;
5237 }
5238 if (fallback_load) {
5239 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5240 }
5241 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005242}
5243
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005244Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5245 Register temp) {
5246 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5247 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5248 if (!invoke->GetLocations()->Intrinsified()) {
5249 return location.AsRegister<Register>();
5250 }
5251 // For intrinsics we allow any location, so it may be on the stack.
5252 if (!location.IsRegister()) {
5253 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5254 return temp;
5255 }
5256 // For register locations, check if the register was saved. If so, get it from the stack.
5257 // Note: There is a chance that the register was saved but not overwritten, so we could
5258 // save one load. However, since this is just an intrinsic slow path we prefer this
5259 // simple and more robust approach rather that trying to determine if that's the case.
5260 SlowPathCode* slow_path = GetCurrentSlowPath();
5261 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5262 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5263 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5264 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5265 return temp;
5266 }
5267 return location.AsRegister<Register>();
5268}
5269
Vladimir Markodc151b22015-10-15 18:02:30 +01005270HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5271 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005272 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005273 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5274 // We disable PC-relative load when there is an irreducible loop, as the optimization
5275 // is incompatible with it.
5276 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5277 bool fallback_load = true;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005278 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005279 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005280 fallback_load = has_irreducible_loops;
5281 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005282 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005283 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005284 break;
5285 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005286 if (fallback_load) {
5287 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5288 dispatch_info.method_load_data = 0;
5289 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005290 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005291}
5292
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005293void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5294 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005295 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005296 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5297 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005298 Register base_reg = invoke->HasPcRelativeDexCache()
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005299 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5300 : ZERO;
5301
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005302 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005303 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005304 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005305 uint32_t offset =
5306 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005307 __ LoadFromOffset(kLoadWord,
5308 temp.AsRegister<Register>(),
5309 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005310 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005311 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005312 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005313 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005314 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005315 break;
5316 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5317 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5318 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005319 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5320 HMipsDexCacheArraysBase* base =
5321 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5322 int32_t offset =
5323 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5324 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5325 break;
5326 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005327 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005328 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005329 Register reg = temp.AsRegister<Register>();
5330 Register method_reg;
5331 if (current_method.IsRegister()) {
5332 method_reg = current_method.AsRegister<Register>();
5333 } else {
5334 // TODO: use the appropriate DCHECK() here if possible.
5335 // DCHECK(invoke->GetLocations()->Intrinsified());
5336 DCHECK(!current_method.IsValid());
5337 method_reg = reg;
5338 __ Lw(reg, SP, kCurrentMethodStackOffset);
5339 }
5340
5341 // temp = temp->dex_cache_resolved_methods_;
5342 __ LoadFromOffset(kLoadWord,
5343 reg,
5344 method_reg,
5345 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005346 // temp = temp[index_in_cache];
5347 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5348 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005349 __ LoadFromOffset(kLoadWord,
5350 reg,
5351 reg,
5352 CodeGenerator::GetCachePointerOffset(index_in_cache));
5353 break;
5354 }
5355 }
5356
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005357 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005358 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005359 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005360 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005361 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5362 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005363 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005364 T9,
5365 callee_method.AsRegister<Register>(),
5366 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005367 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005368 // T9()
5369 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005370 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005371 break;
5372 }
5373 DCHECK(!IsLeafMethod());
5374}
5375
5376void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005377 // Explicit clinit checks triggered by static invokes must have been pruned by
5378 // art::PrepareForRegisterAllocation.
5379 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005380
5381 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5382 return;
5383 }
5384
5385 LocationSummary* locations = invoke->GetLocations();
5386 codegen_->GenerateStaticOrDirectCall(invoke,
5387 locations->HasTemps()
5388 ? locations->GetTemp(0)
5389 : Location::NoLocation());
5390 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5391}
5392
Chris Larsen3acee732015-11-18 13:31:08 -08005393void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005394 // Use the calling convention instead of the location of the receiver, as
5395 // intrinsics may have put the receiver in a different register. In the intrinsics
5396 // slow path, the arguments have been moved to the right place, so here we are
5397 // guaranteed that the receiver is the first register of the calling convention.
5398 InvokeDexCallingConvention calling_convention;
5399 Register receiver = calling_convention.GetRegisterAt(0);
5400
Chris Larsen3acee732015-11-18 13:31:08 -08005401 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005402 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5403 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5404 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005405 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005406
5407 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005408 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005409 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410 // temp = temp->GetMethodAt(method_offset);
5411 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5412 // T9 = temp->GetEntryPoint();
5413 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5414 // T9();
5415 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005416 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005417}
5418
5419void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5420 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5421 return;
5422 }
5423
5424 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425 DCHECK(!codegen_->IsLeafMethod());
5426 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5427}
5428
5429void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005430 if (cls->NeedsAccessCheck()) {
5431 InvokeRuntimeCallingConvention calling_convention;
5432 CodeGenerator::CreateLoadClassLocationSummary(
5433 cls,
5434 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5435 Location::RegisterLocation(V0),
5436 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5437 return;
5438 }
5439
5440 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5441 ? LocationSummary::kCallOnSlowPath
5442 : LocationSummary::kNoCall;
5443 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5444 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5445 switch (load_kind) {
5446 // We need an extra register for PC-relative literals on R2.
5447 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5448 case HLoadClass::LoadKind::kBootImageAddress:
5449 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5450 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5451 break;
5452 }
5453 FALLTHROUGH_INTENDED;
5454 // We need an extra register for PC-relative dex cache accesses.
5455 case HLoadClass::LoadKind::kDexCachePcRelative:
5456 case HLoadClass::LoadKind::kReferrersClass:
5457 case HLoadClass::LoadKind::kDexCacheViaMethod:
5458 locations->SetInAt(0, Location::RequiresRegister());
5459 break;
5460 default:
5461 break;
5462 }
5463 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005464}
5465
5466void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5467 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005468 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005469 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005470 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005471 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005472 return;
5473 }
5474
Alexey Frunze06a46c42016-07-19 15:00:40 -07005475 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5476 Location out_loc = locations->Out();
5477 Register out = out_loc.AsRegister<Register>();
5478 Register base_or_current_method_reg;
5479 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5480 switch (load_kind) {
5481 // We need an extra register for PC-relative literals on R2.
5482 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5483 case HLoadClass::LoadKind::kBootImageAddress:
5484 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5485 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5486 break;
5487 // We need an extra register for PC-relative dex cache accesses.
5488 case HLoadClass::LoadKind::kDexCachePcRelative:
5489 case HLoadClass::LoadKind::kReferrersClass:
5490 case HLoadClass::LoadKind::kDexCacheViaMethod:
5491 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5492 break;
5493 default:
5494 base_or_current_method_reg = ZERO;
5495 break;
5496 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005497
Alexey Frunze06a46c42016-07-19 15:00:40 -07005498 bool generate_null_check = false;
5499 switch (load_kind) {
5500 case HLoadClass::LoadKind::kReferrersClass: {
5501 DCHECK(!cls->CanCallRuntime());
5502 DCHECK(!cls->MustGenerateClinitCheck());
5503 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5504 GenerateGcRootFieldLoad(cls,
5505 out_loc,
5506 base_or_current_method_reg,
5507 ArtMethod::DeclaringClassOffset().Int32Value());
5508 break;
5509 }
5510 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5511 DCHECK(!kEmitCompilerReadBarrier);
5512 __ LoadLiteral(out,
5513 base_or_current_method_reg,
5514 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5515 cls->GetTypeIndex()));
5516 break;
5517 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5518 DCHECK(!kEmitCompilerReadBarrier);
5519 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5520 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005521 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005522 break;
5523 }
5524 case HLoadClass::LoadKind::kBootImageAddress: {
5525 DCHECK(!kEmitCompilerReadBarrier);
5526 DCHECK_NE(cls->GetAddress(), 0u);
5527 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5528 __ LoadLiteral(out,
5529 base_or_current_method_reg,
5530 codegen_->DeduplicateBootImageAddressLiteral(address));
5531 break;
5532 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005533 case HLoadClass::LoadKind::kJitTableAddress: {
5534 LOG(FATAL) << "Unimplemented";
Alexey Frunze06a46c42016-07-19 15:00:40 -07005535 break;
5536 }
5537 case HLoadClass::LoadKind::kDexCachePcRelative: {
5538 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5539 int32_t offset =
5540 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5541 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5542 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5543 generate_null_check = !cls->IsInDexCache();
5544 break;
5545 }
5546 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5547 // /* GcRoot<mirror::Class>[] */ out =
5548 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5549 __ LoadFromOffset(kLoadWord,
5550 out,
5551 base_or_current_method_reg,
5552 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5553 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005554 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005555 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5556 generate_null_check = !cls->IsInDexCache();
5557 }
5558 }
5559
5560 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5561 DCHECK(cls->CanCallRuntime());
5562 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5563 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5564 codegen_->AddSlowPath(slow_path);
5565 if (generate_null_check) {
5566 __ Beqz(out, slow_path->GetEntryLabel());
5567 }
5568 if (cls->MustGenerateClinitCheck()) {
5569 GenerateClassInitializationCheck(slow_path, out);
5570 } else {
5571 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005572 }
5573 }
5574}
5575
5576static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005577 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005578}
5579
5580void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5581 LocationSummary* locations =
5582 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5583 locations->SetOut(Location::RequiresRegister());
5584}
5585
5586void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5587 Register out = load->GetLocations()->Out().AsRegister<Register>();
5588 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5589}
5590
5591void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5592 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5593}
5594
5595void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5596 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5597}
5598
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005599void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005600 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005602 HLoadString::LoadKind load_kind = load->GetLoadKind();
5603 switch (load_kind) {
5604 // We need an extra register for PC-relative literals on R2.
5605 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5606 case HLoadString::LoadKind::kBootImageAddress:
5607 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005608 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005609 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5610 break;
5611 }
5612 FALLTHROUGH_INTENDED;
5613 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005614 case HLoadString::LoadKind::kDexCacheViaMethod:
5615 locations->SetInAt(0, Location::RequiresRegister());
5616 break;
5617 default:
5618 break;
5619 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005620 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5621 InvokeRuntimeCallingConvention calling_convention;
5622 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5623 } else {
5624 locations->SetOut(Location::RequiresRegister());
5625 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005626}
5627
5628void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005629 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005630 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005631 Location out_loc = locations->Out();
5632 Register out = out_loc.AsRegister<Register>();
5633 Register base_or_current_method_reg;
5634 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5635 switch (load_kind) {
5636 // We need an extra register for PC-relative literals on R2.
5637 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5638 case HLoadString::LoadKind::kBootImageAddress:
5639 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005640 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005641 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5642 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005643 default:
5644 base_or_current_method_reg = ZERO;
5645 break;
5646 }
5647
5648 switch (load_kind) {
5649 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005650 __ LoadLiteral(out,
5651 base_or_current_method_reg,
5652 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5653 load->GetStringIndex()));
5654 return; // No dex cache slow path.
5655 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005656 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005657 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005658 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005659 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005660 return; // No dex cache slow path.
5661 }
5662 case HLoadString::LoadKind::kBootImageAddress: {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005663 DCHECK_NE(load->GetAddress(), 0u);
5664 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5665 __ LoadLiteral(out,
5666 base_or_current_method_reg,
5667 codegen_->DeduplicateBootImageAddressLiteral(address));
5668 return; // No dex cache slow path.
5669 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005670 case HLoadString::LoadKind::kBssEntry: {
5671 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5672 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005673 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005674 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5675 __ LoadFromOffset(kLoadWord, out, out, 0);
5676 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5677 codegen_->AddSlowPath(slow_path);
5678 __ Beqz(out, slow_path->GetEntryLabel());
5679 __ Bind(slow_path->GetExitLabel());
5680 return;
5681 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005682 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005683 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005684 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005685
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005686 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005687 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5688 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005689 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005690 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5691 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692}
5693
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005694void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5695 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5696 locations->SetOut(Location::ConstantLocation(constant));
5697}
5698
5699void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5700 // Will be generated at use site.
5701}
5702
5703void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5704 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005706 InvokeRuntimeCallingConvention calling_convention;
5707 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5708}
5709
5710void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5711 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005712 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005713 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5714 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005715 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005716 }
5717 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5718}
5719
5720void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5721 LocationSummary* locations =
5722 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5723 switch (mul->GetResultType()) {
5724 case Primitive::kPrimInt:
5725 case Primitive::kPrimLong:
5726 locations->SetInAt(0, Location::RequiresRegister());
5727 locations->SetInAt(1, Location::RequiresRegister());
5728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5729 break;
5730
5731 case Primitive::kPrimFloat:
5732 case Primitive::kPrimDouble:
5733 locations->SetInAt(0, Location::RequiresFpuRegister());
5734 locations->SetInAt(1, Location::RequiresFpuRegister());
5735 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5736 break;
5737
5738 default:
5739 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5740 }
5741}
5742
5743void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5744 Primitive::Type type = instruction->GetType();
5745 LocationSummary* locations = instruction->GetLocations();
5746 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5747
5748 switch (type) {
5749 case Primitive::kPrimInt: {
5750 Register dst = locations->Out().AsRegister<Register>();
5751 Register lhs = locations->InAt(0).AsRegister<Register>();
5752 Register rhs = locations->InAt(1).AsRegister<Register>();
5753
5754 if (isR6) {
5755 __ MulR6(dst, lhs, rhs);
5756 } else {
5757 __ MulR2(dst, lhs, rhs);
5758 }
5759 break;
5760 }
5761 case Primitive::kPrimLong: {
5762 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5763 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5764 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5765 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5766 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5767 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5768
5769 // Extra checks to protect caused by the existance of A1_A2.
5770 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5771 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5772 DCHECK_NE(dst_high, lhs_low);
5773 DCHECK_NE(dst_high, rhs_low);
5774
5775 // A_B * C_D
5776 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5777 // dst_lo: [ low(B*D) ]
5778 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5779
5780 if (isR6) {
5781 __ MulR6(TMP, lhs_high, rhs_low);
5782 __ MulR6(dst_high, lhs_low, rhs_high);
5783 __ Addu(dst_high, dst_high, TMP);
5784 __ MuhuR6(TMP, lhs_low, rhs_low);
5785 __ Addu(dst_high, dst_high, TMP);
5786 __ MulR6(dst_low, lhs_low, rhs_low);
5787 } else {
5788 __ MulR2(TMP, lhs_high, rhs_low);
5789 __ MulR2(dst_high, lhs_low, rhs_high);
5790 __ Addu(dst_high, dst_high, TMP);
5791 __ MultuR2(lhs_low, rhs_low);
5792 __ Mfhi(TMP);
5793 __ Addu(dst_high, dst_high, TMP);
5794 __ Mflo(dst_low);
5795 }
5796 break;
5797 }
5798 case Primitive::kPrimFloat:
5799 case Primitive::kPrimDouble: {
5800 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5801 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5802 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5803 if (type == Primitive::kPrimFloat) {
5804 __ MulS(dst, lhs, rhs);
5805 } else {
5806 __ MulD(dst, lhs, rhs);
5807 }
5808 break;
5809 }
5810 default:
5811 LOG(FATAL) << "Unexpected mul type " << type;
5812 }
5813}
5814
5815void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5816 LocationSummary* locations =
5817 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5818 switch (neg->GetResultType()) {
5819 case Primitive::kPrimInt:
5820 case Primitive::kPrimLong:
5821 locations->SetInAt(0, Location::RequiresRegister());
5822 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5823 break;
5824
5825 case Primitive::kPrimFloat:
5826 case Primitive::kPrimDouble:
5827 locations->SetInAt(0, Location::RequiresFpuRegister());
5828 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5829 break;
5830
5831 default:
5832 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5833 }
5834}
5835
5836void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5837 Primitive::Type type = instruction->GetType();
5838 LocationSummary* locations = instruction->GetLocations();
5839
5840 switch (type) {
5841 case Primitive::kPrimInt: {
5842 Register dst = locations->Out().AsRegister<Register>();
5843 Register src = locations->InAt(0).AsRegister<Register>();
5844 __ Subu(dst, ZERO, src);
5845 break;
5846 }
5847 case Primitive::kPrimLong: {
5848 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5849 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5850 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5851 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5852 __ Subu(dst_low, ZERO, src_low);
5853 __ Sltu(TMP, ZERO, dst_low);
5854 __ Subu(dst_high, ZERO, src_high);
5855 __ Subu(dst_high, dst_high, TMP);
5856 break;
5857 }
5858 case Primitive::kPrimFloat:
5859 case Primitive::kPrimDouble: {
5860 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5861 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5862 if (type == Primitive::kPrimFloat) {
5863 __ NegS(dst, src);
5864 } else {
5865 __ NegD(dst, src);
5866 }
5867 break;
5868 }
5869 default:
5870 LOG(FATAL) << "Unexpected neg type " << type;
5871 }
5872}
5873
5874void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5875 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005876 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005877 InvokeRuntimeCallingConvention calling_convention;
5878 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5879 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5880 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5881 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5882}
5883
5884void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5885 InvokeRuntimeCallingConvention calling_convention;
5886 Register current_method_register = calling_convention.GetRegisterAt(2);
5887 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5888 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005889 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005890 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005891 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5892 void*, uint32_t, int32_t, ArtMethod*>();
5893}
5894
5895void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5896 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005898 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005899 if (instruction->IsStringAlloc()) {
5900 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5901 } else {
5902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5903 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5904 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005905 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5906}
5907
5908void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005909 if (instruction->IsStringAlloc()) {
5910 // String is allocated through StringFactory. Call NewEmptyString entry point.
5911 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005912 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005913 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5914 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5915 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005916 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5918 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005919 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005920 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5921 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005922}
5923
5924void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5926 locations->SetInAt(0, Location::RequiresRegister());
5927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5928}
5929
5930void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5931 Primitive::Type type = instruction->GetType();
5932 LocationSummary* locations = instruction->GetLocations();
5933
5934 switch (type) {
5935 case Primitive::kPrimInt: {
5936 Register dst = locations->Out().AsRegister<Register>();
5937 Register src = locations->InAt(0).AsRegister<Register>();
5938 __ Nor(dst, src, ZERO);
5939 break;
5940 }
5941
5942 case Primitive::kPrimLong: {
5943 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5944 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5945 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5946 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5947 __ Nor(dst_high, src_high, ZERO);
5948 __ Nor(dst_low, src_low, ZERO);
5949 break;
5950 }
5951
5952 default:
5953 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5954 }
5955}
5956
5957void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5958 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5959 locations->SetInAt(0, Location::RequiresRegister());
5960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5961}
5962
5963void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5964 LocationSummary* locations = instruction->GetLocations();
5965 __ Xori(locations->Out().AsRegister<Register>(),
5966 locations->InAt(0).AsRegister<Register>(),
5967 1);
5968}
5969
5970void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005971 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5972 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005973}
5974
Calin Juravle2ae48182016-03-16 14:05:09 +00005975void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5976 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005977 return;
5978 }
5979 Location obj = instruction->GetLocations()->InAt(0);
5980
5981 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005982 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005983}
5984
Calin Juravle2ae48182016-03-16 14:05:09 +00005985void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005986 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005987 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005988
5989 Location obj = instruction->GetLocations()->InAt(0);
5990
5991 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5992}
5993
5994void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005995 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005996}
5997
5998void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5999 HandleBinaryOp(instruction);
6000}
6001
6002void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6003 HandleBinaryOp(instruction);
6004}
6005
6006void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6007 LOG(FATAL) << "Unreachable";
6008}
6009
6010void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6011 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6012}
6013
6014void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6016 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6017 if (location.IsStackSlot()) {
6018 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6019 } else if (location.IsDoubleStackSlot()) {
6020 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6021 }
6022 locations->SetOut(location);
6023}
6024
6025void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6026 ATTRIBUTE_UNUSED) {
6027 // Nothing to do, the parameter is already at its location.
6028}
6029
6030void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6031 LocationSummary* locations =
6032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6033 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6034}
6035
6036void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6037 ATTRIBUTE_UNUSED) {
6038 // Nothing to do, the method is already at its location.
6039}
6040
6041void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006043 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006044 locations->SetInAt(i, Location::Any());
6045 }
6046 locations->SetOut(Location::Any());
6047}
6048
6049void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6050 LOG(FATAL) << "Unreachable";
6051}
6052
6053void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6054 Primitive::Type type = rem->GetResultType();
6055 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006056 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6058
6059 switch (type) {
6060 case Primitive::kPrimInt:
6061 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006062 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6064 break;
6065
6066 case Primitive::kPrimLong: {
6067 InvokeRuntimeCallingConvention calling_convention;
6068 locations->SetInAt(0, Location::RegisterPairLocation(
6069 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6070 locations->SetInAt(1, Location::RegisterPairLocation(
6071 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6072 locations->SetOut(calling_convention.GetReturnLocation(type));
6073 break;
6074 }
6075
6076 case Primitive::kPrimFloat:
6077 case Primitive::kPrimDouble: {
6078 InvokeRuntimeCallingConvention calling_convention;
6079 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6080 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6081 locations->SetOut(calling_convention.GetReturnLocation(type));
6082 break;
6083 }
6084
6085 default:
6086 LOG(FATAL) << "Unexpected rem type " << type;
6087 }
6088}
6089
6090void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6091 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006092
6093 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006094 case Primitive::kPrimInt:
6095 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006096 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006097 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006098 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006099 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6100 break;
6101 }
6102 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006103 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006104 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006105 break;
6106 }
6107 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006108 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006109 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006110 break;
6111 }
6112 default:
6113 LOG(FATAL) << "Unexpected rem type " << type;
6114 }
6115}
6116
6117void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6118 memory_barrier->SetLocations(nullptr);
6119}
6120
6121void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6122 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6123}
6124
6125void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6126 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6127 Primitive::Type return_type = ret->InputAt(0)->GetType();
6128 locations->SetInAt(0, MipsReturnLocation(return_type));
6129}
6130
6131void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6132 codegen_->GenerateFrameExit();
6133}
6134
6135void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6136 ret->SetLocations(nullptr);
6137}
6138
6139void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6140 codegen_->GenerateFrameExit();
6141}
6142
Alexey Frunze92d90602015-12-18 18:16:36 -08006143void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6144 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006145}
6146
Alexey Frunze92d90602015-12-18 18:16:36 -08006147void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6148 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006149}
6150
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006151void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6152 HandleShift(shl);
6153}
6154
6155void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6156 HandleShift(shl);
6157}
6158
6159void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6160 HandleShift(shr);
6161}
6162
6163void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6164 HandleShift(shr);
6165}
6166
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6168 HandleBinaryOp(instruction);
6169}
6170
6171void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6172 HandleBinaryOp(instruction);
6173}
6174
6175void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6176 HandleFieldGet(instruction, instruction->GetFieldInfo());
6177}
6178
6179void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6180 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6181}
6182
6183void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6184 HandleFieldSet(instruction, instruction->GetFieldInfo());
6185}
6186
6187void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006188 HandleFieldSet(instruction,
6189 instruction->GetFieldInfo(),
6190 instruction->GetDexPc(),
6191 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006192}
6193
6194void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6195 HUnresolvedInstanceFieldGet* instruction) {
6196 FieldAccessCallingConventionMIPS calling_convention;
6197 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6198 instruction->GetFieldType(),
6199 calling_convention);
6200}
6201
6202void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6203 HUnresolvedInstanceFieldGet* instruction) {
6204 FieldAccessCallingConventionMIPS calling_convention;
6205 codegen_->GenerateUnresolvedFieldAccess(instruction,
6206 instruction->GetFieldType(),
6207 instruction->GetFieldIndex(),
6208 instruction->GetDexPc(),
6209 calling_convention);
6210}
6211
6212void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6213 HUnresolvedInstanceFieldSet* instruction) {
6214 FieldAccessCallingConventionMIPS calling_convention;
6215 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6216 instruction->GetFieldType(),
6217 calling_convention);
6218}
6219
6220void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6221 HUnresolvedInstanceFieldSet* instruction) {
6222 FieldAccessCallingConventionMIPS calling_convention;
6223 codegen_->GenerateUnresolvedFieldAccess(instruction,
6224 instruction->GetFieldType(),
6225 instruction->GetFieldIndex(),
6226 instruction->GetDexPc(),
6227 calling_convention);
6228}
6229
6230void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6231 HUnresolvedStaticFieldGet* instruction) {
6232 FieldAccessCallingConventionMIPS calling_convention;
6233 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6234 instruction->GetFieldType(),
6235 calling_convention);
6236}
6237
6238void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6239 HUnresolvedStaticFieldGet* instruction) {
6240 FieldAccessCallingConventionMIPS calling_convention;
6241 codegen_->GenerateUnresolvedFieldAccess(instruction,
6242 instruction->GetFieldType(),
6243 instruction->GetFieldIndex(),
6244 instruction->GetDexPc(),
6245 calling_convention);
6246}
6247
6248void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6249 HUnresolvedStaticFieldSet* instruction) {
6250 FieldAccessCallingConventionMIPS calling_convention;
6251 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6252 instruction->GetFieldType(),
6253 calling_convention);
6254}
6255
6256void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6257 HUnresolvedStaticFieldSet* instruction) {
6258 FieldAccessCallingConventionMIPS calling_convention;
6259 codegen_->GenerateUnresolvedFieldAccess(instruction,
6260 instruction->GetFieldType(),
6261 instruction->GetFieldIndex(),
6262 instruction->GetDexPc(),
6263 calling_convention);
6264}
6265
6266void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006267 LocationSummary* locations =
6268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006269 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006270}
6271
6272void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6273 HBasicBlock* block = instruction->GetBlock();
6274 if (block->GetLoopInformation() != nullptr) {
6275 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6276 // The back edge will generate the suspend check.
6277 return;
6278 }
6279 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6280 // The goto will generate the suspend check.
6281 return;
6282 }
6283 GenerateSuspendCheck(instruction, nullptr);
6284}
6285
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6287 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006289 InvokeRuntimeCallingConvention calling_convention;
6290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6291}
6292
6293void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006294 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6296}
6297
6298void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6299 Primitive::Type input_type = conversion->GetInputType();
6300 Primitive::Type result_type = conversion->GetResultType();
6301 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006302 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303
6304 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6305 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6306 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6307 }
6308
6309 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006310 if (!isR6 &&
6311 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6312 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006313 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314 }
6315
6316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6317
6318 if (call_kind == LocationSummary::kNoCall) {
6319 if (Primitive::IsFloatingPointType(input_type)) {
6320 locations->SetInAt(0, Location::RequiresFpuRegister());
6321 } else {
6322 locations->SetInAt(0, Location::RequiresRegister());
6323 }
6324
6325 if (Primitive::IsFloatingPointType(result_type)) {
6326 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6327 } else {
6328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6329 }
6330 } else {
6331 InvokeRuntimeCallingConvention calling_convention;
6332
6333 if (Primitive::IsFloatingPointType(input_type)) {
6334 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6335 } else {
6336 DCHECK_EQ(input_type, Primitive::kPrimLong);
6337 locations->SetInAt(0, Location::RegisterPairLocation(
6338 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6339 }
6340
6341 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6342 }
6343}
6344
6345void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6346 LocationSummary* locations = conversion->GetLocations();
6347 Primitive::Type result_type = conversion->GetResultType();
6348 Primitive::Type input_type = conversion->GetInputType();
6349 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006350 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006351
6352 DCHECK_NE(input_type, result_type);
6353
6354 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6355 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6356 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6357 Register src = locations->InAt(0).AsRegister<Register>();
6358
Alexey Frunzea871ef12016-06-27 15:20:11 -07006359 if (dst_low != src) {
6360 __ Move(dst_low, src);
6361 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006362 __ Sra(dst_high, src, 31);
6363 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6364 Register dst = locations->Out().AsRegister<Register>();
6365 Register src = (input_type == Primitive::kPrimLong)
6366 ? locations->InAt(0).AsRegisterPairLow<Register>()
6367 : locations->InAt(0).AsRegister<Register>();
6368
6369 switch (result_type) {
6370 case Primitive::kPrimChar:
6371 __ Andi(dst, src, 0xFFFF);
6372 break;
6373 case Primitive::kPrimByte:
6374 if (has_sign_extension) {
6375 __ Seb(dst, src);
6376 } else {
6377 __ Sll(dst, src, 24);
6378 __ Sra(dst, dst, 24);
6379 }
6380 break;
6381 case Primitive::kPrimShort:
6382 if (has_sign_extension) {
6383 __ Seh(dst, src);
6384 } else {
6385 __ Sll(dst, src, 16);
6386 __ Sra(dst, dst, 16);
6387 }
6388 break;
6389 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006390 if (dst != src) {
6391 __ Move(dst, src);
6392 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006393 break;
6394
6395 default:
6396 LOG(FATAL) << "Unexpected type conversion from " << input_type
6397 << " to " << result_type;
6398 }
6399 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006400 if (input_type == Primitive::kPrimLong) {
6401 if (isR6) {
6402 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6403 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6404 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6405 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6406 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6407 __ Mtc1(src_low, FTMP);
6408 __ Mthc1(src_high, FTMP);
6409 if (result_type == Primitive::kPrimFloat) {
6410 __ Cvtsl(dst, FTMP);
6411 } else {
6412 __ Cvtdl(dst, FTMP);
6413 }
6414 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006415 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6416 : kQuickL2d;
6417 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006418 if (result_type == Primitive::kPrimFloat) {
6419 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6420 } else {
6421 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6422 }
6423 }
6424 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425 Register src = locations->InAt(0).AsRegister<Register>();
6426 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6427 __ Mtc1(src, FTMP);
6428 if (result_type == Primitive::kPrimFloat) {
6429 __ Cvtsw(dst, FTMP);
6430 } else {
6431 __ Cvtdw(dst, FTMP);
6432 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006433 }
6434 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6435 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006436 if (result_type == Primitive::kPrimLong) {
6437 if (isR6) {
6438 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6439 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6440 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6441 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6442 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6443 MipsLabel truncate;
6444 MipsLabel done;
6445
6446 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6447 // value when the input is either a NaN or is outside of the range of the output type
6448 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6449 // the same result.
6450 //
6451 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6452 // value of the output type if the input is outside of the range after the truncation or
6453 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6454 // results. This matches the desired float/double-to-int/long conversion exactly.
6455 //
6456 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6457 //
6458 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6459 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6460 // even though it must be NAN2008=1 on R6.
6461 //
6462 // The code takes care of the different behaviors by first comparing the input to the
6463 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6464 // If the input is greater than or equal to the minimum, it procedes to the truncate
6465 // instruction, which will handle such an input the same way irrespective of NAN2008.
6466 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6467 // in order to return either zero or the minimum value.
6468 //
6469 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6470 // truncate instruction for MIPS64R6.
6471 if (input_type == Primitive::kPrimFloat) {
6472 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6473 __ LoadConst32(TMP, min_val);
6474 __ Mtc1(TMP, FTMP);
6475 __ CmpLeS(FTMP, FTMP, src);
6476 } else {
6477 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6478 __ LoadConst32(TMP, High32Bits(min_val));
6479 __ Mtc1(ZERO, FTMP);
6480 __ Mthc1(TMP, FTMP);
6481 __ CmpLeD(FTMP, FTMP, src);
6482 }
6483
6484 __ Bc1nez(FTMP, &truncate);
6485
6486 if (input_type == Primitive::kPrimFloat) {
6487 __ CmpEqS(FTMP, src, src);
6488 } else {
6489 __ CmpEqD(FTMP, src, src);
6490 }
6491 __ Move(dst_low, ZERO);
6492 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6493 __ Mfc1(TMP, FTMP);
6494 __ And(dst_high, dst_high, TMP);
6495
6496 __ B(&done);
6497
6498 __ Bind(&truncate);
6499
6500 if (input_type == Primitive::kPrimFloat) {
6501 __ TruncLS(FTMP, src);
6502 } else {
6503 __ TruncLD(FTMP, src);
6504 }
6505 __ Mfc1(dst_low, FTMP);
6506 __ Mfhc1(dst_high, FTMP);
6507
6508 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006509 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006510 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6511 : kQuickD2l;
6512 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006513 if (input_type == Primitive::kPrimFloat) {
6514 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6515 } else {
6516 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6517 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006518 }
6519 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006520 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6521 Register dst = locations->Out().AsRegister<Register>();
6522 MipsLabel truncate;
6523 MipsLabel done;
6524
6525 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6526 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6527 // even though it must be NAN2008=1 on R6.
6528 //
6529 // For details see the large comment above for the truncation of float/double to long on R6.
6530 //
6531 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6532 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006533 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006534 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6535 __ LoadConst32(TMP, min_val);
6536 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006537 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006538 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6539 __ LoadConst32(TMP, High32Bits(min_val));
6540 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006541 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006542 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006543
6544 if (isR6) {
6545 if (input_type == Primitive::kPrimFloat) {
6546 __ CmpLeS(FTMP, FTMP, src);
6547 } else {
6548 __ CmpLeD(FTMP, FTMP, src);
6549 }
6550 __ Bc1nez(FTMP, &truncate);
6551
6552 if (input_type == Primitive::kPrimFloat) {
6553 __ CmpEqS(FTMP, src, src);
6554 } else {
6555 __ CmpEqD(FTMP, src, src);
6556 }
6557 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6558 __ Mfc1(TMP, FTMP);
6559 __ And(dst, dst, TMP);
6560 } else {
6561 if (input_type == Primitive::kPrimFloat) {
6562 __ ColeS(0, FTMP, src);
6563 } else {
6564 __ ColeD(0, FTMP, src);
6565 }
6566 __ Bc1t(0, &truncate);
6567
6568 if (input_type == Primitive::kPrimFloat) {
6569 __ CeqS(0, src, src);
6570 } else {
6571 __ CeqD(0, src, src);
6572 }
6573 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6574 __ Movf(dst, ZERO, 0);
6575 }
6576
6577 __ B(&done);
6578
6579 __ Bind(&truncate);
6580
6581 if (input_type == Primitive::kPrimFloat) {
6582 __ TruncWS(FTMP, src);
6583 } else {
6584 __ TruncWD(FTMP, src);
6585 }
6586 __ Mfc1(dst, FTMP);
6587
6588 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006589 }
6590 } else if (Primitive::IsFloatingPointType(result_type) &&
6591 Primitive::IsFloatingPointType(input_type)) {
6592 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6593 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6594 if (result_type == Primitive::kPrimFloat) {
6595 __ Cvtsd(dst, src);
6596 } else {
6597 __ Cvtds(dst, src);
6598 }
6599 } else {
6600 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6601 << " to " << result_type;
6602 }
6603}
6604
6605void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6606 HandleShift(ushr);
6607}
6608
6609void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6610 HandleShift(ushr);
6611}
6612
6613void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6614 HandleBinaryOp(instruction);
6615}
6616
6617void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6618 HandleBinaryOp(instruction);
6619}
6620
6621void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6622 // Nothing to do, this should be removed during prepare for register allocator.
6623 LOG(FATAL) << "Unreachable";
6624}
6625
6626void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6627 // Nothing to do, this should be removed during prepare for register allocator.
6628 LOG(FATAL) << "Unreachable";
6629}
6630
6631void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006632 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006633}
6634
6635void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006636 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006637}
6638
6639void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006640 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006641}
6642
6643void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006644 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006645}
6646
6647void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006648 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006649}
6650
6651void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006652 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006653}
6654
6655void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006656 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006657}
6658
6659void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006660 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006661}
6662
6663void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006664 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006665}
6666
6667void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006668 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006669}
6670
6671void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006672 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006673}
6674
6675void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006676 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006677}
6678
6679void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006680 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006681}
6682
6683void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006684 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006685}
6686
6687void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006688 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006689}
6690
6691void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006692 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006693}
6694
6695void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006696 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006697}
6698
6699void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006700 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006701}
6702
6703void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006704 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006705}
6706
6707void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006708 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006709}
6710
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006711void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6712 LocationSummary* locations =
6713 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6714 locations->SetInAt(0, Location::RequiresRegister());
6715}
6716
Alexey Frunze96b66822016-09-10 02:32:44 -07006717void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6718 int32_t lower_bound,
6719 uint32_t num_entries,
6720 HBasicBlock* switch_block,
6721 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006723 Register temp_reg = TMP;
6724 __ Addiu32(temp_reg, value_reg, -lower_bound);
6725 // Jump to default if index is negative
6726 // Note: We don't check the case that index is positive while value < lower_bound, because in
6727 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6728 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6729
Alexey Frunze96b66822016-09-10 02:32:44 -07006730 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006731 // Jump to successors[0] if value == lower_bound.
6732 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6733 int32_t last_index = 0;
6734 for (; num_entries - last_index > 2; last_index += 2) {
6735 __ Addiu(temp_reg, temp_reg, -2);
6736 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6737 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6738 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6739 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6740 }
6741 if (num_entries - last_index == 2) {
6742 // The last missing case_value.
6743 __ Addiu(temp_reg, temp_reg, -1);
6744 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006745 }
6746
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006747 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006748 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006749 __ B(codegen_->GetLabelOf(default_block));
6750 }
6751}
6752
Alexey Frunze96b66822016-09-10 02:32:44 -07006753void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6754 Register constant_area,
6755 int32_t lower_bound,
6756 uint32_t num_entries,
6757 HBasicBlock* switch_block,
6758 HBasicBlock* default_block) {
6759 // Create a jump table.
6760 std::vector<MipsLabel*> labels(num_entries);
6761 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6762 for (uint32_t i = 0; i < num_entries; i++) {
6763 labels[i] = codegen_->GetLabelOf(successors[i]);
6764 }
6765 JumpTable* table = __ CreateJumpTable(std::move(labels));
6766
6767 // Is the value in range?
6768 __ Addiu32(TMP, value_reg, -lower_bound);
6769 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6770 __ Sltiu(AT, TMP, num_entries);
6771 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6772 } else {
6773 __ LoadConst32(AT, num_entries);
6774 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6775 }
6776
6777 // We are in the range of the table.
6778 // Load the target address from the jump table, indexing by the value.
6779 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6780 __ Sll(TMP, TMP, 2);
6781 __ Addu(TMP, TMP, AT);
6782 __ Lw(TMP, TMP, 0);
6783 // Compute the absolute target address by adding the table start address
6784 // (the table contains offsets to targets relative to its start).
6785 __ Addu(TMP, TMP, AT);
6786 // And jump.
6787 __ Jr(TMP);
6788 __ NopIfNoReordering();
6789}
6790
6791void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6792 int32_t lower_bound = switch_instr->GetStartValue();
6793 uint32_t num_entries = switch_instr->GetNumEntries();
6794 LocationSummary* locations = switch_instr->GetLocations();
6795 Register value_reg = locations->InAt(0).AsRegister<Register>();
6796 HBasicBlock* switch_block = switch_instr->GetBlock();
6797 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6798
6799 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6800 num_entries > kPackedSwitchJumpTableThreshold) {
6801 // R6 uses PC-relative addressing to access the jump table.
6802 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6803 // the jump table and it is implemented by changing HPackedSwitch to
6804 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6805 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6806 GenTableBasedPackedSwitch(value_reg,
6807 ZERO,
6808 lower_bound,
6809 num_entries,
6810 switch_block,
6811 default_block);
6812 } else {
6813 GenPackedSwitchWithCompares(value_reg,
6814 lower_bound,
6815 num_entries,
6816 switch_block,
6817 default_block);
6818 }
6819}
6820
6821void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6822 LocationSummary* locations =
6823 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6824 locations->SetInAt(0, Location::RequiresRegister());
6825 // Constant area pointer (HMipsComputeBaseMethodAddress).
6826 locations->SetInAt(1, Location::RequiresRegister());
6827}
6828
6829void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6830 int32_t lower_bound = switch_instr->GetStartValue();
6831 uint32_t num_entries = switch_instr->GetNumEntries();
6832 LocationSummary* locations = switch_instr->GetLocations();
6833 Register value_reg = locations->InAt(0).AsRegister<Register>();
6834 Register constant_area = locations->InAt(1).AsRegister<Register>();
6835 HBasicBlock* switch_block = switch_instr->GetBlock();
6836 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6837
6838 // This is an R2-only path. HPackedSwitch has been changed to
6839 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6840 // required to address the jump table relative to PC.
6841 GenTableBasedPackedSwitch(value_reg,
6842 constant_area,
6843 lower_bound,
6844 num_entries,
6845 switch_block,
6846 default_block);
6847}
6848
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006849void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6850 HMipsComputeBaseMethodAddress* insn) {
6851 LocationSummary* locations =
6852 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6853 locations->SetOut(Location::RequiresRegister());
6854}
6855
6856void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6857 HMipsComputeBaseMethodAddress* insn) {
6858 LocationSummary* locations = insn->GetLocations();
6859 Register reg = locations->Out().AsRegister<Register>();
6860
6861 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6862
6863 // Generate a dummy PC-relative call to obtain PC.
6864 __ Nal();
6865 // Grab the return address off RA.
6866 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006867 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006868
6869 // Remember this offset (the obtained PC value) for later use with constant area.
6870 __ BindPcRelBaseLabel();
6871}
6872
6873void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6875 locations->SetOut(Location::RequiresRegister());
6876}
6877
6878void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6879 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6880 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6881 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006882 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6883 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006884}
6885
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006886void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6887 // The trampoline uses the same calling convention as dex calling conventions,
6888 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6889 // the method_idx.
6890 HandleInvoke(invoke);
6891}
6892
6893void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6894 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6895}
6896
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006897void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6898 LocationSummary* locations =
6899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6900 locations->SetInAt(0, Location::RequiresRegister());
6901 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006902}
6903
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006904void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6905 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006906 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006907 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006908 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006909 __ LoadFromOffset(kLoadWord,
6910 locations->Out().AsRegister<Register>(),
6911 locations->InAt(0).AsRegister<Register>(),
6912 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006913 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006914 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006915 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006916 __ LoadFromOffset(kLoadWord,
6917 locations->Out().AsRegister<Register>(),
6918 locations->InAt(0).AsRegister<Register>(),
6919 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006920 __ LoadFromOffset(kLoadWord,
6921 locations->Out().AsRegister<Register>(),
6922 locations->Out().AsRegister<Register>(),
6923 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006924 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006925}
6926
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006927#undef __
6928#undef QUICK_ENTRY_POINT
6929
6930} // namespace mips
6931} // namespace art