blob: 456c5c6a926c16d61d13f8478de7f5af2dfa4d50 [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 Frunzee3fb2452016-05-10 16:08:05 -0700461 method_patches_(MethodReferenceComparator(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
463 call_patches_(MethodReferenceComparator(),
464 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700465 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
466 boot_image_string_patches_(StringReferenceValueComparator(),
467 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
469 boot_image_type_patches_(TypeReferenceValueComparator(),
470 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
472 boot_image_address_patches_(std::less<uint32_t>(),
473 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200475 // Save RA (containing the return address) to mimic Quick.
476 AddAllocatedRegister(Location::RegisterLocation(RA));
477}
478
479#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100480// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
481#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700482#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200483
484void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
485 // Ensure that we fix up branches.
486 __ FinalizeCode();
487
488 // Adjust native pc offsets in stack maps.
489 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
490 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
491 uint32_t new_position = __ GetAdjustedPosition(old_position);
492 DCHECK_GE(new_position, old_position);
493 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
494 }
495
496 // Adjust pc offsets for the disassembly information.
497 if (disasm_info_ != nullptr) {
498 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
499 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
500 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
501 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
502 it.second.start = __ GetAdjustedPosition(it.second.start);
503 it.second.end = __ GetAdjustedPosition(it.second.end);
504 }
505 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
506 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
507 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
508 }
509 }
510
511 CodeGenerator::Finalize(allocator);
512}
513
514MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
515 return codegen_->GetAssembler();
516}
517
518void ParallelMoveResolverMIPS::EmitMove(size_t index) {
519 DCHECK_LT(index, moves_.size());
520 MoveOperands* move = moves_[index];
521 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
522}
523
524void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
525 DCHECK_LT(index, moves_.size());
526 MoveOperands* move = moves_[index];
527 Primitive::Type type = move->GetType();
528 Location loc1 = move->GetDestination();
529 Location loc2 = move->GetSource();
530
531 DCHECK(!loc1.IsConstant());
532 DCHECK(!loc2.IsConstant());
533
534 if (loc1.Equals(loc2)) {
535 return;
536 }
537
538 if (loc1.IsRegister() && loc2.IsRegister()) {
539 // Swap 2 GPRs.
540 Register r1 = loc1.AsRegister<Register>();
541 Register r2 = loc2.AsRegister<Register>();
542 __ Move(TMP, r2);
543 __ Move(r2, r1);
544 __ Move(r1, TMP);
545 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
546 FRegister f1 = loc1.AsFpuRegister<FRegister>();
547 FRegister f2 = loc2.AsFpuRegister<FRegister>();
548 if (type == Primitive::kPrimFloat) {
549 __ MovS(FTMP, f2);
550 __ MovS(f2, f1);
551 __ MovS(f1, FTMP);
552 } else {
553 DCHECK_EQ(type, Primitive::kPrimDouble);
554 __ MovD(FTMP, f2);
555 __ MovD(f2, f1);
556 __ MovD(f1, FTMP);
557 }
558 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
559 (loc1.IsFpuRegister() && loc2.IsRegister())) {
560 // Swap FPR and GPR.
561 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
562 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
563 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200564 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200565 __ Move(TMP, r2);
566 __ Mfc1(r2, f1);
567 __ Mtc1(TMP, f1);
568 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
569 // Swap 2 GPR register pairs.
570 Register r1 = loc1.AsRegisterPairLow<Register>();
571 Register r2 = loc2.AsRegisterPairLow<Register>();
572 __ Move(TMP, r2);
573 __ Move(r2, r1);
574 __ Move(r1, TMP);
575 r1 = loc1.AsRegisterPairHigh<Register>();
576 r2 = loc2.AsRegisterPairHigh<Register>();
577 __ Move(TMP, r2);
578 __ Move(r2, r1);
579 __ Move(r1, TMP);
580 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
581 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
582 // Swap FPR and GPR register pair.
583 DCHECK_EQ(type, Primitive::kPrimDouble);
584 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
585 : loc2.AsFpuRegister<FRegister>();
586 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
587 : loc2.AsRegisterPairLow<Register>();
588 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
589 : loc2.AsRegisterPairHigh<Register>();
590 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
591 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
592 // unpredictable and the following mfch1 will fail.
593 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800594 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200595 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800596 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200597 __ Move(r2_l, TMP);
598 __ Move(r2_h, AT);
599 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
600 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
601 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
602 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000603 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
604 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200605 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
606 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000607 __ Move(TMP, reg);
608 __ LoadFromOffset(kLoadWord, reg, SP, offset);
609 __ StoreToOffset(kStoreWord, TMP, SP, offset);
610 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
611 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
612 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
613 : loc2.AsRegisterPairLow<Register>();
614 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
615 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200616 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
618 : loc2.GetHighStackIndex(kMipsWordSize);
619 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000620 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000621 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000622 __ Move(TMP, reg_h);
623 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
624 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200625 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
626 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
627 : loc2.AsFpuRegister<FRegister>();
628 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
629 if (type == Primitive::kPrimFloat) {
630 __ MovS(FTMP, reg);
631 __ LoadSFromOffset(reg, SP, offset);
632 __ StoreSToOffset(FTMP, SP, offset);
633 } else {
634 DCHECK_EQ(type, Primitive::kPrimDouble);
635 __ MovD(FTMP, reg);
636 __ LoadDFromOffset(reg, SP, offset);
637 __ StoreDToOffset(FTMP, SP, offset);
638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200639 } else {
640 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
641 }
642}
643
644void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
645 __ Pop(static_cast<Register>(reg));
646}
647
648void ParallelMoveResolverMIPS::SpillScratch(int reg) {
649 __ Push(static_cast<Register>(reg));
650}
651
652void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
653 // Allocate a scratch register other than TMP, if available.
654 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
655 // automatically unspilled when the scratch scope object is destroyed).
656 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
657 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
658 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
659 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
660 __ LoadFromOffset(kLoadWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index1 + stack_offset);
664 __ LoadFromOffset(kLoadWord,
665 TMP,
666 SP,
667 index2 + stack_offset);
668 __ StoreToOffset(kStoreWord,
669 Register(ensure_scratch.GetRegister()),
670 SP,
671 index2 + stack_offset);
672 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
673 }
674}
675
Alexey Frunze73296a72016-06-03 22:51:46 -0700676void CodeGeneratorMIPS::ComputeSpillMask() {
677 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
678 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
679 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
680 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
681 // registers, include the ZERO register to force alignment of FPU callee-saved registers
682 // within the stack frame.
683 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
684 core_spill_mask_ |= (1 << ZERO);
685 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700686}
687
688bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700689 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700690 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
691 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
692 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700693 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
694 // saved in an unused temporary register) and saving of RA and the current method pointer
695 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700696 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700697}
698
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200699static dwarf::Reg DWARFReg(Register reg) {
700 return dwarf::Reg::MipsCore(static_cast<int>(reg));
701}
702
703// TODO: mapping of floating-point registers to DWARF.
704
705void CodeGeneratorMIPS::GenerateFrameEntry() {
706 __ Bind(&frame_entry_label_);
707
708 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
709
710 if (do_overflow_check) {
711 __ LoadFromOffset(kLoadWord,
712 ZERO,
713 SP,
714 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
715 RecordPcInfo(nullptr, 0);
716 }
717
718 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700719 CHECK_EQ(fpu_spill_mask_, 0u);
720 CHECK_EQ(core_spill_mask_, 1u << RA);
721 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200722 return;
723 }
724
725 // Make sure the frame size isn't unreasonably large.
726 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
727 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
728 }
729
730 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200731
Alexey Frunze73296a72016-06-03 22:51:46 -0700732 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200733 __ IncreaseFrameSize(ofs);
734
Alexey Frunze73296a72016-06-03 22:51:46 -0700735 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
736 Register reg = static_cast<Register>(MostSignificantBit(mask));
737 mask ^= 1u << reg;
738 ofs -= kMipsWordSize;
739 // The ZERO register is only included for alignment.
740 if (reg != ZERO) {
741 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200742 __ cfi().RelOffset(DWARFReg(reg), ofs);
743 }
744 }
745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
747 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
748 mask ^= 1u << reg;
749 ofs -= kMipsDoublewordSize;
750 __ StoreDToOffset(reg, SP, ofs);
751 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752 }
753
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100754 // Save the current method if we need it. Note that we do not
755 // do this in HCurrentMethod, as the instruction might have been removed
756 // in the SSA graph.
757 if (RequiresCurrentMethod()) {
758 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
759 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100760
761 if (GetGraph()->HasShouldDeoptimizeFlag()) {
762 // Initialize should deoptimize flag to 0.
763 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200765}
766
767void CodeGeneratorMIPS::GenerateFrameExit() {
768 __ cfi().RememberState();
769
770 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200771 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200772
Alexey Frunze73296a72016-06-03 22:51:46 -0700773 // For better instruction scheduling restore RA before other registers.
774 uint32_t ofs = GetFrameSize();
775 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
776 Register reg = static_cast<Register>(MostSignificantBit(mask));
777 mask ^= 1u << reg;
778 ofs -= kMipsWordSize;
779 // The ZERO register is only included for alignment.
780 if (reg != ZERO) {
781 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200782 __ cfi().Restore(DWARFReg(reg));
783 }
784 }
785
Alexey Frunze73296a72016-06-03 22:51:46 -0700786 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
787 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
788 mask ^= 1u << reg;
789 ofs -= kMipsDoublewordSize;
790 __ LoadDFromOffset(reg, SP, ofs);
791 // TODO: __ cfi().Restore(DWARFReg(reg));
792 }
793
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700794 size_t frame_size = GetFrameSize();
795 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
796 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
797 bool reordering = __ SetReorder(false);
798 if (exchange) {
799 __ Jr(RA);
800 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
801 } else {
802 __ DecreaseFrameSize(frame_size);
803 __ Jr(RA);
804 __ Nop(); // In delay slot.
805 }
806 __ SetReorder(reordering);
807 } else {
808 __ Jr(RA);
809 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200810 }
811
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200812 __ cfi().RestoreState();
813 __ cfi().DefCFAOffset(GetFrameSize());
814}
815
816void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
817 __ Bind(GetLabelOf(block));
818}
819
820void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
821 if (src.Equals(dst)) {
822 return;
823 }
824
825 if (src.IsConstant()) {
826 MoveConstant(dst, src.GetConstant());
827 } else {
828 if (Primitive::Is64BitType(dst_type)) {
829 Move64(dst, src);
830 } else {
831 Move32(dst, src);
832 }
833 }
834}
835
836void CodeGeneratorMIPS::Move32(Location destination, Location source) {
837 if (source.Equals(destination)) {
838 return;
839 }
840
841 if (destination.IsRegister()) {
842 if (source.IsRegister()) {
843 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
844 } else if (source.IsFpuRegister()) {
845 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
846 } else {
847 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
848 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
849 }
850 } else if (destination.IsFpuRegister()) {
851 if (source.IsRegister()) {
852 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
853 } else if (source.IsFpuRegister()) {
854 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
855 } else {
856 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
857 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
858 }
859 } else {
860 DCHECK(destination.IsStackSlot()) << destination;
861 if (source.IsRegister()) {
862 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
863 } else if (source.IsFpuRegister()) {
864 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
865 } else {
866 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
867 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
868 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
869 }
870 }
871}
872
873void CodeGeneratorMIPS::Move64(Location destination, Location source) {
874 if (source.Equals(destination)) {
875 return;
876 }
877
878 if (destination.IsRegisterPair()) {
879 if (source.IsRegisterPair()) {
880 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
881 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
882 } else if (source.IsFpuRegister()) {
883 Register dst_high = destination.AsRegisterPairHigh<Register>();
884 Register dst_low = destination.AsRegisterPairLow<Register>();
885 FRegister src = source.AsFpuRegister<FRegister>();
886 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800887 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200888 } else {
889 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
890 int32_t off = source.GetStackIndex();
891 Register r = destination.AsRegisterPairLow<Register>();
892 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
893 }
894 } else if (destination.IsFpuRegister()) {
895 if (source.IsRegisterPair()) {
896 FRegister dst = destination.AsFpuRegister<FRegister>();
897 Register src_high = source.AsRegisterPairHigh<Register>();
898 Register src_low = source.AsRegisterPairLow<Register>();
899 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800900 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200901 } else if (source.IsFpuRegister()) {
902 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
903 } else {
904 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
905 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
906 }
907 } else {
908 DCHECK(destination.IsDoubleStackSlot()) << destination;
909 int32_t off = destination.GetStackIndex();
910 if (source.IsRegisterPair()) {
911 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
912 } else if (source.IsFpuRegister()) {
913 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
914 } else {
915 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
916 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
917 __ StoreToOffset(kStoreWord, TMP, SP, off);
918 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
919 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
920 }
921 }
922}
923
924void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
925 if (c->IsIntConstant() || c->IsNullConstant()) {
926 // Move 32 bit constant.
927 int32_t value = GetInt32ValueOf(c);
928 if (destination.IsRegister()) {
929 Register dst = destination.AsRegister<Register>();
930 __ LoadConst32(dst, value);
931 } else {
932 DCHECK(destination.IsStackSlot())
933 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700934 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200935 }
936 } else if (c->IsLongConstant()) {
937 // Move 64 bit constant.
938 int64_t value = GetInt64ValueOf(c);
939 if (destination.IsRegisterPair()) {
940 Register r_h = destination.AsRegisterPairHigh<Register>();
941 Register r_l = destination.AsRegisterPairLow<Register>();
942 __ LoadConst64(r_h, r_l, value);
943 } else {
944 DCHECK(destination.IsDoubleStackSlot())
945 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700946 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200947 }
948 } else if (c->IsFloatConstant()) {
949 // Move 32 bit float constant.
950 int32_t value = GetInt32ValueOf(c);
951 if (destination.IsFpuRegister()) {
952 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
953 } else {
954 DCHECK(destination.IsStackSlot())
955 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700956 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200957 }
958 } else {
959 // Move 64 bit double constant.
960 DCHECK(c->IsDoubleConstant()) << c->DebugName();
961 int64_t value = GetInt64ValueOf(c);
962 if (destination.IsFpuRegister()) {
963 FRegister fd = destination.AsFpuRegister<FRegister>();
964 __ LoadDConst64(fd, value, TMP);
965 } else {
966 DCHECK(destination.IsDoubleStackSlot())
967 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700968 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200969 }
970 }
971}
972
973void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
974 DCHECK(destination.IsRegister());
975 Register dst = destination.AsRegister<Register>();
976 __ LoadConst32(dst, value);
977}
978
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200979void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
980 if (location.IsRegister()) {
981 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700982 } else if (location.IsRegisterPair()) {
983 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
984 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200985 } else {
986 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
987 }
988}
989
Vladimir Markoaad75c62016-10-03 08:46:48 +0000990template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
991inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
992 const ArenaDeque<PcRelativePatchInfo>& infos,
993 ArenaVector<LinkerPatch>* linker_patches) {
994 for (const PcRelativePatchInfo& info : infos) {
995 const DexFile& dex_file = info.target_dex_file;
996 size_t offset_or_index = info.offset_or_index;
997 DCHECK(info.high_label.IsBound());
998 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
999 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1000 // the assembler's base label used for PC-relative addressing.
1001 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1002 ? __ GetLabelLocation(&info.pc_rel_label)
1003 : __ GetPcRelBaseLabelLocation();
1004 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1005 }
1006}
1007
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001008void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1009 DCHECK(linker_patches->empty());
1010 size_t size =
1011 method_patches_.size() +
1012 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001013 pc_relative_dex_cache_patches_.size() +
1014 pc_relative_string_patches_.size() +
1015 pc_relative_type_patches_.size() +
1016 boot_image_string_patches_.size() +
1017 boot_image_type_patches_.size() +
1018 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001019 linker_patches->reserve(size);
1020 for (const auto& entry : method_patches_) {
1021 const MethodReference& target_method = entry.first;
1022 Literal* literal = entry.second;
1023 DCHECK(literal->GetLabel()->IsBound());
1024 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1025 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1026 target_method.dex_file,
1027 target_method.dex_method_index));
1028 }
1029 for (const auto& entry : call_patches_) {
1030 const MethodReference& target_method = entry.first;
1031 Literal* literal = entry.second;
1032 DCHECK(literal->GetLabel()->IsBound());
1033 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1034 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1035 target_method.dex_file,
1036 target_method.dex_method_index));
1037 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001038 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1039 linker_patches);
1040 if (!GetCompilerOptions().IsBootImage()) {
1041 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1042 linker_patches);
1043 } else {
1044 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1045 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001046 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001047 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1048 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001049 for (const auto& entry : boot_image_string_patches_) {
1050 const StringReference& target_string = entry.first;
1051 Literal* literal = entry.second;
1052 DCHECK(literal->GetLabel()->IsBound());
1053 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1054 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1055 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001056 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001057 }
1058 for (const auto& entry : boot_image_type_patches_) {
1059 const TypeReference& target_type = entry.first;
1060 Literal* literal = entry.second;
1061 DCHECK(literal->GetLabel()->IsBound());
1062 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1063 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1064 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001065 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001066 }
1067 for (const auto& entry : boot_image_address_patches_) {
1068 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1069 Literal* literal = entry.second;
1070 DCHECK(literal->GetLabel()->IsBound());
1071 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1072 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1073 }
1074}
1075
1076CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1077 const DexFile& dex_file, uint32_t string_index) {
1078 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1079}
1080
1081CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001082 const DexFile& dex_file, dex::TypeIndex type_index) {
1083 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001084}
1085
1086CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1087 const DexFile& dex_file, uint32_t element_offset) {
1088 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1089}
1090
1091CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1092 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1093 patches->emplace_back(dex_file, offset_or_index);
1094 return &patches->back();
1095}
1096
Alexey Frunze06a46c42016-07-19 15:00:40 -07001097Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1098 return map->GetOrCreate(
1099 value,
1100 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1101}
1102
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001103Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1104 MethodToLiteralMap* map) {
1105 return map->GetOrCreate(
1106 target_method,
1107 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1108}
1109
1110Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1111 return DeduplicateMethodLiteral(target_method, &method_patches_);
1112}
1113
1114Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1115 return DeduplicateMethodLiteral(target_method, &call_patches_);
1116}
1117
Alexey Frunze06a46c42016-07-19 15:00:40 -07001118Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001119 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001120 return boot_image_string_patches_.GetOrCreate(
1121 StringReference(&dex_file, string_index),
1122 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1123}
1124
1125Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001126 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001127 return boot_image_type_patches_.GetOrCreate(
1128 TypeReference(&dex_file, type_index),
1129 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1130}
1131
1132Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1133 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1134 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1135 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1136}
1137
Vladimir Markoaad75c62016-10-03 08:46:48 +00001138void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1139 PcRelativePatchInfo* info, Register out, Register base) {
1140 bool reordering = __ SetReorder(false);
1141 if (GetInstructionSetFeatures().IsR6()) {
1142 DCHECK_EQ(base, ZERO);
1143 __ Bind(&info->high_label);
1144 __ Bind(&info->pc_rel_label);
1145 // Add a 32-bit offset to PC.
1146 __ Auipc(out, /* placeholder */ 0x1234);
1147 __ Addiu(out, out, /* placeholder */ 0x5678);
1148 } else {
1149 // If base is ZERO, emit NAL to obtain the actual base.
1150 if (base == ZERO) {
1151 // Generate a dummy PC-relative call to obtain PC.
1152 __ Nal();
1153 }
1154 __ Bind(&info->high_label);
1155 __ Lui(out, /* placeholder */ 0x1234);
1156 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1157 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1158 if (base == ZERO) {
1159 __ Bind(&info->pc_rel_label);
1160 }
1161 __ Ori(out, out, /* placeholder */ 0x5678);
1162 // Add a 32-bit offset to PC.
1163 __ Addu(out, out, (base == ZERO) ? RA : base);
1164 }
1165 __ SetReorder(reordering);
1166}
1167
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001168void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1169 MipsLabel done;
1170 Register card = AT;
1171 Register temp = TMP;
1172 __ Beqz(value, &done);
1173 __ LoadFromOffset(kLoadWord,
1174 card,
1175 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001176 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001177 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1178 __ Addu(temp, card, temp);
1179 __ Sb(card, temp, 0);
1180 __ Bind(&done);
1181}
1182
David Brazdil58282f42016-01-14 12:45:10 +00001183void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001184 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1185 blocked_core_registers_[ZERO] = true;
1186 blocked_core_registers_[K0] = true;
1187 blocked_core_registers_[K1] = true;
1188 blocked_core_registers_[GP] = true;
1189 blocked_core_registers_[SP] = true;
1190 blocked_core_registers_[RA] = true;
1191
1192 // AT and TMP(T8) are used as temporary/scratch registers
1193 // (similar to how AT is used by MIPS assemblers).
1194 blocked_core_registers_[AT] = true;
1195 blocked_core_registers_[TMP] = true;
1196 blocked_fpu_registers_[FTMP] = true;
1197
1198 // Reserve suspend and thread registers.
1199 blocked_core_registers_[S0] = true;
1200 blocked_core_registers_[TR] = true;
1201
1202 // Reserve T9 for function calls
1203 blocked_core_registers_[T9] = true;
1204
1205 // Reserve odd-numbered FPU registers.
1206 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1207 blocked_fpu_registers_[i] = true;
1208 }
1209
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001210 if (GetGraph()->IsDebuggable()) {
1211 // Stubs do not save callee-save floating point registers. If the graph
1212 // is debuggable, we need to deal with these registers differently. For
1213 // now, just block them.
1214 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1215 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1216 }
1217 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001218}
1219
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001220size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1221 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1222 return kMipsWordSize;
1223}
1224
1225size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1226 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1227 return kMipsWordSize;
1228}
1229
1230size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1231 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1232 return kMipsDoublewordSize;
1233}
1234
1235size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1236 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1237 return kMipsDoublewordSize;
1238}
1239
1240void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001241 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242}
1243
1244void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001245 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246}
1247
Serban Constantinescufca16662016-07-14 09:21:59 +01001248constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1249
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001250void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1251 HInstruction* instruction,
1252 uint32_t dex_pc,
1253 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001254 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001255 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001256 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001257 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001258 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259 // Reserve argument space on stack (for $a0-$a3) for
1260 // entrypoints that directly reference native implementations.
1261 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001263 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001264 } else {
1265 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001267 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001268 if (EntrypointRequiresStackMap(entrypoint)) {
1269 RecordPcInfo(instruction, dex_pc, slow_path);
1270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271}
1272
1273void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1274 Register class_reg) {
1275 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1276 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1277 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1278 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1279 __ Sync(0);
1280 __ Bind(slow_path->GetExitLabel());
1281}
1282
1283void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1284 __ Sync(0); // Only stype 0 is supported.
1285}
1286
1287void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1288 HBasicBlock* successor) {
1289 SuspendCheckSlowPathMIPS* slow_path =
1290 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1291 codegen_->AddSlowPath(slow_path);
1292
1293 __ LoadFromOffset(kLoadUnsignedHalfword,
1294 TMP,
1295 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001296 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 if (successor == nullptr) {
1298 __ Bnez(TMP, slow_path->GetEntryLabel());
1299 __ Bind(slow_path->GetReturnLabel());
1300 } else {
1301 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1302 __ B(slow_path->GetEntryLabel());
1303 // slow_path will return to GetLabelOf(successor).
1304 }
1305}
1306
1307InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1308 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001309 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 assembler_(codegen->GetAssembler()),
1311 codegen_(codegen) {}
1312
1313void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1314 DCHECK_EQ(instruction->InputCount(), 2U);
1315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1316 Primitive::Type type = instruction->GetResultType();
1317 switch (type) {
1318 case Primitive::kPrimInt: {
1319 locations->SetInAt(0, Location::RequiresRegister());
1320 HInstruction* right = instruction->InputAt(1);
1321 bool can_use_imm = false;
1322 if (right->IsConstant()) {
1323 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1324 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1325 can_use_imm = IsUint<16>(imm);
1326 } else if (instruction->IsAdd()) {
1327 can_use_imm = IsInt<16>(imm);
1328 } else {
1329 DCHECK(instruction->IsSub());
1330 can_use_imm = IsInt<16>(-imm);
1331 }
1332 }
1333 if (can_use_imm)
1334 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1335 else
1336 locations->SetInAt(1, Location::RequiresRegister());
1337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1338 break;
1339 }
1340
1341 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001343 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345 break;
1346 }
1347
1348 case Primitive::kPrimFloat:
1349 case Primitive::kPrimDouble:
1350 DCHECK(instruction->IsAdd() || instruction->IsSub());
1351 locations->SetInAt(0, Location::RequiresFpuRegister());
1352 locations->SetInAt(1, Location::RequiresFpuRegister());
1353 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1354 break;
1355
1356 default:
1357 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1358 }
1359}
1360
1361void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1362 Primitive::Type type = instruction->GetType();
1363 LocationSummary* locations = instruction->GetLocations();
1364
1365 switch (type) {
1366 case Primitive::kPrimInt: {
1367 Register dst = locations->Out().AsRegister<Register>();
1368 Register lhs = locations->InAt(0).AsRegister<Register>();
1369 Location rhs_location = locations->InAt(1);
1370
1371 Register rhs_reg = ZERO;
1372 int32_t rhs_imm = 0;
1373 bool use_imm = rhs_location.IsConstant();
1374 if (use_imm) {
1375 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1376 } else {
1377 rhs_reg = rhs_location.AsRegister<Register>();
1378 }
1379
1380 if (instruction->IsAnd()) {
1381 if (use_imm)
1382 __ Andi(dst, lhs, rhs_imm);
1383 else
1384 __ And(dst, lhs, rhs_reg);
1385 } else if (instruction->IsOr()) {
1386 if (use_imm)
1387 __ Ori(dst, lhs, rhs_imm);
1388 else
1389 __ Or(dst, lhs, rhs_reg);
1390 } else if (instruction->IsXor()) {
1391 if (use_imm)
1392 __ Xori(dst, lhs, rhs_imm);
1393 else
1394 __ Xor(dst, lhs, rhs_reg);
1395 } else if (instruction->IsAdd()) {
1396 if (use_imm)
1397 __ Addiu(dst, lhs, rhs_imm);
1398 else
1399 __ Addu(dst, lhs, rhs_reg);
1400 } else {
1401 DCHECK(instruction->IsSub());
1402 if (use_imm)
1403 __ Addiu(dst, lhs, -rhs_imm);
1404 else
1405 __ Subu(dst, lhs, rhs_reg);
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1412 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1413 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1414 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001415 Location rhs_location = locations->InAt(1);
1416 bool use_imm = rhs_location.IsConstant();
1417 if (!use_imm) {
1418 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1419 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1420 if (instruction->IsAnd()) {
1421 __ And(dst_low, lhs_low, rhs_low);
1422 __ And(dst_high, lhs_high, rhs_high);
1423 } else if (instruction->IsOr()) {
1424 __ Or(dst_low, lhs_low, rhs_low);
1425 __ Or(dst_high, lhs_high, rhs_high);
1426 } else if (instruction->IsXor()) {
1427 __ Xor(dst_low, lhs_low, rhs_low);
1428 __ Xor(dst_high, lhs_high, rhs_high);
1429 } else if (instruction->IsAdd()) {
1430 if (lhs_low == rhs_low) {
1431 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1432 __ Slt(TMP, lhs_low, ZERO);
1433 __ Addu(dst_low, lhs_low, rhs_low);
1434 } else {
1435 __ Addu(dst_low, lhs_low, rhs_low);
1436 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1437 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1438 }
1439 __ Addu(dst_high, lhs_high, rhs_high);
1440 __ Addu(dst_high, dst_high, TMP);
1441 } else {
1442 DCHECK(instruction->IsSub());
1443 __ Sltu(TMP, lhs_low, rhs_low);
1444 __ Subu(dst_low, lhs_low, rhs_low);
1445 __ Subu(dst_high, lhs_high, rhs_high);
1446 __ Subu(dst_high, dst_high, TMP);
1447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001449 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1450 if (instruction->IsOr()) {
1451 uint32_t low = Low32Bits(value);
1452 uint32_t high = High32Bits(value);
1453 if (IsUint<16>(low)) {
1454 if (dst_low != lhs_low || low != 0) {
1455 __ Ori(dst_low, lhs_low, low);
1456 }
1457 } else {
1458 __ LoadConst32(TMP, low);
1459 __ Or(dst_low, lhs_low, TMP);
1460 }
1461 if (IsUint<16>(high)) {
1462 if (dst_high != lhs_high || high != 0) {
1463 __ Ori(dst_high, lhs_high, high);
1464 }
1465 } else {
1466 if (high != low) {
1467 __ LoadConst32(TMP, high);
1468 }
1469 __ Or(dst_high, lhs_high, TMP);
1470 }
1471 } else if (instruction->IsXor()) {
1472 uint32_t low = Low32Bits(value);
1473 uint32_t high = High32Bits(value);
1474 if (IsUint<16>(low)) {
1475 if (dst_low != lhs_low || low != 0) {
1476 __ Xori(dst_low, lhs_low, low);
1477 }
1478 } else {
1479 __ LoadConst32(TMP, low);
1480 __ Xor(dst_low, lhs_low, TMP);
1481 }
1482 if (IsUint<16>(high)) {
1483 if (dst_high != lhs_high || high != 0) {
1484 __ Xori(dst_high, lhs_high, high);
1485 }
1486 } else {
1487 if (high != low) {
1488 __ LoadConst32(TMP, high);
1489 }
1490 __ Xor(dst_high, lhs_high, TMP);
1491 }
1492 } else if (instruction->IsAnd()) {
1493 uint32_t low = Low32Bits(value);
1494 uint32_t high = High32Bits(value);
1495 if (IsUint<16>(low)) {
1496 __ Andi(dst_low, lhs_low, low);
1497 } else if (low != 0xFFFFFFFF) {
1498 __ LoadConst32(TMP, low);
1499 __ And(dst_low, lhs_low, TMP);
1500 } else if (dst_low != lhs_low) {
1501 __ Move(dst_low, lhs_low);
1502 }
1503 if (IsUint<16>(high)) {
1504 __ Andi(dst_high, lhs_high, high);
1505 } else if (high != 0xFFFFFFFF) {
1506 if (high != low) {
1507 __ LoadConst32(TMP, high);
1508 }
1509 __ And(dst_high, lhs_high, TMP);
1510 } else if (dst_high != lhs_high) {
1511 __ Move(dst_high, lhs_high);
1512 }
1513 } else {
1514 if (instruction->IsSub()) {
1515 value = -value;
1516 } else {
1517 DCHECK(instruction->IsAdd());
1518 }
1519 int32_t low = Low32Bits(value);
1520 int32_t high = High32Bits(value);
1521 if (IsInt<16>(low)) {
1522 if (dst_low != lhs_low || low != 0) {
1523 __ Addiu(dst_low, lhs_low, low);
1524 }
1525 if (low != 0) {
1526 __ Sltiu(AT, dst_low, low);
1527 }
1528 } else {
1529 __ LoadConst32(TMP, low);
1530 __ Addu(dst_low, lhs_low, TMP);
1531 __ Sltu(AT, dst_low, TMP);
1532 }
1533 if (IsInt<16>(high)) {
1534 if (dst_high != lhs_high || high != 0) {
1535 __ Addiu(dst_high, lhs_high, high);
1536 }
1537 } else {
1538 if (high != low) {
1539 __ LoadConst32(TMP, high);
1540 }
1541 __ Addu(dst_high, lhs_high, TMP);
1542 }
1543 if (low != 0) {
1544 __ Addu(dst_high, dst_high, AT);
1545 }
1546 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 break;
1549 }
1550
1551 case Primitive::kPrimFloat:
1552 case Primitive::kPrimDouble: {
1553 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1554 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1555 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1556 if (instruction->IsAdd()) {
1557 if (type == Primitive::kPrimFloat) {
1558 __ AddS(dst, lhs, rhs);
1559 } else {
1560 __ AddD(dst, lhs, rhs);
1561 }
1562 } else {
1563 DCHECK(instruction->IsSub());
1564 if (type == Primitive::kPrimFloat) {
1565 __ SubS(dst, lhs, rhs);
1566 } else {
1567 __ SubD(dst, lhs, rhs);
1568 }
1569 }
1570 break;
1571 }
1572
1573 default:
1574 LOG(FATAL) << "Unexpected binary operation type " << type;
1575 }
1576}
1577
1578void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001579 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001580
1581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1582 Primitive::Type type = instr->GetResultType();
1583 switch (type) {
1584 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1588 break;
1589 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1592 locations->SetOut(Location::RequiresRegister());
1593 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 default:
1595 LOG(FATAL) << "Unexpected shift type " << type;
1596 }
1597}
1598
1599static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1600
1601void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001602 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 LocationSummary* locations = instr->GetLocations();
1604 Primitive::Type type = instr->GetType();
1605
1606 Location rhs_location = locations->InAt(1);
1607 bool use_imm = rhs_location.IsConstant();
1608 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1609 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001610 const uint32_t shift_mask =
1611 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001612 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1614 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001615
1616 switch (type) {
1617 case Primitive::kPrimInt: {
1618 Register dst = locations->Out().AsRegister<Register>();
1619 Register lhs = locations->InAt(0).AsRegister<Register>();
1620 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 if (shift_value == 0) {
1622 if (dst != lhs) {
1623 __ Move(dst, lhs);
1624 }
1625 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 __ Sll(dst, lhs, shift_value);
1627 } else if (instr->IsShr()) {
1628 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001629 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 } else {
1632 if (has_ins_rotr) {
1633 __ Rotr(dst, lhs, shift_value);
1634 } else {
1635 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1636 __ Srl(dst, lhs, shift_value);
1637 __ Or(dst, dst, TMP);
1638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001639 }
1640 } else {
1641 if (instr->IsShl()) {
1642 __ Sllv(dst, lhs, rhs_reg);
1643 } else if (instr->IsShr()) {
1644 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001646 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001647 } else {
1648 if (has_ins_rotr) {
1649 __ Rotrv(dst, lhs, rhs_reg);
1650 } else {
1651 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001652 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1653 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1654 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1655 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1656 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001657 __ Sllv(TMP, lhs, TMP);
1658 __ Srlv(dst, lhs, rhs_reg);
1659 __ Or(dst, dst, TMP);
1660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001661 }
1662 }
1663 break;
1664 }
1665
1666 case Primitive::kPrimLong: {
1667 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1668 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1669 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1670 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1671 if (use_imm) {
1672 if (shift_value == 0) {
1673 codegen_->Move64(locations->Out(), locations->InAt(0));
1674 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001675 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001676 if (instr->IsShl()) {
1677 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1678 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1679 __ Sll(dst_low, lhs_low, shift_value);
1680 } else if (instr->IsShr()) {
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1683 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001684 } else if (instr->IsUShr()) {
1685 __ Srl(dst_low, lhs_low, shift_value);
1686 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1687 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 } else {
1689 __ Srl(dst_low, lhs_low, shift_value);
1690 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1691 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001693 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001695 if (instr->IsShl()) {
1696 __ Sll(dst_low, lhs_low, shift_value);
1697 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1698 __ Sll(dst_high, lhs_high, shift_value);
1699 __ Or(dst_high, dst_high, TMP);
1700 } else if (instr->IsShr()) {
1701 __ Sra(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001706 __ Srl(dst_high, lhs_high, shift_value);
1707 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Srl(dst_low, lhs_low, shift_value);
1709 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001710 } else {
1711 __ Srl(TMP, lhs_low, shift_value);
1712 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1713 __ Or(dst_low, dst_low, TMP);
1714 __ Srl(TMP, lhs_high, shift_value);
1715 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1716 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001717 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 }
1719 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 __ Move(dst_low, ZERO);
1724 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001728 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001729 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001730 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001731 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 // 64-bit rotation by 32 is just a swap.
1733 __ Move(dst_low, lhs_high);
1734 __ Move(dst_high, lhs_low);
1735 } else {
1736 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Srl(dst_low, lhs_high, shift_value_high);
1738 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1739 __ Srl(dst_high, lhs_low, shift_value_high);
1740 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001742 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1743 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001745 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1746 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001747 __ Or(dst_high, dst_high, TMP);
1748 }
1749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001750 }
1751 }
1752 } else {
1753 MipsLabel done;
1754 if (instr->IsShl()) {
1755 __ Sllv(dst_low, lhs_low, rhs_reg);
1756 __ Nor(AT, ZERO, rhs_reg);
1757 __ Srl(TMP, lhs_low, 1);
1758 __ Srlv(TMP, TMP, AT);
1759 __ Sllv(dst_high, lhs_high, rhs_reg);
1760 __ Or(dst_high, dst_high, TMP);
1761 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1762 __ Beqz(TMP, &done);
1763 __ Move(dst_high, dst_low);
1764 __ Move(dst_low, ZERO);
1765 } else if (instr->IsShr()) {
1766 __ Srav(dst_high, lhs_high, rhs_reg);
1767 __ Nor(AT, ZERO, rhs_reg);
1768 __ Sll(TMP, lhs_high, 1);
1769 __ Sllv(TMP, TMP, AT);
1770 __ Srlv(dst_low, lhs_low, rhs_reg);
1771 __ Or(dst_low, dst_low, TMP);
1772 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1773 __ Beqz(TMP, &done);
1774 __ Move(dst_low, dst_high);
1775 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001776 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001777 __ Srlv(dst_high, lhs_high, rhs_reg);
1778 __ Nor(AT, ZERO, rhs_reg);
1779 __ Sll(TMP, lhs_high, 1);
1780 __ Sllv(TMP, TMP, AT);
1781 __ Srlv(dst_low, lhs_low, rhs_reg);
1782 __ Or(dst_low, dst_low, TMP);
1783 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1784 __ Beqz(TMP, &done);
1785 __ Move(dst_low, dst_high);
1786 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001787 } else {
1788 __ Nor(AT, ZERO, rhs_reg);
1789 __ Srlv(TMP, lhs_low, rhs_reg);
1790 __ Sll(dst_low, lhs_high, 1);
1791 __ Sllv(dst_low, dst_low, AT);
1792 __ Or(dst_low, dst_low, TMP);
1793 __ Srlv(TMP, lhs_high, rhs_reg);
1794 __ Sll(dst_high, lhs_low, 1);
1795 __ Sllv(dst_high, dst_high, AT);
1796 __ Or(dst_high, dst_high, TMP);
1797 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1798 __ Beqz(TMP, &done);
1799 __ Move(TMP, dst_high);
1800 __ Move(dst_high, dst_low);
1801 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001802 }
1803 __ Bind(&done);
1804 }
1805 break;
1806 }
1807
1808 default:
1809 LOG(FATAL) << "Unexpected shift operation type " << type;
1810 }
1811}
1812
1813void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1818 HandleBinaryOp(instruction);
1819}
1820
1821void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1822 HandleBinaryOp(instruction);
1823}
1824
1825void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1826 HandleBinaryOp(instruction);
1827}
1828
1829void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1834 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1836 } else {
1837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1838 }
1839}
1840
Alexey Frunze2923db72016-08-20 01:55:47 -07001841auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1842 auto null_checker = [this, instruction]() {
1843 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1844 };
1845 return null_checker;
1846}
1847
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1849 LocationSummary* locations = instruction->GetLocations();
1850 Register obj = locations->InAt(0).AsRegister<Register>();
1851 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001852 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001853 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001855 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 switch (type) {
1857 case Primitive::kPrimBoolean: {
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_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001862 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 } else {
1864 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001878 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 }
1880 break;
1881 }
1882
1883 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 Register out = locations->Out().AsRegister<Register>();
1885 if (index.IsConstant()) {
1886 size_t offset =
1887 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001888 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889 } else {
1890 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1891 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001892 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 }
1894 break;
1895 }
1896
1897 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 Register out = locations->Out().AsRegister<Register>();
1899 if (index.IsConstant()) {
1900 size_t offset =
1901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001902 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 } else {
1904 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1905 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 }
1908 break;
1909 }
1910
1911 case Primitive::kPrimInt:
1912 case Primitive::kPrimNot: {
1913 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 Register out = locations->Out().AsRegister<Register>();
1915 if (index.IsConstant()) {
1916 size_t offset =
1917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001918 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 } else {
1920 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1921 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001922 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 }
1924 break;
1925 }
1926
1927 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 Register out = locations->Out().AsRegisterPairLow<Register>();
1929 if (index.IsConstant()) {
1930 size_t offset =
1931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 } else {
1934 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1935 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001936 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 }
1938 break;
1939 }
1940
1941 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1943 if (index.IsConstant()) {
1944 size_t offset =
1945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001946 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 } else {
1948 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1949 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001950 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 }
1952 break;
1953 }
1954
1955 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1957 if (index.IsConstant()) {
1958 size_t offset =
1959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001960 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 } else {
1962 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1963 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001964 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 }
1966 break;
1967 }
1968
1969 case Primitive::kPrimVoid:
1970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1971 UNREACHABLE();
1972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973}
1974
1975void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1977 locations->SetInAt(0, Location::RequiresRegister());
1978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1979}
1980
1981void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1982 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001983 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 Register obj = locations->InAt(0).AsRegister<Register>();
1985 Register out = locations->Out().AsRegister<Register>();
1986 __ LoadFromOffset(kLoadWord, out, obj, offset);
1987 codegen_->MaybeRecordImplicitNullCheck(instruction);
1988}
1989
Alexey Frunzef58b2482016-09-02 22:14:06 -07001990Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1991 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1992 ? Location::ConstantLocation(instruction->AsConstant())
1993 : Location::RequiresRegister();
1994}
1995
1996Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1997 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1998 // We can store a non-zero float or double constant without first loading it into the FPU,
1999 // but we should only prefer this if the constant has a single use.
2000 if (instruction->IsConstant() &&
2001 (instruction->AsConstant()->IsZeroBitPattern() ||
2002 instruction->GetUses().HasExactlyOneElement())) {
2003 return Location::ConstantLocation(instruction->AsConstant());
2004 // Otherwise fall through and require an FPU register for the constant.
2005 }
2006 return Location::RequiresFpuRegister();
2007}
2008
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002010 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2012 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002013 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002014 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015 InvokeRuntimeCallingConvention calling_convention;
2016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2017 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2018 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2019 } else {
2020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2022 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002023 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002025 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 }
2028}
2029
2030void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2031 LocationSummary* locations = instruction->GetLocations();
2032 Register obj = locations->InAt(0).AsRegister<Register>();
2033 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002034 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 Primitive::Type value_type = instruction->GetComponentType();
2036 bool needs_runtime_call = locations->WillCall();
2037 bool needs_write_barrier =
2038 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002040 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041
2042 switch (value_type) {
2043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte: {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002047 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002049 __ Addu(base_reg, obj, index.AsRegister<Register>());
2050 }
2051 if (value_location.IsConstant()) {
2052 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2053 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2054 } else {
2055 Register value = value_location.AsRegister<Register>();
2056 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimShort:
2062 case Primitive::kPrimChar: {
2063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002067 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2068 __ Addu(base_reg, obj, base_reg);
2069 }
2070 if (value_location.IsConstant()) {
2071 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2072 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2073 } else {
2074 Register value = value_location.AsRegister<Register>();
2075 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 }
2077 break;
2078 }
2079
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot: {
2082 if (!needs_runtime_call) {
2083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002085 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002087 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2088 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002090 if (value_location.IsConstant()) {
2091 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2092 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2093 DCHECK(!needs_write_barrier);
2094 } else {
2095 Register value = value_location.AsRegister<Register>();
2096 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2097 if (needs_write_barrier) {
2098 DCHECK_EQ(value_type, Primitive::kPrimNot);
2099 codegen_->MarkGCCard(obj, value);
2100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 }
2102 } else {
2103 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002104 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimLong: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002115 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2116 __ Addu(base_reg, obj, base_reg);
2117 }
2118 if (value_location.IsConstant()) {
2119 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2120 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2121 } else {
2122 Register value = value_location.AsRegisterPairLow<Register>();
2123 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 }
2125 break;
2126 }
2127
2128 case Primitive::kPrimFloat: {
2129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002131 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002133 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2134 __ Addu(base_reg, obj, base_reg);
2135 }
2136 if (value_location.IsConstant()) {
2137 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2138 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2139 } else {
2140 FRegister value = value_location.AsFpuRegister<FRegister>();
2141 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 }
2143 break;
2144 }
2145
2146 case Primitive::kPrimDouble: {
2147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002149 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002151 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2152 __ Addu(base_reg, obj, base_reg);
2153 }
2154 if (value_location.IsConstant()) {
2155 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2156 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2157 } else {
2158 FRegister value = value_location.AsFpuRegister<FRegister>();
2159 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160 }
2161 break;
2162 }
2163
2164 case Primitive::kPrimVoid:
2165 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2166 UNREACHABLE();
2167 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002168}
2169
2170void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002171 RegisterSet caller_saves = RegisterSet::Empty();
2172 InvokeRuntimeCallingConvention calling_convention;
2173 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2174 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2175 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002176 locations->SetInAt(0, Location::RequiresRegister());
2177 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002178}
2179
2180void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2181 LocationSummary* locations = instruction->GetLocations();
2182 BoundsCheckSlowPathMIPS* slow_path =
2183 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2184 codegen_->AddSlowPath(slow_path);
2185
2186 Register index = locations->InAt(0).AsRegister<Register>();
2187 Register length = locations->InAt(1).AsRegister<Register>();
2188
2189 // length is limited by the maximum positive signed 32-bit integer.
2190 // Unsigned comparison of length and index checks for index < 0
2191 // and for length <= index simultaneously.
2192 __ Bgeu(index, length, slow_path->GetEntryLabel());
2193}
2194
2195void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2197 instruction,
2198 LocationSummary::kCallOnSlowPath);
2199 locations->SetInAt(0, Location::RequiresRegister());
2200 locations->SetInAt(1, Location::RequiresRegister());
2201 // Note that TypeCheckSlowPathMIPS uses this register too.
2202 locations->AddTemp(Location::RequiresRegister());
2203}
2204
2205void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2206 LocationSummary* locations = instruction->GetLocations();
2207 Register obj = locations->InAt(0).AsRegister<Register>();
2208 Register cls = locations->InAt(1).AsRegister<Register>();
2209 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2210
2211 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2212 codegen_->AddSlowPath(slow_path);
2213
2214 // TODO: avoid this check if we know obj is not null.
2215 __ Beqz(obj, slow_path->GetExitLabel());
2216 // Compare the class of `obj` with `cls`.
2217 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2218 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2219 __ Bind(slow_path->GetExitLabel());
2220}
2221
2222void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2223 LocationSummary* locations =
2224 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2225 locations->SetInAt(0, Location::RequiresRegister());
2226 if (check->HasUses()) {
2227 locations->SetOut(Location::SameAsFirstInput());
2228 }
2229}
2230
2231void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2232 // We assume the class is not null.
2233 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2234 check->GetLoadClass(),
2235 check,
2236 check->GetDexPc(),
2237 true);
2238 codegen_->AddSlowPath(slow_path);
2239 GenerateClassInitializationCheck(slow_path,
2240 check->GetLocations()->InAt(0).AsRegister<Register>());
2241}
2242
2243void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2244 Primitive::Type in_type = compare->InputAt(0)->GetType();
2245
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248
2249 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002250 case Primitive::kPrimBoolean:
2251 case Primitive::kPrimByte:
2252 case Primitive::kPrimShort:
2253 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002254 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetInAt(1, Location::RequiresRegister());
2257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2258 break;
2259
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 case Primitive::kPrimLong:
2261 locations->SetInAt(0, Location::RequiresRegister());
2262 locations->SetInAt(1, Location::RequiresRegister());
2263 // Output overlaps because it is written before doing the low comparison.
2264 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2265 break;
2266
2267 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002268 case Primitive::kPrimDouble:
2269 locations->SetInAt(0, Location::RequiresFpuRegister());
2270 locations->SetInAt(1, Location::RequiresFpuRegister());
2271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273
2274 default:
2275 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2276 }
2277}
2278
2279void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2280 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002281 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002283 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284
2285 // 0 if: left == right
2286 // 1 if: left > right
2287 // -1 if: left < right
2288 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002289 case Primitive::kPrimBoolean:
2290 case Primitive::kPrimByte:
2291 case Primitive::kPrimShort:
2292 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002293 case Primitive::kPrimInt: {
2294 Register lhs = locations->InAt(0).AsRegister<Register>();
2295 Register rhs = locations->InAt(1).AsRegister<Register>();
2296 __ Slt(TMP, lhs, rhs);
2297 __ Slt(res, rhs, lhs);
2298 __ Subu(res, res, TMP);
2299 break;
2300 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 case Primitive::kPrimLong: {
2302 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2304 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2305 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2306 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2307 // TODO: more efficient (direct) comparison with a constant.
2308 __ Slt(TMP, lhs_high, rhs_high);
2309 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2310 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2311 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2312 __ Sltu(TMP, lhs_low, rhs_low);
2313 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2314 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2315 __ Bind(&done);
2316 break;
2317 }
2318
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002319 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002320 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002321 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2322 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2323 MipsLabel done;
2324 if (isR6) {
2325 __ CmpEqS(FTMP, lhs, rhs);
2326 __ LoadConst32(res, 0);
2327 __ Bc1nez(FTMP, &done);
2328 if (gt_bias) {
2329 __ CmpLtS(FTMP, lhs, rhs);
2330 __ LoadConst32(res, -1);
2331 __ Bc1nez(FTMP, &done);
2332 __ LoadConst32(res, 1);
2333 } else {
2334 __ CmpLtS(FTMP, rhs, lhs);
2335 __ LoadConst32(res, 1);
2336 __ Bc1nez(FTMP, &done);
2337 __ LoadConst32(res, -1);
2338 }
2339 } else {
2340 if (gt_bias) {
2341 __ ColtS(0, lhs, rhs);
2342 __ LoadConst32(res, -1);
2343 __ Bc1t(0, &done);
2344 __ CeqS(0, lhs, rhs);
2345 __ LoadConst32(res, 1);
2346 __ Movt(res, ZERO, 0);
2347 } else {
2348 __ ColtS(0, rhs, lhs);
2349 __ LoadConst32(res, 1);
2350 __ Bc1t(0, &done);
2351 __ CeqS(0, lhs, rhs);
2352 __ LoadConst32(res, -1);
2353 __ Movt(res, ZERO, 0);
2354 }
2355 }
2356 __ Bind(&done);
2357 break;
2358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002360 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002361 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2362 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2363 MipsLabel done;
2364 if (isR6) {
2365 __ CmpEqD(FTMP, lhs, rhs);
2366 __ LoadConst32(res, 0);
2367 __ Bc1nez(FTMP, &done);
2368 if (gt_bias) {
2369 __ CmpLtD(FTMP, lhs, rhs);
2370 __ LoadConst32(res, -1);
2371 __ Bc1nez(FTMP, &done);
2372 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002374 __ CmpLtD(FTMP, rhs, lhs);
2375 __ LoadConst32(res, 1);
2376 __ Bc1nez(FTMP, &done);
2377 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 }
2379 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002380 if (gt_bias) {
2381 __ ColtD(0, lhs, rhs);
2382 __ LoadConst32(res, -1);
2383 __ Bc1t(0, &done);
2384 __ CeqD(0, lhs, rhs);
2385 __ LoadConst32(res, 1);
2386 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002387 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002388 __ ColtD(0, rhs, lhs);
2389 __ LoadConst32(res, 1);
2390 __ Bc1t(0, &done);
2391 __ CeqD(0, lhs, rhs);
2392 __ LoadConst32(res, -1);
2393 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002394 }
2395 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002396 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397 break;
2398 }
2399
2400 default:
2401 LOG(FATAL) << "Unimplemented compare type " << in_type;
2402 }
2403}
2404
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002405void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002407 switch (instruction->InputAt(0)->GetType()) {
2408 default:
2409 case Primitive::kPrimLong:
2410 locations->SetInAt(0, Location::RequiresRegister());
2411 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2412 break;
2413
2414 case Primitive::kPrimFloat:
2415 case Primitive::kPrimDouble:
2416 locations->SetInAt(0, Location::RequiresFpuRegister());
2417 locations->SetInAt(1, Location::RequiresFpuRegister());
2418 break;
2419 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002420 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2422 }
2423}
2424
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002425void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002426 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427 return;
2428 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002430 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431 LocationSummary* locations = instruction->GetLocations();
2432 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002433 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002435 switch (type) {
2436 default:
2437 // Integer case.
2438 GenerateIntCompare(instruction->GetCondition(), locations);
2439 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002441 case Primitive::kPrimLong:
2442 // TODO: don't use branches.
2443 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002444 break;
2445
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002446 case Primitive::kPrimFloat:
2447 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002448 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2449 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002451
2452 // Convert the branches into the result.
2453 MipsLabel done;
2454
2455 // False case: result = 0.
2456 __ LoadConst32(dst, 0);
2457 __ B(&done);
2458
2459 // True case: result = 1.
2460 __ Bind(&true_label);
2461 __ LoadConst32(dst, 1);
2462 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002463}
2464
Alexey Frunze7e99e052015-11-24 19:28:01 -08002465void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2466 DCHECK(instruction->IsDiv() || instruction->IsRem());
2467 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2468
2469 LocationSummary* locations = instruction->GetLocations();
2470 Location second = locations->InAt(1);
2471 DCHECK(second.IsConstant());
2472
2473 Register out = locations->Out().AsRegister<Register>();
2474 Register dividend = locations->InAt(0).AsRegister<Register>();
2475 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2476 DCHECK(imm == 1 || imm == -1);
2477
2478 if (instruction->IsRem()) {
2479 __ Move(out, ZERO);
2480 } else {
2481 if (imm == -1) {
2482 __ Subu(out, ZERO, dividend);
2483 } else if (out != dividend) {
2484 __ Move(out, dividend);
2485 }
2486 }
2487}
2488
2489void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2490 DCHECK(instruction->IsDiv() || instruction->IsRem());
2491 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2492
2493 LocationSummary* locations = instruction->GetLocations();
2494 Location second = locations->InAt(1);
2495 DCHECK(second.IsConstant());
2496
2497 Register out = locations->Out().AsRegister<Register>();
2498 Register dividend = locations->InAt(0).AsRegister<Register>();
2499 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002500 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002501 int ctz_imm = CTZ(abs_imm);
2502
2503 if (instruction->IsDiv()) {
2504 if (ctz_imm == 1) {
2505 // Fast path for division by +/-2, which is very common.
2506 __ Srl(TMP, dividend, 31);
2507 } else {
2508 __ Sra(TMP, dividend, 31);
2509 __ Srl(TMP, TMP, 32 - ctz_imm);
2510 }
2511 __ Addu(out, dividend, TMP);
2512 __ Sra(out, out, ctz_imm);
2513 if (imm < 0) {
2514 __ Subu(out, ZERO, out);
2515 }
2516 } else {
2517 if (ctz_imm == 1) {
2518 // Fast path for modulo +/-2, which is very common.
2519 __ Sra(TMP, dividend, 31);
2520 __ Subu(out, dividend, TMP);
2521 __ Andi(out, out, 1);
2522 __ Addu(out, out, TMP);
2523 } else {
2524 __ Sra(TMP, dividend, 31);
2525 __ Srl(TMP, TMP, 32 - ctz_imm);
2526 __ Addu(out, dividend, TMP);
2527 if (IsUint<16>(abs_imm - 1)) {
2528 __ Andi(out, out, abs_imm - 1);
2529 } else {
2530 __ Sll(out, out, 32 - ctz_imm);
2531 __ Srl(out, out, 32 - ctz_imm);
2532 }
2533 __ Subu(out, out, TMP);
2534 }
2535 }
2536}
2537
2538void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2539 DCHECK(instruction->IsDiv() || instruction->IsRem());
2540 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2541
2542 LocationSummary* locations = instruction->GetLocations();
2543 Location second = locations->InAt(1);
2544 DCHECK(second.IsConstant());
2545
2546 Register out = locations->Out().AsRegister<Register>();
2547 Register dividend = locations->InAt(0).AsRegister<Register>();
2548 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2549
2550 int64_t magic;
2551 int shift;
2552 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2553
2554 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2555
2556 __ LoadConst32(TMP, magic);
2557 if (isR6) {
2558 __ MuhR6(TMP, dividend, TMP);
2559 } else {
2560 __ MultR2(dividend, TMP);
2561 __ Mfhi(TMP);
2562 }
2563 if (imm > 0 && magic < 0) {
2564 __ Addu(TMP, TMP, dividend);
2565 } else if (imm < 0 && magic > 0) {
2566 __ Subu(TMP, TMP, dividend);
2567 }
2568
2569 if (shift != 0) {
2570 __ Sra(TMP, TMP, shift);
2571 }
2572
2573 if (instruction->IsDiv()) {
2574 __ Sra(out, TMP, 31);
2575 __ Subu(out, TMP, out);
2576 } else {
2577 __ Sra(AT, TMP, 31);
2578 __ Subu(AT, TMP, AT);
2579 __ LoadConst32(TMP, imm);
2580 if (isR6) {
2581 __ MulR6(TMP, AT, TMP);
2582 } else {
2583 __ MulR2(TMP, AT, TMP);
2584 }
2585 __ Subu(out, dividend, TMP);
2586 }
2587}
2588
2589void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2590 DCHECK(instruction->IsDiv() || instruction->IsRem());
2591 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2592
2593 LocationSummary* locations = instruction->GetLocations();
2594 Register out = locations->Out().AsRegister<Register>();
2595 Location second = locations->InAt(1);
2596
2597 if (second.IsConstant()) {
2598 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2599 if (imm == 0) {
2600 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2601 } else if (imm == 1 || imm == -1) {
2602 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002603 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002604 DivRemByPowerOfTwo(instruction);
2605 } else {
2606 DCHECK(imm <= -2 || imm >= 2);
2607 GenerateDivRemWithAnyConstant(instruction);
2608 }
2609 } else {
2610 Register dividend = locations->InAt(0).AsRegister<Register>();
2611 Register divisor = second.AsRegister<Register>();
2612 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2613 if (instruction->IsDiv()) {
2614 if (isR6) {
2615 __ DivR6(out, dividend, divisor);
2616 } else {
2617 __ DivR2(out, dividend, divisor);
2618 }
2619 } else {
2620 if (isR6) {
2621 __ ModR6(out, dividend, divisor);
2622 } else {
2623 __ ModR2(out, dividend, divisor);
2624 }
2625 }
2626 }
2627}
2628
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2630 Primitive::Type type = div->GetResultType();
2631 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002632 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 : LocationSummary::kNoCall;
2634
2635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2636
2637 switch (type) {
2638 case Primitive::kPrimInt:
2639 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002640 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2642 break;
2643
2644 case Primitive::kPrimLong: {
2645 InvokeRuntimeCallingConvention calling_convention;
2646 locations->SetInAt(0, Location::RegisterPairLocation(
2647 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2648 locations->SetInAt(1, Location::RegisterPairLocation(
2649 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2650 locations->SetOut(calling_convention.GetReturnLocation(type));
2651 break;
2652 }
2653
2654 case Primitive::kPrimFloat:
2655 case Primitive::kPrimDouble:
2656 locations->SetInAt(0, Location::RequiresFpuRegister());
2657 locations->SetInAt(1, Location::RequiresFpuRegister());
2658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected div type " << type;
2663 }
2664}
2665
2666void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2667 Primitive::Type type = instruction->GetType();
2668 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669
2670 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002671 case Primitive::kPrimInt:
2672 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002673 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002675 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002676 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2677 break;
2678 }
2679 case Primitive::kPrimFloat:
2680 case Primitive::kPrimDouble: {
2681 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2682 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2683 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2684 if (type == Primitive::kPrimFloat) {
2685 __ DivS(dst, lhs, rhs);
2686 } else {
2687 __ DivD(dst, lhs, rhs);
2688 }
2689 break;
2690 }
2691 default:
2692 LOG(FATAL) << "Unexpected div type " << type;
2693 }
2694}
2695
2696void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002697 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699}
2700
2701void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2702 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2703 codegen_->AddSlowPath(slow_path);
2704 Location value = instruction->GetLocations()->InAt(0);
2705 Primitive::Type type = instruction->GetType();
2706
2707 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002708 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 case Primitive::kPrimByte:
2710 case Primitive::kPrimChar:
2711 case Primitive::kPrimShort:
2712 case Primitive::kPrimInt: {
2713 if (value.IsConstant()) {
2714 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2715 __ B(slow_path->GetEntryLabel());
2716 } else {
2717 // A division by a non-null constant is valid. We don't need to perform
2718 // any check, so simply fall through.
2719 }
2720 } else {
2721 DCHECK(value.IsRegister()) << value;
2722 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2723 }
2724 break;
2725 }
2726 case Primitive::kPrimLong: {
2727 if (value.IsConstant()) {
2728 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2729 __ B(slow_path->GetEntryLabel());
2730 } else {
2731 // A division by a non-null constant is valid. We don't need to perform
2732 // any check, so simply fall through.
2733 }
2734 } else {
2735 DCHECK(value.IsRegisterPair()) << value;
2736 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2737 __ Beqz(TMP, slow_path->GetEntryLabel());
2738 }
2739 break;
2740 }
2741 default:
2742 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2743 }
2744}
2745
2746void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2747 LocationSummary* locations =
2748 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2749 locations->SetOut(Location::ConstantLocation(constant));
2750}
2751
2752void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2753 // Will be generated at use site.
2754}
2755
2756void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2757 exit->SetLocations(nullptr);
2758}
2759
2760void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2761}
2762
2763void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2764 LocationSummary* locations =
2765 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2766 locations->SetOut(Location::ConstantLocation(constant));
2767}
2768
2769void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2770 // Will be generated at use site.
2771}
2772
2773void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2774 got->SetLocations(nullptr);
2775}
2776
2777void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2778 DCHECK(!successor->IsExitBlock());
2779 HBasicBlock* block = got->GetBlock();
2780 HInstruction* previous = got->GetPrevious();
2781 HLoopInformation* info = block->GetLoopInformation();
2782
2783 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2784 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2785 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2786 return;
2787 }
2788 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2789 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2790 }
2791 if (!codegen_->GoesToNextBlock(block, successor)) {
2792 __ B(codegen_->GetLabelOf(successor));
2793 }
2794}
2795
2796void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2797 HandleGoto(got, got->GetSuccessor());
2798}
2799
2800void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2801 try_boundary->SetLocations(nullptr);
2802}
2803
2804void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2805 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2806 if (!successor->IsExitBlock()) {
2807 HandleGoto(try_boundary, successor);
2808 }
2809}
2810
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002811void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2812 LocationSummary* locations) {
2813 Register dst = locations->Out().AsRegister<Register>();
2814 Register lhs = locations->InAt(0).AsRegister<Register>();
2815 Location rhs_location = locations->InAt(1);
2816 Register rhs_reg = ZERO;
2817 int64_t rhs_imm = 0;
2818 bool use_imm = rhs_location.IsConstant();
2819 if (use_imm) {
2820 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2821 } else {
2822 rhs_reg = rhs_location.AsRegister<Register>();
2823 }
2824
2825 switch (cond) {
2826 case kCondEQ:
2827 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002828 if (use_imm && IsInt<16>(-rhs_imm)) {
2829 if (rhs_imm == 0) {
2830 if (cond == kCondEQ) {
2831 __ Sltiu(dst, lhs, 1);
2832 } else {
2833 __ Sltu(dst, ZERO, lhs);
2834 }
2835 } else {
2836 __ Addiu(dst, lhs, -rhs_imm);
2837 if (cond == kCondEQ) {
2838 __ Sltiu(dst, dst, 1);
2839 } else {
2840 __ Sltu(dst, ZERO, dst);
2841 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002842 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002843 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002844 if (use_imm && IsUint<16>(rhs_imm)) {
2845 __ Xori(dst, lhs, rhs_imm);
2846 } else {
2847 if (use_imm) {
2848 rhs_reg = TMP;
2849 __ LoadConst32(rhs_reg, rhs_imm);
2850 }
2851 __ Xor(dst, lhs, rhs_reg);
2852 }
2853 if (cond == kCondEQ) {
2854 __ Sltiu(dst, dst, 1);
2855 } else {
2856 __ Sltu(dst, ZERO, dst);
2857 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002858 }
2859 break;
2860
2861 case kCondLT:
2862 case kCondGE:
2863 if (use_imm && IsInt<16>(rhs_imm)) {
2864 __ Slti(dst, lhs, rhs_imm);
2865 } else {
2866 if (use_imm) {
2867 rhs_reg = TMP;
2868 __ LoadConst32(rhs_reg, rhs_imm);
2869 }
2870 __ Slt(dst, lhs, rhs_reg);
2871 }
2872 if (cond == kCondGE) {
2873 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2874 // only the slt instruction but no sge.
2875 __ Xori(dst, dst, 1);
2876 }
2877 break;
2878
2879 case kCondLE:
2880 case kCondGT:
2881 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2882 // Simulate lhs <= rhs via lhs < rhs + 1.
2883 __ Slti(dst, lhs, rhs_imm + 1);
2884 if (cond == kCondGT) {
2885 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2886 // only the slti instruction but no sgti.
2887 __ Xori(dst, dst, 1);
2888 }
2889 } else {
2890 if (use_imm) {
2891 rhs_reg = TMP;
2892 __ LoadConst32(rhs_reg, rhs_imm);
2893 }
2894 __ Slt(dst, rhs_reg, lhs);
2895 if (cond == kCondLE) {
2896 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2897 // only the slt instruction but no sle.
2898 __ Xori(dst, dst, 1);
2899 }
2900 }
2901 break;
2902
2903 case kCondB:
2904 case kCondAE:
2905 if (use_imm && IsInt<16>(rhs_imm)) {
2906 // Sltiu sign-extends its 16-bit immediate operand before
2907 // the comparison and thus lets us compare directly with
2908 // unsigned values in the ranges [0, 0x7fff] and
2909 // [0xffff8000, 0xffffffff].
2910 __ Sltiu(dst, lhs, rhs_imm);
2911 } else {
2912 if (use_imm) {
2913 rhs_reg = TMP;
2914 __ LoadConst32(rhs_reg, rhs_imm);
2915 }
2916 __ Sltu(dst, lhs, rhs_reg);
2917 }
2918 if (cond == kCondAE) {
2919 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2920 // only the sltu instruction but no sgeu.
2921 __ Xori(dst, dst, 1);
2922 }
2923 break;
2924
2925 case kCondBE:
2926 case kCondA:
2927 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2928 // Simulate lhs <= rhs via lhs < rhs + 1.
2929 // Note that this only works if rhs + 1 does not overflow
2930 // to 0, hence the check above.
2931 // Sltiu sign-extends its 16-bit immediate operand before
2932 // the comparison and thus lets us compare directly with
2933 // unsigned values in the ranges [0, 0x7fff] and
2934 // [0xffff8000, 0xffffffff].
2935 __ Sltiu(dst, lhs, rhs_imm + 1);
2936 if (cond == kCondA) {
2937 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2938 // only the sltiu instruction but no sgtiu.
2939 __ Xori(dst, dst, 1);
2940 }
2941 } else {
2942 if (use_imm) {
2943 rhs_reg = TMP;
2944 __ LoadConst32(rhs_reg, rhs_imm);
2945 }
2946 __ Sltu(dst, rhs_reg, lhs);
2947 if (cond == kCondBE) {
2948 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2949 // only the sltu instruction but no sleu.
2950 __ Xori(dst, dst, 1);
2951 }
2952 }
2953 break;
2954 }
2955}
2956
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002957bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2958 LocationSummary* input_locations,
2959 Register dst) {
2960 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2961 Location rhs_location = input_locations->InAt(1);
2962 Register rhs_reg = ZERO;
2963 int64_t rhs_imm = 0;
2964 bool use_imm = rhs_location.IsConstant();
2965 if (use_imm) {
2966 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2967 } else {
2968 rhs_reg = rhs_location.AsRegister<Register>();
2969 }
2970
2971 switch (cond) {
2972 case kCondEQ:
2973 case kCondNE:
2974 if (use_imm && IsInt<16>(-rhs_imm)) {
2975 __ Addiu(dst, lhs, -rhs_imm);
2976 } else if (use_imm && IsUint<16>(rhs_imm)) {
2977 __ Xori(dst, lhs, rhs_imm);
2978 } else {
2979 if (use_imm) {
2980 rhs_reg = TMP;
2981 __ LoadConst32(rhs_reg, rhs_imm);
2982 }
2983 __ Xor(dst, lhs, rhs_reg);
2984 }
2985 return (cond == kCondEQ);
2986
2987 case kCondLT:
2988 case kCondGE:
2989 if (use_imm && IsInt<16>(rhs_imm)) {
2990 __ Slti(dst, lhs, rhs_imm);
2991 } else {
2992 if (use_imm) {
2993 rhs_reg = TMP;
2994 __ LoadConst32(rhs_reg, rhs_imm);
2995 }
2996 __ Slt(dst, lhs, rhs_reg);
2997 }
2998 return (cond == kCondGE);
2999
3000 case kCondLE:
3001 case kCondGT:
3002 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3003 // Simulate lhs <= rhs via lhs < rhs + 1.
3004 __ Slti(dst, lhs, rhs_imm + 1);
3005 return (cond == kCondGT);
3006 } else {
3007 if (use_imm) {
3008 rhs_reg = TMP;
3009 __ LoadConst32(rhs_reg, rhs_imm);
3010 }
3011 __ Slt(dst, rhs_reg, lhs);
3012 return (cond == kCondLE);
3013 }
3014
3015 case kCondB:
3016 case kCondAE:
3017 if (use_imm && IsInt<16>(rhs_imm)) {
3018 // Sltiu sign-extends its 16-bit immediate operand before
3019 // the comparison and thus lets us compare directly with
3020 // unsigned values in the ranges [0, 0x7fff] and
3021 // [0xffff8000, 0xffffffff].
3022 __ Sltiu(dst, lhs, rhs_imm);
3023 } else {
3024 if (use_imm) {
3025 rhs_reg = TMP;
3026 __ LoadConst32(rhs_reg, rhs_imm);
3027 }
3028 __ Sltu(dst, lhs, rhs_reg);
3029 }
3030 return (cond == kCondAE);
3031
3032 case kCondBE:
3033 case kCondA:
3034 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3035 // Simulate lhs <= rhs via lhs < rhs + 1.
3036 // Note that this only works if rhs + 1 does not overflow
3037 // to 0, hence the check above.
3038 // Sltiu sign-extends its 16-bit immediate operand before
3039 // the comparison and thus lets us compare directly with
3040 // unsigned values in the ranges [0, 0x7fff] and
3041 // [0xffff8000, 0xffffffff].
3042 __ Sltiu(dst, lhs, rhs_imm + 1);
3043 return (cond == kCondA);
3044 } else {
3045 if (use_imm) {
3046 rhs_reg = TMP;
3047 __ LoadConst32(rhs_reg, rhs_imm);
3048 }
3049 __ Sltu(dst, rhs_reg, lhs);
3050 return (cond == kCondBE);
3051 }
3052 }
3053}
3054
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003055void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3056 LocationSummary* locations,
3057 MipsLabel* label) {
3058 Register lhs = locations->InAt(0).AsRegister<Register>();
3059 Location rhs_location = locations->InAt(1);
3060 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003061 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003062 bool use_imm = rhs_location.IsConstant();
3063 if (use_imm) {
3064 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3065 } else {
3066 rhs_reg = rhs_location.AsRegister<Register>();
3067 }
3068
3069 if (use_imm && rhs_imm == 0) {
3070 switch (cond) {
3071 case kCondEQ:
3072 case kCondBE: // <= 0 if zero
3073 __ Beqz(lhs, label);
3074 break;
3075 case kCondNE:
3076 case kCondA: // > 0 if non-zero
3077 __ Bnez(lhs, label);
3078 break;
3079 case kCondLT:
3080 __ Bltz(lhs, label);
3081 break;
3082 case kCondGE:
3083 __ Bgez(lhs, label);
3084 break;
3085 case kCondLE:
3086 __ Blez(lhs, label);
3087 break;
3088 case kCondGT:
3089 __ Bgtz(lhs, label);
3090 break;
3091 case kCondB: // always false
3092 break;
3093 case kCondAE: // always true
3094 __ B(label);
3095 break;
3096 }
3097 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003098 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3099 if (isR6 || !use_imm) {
3100 if (use_imm) {
3101 rhs_reg = TMP;
3102 __ LoadConst32(rhs_reg, rhs_imm);
3103 }
3104 switch (cond) {
3105 case kCondEQ:
3106 __ Beq(lhs, rhs_reg, label);
3107 break;
3108 case kCondNE:
3109 __ Bne(lhs, rhs_reg, label);
3110 break;
3111 case kCondLT:
3112 __ Blt(lhs, rhs_reg, label);
3113 break;
3114 case kCondGE:
3115 __ Bge(lhs, rhs_reg, label);
3116 break;
3117 case kCondLE:
3118 __ Bge(rhs_reg, lhs, label);
3119 break;
3120 case kCondGT:
3121 __ Blt(rhs_reg, lhs, label);
3122 break;
3123 case kCondB:
3124 __ Bltu(lhs, rhs_reg, label);
3125 break;
3126 case kCondAE:
3127 __ Bgeu(lhs, rhs_reg, label);
3128 break;
3129 case kCondBE:
3130 __ Bgeu(rhs_reg, lhs, label);
3131 break;
3132 case kCondA:
3133 __ Bltu(rhs_reg, lhs, label);
3134 break;
3135 }
3136 } else {
3137 // Special cases for more efficient comparison with constants on R2.
3138 switch (cond) {
3139 case kCondEQ:
3140 __ LoadConst32(TMP, rhs_imm);
3141 __ Beq(lhs, TMP, label);
3142 break;
3143 case kCondNE:
3144 __ LoadConst32(TMP, rhs_imm);
3145 __ Bne(lhs, TMP, label);
3146 break;
3147 case kCondLT:
3148 if (IsInt<16>(rhs_imm)) {
3149 __ Slti(TMP, lhs, rhs_imm);
3150 __ Bnez(TMP, label);
3151 } else {
3152 __ LoadConst32(TMP, rhs_imm);
3153 __ Blt(lhs, TMP, label);
3154 }
3155 break;
3156 case kCondGE:
3157 if (IsInt<16>(rhs_imm)) {
3158 __ Slti(TMP, lhs, rhs_imm);
3159 __ Beqz(TMP, label);
3160 } else {
3161 __ LoadConst32(TMP, rhs_imm);
3162 __ Bge(lhs, TMP, label);
3163 }
3164 break;
3165 case kCondLE:
3166 if (IsInt<16>(rhs_imm + 1)) {
3167 // Simulate lhs <= rhs via lhs < rhs + 1.
3168 __ Slti(TMP, lhs, rhs_imm + 1);
3169 __ Bnez(TMP, label);
3170 } else {
3171 __ LoadConst32(TMP, rhs_imm);
3172 __ Bge(TMP, lhs, label);
3173 }
3174 break;
3175 case kCondGT:
3176 if (IsInt<16>(rhs_imm + 1)) {
3177 // Simulate lhs > rhs via !(lhs < rhs + 1).
3178 __ Slti(TMP, lhs, rhs_imm + 1);
3179 __ Beqz(TMP, label);
3180 } else {
3181 __ LoadConst32(TMP, rhs_imm);
3182 __ Blt(TMP, lhs, label);
3183 }
3184 break;
3185 case kCondB:
3186 if (IsInt<16>(rhs_imm)) {
3187 __ Sltiu(TMP, lhs, rhs_imm);
3188 __ Bnez(TMP, label);
3189 } else {
3190 __ LoadConst32(TMP, rhs_imm);
3191 __ Bltu(lhs, TMP, label);
3192 }
3193 break;
3194 case kCondAE:
3195 if (IsInt<16>(rhs_imm)) {
3196 __ Sltiu(TMP, lhs, rhs_imm);
3197 __ Beqz(TMP, label);
3198 } else {
3199 __ LoadConst32(TMP, rhs_imm);
3200 __ Bgeu(lhs, TMP, label);
3201 }
3202 break;
3203 case kCondBE:
3204 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3205 // Simulate lhs <= rhs via lhs < rhs + 1.
3206 // Note that this only works if rhs + 1 does not overflow
3207 // to 0, hence the check above.
3208 __ Sltiu(TMP, lhs, rhs_imm + 1);
3209 __ Bnez(TMP, label);
3210 } else {
3211 __ LoadConst32(TMP, rhs_imm);
3212 __ Bgeu(TMP, lhs, label);
3213 }
3214 break;
3215 case kCondA:
3216 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3217 // Simulate lhs > rhs via !(lhs < rhs + 1).
3218 // Note that this only works if rhs + 1 does not overflow
3219 // to 0, hence the check above.
3220 __ Sltiu(TMP, lhs, rhs_imm + 1);
3221 __ Beqz(TMP, label);
3222 } else {
3223 __ LoadConst32(TMP, rhs_imm);
3224 __ Bltu(TMP, lhs, label);
3225 }
3226 break;
3227 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003228 }
3229 }
3230}
3231
3232void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3233 LocationSummary* locations,
3234 MipsLabel* label) {
3235 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3236 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3237 Location rhs_location = locations->InAt(1);
3238 Register rhs_high = ZERO;
3239 Register rhs_low = ZERO;
3240 int64_t imm = 0;
3241 uint32_t imm_high = 0;
3242 uint32_t imm_low = 0;
3243 bool use_imm = rhs_location.IsConstant();
3244 if (use_imm) {
3245 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3246 imm_high = High32Bits(imm);
3247 imm_low = Low32Bits(imm);
3248 } else {
3249 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3250 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3251 }
3252
3253 if (use_imm && imm == 0) {
3254 switch (cond) {
3255 case kCondEQ:
3256 case kCondBE: // <= 0 if zero
3257 __ Or(TMP, lhs_high, lhs_low);
3258 __ Beqz(TMP, label);
3259 break;
3260 case kCondNE:
3261 case kCondA: // > 0 if non-zero
3262 __ Or(TMP, lhs_high, lhs_low);
3263 __ Bnez(TMP, label);
3264 break;
3265 case kCondLT:
3266 __ Bltz(lhs_high, label);
3267 break;
3268 case kCondGE:
3269 __ Bgez(lhs_high, label);
3270 break;
3271 case kCondLE:
3272 __ Or(TMP, lhs_high, lhs_low);
3273 __ Sra(AT, lhs_high, 31);
3274 __ Bgeu(AT, TMP, label);
3275 break;
3276 case kCondGT:
3277 __ Or(TMP, lhs_high, lhs_low);
3278 __ Sra(AT, lhs_high, 31);
3279 __ Bltu(AT, TMP, label);
3280 break;
3281 case kCondB: // always false
3282 break;
3283 case kCondAE: // always true
3284 __ B(label);
3285 break;
3286 }
3287 } else if (use_imm) {
3288 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3289 switch (cond) {
3290 case kCondEQ:
3291 __ LoadConst32(TMP, imm_high);
3292 __ Xor(TMP, TMP, lhs_high);
3293 __ LoadConst32(AT, imm_low);
3294 __ Xor(AT, AT, lhs_low);
3295 __ Or(TMP, TMP, AT);
3296 __ Beqz(TMP, label);
3297 break;
3298 case kCondNE:
3299 __ LoadConst32(TMP, imm_high);
3300 __ Xor(TMP, TMP, lhs_high);
3301 __ LoadConst32(AT, imm_low);
3302 __ Xor(AT, AT, lhs_low);
3303 __ Or(TMP, TMP, AT);
3304 __ Bnez(TMP, label);
3305 break;
3306 case kCondLT:
3307 __ LoadConst32(TMP, imm_high);
3308 __ Blt(lhs_high, TMP, label);
3309 __ Slt(TMP, TMP, lhs_high);
3310 __ LoadConst32(AT, imm_low);
3311 __ Sltu(AT, lhs_low, AT);
3312 __ Blt(TMP, AT, label);
3313 break;
3314 case kCondGE:
3315 __ LoadConst32(TMP, imm_high);
3316 __ Blt(TMP, lhs_high, label);
3317 __ Slt(TMP, lhs_high, TMP);
3318 __ LoadConst32(AT, imm_low);
3319 __ Sltu(AT, lhs_low, AT);
3320 __ Or(TMP, TMP, AT);
3321 __ Beqz(TMP, label);
3322 break;
3323 case kCondLE:
3324 __ LoadConst32(TMP, imm_high);
3325 __ Blt(lhs_high, TMP, label);
3326 __ Slt(TMP, TMP, lhs_high);
3327 __ LoadConst32(AT, imm_low);
3328 __ Sltu(AT, AT, lhs_low);
3329 __ Or(TMP, TMP, AT);
3330 __ Beqz(TMP, label);
3331 break;
3332 case kCondGT:
3333 __ LoadConst32(TMP, imm_high);
3334 __ Blt(TMP, lhs_high, label);
3335 __ Slt(TMP, lhs_high, TMP);
3336 __ LoadConst32(AT, imm_low);
3337 __ Sltu(AT, AT, lhs_low);
3338 __ Blt(TMP, AT, label);
3339 break;
3340 case kCondB:
3341 __ LoadConst32(TMP, imm_high);
3342 __ Bltu(lhs_high, TMP, label);
3343 __ Sltu(TMP, TMP, lhs_high);
3344 __ LoadConst32(AT, imm_low);
3345 __ Sltu(AT, lhs_low, AT);
3346 __ Blt(TMP, AT, label);
3347 break;
3348 case kCondAE:
3349 __ LoadConst32(TMP, imm_high);
3350 __ Bltu(TMP, lhs_high, label);
3351 __ Sltu(TMP, lhs_high, TMP);
3352 __ LoadConst32(AT, imm_low);
3353 __ Sltu(AT, lhs_low, AT);
3354 __ Or(TMP, TMP, AT);
3355 __ Beqz(TMP, label);
3356 break;
3357 case kCondBE:
3358 __ LoadConst32(TMP, imm_high);
3359 __ Bltu(lhs_high, TMP, label);
3360 __ Sltu(TMP, TMP, lhs_high);
3361 __ LoadConst32(AT, imm_low);
3362 __ Sltu(AT, AT, lhs_low);
3363 __ Or(TMP, TMP, AT);
3364 __ Beqz(TMP, label);
3365 break;
3366 case kCondA:
3367 __ LoadConst32(TMP, imm_high);
3368 __ Bltu(TMP, lhs_high, label);
3369 __ Sltu(TMP, lhs_high, TMP);
3370 __ LoadConst32(AT, imm_low);
3371 __ Sltu(AT, AT, lhs_low);
3372 __ Blt(TMP, AT, label);
3373 break;
3374 }
3375 } else {
3376 switch (cond) {
3377 case kCondEQ:
3378 __ Xor(TMP, lhs_high, rhs_high);
3379 __ Xor(AT, lhs_low, rhs_low);
3380 __ Or(TMP, TMP, AT);
3381 __ Beqz(TMP, label);
3382 break;
3383 case kCondNE:
3384 __ Xor(TMP, lhs_high, rhs_high);
3385 __ Xor(AT, lhs_low, rhs_low);
3386 __ Or(TMP, TMP, AT);
3387 __ Bnez(TMP, label);
3388 break;
3389 case kCondLT:
3390 __ Blt(lhs_high, rhs_high, label);
3391 __ Slt(TMP, rhs_high, lhs_high);
3392 __ Sltu(AT, lhs_low, rhs_low);
3393 __ Blt(TMP, AT, label);
3394 break;
3395 case kCondGE:
3396 __ Blt(rhs_high, lhs_high, label);
3397 __ Slt(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 kCondLE:
3403 __ Blt(lhs_high, rhs_high, label);
3404 __ Slt(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 kCondGT:
3410 __ Blt(rhs_high, lhs_high, label);
3411 __ Slt(TMP, lhs_high, rhs_high);
3412 __ Sltu(AT, rhs_low, lhs_low);
3413 __ Blt(TMP, AT, label);
3414 break;
3415 case kCondB:
3416 __ Bltu(lhs_high, rhs_high, label);
3417 __ Sltu(TMP, rhs_high, lhs_high);
3418 __ Sltu(AT, lhs_low, rhs_low);
3419 __ Blt(TMP, AT, label);
3420 break;
3421 case kCondAE:
3422 __ Bltu(rhs_high, lhs_high, label);
3423 __ Sltu(TMP, lhs_high, rhs_high);
3424 __ Sltu(AT, lhs_low, rhs_low);
3425 __ Or(TMP, TMP, AT);
3426 __ Beqz(TMP, label);
3427 break;
3428 case kCondBE:
3429 __ Bltu(lhs_high, rhs_high, label);
3430 __ Sltu(TMP, rhs_high, lhs_high);
3431 __ Sltu(AT, rhs_low, lhs_low);
3432 __ Or(TMP, TMP, AT);
3433 __ Beqz(TMP, label);
3434 break;
3435 case kCondA:
3436 __ Bltu(rhs_high, lhs_high, label);
3437 __ Sltu(TMP, lhs_high, rhs_high);
3438 __ Sltu(AT, rhs_low, lhs_low);
3439 __ Blt(TMP, AT, label);
3440 break;
3441 }
3442 }
3443}
3444
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003445void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3446 bool gt_bias,
3447 Primitive::Type type,
3448 LocationSummary* locations) {
3449 Register dst = locations->Out().AsRegister<Register>();
3450 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3451 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3452 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3453 if (type == Primitive::kPrimFloat) {
3454 if (isR6) {
3455 switch (cond) {
3456 case kCondEQ:
3457 __ CmpEqS(FTMP, lhs, rhs);
3458 __ Mfc1(dst, FTMP);
3459 __ Andi(dst, dst, 1);
3460 break;
3461 case kCondNE:
3462 __ CmpEqS(FTMP, lhs, rhs);
3463 __ Mfc1(dst, FTMP);
3464 __ Addiu(dst, dst, 1);
3465 break;
3466 case kCondLT:
3467 if (gt_bias) {
3468 __ CmpLtS(FTMP, lhs, rhs);
3469 } else {
3470 __ CmpUltS(FTMP, lhs, rhs);
3471 }
3472 __ Mfc1(dst, FTMP);
3473 __ Andi(dst, dst, 1);
3474 break;
3475 case kCondLE:
3476 if (gt_bias) {
3477 __ CmpLeS(FTMP, lhs, rhs);
3478 } else {
3479 __ CmpUleS(FTMP, lhs, rhs);
3480 }
3481 __ Mfc1(dst, FTMP);
3482 __ Andi(dst, dst, 1);
3483 break;
3484 case kCondGT:
3485 if (gt_bias) {
3486 __ CmpUltS(FTMP, rhs, lhs);
3487 } else {
3488 __ CmpLtS(FTMP, rhs, lhs);
3489 }
3490 __ Mfc1(dst, FTMP);
3491 __ Andi(dst, dst, 1);
3492 break;
3493 case kCondGE:
3494 if (gt_bias) {
3495 __ CmpUleS(FTMP, rhs, lhs);
3496 } else {
3497 __ CmpLeS(FTMP, rhs, lhs);
3498 }
3499 __ Mfc1(dst, FTMP);
3500 __ Andi(dst, dst, 1);
3501 break;
3502 default:
3503 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3504 UNREACHABLE();
3505 }
3506 } else {
3507 switch (cond) {
3508 case kCondEQ:
3509 __ CeqS(0, lhs, rhs);
3510 __ LoadConst32(dst, 1);
3511 __ Movf(dst, ZERO, 0);
3512 break;
3513 case kCondNE:
3514 __ CeqS(0, lhs, rhs);
3515 __ LoadConst32(dst, 1);
3516 __ Movt(dst, ZERO, 0);
3517 break;
3518 case kCondLT:
3519 if (gt_bias) {
3520 __ ColtS(0, lhs, rhs);
3521 } else {
3522 __ CultS(0, lhs, rhs);
3523 }
3524 __ LoadConst32(dst, 1);
3525 __ Movf(dst, ZERO, 0);
3526 break;
3527 case kCondLE:
3528 if (gt_bias) {
3529 __ ColeS(0, lhs, rhs);
3530 } else {
3531 __ CuleS(0, lhs, rhs);
3532 }
3533 __ LoadConst32(dst, 1);
3534 __ Movf(dst, ZERO, 0);
3535 break;
3536 case kCondGT:
3537 if (gt_bias) {
3538 __ CultS(0, rhs, lhs);
3539 } else {
3540 __ ColtS(0, rhs, lhs);
3541 }
3542 __ LoadConst32(dst, 1);
3543 __ Movf(dst, ZERO, 0);
3544 break;
3545 case kCondGE:
3546 if (gt_bias) {
3547 __ CuleS(0, rhs, lhs);
3548 } else {
3549 __ ColeS(0, rhs, lhs);
3550 }
3551 __ LoadConst32(dst, 1);
3552 __ Movf(dst, ZERO, 0);
3553 break;
3554 default:
3555 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3556 UNREACHABLE();
3557 }
3558 }
3559 } else {
3560 DCHECK_EQ(type, Primitive::kPrimDouble);
3561 if (isR6) {
3562 switch (cond) {
3563 case kCondEQ:
3564 __ CmpEqD(FTMP, lhs, rhs);
3565 __ Mfc1(dst, FTMP);
3566 __ Andi(dst, dst, 1);
3567 break;
3568 case kCondNE:
3569 __ CmpEqD(FTMP, lhs, rhs);
3570 __ Mfc1(dst, FTMP);
3571 __ Addiu(dst, dst, 1);
3572 break;
3573 case kCondLT:
3574 if (gt_bias) {
3575 __ CmpLtD(FTMP, lhs, rhs);
3576 } else {
3577 __ CmpUltD(FTMP, lhs, rhs);
3578 }
3579 __ Mfc1(dst, FTMP);
3580 __ Andi(dst, dst, 1);
3581 break;
3582 case kCondLE:
3583 if (gt_bias) {
3584 __ CmpLeD(FTMP, lhs, rhs);
3585 } else {
3586 __ CmpUleD(FTMP, lhs, rhs);
3587 }
3588 __ Mfc1(dst, FTMP);
3589 __ Andi(dst, dst, 1);
3590 break;
3591 case kCondGT:
3592 if (gt_bias) {
3593 __ CmpUltD(FTMP, rhs, lhs);
3594 } else {
3595 __ CmpLtD(FTMP, rhs, lhs);
3596 }
3597 __ Mfc1(dst, FTMP);
3598 __ Andi(dst, dst, 1);
3599 break;
3600 case kCondGE:
3601 if (gt_bias) {
3602 __ CmpUleD(FTMP, rhs, lhs);
3603 } else {
3604 __ CmpLeD(FTMP, rhs, lhs);
3605 }
3606 __ Mfc1(dst, FTMP);
3607 __ Andi(dst, dst, 1);
3608 break;
3609 default:
3610 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3611 UNREACHABLE();
3612 }
3613 } else {
3614 switch (cond) {
3615 case kCondEQ:
3616 __ CeqD(0, lhs, rhs);
3617 __ LoadConst32(dst, 1);
3618 __ Movf(dst, ZERO, 0);
3619 break;
3620 case kCondNE:
3621 __ CeqD(0, lhs, rhs);
3622 __ LoadConst32(dst, 1);
3623 __ Movt(dst, ZERO, 0);
3624 break;
3625 case kCondLT:
3626 if (gt_bias) {
3627 __ ColtD(0, lhs, rhs);
3628 } else {
3629 __ CultD(0, lhs, rhs);
3630 }
3631 __ LoadConst32(dst, 1);
3632 __ Movf(dst, ZERO, 0);
3633 break;
3634 case kCondLE:
3635 if (gt_bias) {
3636 __ ColeD(0, lhs, rhs);
3637 } else {
3638 __ CuleD(0, lhs, rhs);
3639 }
3640 __ LoadConst32(dst, 1);
3641 __ Movf(dst, ZERO, 0);
3642 break;
3643 case kCondGT:
3644 if (gt_bias) {
3645 __ CultD(0, rhs, lhs);
3646 } else {
3647 __ ColtD(0, rhs, lhs);
3648 }
3649 __ LoadConst32(dst, 1);
3650 __ Movf(dst, ZERO, 0);
3651 break;
3652 case kCondGE:
3653 if (gt_bias) {
3654 __ CuleD(0, rhs, lhs);
3655 } else {
3656 __ ColeD(0, rhs, lhs);
3657 }
3658 __ LoadConst32(dst, 1);
3659 __ Movf(dst, ZERO, 0);
3660 break;
3661 default:
3662 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3663 UNREACHABLE();
3664 }
3665 }
3666 }
3667}
3668
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003669bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3670 bool gt_bias,
3671 Primitive::Type type,
3672 LocationSummary* input_locations,
3673 int cc) {
3674 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3675 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3676 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3677 if (type == Primitive::kPrimFloat) {
3678 switch (cond) {
3679 case kCondEQ:
3680 __ CeqS(cc, lhs, rhs);
3681 return false;
3682 case kCondNE:
3683 __ CeqS(cc, lhs, rhs);
3684 return true;
3685 case kCondLT:
3686 if (gt_bias) {
3687 __ ColtS(cc, lhs, rhs);
3688 } else {
3689 __ CultS(cc, lhs, rhs);
3690 }
3691 return false;
3692 case kCondLE:
3693 if (gt_bias) {
3694 __ ColeS(cc, lhs, rhs);
3695 } else {
3696 __ CuleS(cc, lhs, rhs);
3697 }
3698 return false;
3699 case kCondGT:
3700 if (gt_bias) {
3701 __ CultS(cc, rhs, lhs);
3702 } else {
3703 __ ColtS(cc, rhs, lhs);
3704 }
3705 return false;
3706 case kCondGE:
3707 if (gt_bias) {
3708 __ CuleS(cc, rhs, lhs);
3709 } else {
3710 __ ColeS(cc, rhs, lhs);
3711 }
3712 return false;
3713 default:
3714 LOG(FATAL) << "Unexpected non-floating-point condition";
3715 UNREACHABLE();
3716 }
3717 } else {
3718 DCHECK_EQ(type, Primitive::kPrimDouble);
3719 switch (cond) {
3720 case kCondEQ:
3721 __ CeqD(cc, lhs, rhs);
3722 return false;
3723 case kCondNE:
3724 __ CeqD(cc, lhs, rhs);
3725 return true;
3726 case kCondLT:
3727 if (gt_bias) {
3728 __ ColtD(cc, lhs, rhs);
3729 } else {
3730 __ CultD(cc, lhs, rhs);
3731 }
3732 return false;
3733 case kCondLE:
3734 if (gt_bias) {
3735 __ ColeD(cc, lhs, rhs);
3736 } else {
3737 __ CuleD(cc, lhs, rhs);
3738 }
3739 return false;
3740 case kCondGT:
3741 if (gt_bias) {
3742 __ CultD(cc, rhs, lhs);
3743 } else {
3744 __ ColtD(cc, rhs, lhs);
3745 }
3746 return false;
3747 case kCondGE:
3748 if (gt_bias) {
3749 __ CuleD(cc, rhs, lhs);
3750 } else {
3751 __ ColeD(cc, rhs, lhs);
3752 }
3753 return false;
3754 default:
3755 LOG(FATAL) << "Unexpected non-floating-point condition";
3756 UNREACHABLE();
3757 }
3758 }
3759}
3760
3761bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3762 bool gt_bias,
3763 Primitive::Type type,
3764 LocationSummary* input_locations,
3765 FRegister dst) {
3766 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3767 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3768 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3769 if (type == Primitive::kPrimFloat) {
3770 switch (cond) {
3771 case kCondEQ:
3772 __ CmpEqS(dst, lhs, rhs);
3773 return false;
3774 case kCondNE:
3775 __ CmpEqS(dst, lhs, rhs);
3776 return true;
3777 case kCondLT:
3778 if (gt_bias) {
3779 __ CmpLtS(dst, lhs, rhs);
3780 } else {
3781 __ CmpUltS(dst, lhs, rhs);
3782 }
3783 return false;
3784 case kCondLE:
3785 if (gt_bias) {
3786 __ CmpLeS(dst, lhs, rhs);
3787 } else {
3788 __ CmpUleS(dst, lhs, rhs);
3789 }
3790 return false;
3791 case kCondGT:
3792 if (gt_bias) {
3793 __ CmpUltS(dst, rhs, lhs);
3794 } else {
3795 __ CmpLtS(dst, rhs, lhs);
3796 }
3797 return false;
3798 case kCondGE:
3799 if (gt_bias) {
3800 __ CmpUleS(dst, rhs, lhs);
3801 } else {
3802 __ CmpLeS(dst, rhs, lhs);
3803 }
3804 return false;
3805 default:
3806 LOG(FATAL) << "Unexpected non-floating-point condition";
3807 UNREACHABLE();
3808 }
3809 } else {
3810 DCHECK_EQ(type, Primitive::kPrimDouble);
3811 switch (cond) {
3812 case kCondEQ:
3813 __ CmpEqD(dst, lhs, rhs);
3814 return false;
3815 case kCondNE:
3816 __ CmpEqD(dst, lhs, rhs);
3817 return true;
3818 case kCondLT:
3819 if (gt_bias) {
3820 __ CmpLtD(dst, lhs, rhs);
3821 } else {
3822 __ CmpUltD(dst, lhs, rhs);
3823 }
3824 return false;
3825 case kCondLE:
3826 if (gt_bias) {
3827 __ CmpLeD(dst, lhs, rhs);
3828 } else {
3829 __ CmpUleD(dst, lhs, rhs);
3830 }
3831 return false;
3832 case kCondGT:
3833 if (gt_bias) {
3834 __ CmpUltD(dst, rhs, lhs);
3835 } else {
3836 __ CmpLtD(dst, rhs, lhs);
3837 }
3838 return false;
3839 case kCondGE:
3840 if (gt_bias) {
3841 __ CmpUleD(dst, rhs, lhs);
3842 } else {
3843 __ CmpLeD(dst, rhs, lhs);
3844 }
3845 return false;
3846 default:
3847 LOG(FATAL) << "Unexpected non-floating-point condition";
3848 UNREACHABLE();
3849 }
3850 }
3851}
3852
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003853void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3854 bool gt_bias,
3855 Primitive::Type type,
3856 LocationSummary* locations,
3857 MipsLabel* label) {
3858 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3859 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3860 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3861 if (type == Primitive::kPrimFloat) {
3862 if (isR6) {
3863 switch (cond) {
3864 case kCondEQ:
3865 __ CmpEqS(FTMP, lhs, rhs);
3866 __ Bc1nez(FTMP, label);
3867 break;
3868 case kCondNE:
3869 __ CmpEqS(FTMP, lhs, rhs);
3870 __ Bc1eqz(FTMP, label);
3871 break;
3872 case kCondLT:
3873 if (gt_bias) {
3874 __ CmpLtS(FTMP, lhs, rhs);
3875 } else {
3876 __ CmpUltS(FTMP, lhs, rhs);
3877 }
3878 __ Bc1nez(FTMP, label);
3879 break;
3880 case kCondLE:
3881 if (gt_bias) {
3882 __ CmpLeS(FTMP, lhs, rhs);
3883 } else {
3884 __ CmpUleS(FTMP, lhs, rhs);
3885 }
3886 __ Bc1nez(FTMP, label);
3887 break;
3888 case kCondGT:
3889 if (gt_bias) {
3890 __ CmpUltS(FTMP, rhs, lhs);
3891 } else {
3892 __ CmpLtS(FTMP, rhs, lhs);
3893 }
3894 __ Bc1nez(FTMP, label);
3895 break;
3896 case kCondGE:
3897 if (gt_bias) {
3898 __ CmpUleS(FTMP, rhs, lhs);
3899 } else {
3900 __ CmpLeS(FTMP, rhs, lhs);
3901 }
3902 __ Bc1nez(FTMP, label);
3903 break;
3904 default:
3905 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003906 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003907 }
3908 } else {
3909 switch (cond) {
3910 case kCondEQ:
3911 __ CeqS(0, lhs, rhs);
3912 __ Bc1t(0, label);
3913 break;
3914 case kCondNE:
3915 __ CeqS(0, lhs, rhs);
3916 __ Bc1f(0, label);
3917 break;
3918 case kCondLT:
3919 if (gt_bias) {
3920 __ ColtS(0, lhs, rhs);
3921 } else {
3922 __ CultS(0, lhs, rhs);
3923 }
3924 __ Bc1t(0, label);
3925 break;
3926 case kCondLE:
3927 if (gt_bias) {
3928 __ ColeS(0, lhs, rhs);
3929 } else {
3930 __ CuleS(0, lhs, rhs);
3931 }
3932 __ Bc1t(0, label);
3933 break;
3934 case kCondGT:
3935 if (gt_bias) {
3936 __ CultS(0, rhs, lhs);
3937 } else {
3938 __ ColtS(0, rhs, lhs);
3939 }
3940 __ Bc1t(0, label);
3941 break;
3942 case kCondGE:
3943 if (gt_bias) {
3944 __ CuleS(0, rhs, lhs);
3945 } else {
3946 __ ColeS(0, rhs, lhs);
3947 }
3948 __ Bc1t(0, label);
3949 break;
3950 default:
3951 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003952 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003953 }
3954 }
3955 } else {
3956 DCHECK_EQ(type, Primitive::kPrimDouble);
3957 if (isR6) {
3958 switch (cond) {
3959 case kCondEQ:
3960 __ CmpEqD(FTMP, lhs, rhs);
3961 __ Bc1nez(FTMP, label);
3962 break;
3963 case kCondNE:
3964 __ CmpEqD(FTMP, lhs, rhs);
3965 __ Bc1eqz(FTMP, label);
3966 break;
3967 case kCondLT:
3968 if (gt_bias) {
3969 __ CmpLtD(FTMP, lhs, rhs);
3970 } else {
3971 __ CmpUltD(FTMP, lhs, rhs);
3972 }
3973 __ Bc1nez(FTMP, label);
3974 break;
3975 case kCondLE:
3976 if (gt_bias) {
3977 __ CmpLeD(FTMP, lhs, rhs);
3978 } else {
3979 __ CmpUleD(FTMP, lhs, rhs);
3980 }
3981 __ Bc1nez(FTMP, label);
3982 break;
3983 case kCondGT:
3984 if (gt_bias) {
3985 __ CmpUltD(FTMP, rhs, lhs);
3986 } else {
3987 __ CmpLtD(FTMP, rhs, lhs);
3988 }
3989 __ Bc1nez(FTMP, label);
3990 break;
3991 case kCondGE:
3992 if (gt_bias) {
3993 __ CmpUleD(FTMP, rhs, lhs);
3994 } else {
3995 __ CmpLeD(FTMP, rhs, lhs);
3996 }
3997 __ Bc1nez(FTMP, label);
3998 break;
3999 default:
4000 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004001 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004002 }
4003 } else {
4004 switch (cond) {
4005 case kCondEQ:
4006 __ CeqD(0, lhs, rhs);
4007 __ Bc1t(0, label);
4008 break;
4009 case kCondNE:
4010 __ CeqD(0, lhs, rhs);
4011 __ Bc1f(0, label);
4012 break;
4013 case kCondLT:
4014 if (gt_bias) {
4015 __ ColtD(0, lhs, rhs);
4016 } else {
4017 __ CultD(0, lhs, rhs);
4018 }
4019 __ Bc1t(0, label);
4020 break;
4021 case kCondLE:
4022 if (gt_bias) {
4023 __ ColeD(0, lhs, rhs);
4024 } else {
4025 __ CuleD(0, lhs, rhs);
4026 }
4027 __ Bc1t(0, label);
4028 break;
4029 case kCondGT:
4030 if (gt_bias) {
4031 __ CultD(0, rhs, lhs);
4032 } else {
4033 __ ColtD(0, rhs, lhs);
4034 }
4035 __ Bc1t(0, label);
4036 break;
4037 case kCondGE:
4038 if (gt_bias) {
4039 __ CuleD(0, rhs, lhs);
4040 } else {
4041 __ ColeD(0, rhs, lhs);
4042 }
4043 __ Bc1t(0, label);
4044 break;
4045 default:
4046 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004047 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004048 }
4049 }
4050 }
4051}
4052
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004053void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004054 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004055 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004056 MipsLabel* false_target) {
4057 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004058
David Brazdil0debae72015-11-12 18:37:00 +00004059 if (true_target == nullptr && false_target == nullptr) {
4060 // Nothing to do. The code always falls through.
4061 return;
4062 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004063 // Constant condition, statically compared against "true" (integer value 1).
4064 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004065 if (true_target != nullptr) {
4066 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004067 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004068 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004069 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004070 if (false_target != nullptr) {
4071 __ B(false_target);
4072 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004073 }
David Brazdil0debae72015-11-12 18:37:00 +00004074 return;
4075 }
4076
4077 // The following code generates these patterns:
4078 // (1) true_target == nullptr && false_target != nullptr
4079 // - opposite condition true => branch to false_target
4080 // (2) true_target != nullptr && false_target == nullptr
4081 // - condition true => branch to true_target
4082 // (3) true_target != nullptr && false_target != nullptr
4083 // - condition true => branch to true_target
4084 // - branch to false_target
4085 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004086 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004087 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004088 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004089 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004090 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4091 } else {
4092 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4093 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004094 } else {
4095 // The condition instruction has not been materialized, use its inputs as
4096 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004097 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004098 Primitive::Type type = condition->InputAt(0)->GetType();
4099 LocationSummary* locations = cond->GetLocations();
4100 IfCondition if_cond = condition->GetCondition();
4101 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004102
David Brazdil0debae72015-11-12 18:37:00 +00004103 if (true_target == nullptr) {
4104 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004105 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004106 }
4107
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004108 switch (type) {
4109 default:
4110 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4111 break;
4112 case Primitive::kPrimLong:
4113 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4114 break;
4115 case Primitive::kPrimFloat:
4116 case Primitive::kPrimDouble:
4117 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4118 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119 }
4120 }
David Brazdil0debae72015-11-12 18:37:00 +00004121
4122 // If neither branch falls through (case 3), the conditional branch to `true_target`
4123 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4124 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004125 __ B(false_target);
4126 }
4127}
4128
4129void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4130 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004131 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004132 locations->SetInAt(0, Location::RequiresRegister());
4133 }
4134}
4135
4136void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004137 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4138 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4139 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4140 nullptr : codegen_->GetLabelOf(true_successor);
4141 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4142 nullptr : codegen_->GetLabelOf(false_successor);
4143 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004144}
4145
4146void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4147 LocationSummary* locations = new (GetGraph()->GetArena())
4148 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004149 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004150 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004151 locations->SetInAt(0, Location::RequiresRegister());
4152 }
4153}
4154
4155void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004156 SlowPathCodeMIPS* slow_path =
4157 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004158 GenerateTestAndBranch(deoptimize,
4159 /* condition_input_index */ 0,
4160 slow_path->GetEntryLabel(),
4161 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004162}
4163
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004164// This function returns true if a conditional move can be generated for HSelect.
4165// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4166// branches and regular moves.
4167//
4168// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4169//
4170// While determining feasibility of a conditional move and setting inputs/outputs
4171// are two distinct tasks, this function does both because they share quite a bit
4172// of common logic.
4173static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4174 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4175 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4176 HCondition* condition = cond->AsCondition();
4177
4178 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4179 Primitive::Type dst_type = select->GetType();
4180
4181 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4182 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4183 bool is_true_value_zero_constant =
4184 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4185 bool is_false_value_zero_constant =
4186 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4187
4188 bool can_move_conditionally = false;
4189 bool use_const_for_false_in = false;
4190 bool use_const_for_true_in = false;
4191
4192 if (!cond->IsConstant()) {
4193 switch (cond_type) {
4194 default:
4195 switch (dst_type) {
4196 default:
4197 // Moving int on int condition.
4198 if (is_r6) {
4199 if (is_true_value_zero_constant) {
4200 // seleqz out_reg, false_reg, 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, true_reg, cond_reg
4205 can_move_conditionally = true;
4206 use_const_for_false_in = true;
4207 } else if (materialized) {
4208 // Not materializing unmaterialized int conditions
4209 // to keep the instruction count low.
4210 // selnez AT, true_reg, cond_reg
4211 // seleqz TMP, false_reg, cond_reg
4212 // or out_reg, AT, TMP
4213 can_move_conditionally = true;
4214 }
4215 } else {
4216 // movn out_reg, true_reg/ZERO, cond_reg
4217 can_move_conditionally = true;
4218 use_const_for_true_in = is_true_value_zero_constant;
4219 }
4220 break;
4221 case Primitive::kPrimLong:
4222 // Moving long on int condition.
4223 if (is_r6) {
4224 if (is_true_value_zero_constant) {
4225 // seleqz out_reg_lo, false_reg_lo, cond_reg
4226 // seleqz out_reg_hi, false_reg_hi, cond_reg
4227 can_move_conditionally = true;
4228 use_const_for_true_in = true;
4229 } else if (is_false_value_zero_constant) {
4230 // selnez out_reg_lo, true_reg_lo, cond_reg
4231 // selnez out_reg_hi, true_reg_hi, cond_reg
4232 can_move_conditionally = true;
4233 use_const_for_false_in = true;
4234 }
4235 // Other long conditional moves would generate 6+ instructions,
4236 // which is too many.
4237 } else {
4238 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4239 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4240 can_move_conditionally = true;
4241 use_const_for_true_in = is_true_value_zero_constant;
4242 }
4243 break;
4244 case Primitive::kPrimFloat:
4245 case Primitive::kPrimDouble:
4246 // Moving float/double on int condition.
4247 if (is_r6) {
4248 if (materialized) {
4249 // Not materializing unmaterialized int conditions
4250 // to keep the instruction count low.
4251 can_move_conditionally = true;
4252 if (is_true_value_zero_constant) {
4253 // sltu TMP, ZERO, cond_reg
4254 // mtc1 TMP, temp_cond_reg
4255 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4256 use_const_for_true_in = true;
4257 } else if (is_false_value_zero_constant) {
4258 // sltu TMP, ZERO, cond_reg
4259 // mtc1 TMP, temp_cond_reg
4260 // selnez.fmt out_reg, true_reg, temp_cond_reg
4261 use_const_for_false_in = true;
4262 } else {
4263 // sltu TMP, ZERO, cond_reg
4264 // mtc1 TMP, temp_cond_reg
4265 // sel.fmt temp_cond_reg, false_reg, true_reg
4266 // mov.fmt out_reg, temp_cond_reg
4267 }
4268 }
4269 } else {
4270 // movn.fmt out_reg, true_reg, cond_reg
4271 can_move_conditionally = true;
4272 }
4273 break;
4274 }
4275 break;
4276 case Primitive::kPrimLong:
4277 // We don't materialize long comparison now
4278 // and use conditional branches instead.
4279 break;
4280 case Primitive::kPrimFloat:
4281 case Primitive::kPrimDouble:
4282 switch (dst_type) {
4283 default:
4284 // Moving int 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, false_reg, TMP
4289 can_move_conditionally = true;
4290 use_const_for_true_in = true;
4291 } else if (is_false_value_zero_constant) {
4292 // mfc1 TMP, temp_cond_reg
4293 // selnez out_reg, true_reg, TMP
4294 can_move_conditionally = true;
4295 use_const_for_false_in = true;
4296 } else {
4297 // mfc1 TMP, temp_cond_reg
4298 // selnez AT, true_reg, TMP
4299 // seleqz TMP, false_reg, TMP
4300 // or out_reg, AT, TMP
4301 can_move_conditionally = true;
4302 }
4303 } else {
4304 // movt out_reg, true_reg/ZERO, cc
4305 can_move_conditionally = true;
4306 use_const_for_true_in = is_true_value_zero_constant;
4307 }
4308 break;
4309 case Primitive::kPrimLong:
4310 // Moving long on float/double condition.
4311 if (is_r6) {
4312 if (is_true_value_zero_constant) {
4313 // mfc1 TMP, temp_cond_reg
4314 // seleqz out_reg_lo, false_reg_lo, TMP
4315 // seleqz out_reg_hi, false_reg_hi, TMP
4316 can_move_conditionally = true;
4317 use_const_for_true_in = true;
4318 } else if (is_false_value_zero_constant) {
4319 // mfc1 TMP, temp_cond_reg
4320 // selnez out_reg_lo, true_reg_lo, TMP
4321 // selnez out_reg_hi, true_reg_hi, TMP
4322 can_move_conditionally = true;
4323 use_const_for_false_in = true;
4324 }
4325 // Other long conditional moves would generate 6+ instructions,
4326 // which is too many.
4327 } else {
4328 // movt out_reg_lo, true_reg_lo/ZERO, cc
4329 // movt out_reg_hi, true_reg_hi/ZERO, cc
4330 can_move_conditionally = true;
4331 use_const_for_true_in = is_true_value_zero_constant;
4332 }
4333 break;
4334 case Primitive::kPrimFloat:
4335 case Primitive::kPrimDouble:
4336 // Moving float/double on float/double condition.
4337 if (is_r6) {
4338 can_move_conditionally = true;
4339 if (is_true_value_zero_constant) {
4340 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4341 use_const_for_true_in = true;
4342 } else if (is_false_value_zero_constant) {
4343 // selnez.fmt out_reg, true_reg, temp_cond_reg
4344 use_const_for_false_in = true;
4345 } else {
4346 // sel.fmt temp_cond_reg, false_reg, true_reg
4347 // mov.fmt out_reg, temp_cond_reg
4348 }
4349 } else {
4350 // movt.fmt out_reg, true_reg, cc
4351 can_move_conditionally = true;
4352 }
4353 break;
4354 }
4355 break;
4356 }
4357 }
4358
4359 if (can_move_conditionally) {
4360 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4361 } else {
4362 DCHECK(!use_const_for_false_in);
4363 DCHECK(!use_const_for_true_in);
4364 }
4365
4366 if (locations_to_set != nullptr) {
4367 if (use_const_for_false_in) {
4368 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4369 } else {
4370 locations_to_set->SetInAt(0,
4371 Primitive::IsFloatingPointType(dst_type)
4372 ? Location::RequiresFpuRegister()
4373 : Location::RequiresRegister());
4374 }
4375 if (use_const_for_true_in) {
4376 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4377 } else {
4378 locations_to_set->SetInAt(1,
4379 Primitive::IsFloatingPointType(dst_type)
4380 ? Location::RequiresFpuRegister()
4381 : Location::RequiresRegister());
4382 }
4383 if (materialized) {
4384 locations_to_set->SetInAt(2, Location::RequiresRegister());
4385 }
4386 // On R6 we don't require the output to be the same as the
4387 // first input for conditional moves unlike on R2.
4388 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4389 if (is_out_same_as_first_in) {
4390 locations_to_set->SetOut(Location::SameAsFirstInput());
4391 } else {
4392 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4393 ? Location::RequiresFpuRegister()
4394 : Location::RequiresRegister());
4395 }
4396 }
4397
4398 return can_move_conditionally;
4399}
4400
4401void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4402 LocationSummary* locations = select->GetLocations();
4403 Location dst = locations->Out();
4404 Location src = locations->InAt(1);
4405 Register src_reg = ZERO;
4406 Register src_reg_high = ZERO;
4407 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4408 Register cond_reg = TMP;
4409 int cond_cc = 0;
4410 Primitive::Type cond_type = Primitive::kPrimInt;
4411 bool cond_inverted = false;
4412 Primitive::Type dst_type = select->GetType();
4413
4414 if (IsBooleanValueOrMaterializedCondition(cond)) {
4415 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4416 } else {
4417 HCondition* condition = cond->AsCondition();
4418 LocationSummary* cond_locations = cond->GetLocations();
4419 IfCondition if_cond = condition->GetCondition();
4420 cond_type = condition->InputAt(0)->GetType();
4421 switch (cond_type) {
4422 default:
4423 DCHECK_NE(cond_type, Primitive::kPrimLong);
4424 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4425 break;
4426 case Primitive::kPrimFloat:
4427 case Primitive::kPrimDouble:
4428 cond_inverted = MaterializeFpCompareR2(if_cond,
4429 condition->IsGtBias(),
4430 cond_type,
4431 cond_locations,
4432 cond_cc);
4433 break;
4434 }
4435 }
4436
4437 DCHECK(dst.Equals(locations->InAt(0)));
4438 if (src.IsRegister()) {
4439 src_reg = src.AsRegister<Register>();
4440 } else if (src.IsRegisterPair()) {
4441 src_reg = src.AsRegisterPairLow<Register>();
4442 src_reg_high = src.AsRegisterPairHigh<Register>();
4443 } else if (src.IsConstant()) {
4444 DCHECK(src.GetConstant()->IsZeroBitPattern());
4445 }
4446
4447 switch (cond_type) {
4448 default:
4449 switch (dst_type) {
4450 default:
4451 if (cond_inverted) {
4452 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4453 } else {
4454 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4455 }
4456 break;
4457 case Primitive::kPrimLong:
4458 if (cond_inverted) {
4459 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4460 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4461 } else {
4462 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4463 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4464 }
4465 break;
4466 case Primitive::kPrimFloat:
4467 if (cond_inverted) {
4468 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4469 } else {
4470 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4471 }
4472 break;
4473 case Primitive::kPrimDouble:
4474 if (cond_inverted) {
4475 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4476 } else {
4477 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4478 }
4479 break;
4480 }
4481 break;
4482 case Primitive::kPrimLong:
4483 LOG(FATAL) << "Unreachable";
4484 UNREACHABLE();
4485 case Primitive::kPrimFloat:
4486 case Primitive::kPrimDouble:
4487 switch (dst_type) {
4488 default:
4489 if (cond_inverted) {
4490 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4491 } else {
4492 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4493 }
4494 break;
4495 case Primitive::kPrimLong:
4496 if (cond_inverted) {
4497 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4498 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4499 } else {
4500 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4501 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4502 }
4503 break;
4504 case Primitive::kPrimFloat:
4505 if (cond_inverted) {
4506 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4507 } else {
4508 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4509 }
4510 break;
4511 case Primitive::kPrimDouble:
4512 if (cond_inverted) {
4513 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4514 } else {
4515 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4516 }
4517 break;
4518 }
4519 break;
4520 }
4521}
4522
4523void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4524 LocationSummary* locations = select->GetLocations();
4525 Location dst = locations->Out();
4526 Location false_src = locations->InAt(0);
4527 Location true_src = locations->InAt(1);
4528 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4529 Register cond_reg = TMP;
4530 FRegister fcond_reg = FTMP;
4531 Primitive::Type cond_type = Primitive::kPrimInt;
4532 bool cond_inverted = false;
4533 Primitive::Type dst_type = select->GetType();
4534
4535 if (IsBooleanValueOrMaterializedCondition(cond)) {
4536 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4537 } else {
4538 HCondition* condition = cond->AsCondition();
4539 LocationSummary* cond_locations = cond->GetLocations();
4540 IfCondition if_cond = condition->GetCondition();
4541 cond_type = condition->InputAt(0)->GetType();
4542 switch (cond_type) {
4543 default:
4544 DCHECK_NE(cond_type, Primitive::kPrimLong);
4545 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4546 break;
4547 case Primitive::kPrimFloat:
4548 case Primitive::kPrimDouble:
4549 cond_inverted = MaterializeFpCompareR6(if_cond,
4550 condition->IsGtBias(),
4551 cond_type,
4552 cond_locations,
4553 fcond_reg);
4554 break;
4555 }
4556 }
4557
4558 if (true_src.IsConstant()) {
4559 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4560 }
4561 if (false_src.IsConstant()) {
4562 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4563 }
4564
4565 switch (dst_type) {
4566 default:
4567 if (Primitive::IsFloatingPointType(cond_type)) {
4568 __ Mfc1(cond_reg, fcond_reg);
4569 }
4570 if (true_src.IsConstant()) {
4571 if (cond_inverted) {
4572 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4573 } else {
4574 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4575 }
4576 } else if (false_src.IsConstant()) {
4577 if (cond_inverted) {
4578 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4579 } else {
4580 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4581 }
4582 } else {
4583 DCHECK_NE(cond_reg, AT);
4584 if (cond_inverted) {
4585 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4586 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4587 } else {
4588 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4589 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4590 }
4591 __ Or(dst.AsRegister<Register>(), AT, TMP);
4592 }
4593 break;
4594 case Primitive::kPrimLong: {
4595 if (Primitive::IsFloatingPointType(cond_type)) {
4596 __ Mfc1(cond_reg, fcond_reg);
4597 }
4598 Register dst_lo = dst.AsRegisterPairLow<Register>();
4599 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4600 if (true_src.IsConstant()) {
4601 Register src_lo = false_src.AsRegisterPairLow<Register>();
4602 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4603 if (cond_inverted) {
4604 __ Selnez(dst_lo, src_lo, cond_reg);
4605 __ Selnez(dst_hi, src_hi, cond_reg);
4606 } else {
4607 __ Seleqz(dst_lo, src_lo, cond_reg);
4608 __ Seleqz(dst_hi, src_hi, cond_reg);
4609 }
4610 } else {
4611 DCHECK(false_src.IsConstant());
4612 Register src_lo = true_src.AsRegisterPairLow<Register>();
4613 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4614 if (cond_inverted) {
4615 __ Seleqz(dst_lo, src_lo, cond_reg);
4616 __ Seleqz(dst_hi, src_hi, cond_reg);
4617 } else {
4618 __ Selnez(dst_lo, src_lo, cond_reg);
4619 __ Selnez(dst_hi, src_hi, cond_reg);
4620 }
4621 }
4622 break;
4623 }
4624 case Primitive::kPrimFloat: {
4625 if (!Primitive::IsFloatingPointType(cond_type)) {
4626 // sel*.fmt tests bit 0 of the condition register, account for that.
4627 __ Sltu(TMP, ZERO, cond_reg);
4628 __ Mtc1(TMP, fcond_reg);
4629 }
4630 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4631 if (true_src.IsConstant()) {
4632 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4633 if (cond_inverted) {
4634 __ SelnezS(dst_reg, src_reg, fcond_reg);
4635 } else {
4636 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4637 }
4638 } else if (false_src.IsConstant()) {
4639 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4640 if (cond_inverted) {
4641 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4642 } else {
4643 __ SelnezS(dst_reg, src_reg, fcond_reg);
4644 }
4645 } else {
4646 if (cond_inverted) {
4647 __ SelS(fcond_reg,
4648 true_src.AsFpuRegister<FRegister>(),
4649 false_src.AsFpuRegister<FRegister>());
4650 } else {
4651 __ SelS(fcond_reg,
4652 false_src.AsFpuRegister<FRegister>(),
4653 true_src.AsFpuRegister<FRegister>());
4654 }
4655 __ MovS(dst_reg, fcond_reg);
4656 }
4657 break;
4658 }
4659 case Primitive::kPrimDouble: {
4660 if (!Primitive::IsFloatingPointType(cond_type)) {
4661 // sel*.fmt tests bit 0 of the condition register, account for that.
4662 __ Sltu(TMP, ZERO, cond_reg);
4663 __ Mtc1(TMP, fcond_reg);
4664 }
4665 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4666 if (true_src.IsConstant()) {
4667 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4668 if (cond_inverted) {
4669 __ SelnezD(dst_reg, src_reg, fcond_reg);
4670 } else {
4671 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4672 }
4673 } else if (false_src.IsConstant()) {
4674 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4675 if (cond_inverted) {
4676 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4677 } else {
4678 __ SelnezD(dst_reg, src_reg, fcond_reg);
4679 }
4680 } else {
4681 if (cond_inverted) {
4682 __ SelD(fcond_reg,
4683 true_src.AsFpuRegister<FRegister>(),
4684 false_src.AsFpuRegister<FRegister>());
4685 } else {
4686 __ SelD(fcond_reg,
4687 false_src.AsFpuRegister<FRegister>(),
4688 true_src.AsFpuRegister<FRegister>());
4689 }
4690 __ MovD(dst_reg, fcond_reg);
4691 }
4692 break;
4693 }
4694 }
4695}
4696
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004697void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4698 LocationSummary* locations = new (GetGraph()->GetArena())
4699 LocationSummary(flag, LocationSummary::kNoCall);
4700 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004701}
4702
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004703void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4704 __ LoadFromOffset(kLoadWord,
4705 flag->GetLocations()->Out().AsRegister<Register>(),
4706 SP,
4707 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004708}
4709
David Brazdil74eb1b22015-12-14 11:44:01 +00004710void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004712 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004713}
4714
4715void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004716 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4717 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4718 if (is_r6) {
4719 GenConditionalMoveR6(select);
4720 } else {
4721 GenConditionalMoveR2(select);
4722 }
4723 } else {
4724 LocationSummary* locations = select->GetLocations();
4725 MipsLabel false_target;
4726 GenerateTestAndBranch(select,
4727 /* condition_input_index */ 2,
4728 /* true_target */ nullptr,
4729 &false_target);
4730 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4731 __ Bind(&false_target);
4732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004733}
4734
David Srbecky0cf44932015-12-09 14:09:59 +00004735void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4736 new (GetGraph()->GetArena()) LocationSummary(info);
4737}
4738
David Srbeckyd28f4a02016-03-14 17:14:24 +00004739void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4740 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004741}
4742
4743void CodeGeneratorMIPS::GenerateNop() {
4744 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004745}
4746
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004747void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4748 Primitive::Type field_type = field_info.GetFieldType();
4749 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4750 bool generate_volatile = field_info.IsVolatile() && is_wide;
4751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004752 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004753
4754 locations->SetInAt(0, Location::RequiresRegister());
4755 if (generate_volatile) {
4756 InvokeRuntimeCallingConvention calling_convention;
4757 // need A0 to hold base + offset
4758 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4759 if (field_type == Primitive::kPrimLong) {
4760 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4761 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004762 // Use Location::Any() to prevent situations when running out of available fp registers.
4763 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004764 // Need some temp core regs since FP results are returned in core registers
4765 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4766 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4767 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4768 }
4769 } else {
4770 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4771 locations->SetOut(Location::RequiresFpuRegister());
4772 } else {
4773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4774 }
4775 }
4776}
4777
4778void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4779 const FieldInfo& field_info,
4780 uint32_t dex_pc) {
4781 Primitive::Type type = field_info.GetFieldType();
4782 LocationSummary* locations = instruction->GetLocations();
4783 Register obj = locations->InAt(0).AsRegister<Register>();
4784 LoadOperandType load_type = kLoadUnsignedByte;
4785 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004786 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004787 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004788
4789 switch (type) {
4790 case Primitive::kPrimBoolean:
4791 load_type = kLoadUnsignedByte;
4792 break;
4793 case Primitive::kPrimByte:
4794 load_type = kLoadSignedByte;
4795 break;
4796 case Primitive::kPrimShort:
4797 load_type = kLoadSignedHalfword;
4798 break;
4799 case Primitive::kPrimChar:
4800 load_type = kLoadUnsignedHalfword;
4801 break;
4802 case Primitive::kPrimInt:
4803 case Primitive::kPrimFloat:
4804 case Primitive::kPrimNot:
4805 load_type = kLoadWord;
4806 break;
4807 case Primitive::kPrimLong:
4808 case Primitive::kPrimDouble:
4809 load_type = kLoadDoubleword;
4810 break;
4811 case Primitive::kPrimVoid:
4812 LOG(FATAL) << "Unreachable type " << type;
4813 UNREACHABLE();
4814 }
4815
4816 if (is_volatile && load_type == kLoadDoubleword) {
4817 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004818 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004819 // Do implicit Null check
4820 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4821 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004822 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004823 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4824 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004825 // FP results are returned in core registers. Need to move them.
4826 Location out = locations->Out();
4827 if (out.IsFpuRegister()) {
4828 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4829 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4830 out.AsFpuRegister<FRegister>());
4831 } else {
4832 DCHECK(out.IsDoubleStackSlot());
4833 __ StoreToOffset(kStoreWord,
4834 locations->GetTemp(1).AsRegister<Register>(),
4835 SP,
4836 out.GetStackIndex());
4837 __ StoreToOffset(kStoreWord,
4838 locations->GetTemp(2).AsRegister<Register>(),
4839 SP,
4840 out.GetStackIndex() + 4);
4841 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842 }
4843 } else {
4844 if (!Primitive::IsFloatingPointType(type)) {
4845 Register dst;
4846 if (type == Primitive::kPrimLong) {
4847 DCHECK(locations->Out().IsRegisterPair());
4848 dst = locations->Out().AsRegisterPairLow<Register>();
4849 } else {
4850 DCHECK(locations->Out().IsRegister());
4851 dst = locations->Out().AsRegister<Register>();
4852 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004853 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004854 } else {
4855 DCHECK(locations->Out().IsFpuRegister());
4856 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4857 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004858 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004859 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004860 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004861 }
4862 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004863 }
4864
4865 if (is_volatile) {
4866 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4867 }
4868}
4869
4870void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4871 Primitive::Type field_type = field_info.GetFieldType();
4872 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4873 bool generate_volatile = field_info.IsVolatile() && is_wide;
4874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004875 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004876
4877 locations->SetInAt(0, Location::RequiresRegister());
4878 if (generate_volatile) {
4879 InvokeRuntimeCallingConvention calling_convention;
4880 // need A0 to hold base + offset
4881 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4882 if (field_type == Primitive::kPrimLong) {
4883 locations->SetInAt(1, Location::RegisterPairLocation(
4884 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4885 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004886 // Use Location::Any() to prevent situations when running out of available fp registers.
4887 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004888 // Pass FP parameters in core registers.
4889 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4890 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4891 }
4892 } else {
4893 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004894 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004895 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004896 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004897 }
4898 }
4899}
4900
4901void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4902 const FieldInfo& field_info,
4903 uint32_t dex_pc) {
4904 Primitive::Type type = field_info.GetFieldType();
4905 LocationSummary* locations = instruction->GetLocations();
4906 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004907 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004908 StoreOperandType store_type = kStoreByte;
4909 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004910 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004911 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004912
4913 switch (type) {
4914 case Primitive::kPrimBoolean:
4915 case Primitive::kPrimByte:
4916 store_type = kStoreByte;
4917 break;
4918 case Primitive::kPrimShort:
4919 case Primitive::kPrimChar:
4920 store_type = kStoreHalfword;
4921 break;
4922 case Primitive::kPrimInt:
4923 case Primitive::kPrimFloat:
4924 case Primitive::kPrimNot:
4925 store_type = kStoreWord;
4926 break;
4927 case Primitive::kPrimLong:
4928 case Primitive::kPrimDouble:
4929 store_type = kStoreDoubleword;
4930 break;
4931 case Primitive::kPrimVoid:
4932 LOG(FATAL) << "Unreachable type " << type;
4933 UNREACHABLE();
4934 }
4935
4936 if (is_volatile) {
4937 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4938 }
4939
4940 if (is_volatile && store_type == kStoreDoubleword) {
4941 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004942 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004943 // Do implicit Null check.
4944 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4945 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4946 if (type == Primitive::kPrimDouble) {
4947 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004948 if (value_location.IsFpuRegister()) {
4949 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4950 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004951 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004952 value_location.AsFpuRegister<FRegister>());
4953 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004954 __ LoadFromOffset(kLoadWord,
4955 locations->GetTemp(1).AsRegister<Register>(),
4956 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004957 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004958 __ LoadFromOffset(kLoadWord,
4959 locations->GetTemp(2).AsRegister<Register>(),
4960 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004961 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004962 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004963 DCHECK(value_location.IsConstant());
4964 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4965 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004966 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4967 locations->GetTemp(1).AsRegister<Register>(),
4968 value);
4969 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004970 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004971 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4973 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004974 if (value_location.IsConstant()) {
4975 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4976 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4977 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004978 Register src;
4979 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004980 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004981 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004982 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004983 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004984 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004985 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004986 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004987 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004988 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004989 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004990 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004991 }
4992 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004993 }
4994
4995 // TODO: memory barriers?
4996 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004997 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004998 codegen_->MarkGCCard(obj, src);
4999 }
5000
5001 if (is_volatile) {
5002 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5003 }
5004}
5005
5006void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5007 HandleFieldGet(instruction, instruction->GetFieldInfo());
5008}
5009
5010void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5011 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5012}
5013
5014void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5015 HandleFieldSet(instruction, instruction->GetFieldInfo());
5016}
5017
5018void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5019 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5020}
5021
Alexey Frunze06a46c42016-07-19 15:00:40 -07005022void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5023 HInstruction* instruction ATTRIBUTE_UNUSED,
5024 Location root,
5025 Register obj,
5026 uint32_t offset) {
5027 Register root_reg = root.AsRegister<Register>();
5028 if (kEmitCompilerReadBarrier) {
5029 UNIMPLEMENTED(FATAL) << "for read barrier";
5030 } else {
5031 // Plain GC root load with no read barrier.
5032 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5033 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5034 // Note that GC roots are not affected by heap poisoning, thus we
5035 // do not have to unpoison `root_reg` here.
5036 }
5037}
5038
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005039void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5040 LocationSummary::CallKind call_kind =
5041 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5043 locations->SetInAt(0, Location::RequiresRegister());
5044 locations->SetInAt(1, Location::RequiresRegister());
5045 // The output does overlap inputs.
5046 // Note that TypeCheckSlowPathMIPS uses this register too.
5047 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5048}
5049
5050void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5051 LocationSummary* locations = instruction->GetLocations();
5052 Register obj = locations->InAt(0).AsRegister<Register>();
5053 Register cls = locations->InAt(1).AsRegister<Register>();
5054 Register out = locations->Out().AsRegister<Register>();
5055
5056 MipsLabel done;
5057
5058 // Return 0 if `obj` is null.
5059 // TODO: Avoid this check if we know `obj` is not null.
5060 __ Move(out, ZERO);
5061 __ Beqz(obj, &done);
5062
5063 // Compare the class of `obj` with `cls`.
5064 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5065 if (instruction->IsExactCheck()) {
5066 // Classes must be equal for the instanceof to succeed.
5067 __ Xor(out, out, cls);
5068 __ Sltiu(out, out, 1);
5069 } else {
5070 // If the classes are not equal, we go into a slow path.
5071 DCHECK(locations->OnlyCallsOnSlowPath());
5072 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5073 codegen_->AddSlowPath(slow_path);
5074 __ Bne(out, cls, slow_path->GetEntryLabel());
5075 __ LoadConst32(out, 1);
5076 __ Bind(slow_path->GetExitLabel());
5077 }
5078
5079 __ Bind(&done);
5080}
5081
5082void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5084 locations->SetOut(Location::ConstantLocation(constant));
5085}
5086
5087void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5088 // Will be generated at use site.
5089}
5090
5091void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5093 locations->SetOut(Location::ConstantLocation(constant));
5094}
5095
5096void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5097 // Will be generated at use site.
5098}
5099
5100void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5101 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5102 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5103}
5104
5105void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5106 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005107 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005108 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005109 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110}
5111
5112void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5113 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5114 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005115 Location receiver = invoke->GetLocations()->InAt(0);
5116 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005117 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005118
5119 // Set the hidden argument.
5120 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5121 invoke->GetDexMethodIndex());
5122
5123 // temp = object->GetClass();
5124 if (receiver.IsStackSlot()) {
5125 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5126 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5127 } else {
5128 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5129 }
5130 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005131 __ LoadFromOffset(kLoadWord, temp, temp,
5132 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5133 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005134 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005135 // temp = temp->GetImtEntryAt(method_offset);
5136 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5137 // T9 = temp->GetEntryPoint();
5138 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5139 // T9();
5140 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005141 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005142 DCHECK(!codegen_->IsLeafMethod());
5143 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5144}
5145
5146void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005147 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5148 if (intrinsic.TryDispatch(invoke)) {
5149 return;
5150 }
5151
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005152 HandleInvoke(invoke);
5153}
5154
5155void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005156 // Explicit clinit checks triggered by static invokes must have been pruned by
5157 // art::PrepareForRegisterAllocation.
5158 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005159
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005160 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5161 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5162 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5163
5164 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
5165 // R6 has PC-relative addressing.
5166 bool has_extra_input = !isR6 &&
5167 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5168 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
5169
5170 if (invoke->HasPcRelativeDexCache()) {
5171 // kDexCachePcRelative is mutually exclusive with
5172 // kDirectAddressWithFixup/kCallDirectWithFixup.
5173 CHECK(!has_extra_input);
5174 has_extra_input = true;
5175 }
5176
Chris Larsen701566a2015-10-27 15:29:13 -07005177 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5178 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005179 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5180 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5181 }
Chris Larsen701566a2015-10-27 15:29:13 -07005182 return;
5183 }
5184
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005185 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005186
5187 // Add the extra input register if either the dex cache array base register
5188 // or the PC-relative base register for accessing literals is needed.
5189 if (has_extra_input) {
5190 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5191 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005192}
5193
Chris Larsen701566a2015-10-27 15:29:13 -07005194static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005195 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005196 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5197 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005198 return true;
5199 }
5200 return false;
5201}
5202
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005203HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005204 HLoadString::LoadKind desired_string_load_kind) {
5205 if (kEmitCompilerReadBarrier) {
5206 UNIMPLEMENTED(FATAL) << "for read barrier";
5207 }
5208 // We disable PC-relative load when there is an irreducible loop, as the optimization
5209 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005210 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5211 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005212 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5213 bool fallback_load = has_irreducible_loops;
5214 switch (desired_string_load_kind) {
5215 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5216 DCHECK(!GetCompilerOptions().GetCompilePic());
5217 break;
5218 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5219 DCHECK(GetCompilerOptions().GetCompilePic());
5220 break;
5221 case HLoadString::LoadKind::kBootImageAddress:
5222 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005223 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005224 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005225 break;
5226 case HLoadString::LoadKind::kDexCacheViaMethod:
5227 fallback_load = false;
5228 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005229 case HLoadString::LoadKind::kJitTableAddress:
5230 DCHECK(Runtime::Current()->UseJitCompilation());
5231 // TODO: implement.
5232 fallback_load = true;
5233 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005234 }
5235 if (fallback_load) {
5236 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5237 }
5238 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005239}
5240
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005241HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5242 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005243 if (kEmitCompilerReadBarrier) {
5244 UNIMPLEMENTED(FATAL) << "for read barrier";
5245 }
5246 // We disable pc-relative load when there is an irreducible loop, as the optimization
5247 // is incompatible with it.
5248 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5249 bool fallback_load = has_irreducible_loops;
5250 switch (desired_class_load_kind) {
5251 case HLoadClass::LoadKind::kReferrersClass:
5252 fallback_load = false;
5253 break;
5254 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5255 DCHECK(!GetCompilerOptions().GetCompilePic());
5256 break;
5257 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5258 DCHECK(GetCompilerOptions().GetCompilePic());
5259 break;
5260 case HLoadClass::LoadKind::kBootImageAddress:
5261 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005262 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005263 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005264 fallback_load = true;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005265 break;
5266 case HLoadClass::LoadKind::kDexCachePcRelative:
5267 DCHECK(!Runtime::Current()->UseJitCompilation());
5268 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5269 // with irreducible loops.
5270 break;
5271 case HLoadClass::LoadKind::kDexCacheViaMethod:
5272 fallback_load = false;
5273 break;
5274 }
5275 if (fallback_load) {
5276 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5277 }
5278 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005279}
5280
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005281Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5282 Register temp) {
5283 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5284 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5285 if (!invoke->GetLocations()->Intrinsified()) {
5286 return location.AsRegister<Register>();
5287 }
5288 // For intrinsics we allow any location, so it may be on the stack.
5289 if (!location.IsRegister()) {
5290 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5291 return temp;
5292 }
5293 // For register locations, check if the register was saved. If so, get it from the stack.
5294 // Note: There is a chance that the register was saved but not overwritten, so we could
5295 // save one load. However, since this is just an intrinsic slow path we prefer this
5296 // simple and more robust approach rather that trying to determine if that's the case.
5297 SlowPathCode* slow_path = GetCurrentSlowPath();
5298 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5299 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5300 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5301 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5302 return temp;
5303 }
5304 return location.AsRegister<Register>();
5305}
5306
Vladimir Markodc151b22015-10-15 18:02:30 +01005307HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5308 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005309 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005310 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5311 // We disable PC-relative load when there is an irreducible loop, as the optimization
5312 // is incompatible with it.
5313 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5314 bool fallback_load = true;
5315 bool fallback_call = true;
5316 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005317 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5318 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005319 fallback_load = has_irreducible_loops;
5320 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005321 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005322 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005323 break;
5324 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005325 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005326 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005327 fallback_call = has_irreducible_loops;
5328 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005329 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005330 // TODO: Implement this type.
5331 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005332 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005333 fallback_call = false;
5334 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005335 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005336 if (fallback_load) {
5337 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5338 dispatch_info.method_load_data = 0;
5339 }
5340 if (fallback_call) {
5341 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5342 dispatch_info.direct_code_ptr = 0;
5343 }
5344 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005345}
5346
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005347void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5348 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005349 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005350 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5351 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5352 bool isR6 = isa_features_.IsR6();
5353 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5354 // R6 has PC-relative addressing.
5355 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5356 (!isR6 &&
5357 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5358 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5359 Register base_reg = has_extra_input
5360 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5361 : ZERO;
5362
5363 // For better instruction scheduling we load the direct code pointer before the method pointer.
5364 switch (code_ptr_location) {
5365 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5366 // T9 = invoke->GetDirectCodePtr();
5367 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5368 break;
5369 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5370 // T9 = code address from literal pool with link-time patch.
5371 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5372 break;
5373 default:
5374 break;
5375 }
5376
5377 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005378 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005379 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005380 uint32_t offset =
5381 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005382 __ LoadFromOffset(kLoadWord,
5383 temp.AsRegister<Register>(),
5384 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005385 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005386 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005387 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005388 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005389 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005390 break;
5391 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5392 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5393 break;
5394 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005395 __ LoadLiteral(temp.AsRegister<Register>(),
5396 base_reg,
5397 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5398 break;
5399 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5400 HMipsDexCacheArraysBase* base =
5401 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5402 int32_t offset =
5403 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5404 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5405 break;
5406 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005407 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005408 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005409 Register reg = temp.AsRegister<Register>();
5410 Register method_reg;
5411 if (current_method.IsRegister()) {
5412 method_reg = current_method.AsRegister<Register>();
5413 } else {
5414 // TODO: use the appropriate DCHECK() here if possible.
5415 // DCHECK(invoke->GetLocations()->Intrinsified());
5416 DCHECK(!current_method.IsValid());
5417 method_reg = reg;
5418 __ Lw(reg, SP, kCurrentMethodStackOffset);
5419 }
5420
5421 // temp = temp->dex_cache_resolved_methods_;
5422 __ LoadFromOffset(kLoadWord,
5423 reg,
5424 method_reg,
5425 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005426 // temp = temp[index_in_cache];
5427 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5428 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 __ LoadFromOffset(kLoadWord,
5430 reg,
5431 reg,
5432 CodeGenerator::GetCachePointerOffset(index_in_cache));
5433 break;
5434 }
5435 }
5436
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005437 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005438 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005439 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005440 break;
5441 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005442 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5443 // T9 prepared above for better instruction scheduling.
5444 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005445 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005446 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005447 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005448 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005449 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005450 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5451 LOG(FATAL) << "Unsupported";
5452 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005453 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5454 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005455 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005456 T9,
5457 callee_method.AsRegister<Register>(),
5458 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005459 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005460 // T9()
5461 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005462 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005463 break;
5464 }
5465 DCHECK(!IsLeafMethod());
5466}
5467
5468void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005469 // Explicit clinit checks triggered by static invokes must have been pruned by
5470 // art::PrepareForRegisterAllocation.
5471 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005472
5473 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5474 return;
5475 }
5476
5477 LocationSummary* locations = invoke->GetLocations();
5478 codegen_->GenerateStaticOrDirectCall(invoke,
5479 locations->HasTemps()
5480 ? locations->GetTemp(0)
5481 : Location::NoLocation());
5482 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5483}
5484
Chris Larsen3acee732015-11-18 13:31:08 -08005485void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005486 // Use the calling convention instead of the location of the receiver, as
5487 // intrinsics may have put the receiver in a different register. In the intrinsics
5488 // slow path, the arguments have been moved to the right place, so here we are
5489 // guaranteed that the receiver is the first register of the calling convention.
5490 InvokeDexCallingConvention calling_convention;
5491 Register receiver = calling_convention.GetRegisterAt(0);
5492
Chris Larsen3acee732015-11-18 13:31:08 -08005493 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005494 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5495 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5496 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005497 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005498
5499 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005500 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005501 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005502 // temp = temp->GetMethodAt(method_offset);
5503 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5504 // T9 = temp->GetEntryPoint();
5505 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5506 // T9();
5507 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005508 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005509}
5510
5511void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5512 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5513 return;
5514 }
5515
5516 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005517 DCHECK(!codegen_->IsLeafMethod());
5518 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5519}
5520
5521void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005522 if (cls->NeedsAccessCheck()) {
5523 InvokeRuntimeCallingConvention calling_convention;
5524 CodeGenerator::CreateLoadClassLocationSummary(
5525 cls,
5526 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5527 Location::RegisterLocation(V0),
5528 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5529 return;
5530 }
5531
5532 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5533 ? LocationSummary::kCallOnSlowPath
5534 : LocationSummary::kNoCall;
5535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5536 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5537 switch (load_kind) {
5538 // We need an extra register for PC-relative literals on R2.
5539 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5540 case HLoadClass::LoadKind::kBootImageAddress:
5541 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5542 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5543 break;
5544 }
5545 FALLTHROUGH_INTENDED;
5546 // We need an extra register for PC-relative dex cache accesses.
5547 case HLoadClass::LoadKind::kDexCachePcRelative:
5548 case HLoadClass::LoadKind::kReferrersClass:
5549 case HLoadClass::LoadKind::kDexCacheViaMethod:
5550 locations->SetInAt(0, Location::RequiresRegister());
5551 break;
5552 default:
5553 break;
5554 }
5555 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005556}
5557
5558void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5559 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005560 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005561 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005562 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005563 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005564 return;
5565 }
5566
Alexey Frunze06a46c42016-07-19 15:00:40 -07005567 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5568 Location out_loc = locations->Out();
5569 Register out = out_loc.AsRegister<Register>();
5570 Register base_or_current_method_reg;
5571 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5572 switch (load_kind) {
5573 // We need an extra register for PC-relative literals on R2.
5574 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5575 case HLoadClass::LoadKind::kBootImageAddress:
5576 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5577 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5578 break;
5579 // We need an extra register for PC-relative dex cache accesses.
5580 case HLoadClass::LoadKind::kDexCachePcRelative:
5581 case HLoadClass::LoadKind::kReferrersClass:
5582 case HLoadClass::LoadKind::kDexCacheViaMethod:
5583 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5584 break;
5585 default:
5586 base_or_current_method_reg = ZERO;
5587 break;
5588 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005589
Alexey Frunze06a46c42016-07-19 15:00:40 -07005590 bool generate_null_check = false;
5591 switch (load_kind) {
5592 case HLoadClass::LoadKind::kReferrersClass: {
5593 DCHECK(!cls->CanCallRuntime());
5594 DCHECK(!cls->MustGenerateClinitCheck());
5595 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5596 GenerateGcRootFieldLoad(cls,
5597 out_loc,
5598 base_or_current_method_reg,
5599 ArtMethod::DeclaringClassOffset().Int32Value());
5600 break;
5601 }
5602 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5603 DCHECK(!kEmitCompilerReadBarrier);
5604 __ LoadLiteral(out,
5605 base_or_current_method_reg,
5606 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5607 cls->GetTypeIndex()));
5608 break;
5609 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5610 DCHECK(!kEmitCompilerReadBarrier);
5611 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5612 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005613 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005614 break;
5615 }
5616 case HLoadClass::LoadKind::kBootImageAddress: {
5617 DCHECK(!kEmitCompilerReadBarrier);
5618 DCHECK_NE(cls->GetAddress(), 0u);
5619 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5620 __ LoadLiteral(out,
5621 base_or_current_method_reg,
5622 codegen_->DeduplicateBootImageAddressLiteral(address));
5623 break;
5624 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005625 case HLoadClass::LoadKind::kJitTableAddress: {
5626 LOG(FATAL) << "Unimplemented";
Alexey Frunze06a46c42016-07-19 15:00:40 -07005627 break;
5628 }
5629 case HLoadClass::LoadKind::kDexCachePcRelative: {
5630 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5631 int32_t offset =
5632 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5633 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5634 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5635 generate_null_check = !cls->IsInDexCache();
5636 break;
5637 }
5638 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5639 // /* GcRoot<mirror::Class>[] */ out =
5640 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5641 __ LoadFromOffset(kLoadWord,
5642 out,
5643 base_or_current_method_reg,
5644 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5645 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005646 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005647 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5648 generate_null_check = !cls->IsInDexCache();
5649 }
5650 }
5651
5652 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5653 DCHECK(cls->CanCallRuntime());
5654 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5655 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5656 codegen_->AddSlowPath(slow_path);
5657 if (generate_null_check) {
5658 __ Beqz(out, slow_path->GetEntryLabel());
5659 }
5660 if (cls->MustGenerateClinitCheck()) {
5661 GenerateClassInitializationCheck(slow_path, out);
5662 } else {
5663 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005664 }
5665 }
5666}
5667
5668static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005669 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005670}
5671
5672void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5673 LocationSummary* locations =
5674 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5675 locations->SetOut(Location::RequiresRegister());
5676}
5677
5678void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5679 Register out = load->GetLocations()->Out().AsRegister<Register>();
5680 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5681}
5682
5683void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5684 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5685}
5686
5687void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5688 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5689}
5690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005691void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005692 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005693 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5694 ? LocationSummary::kCallOnMainOnly
5695 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005696 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005698 HLoadString::LoadKind load_kind = load->GetLoadKind();
5699 switch (load_kind) {
5700 // We need an extra register for PC-relative literals on R2.
5701 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5702 case HLoadString::LoadKind::kBootImageAddress:
5703 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005704 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005705 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5706 break;
5707 }
5708 FALLTHROUGH_INTENDED;
5709 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005710 case HLoadString::LoadKind::kDexCacheViaMethod:
5711 locations->SetInAt(0, Location::RequiresRegister());
5712 break;
5713 default:
5714 break;
5715 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005716 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5717 InvokeRuntimeCallingConvention calling_convention;
5718 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5719 } else {
5720 locations->SetOut(Location::RequiresRegister());
5721 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005722}
5723
5724void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005725 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005726 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005727 Location out_loc = locations->Out();
5728 Register out = out_loc.AsRegister<Register>();
5729 Register base_or_current_method_reg;
5730 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5731 switch (load_kind) {
5732 // We need an extra register for PC-relative literals on R2.
5733 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5734 case HLoadString::LoadKind::kBootImageAddress:
5735 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005736 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005737 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5738 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005739 default:
5740 base_or_current_method_reg = ZERO;
5741 break;
5742 }
5743
5744 switch (load_kind) {
5745 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5746 DCHECK(!kEmitCompilerReadBarrier);
5747 __ LoadLiteral(out,
5748 base_or_current_method_reg,
5749 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5750 load->GetStringIndex()));
5751 return; // No dex cache slow path.
5752 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5753 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005754 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005755 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005756 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005757 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005758 return; // No dex cache slow path.
5759 }
5760 case HLoadString::LoadKind::kBootImageAddress: {
5761 DCHECK(!kEmitCompilerReadBarrier);
5762 DCHECK_NE(load->GetAddress(), 0u);
5763 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5764 __ LoadLiteral(out,
5765 base_or_current_method_reg,
5766 codegen_->DeduplicateBootImageAddressLiteral(address));
5767 return; // No dex cache slow path.
5768 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005769 case HLoadString::LoadKind::kBssEntry: {
5770 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5771 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005772 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005773 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5774 __ LoadFromOffset(kLoadWord, out, out, 0);
5775 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5776 codegen_->AddSlowPath(slow_path);
5777 __ Beqz(out, slow_path->GetEntryLabel());
5778 __ Bind(slow_path->GetExitLabel());
5779 return;
5780 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005781 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005782 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005783 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005784
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005785 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005786 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5787 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005788 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005789 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5790 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005791}
5792
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005793void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5794 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5795 locations->SetOut(Location::ConstantLocation(constant));
5796}
5797
5798void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5799 // Will be generated at use site.
5800}
5801
5802void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5803 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805 InvokeRuntimeCallingConvention calling_convention;
5806 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5807}
5808
5809void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5810 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005811 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005812 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5813 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005814 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005815 }
5816 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5817}
5818
5819void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5820 LocationSummary* locations =
5821 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5822 switch (mul->GetResultType()) {
5823 case Primitive::kPrimInt:
5824 case Primitive::kPrimLong:
5825 locations->SetInAt(0, Location::RequiresRegister());
5826 locations->SetInAt(1, Location::RequiresRegister());
5827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5828 break;
5829
5830 case Primitive::kPrimFloat:
5831 case Primitive::kPrimDouble:
5832 locations->SetInAt(0, Location::RequiresFpuRegister());
5833 locations->SetInAt(1, Location::RequiresFpuRegister());
5834 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5835 break;
5836
5837 default:
5838 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5839 }
5840}
5841
5842void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5843 Primitive::Type type = instruction->GetType();
5844 LocationSummary* locations = instruction->GetLocations();
5845 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5846
5847 switch (type) {
5848 case Primitive::kPrimInt: {
5849 Register dst = locations->Out().AsRegister<Register>();
5850 Register lhs = locations->InAt(0).AsRegister<Register>();
5851 Register rhs = locations->InAt(1).AsRegister<Register>();
5852
5853 if (isR6) {
5854 __ MulR6(dst, lhs, rhs);
5855 } else {
5856 __ MulR2(dst, lhs, rhs);
5857 }
5858 break;
5859 }
5860 case Primitive::kPrimLong: {
5861 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5862 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5863 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5864 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5865 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5866 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5867
5868 // Extra checks to protect caused by the existance of A1_A2.
5869 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5870 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5871 DCHECK_NE(dst_high, lhs_low);
5872 DCHECK_NE(dst_high, rhs_low);
5873
5874 // A_B * C_D
5875 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5876 // dst_lo: [ low(B*D) ]
5877 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5878
5879 if (isR6) {
5880 __ MulR6(TMP, lhs_high, rhs_low);
5881 __ MulR6(dst_high, lhs_low, rhs_high);
5882 __ Addu(dst_high, dst_high, TMP);
5883 __ MuhuR6(TMP, lhs_low, rhs_low);
5884 __ Addu(dst_high, dst_high, TMP);
5885 __ MulR6(dst_low, lhs_low, rhs_low);
5886 } else {
5887 __ MulR2(TMP, lhs_high, rhs_low);
5888 __ MulR2(dst_high, lhs_low, rhs_high);
5889 __ Addu(dst_high, dst_high, TMP);
5890 __ MultuR2(lhs_low, rhs_low);
5891 __ Mfhi(TMP);
5892 __ Addu(dst_high, dst_high, TMP);
5893 __ Mflo(dst_low);
5894 }
5895 break;
5896 }
5897 case Primitive::kPrimFloat:
5898 case Primitive::kPrimDouble: {
5899 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5900 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5901 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5902 if (type == Primitive::kPrimFloat) {
5903 __ MulS(dst, lhs, rhs);
5904 } else {
5905 __ MulD(dst, lhs, rhs);
5906 }
5907 break;
5908 }
5909 default:
5910 LOG(FATAL) << "Unexpected mul type " << type;
5911 }
5912}
5913
5914void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5915 LocationSummary* locations =
5916 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5917 switch (neg->GetResultType()) {
5918 case Primitive::kPrimInt:
5919 case Primitive::kPrimLong:
5920 locations->SetInAt(0, Location::RequiresRegister());
5921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5922 break;
5923
5924 case Primitive::kPrimFloat:
5925 case Primitive::kPrimDouble:
5926 locations->SetInAt(0, Location::RequiresFpuRegister());
5927 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5928 break;
5929
5930 default:
5931 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5932 }
5933}
5934
5935void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5936 Primitive::Type type = instruction->GetType();
5937 LocationSummary* locations = instruction->GetLocations();
5938
5939 switch (type) {
5940 case Primitive::kPrimInt: {
5941 Register dst = locations->Out().AsRegister<Register>();
5942 Register src = locations->InAt(0).AsRegister<Register>();
5943 __ Subu(dst, ZERO, src);
5944 break;
5945 }
5946 case Primitive::kPrimLong: {
5947 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5948 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5949 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5950 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5951 __ Subu(dst_low, ZERO, src_low);
5952 __ Sltu(TMP, ZERO, dst_low);
5953 __ Subu(dst_high, ZERO, src_high);
5954 __ Subu(dst_high, dst_high, TMP);
5955 break;
5956 }
5957 case Primitive::kPrimFloat:
5958 case Primitive::kPrimDouble: {
5959 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5960 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5961 if (type == Primitive::kPrimFloat) {
5962 __ NegS(dst, src);
5963 } else {
5964 __ NegD(dst, src);
5965 }
5966 break;
5967 }
5968 default:
5969 LOG(FATAL) << "Unexpected neg type " << type;
5970 }
5971}
5972
5973void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5974 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005975 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976 InvokeRuntimeCallingConvention calling_convention;
5977 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5978 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5979 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5980 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5981}
5982
5983void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5984 InvokeRuntimeCallingConvention calling_convention;
5985 Register current_method_register = calling_convention.GetRegisterAt(2);
5986 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5987 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005988 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005989 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005990 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5991 void*, uint32_t, int32_t, ArtMethod*>();
5992}
5993
5994void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5995 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005997 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005998 if (instruction->IsStringAlloc()) {
5999 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6000 } else {
6001 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6002 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6003 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006004 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6005}
6006
6007void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00006008 if (instruction->IsStringAlloc()) {
6009 // String is allocated through StringFactory. Call NewEmptyString entry point.
6010 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006011 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006012 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6013 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6014 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006015 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006016 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6017 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006018 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006019 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6020 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006021}
6022
6023void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6025 locations->SetInAt(0, Location::RequiresRegister());
6026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6027}
6028
6029void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6030 Primitive::Type type = instruction->GetType();
6031 LocationSummary* locations = instruction->GetLocations();
6032
6033 switch (type) {
6034 case Primitive::kPrimInt: {
6035 Register dst = locations->Out().AsRegister<Register>();
6036 Register src = locations->InAt(0).AsRegister<Register>();
6037 __ Nor(dst, src, ZERO);
6038 break;
6039 }
6040
6041 case Primitive::kPrimLong: {
6042 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6043 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6044 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6045 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6046 __ Nor(dst_high, src_high, ZERO);
6047 __ Nor(dst_low, src_low, ZERO);
6048 break;
6049 }
6050
6051 default:
6052 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6053 }
6054}
6055
6056void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6058 locations->SetInAt(0, Location::RequiresRegister());
6059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6060}
6061
6062void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6063 LocationSummary* locations = instruction->GetLocations();
6064 __ Xori(locations->Out().AsRegister<Register>(),
6065 locations->InAt(0).AsRegister<Register>(),
6066 1);
6067}
6068
6069void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006070 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6071 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006072}
6073
Calin Juravle2ae48182016-03-16 14:05:09 +00006074void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6075 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006076 return;
6077 }
6078 Location obj = instruction->GetLocations()->InAt(0);
6079
6080 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006081 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006082}
6083
Calin Juravle2ae48182016-03-16 14:05:09 +00006084void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006085 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006086 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006087
6088 Location obj = instruction->GetLocations()->InAt(0);
6089
6090 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6091}
6092
6093void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006094 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006095}
6096
6097void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6098 HandleBinaryOp(instruction);
6099}
6100
6101void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6102 HandleBinaryOp(instruction);
6103}
6104
6105void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6106 LOG(FATAL) << "Unreachable";
6107}
6108
6109void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6110 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6111}
6112
6113void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6115 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6116 if (location.IsStackSlot()) {
6117 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6118 } else if (location.IsDoubleStackSlot()) {
6119 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6120 }
6121 locations->SetOut(location);
6122}
6123
6124void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6125 ATTRIBUTE_UNUSED) {
6126 // Nothing to do, the parameter is already at its location.
6127}
6128
6129void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6130 LocationSummary* locations =
6131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6132 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6133}
6134
6135void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6136 ATTRIBUTE_UNUSED) {
6137 // Nothing to do, the method is already at its location.
6138}
6139
6140void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006142 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006143 locations->SetInAt(i, Location::Any());
6144 }
6145 locations->SetOut(Location::Any());
6146}
6147
6148void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6149 LOG(FATAL) << "Unreachable";
6150}
6151
6152void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6153 Primitive::Type type = rem->GetResultType();
6154 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006155 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006156 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6157
6158 switch (type) {
6159 case Primitive::kPrimInt:
6160 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006161 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6163 break;
6164
6165 case Primitive::kPrimLong: {
6166 InvokeRuntimeCallingConvention calling_convention;
6167 locations->SetInAt(0, Location::RegisterPairLocation(
6168 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6169 locations->SetInAt(1, Location::RegisterPairLocation(
6170 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6171 locations->SetOut(calling_convention.GetReturnLocation(type));
6172 break;
6173 }
6174
6175 case Primitive::kPrimFloat:
6176 case Primitive::kPrimDouble: {
6177 InvokeRuntimeCallingConvention calling_convention;
6178 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6179 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6180 locations->SetOut(calling_convention.GetReturnLocation(type));
6181 break;
6182 }
6183
6184 default:
6185 LOG(FATAL) << "Unexpected rem type " << type;
6186 }
6187}
6188
6189void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6190 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006191
6192 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006193 case Primitive::kPrimInt:
6194 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006195 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006196 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006197 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006198 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6199 break;
6200 }
6201 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006202 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006203 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006204 break;
6205 }
6206 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006207 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006208 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006209 break;
6210 }
6211 default:
6212 LOG(FATAL) << "Unexpected rem type " << type;
6213 }
6214}
6215
6216void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6217 memory_barrier->SetLocations(nullptr);
6218}
6219
6220void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6221 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6222}
6223
6224void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6226 Primitive::Type return_type = ret->InputAt(0)->GetType();
6227 locations->SetInAt(0, MipsReturnLocation(return_type));
6228}
6229
6230void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6231 codegen_->GenerateFrameExit();
6232}
6233
6234void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6235 ret->SetLocations(nullptr);
6236}
6237
6238void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6239 codegen_->GenerateFrameExit();
6240}
6241
Alexey Frunze92d90602015-12-18 18:16:36 -08006242void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6243 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006244}
6245
Alexey Frunze92d90602015-12-18 18:16:36 -08006246void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6247 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006248}
6249
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6251 HandleShift(shl);
6252}
6253
6254void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6255 HandleShift(shl);
6256}
6257
6258void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6259 HandleShift(shr);
6260}
6261
6262void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6263 HandleShift(shr);
6264}
6265
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6267 HandleBinaryOp(instruction);
6268}
6269
6270void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6271 HandleBinaryOp(instruction);
6272}
6273
6274void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6275 HandleFieldGet(instruction, instruction->GetFieldInfo());
6276}
6277
6278void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6279 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6280}
6281
6282void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6283 HandleFieldSet(instruction, instruction->GetFieldInfo());
6284}
6285
6286void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6287 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6288}
6289
6290void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6291 HUnresolvedInstanceFieldGet* instruction) {
6292 FieldAccessCallingConventionMIPS calling_convention;
6293 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6294 instruction->GetFieldType(),
6295 calling_convention);
6296}
6297
6298void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6299 HUnresolvedInstanceFieldGet* instruction) {
6300 FieldAccessCallingConventionMIPS calling_convention;
6301 codegen_->GenerateUnresolvedFieldAccess(instruction,
6302 instruction->GetFieldType(),
6303 instruction->GetFieldIndex(),
6304 instruction->GetDexPc(),
6305 calling_convention);
6306}
6307
6308void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6309 HUnresolvedInstanceFieldSet* instruction) {
6310 FieldAccessCallingConventionMIPS calling_convention;
6311 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6312 instruction->GetFieldType(),
6313 calling_convention);
6314}
6315
6316void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6317 HUnresolvedInstanceFieldSet* instruction) {
6318 FieldAccessCallingConventionMIPS calling_convention;
6319 codegen_->GenerateUnresolvedFieldAccess(instruction,
6320 instruction->GetFieldType(),
6321 instruction->GetFieldIndex(),
6322 instruction->GetDexPc(),
6323 calling_convention);
6324}
6325
6326void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6327 HUnresolvedStaticFieldGet* instruction) {
6328 FieldAccessCallingConventionMIPS calling_convention;
6329 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6330 instruction->GetFieldType(),
6331 calling_convention);
6332}
6333
6334void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6335 HUnresolvedStaticFieldGet* instruction) {
6336 FieldAccessCallingConventionMIPS calling_convention;
6337 codegen_->GenerateUnresolvedFieldAccess(instruction,
6338 instruction->GetFieldType(),
6339 instruction->GetFieldIndex(),
6340 instruction->GetDexPc(),
6341 calling_convention);
6342}
6343
6344void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6345 HUnresolvedStaticFieldSet* instruction) {
6346 FieldAccessCallingConventionMIPS calling_convention;
6347 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6348 instruction->GetFieldType(),
6349 calling_convention);
6350}
6351
6352void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6353 HUnresolvedStaticFieldSet* instruction) {
6354 FieldAccessCallingConventionMIPS calling_convention;
6355 codegen_->GenerateUnresolvedFieldAccess(instruction,
6356 instruction->GetFieldType(),
6357 instruction->GetFieldIndex(),
6358 instruction->GetDexPc(),
6359 calling_convention);
6360}
6361
6362void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006363 LocationSummary* locations =
6364 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006365 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006366}
6367
6368void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6369 HBasicBlock* block = instruction->GetBlock();
6370 if (block->GetLoopInformation() != nullptr) {
6371 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6372 // The back edge will generate the suspend check.
6373 return;
6374 }
6375 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6376 // The goto will generate the suspend check.
6377 return;
6378 }
6379 GenerateSuspendCheck(instruction, nullptr);
6380}
6381
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006382void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6383 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 InvokeRuntimeCallingConvention calling_convention;
6386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6387}
6388
6389void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006390 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006391 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6392}
6393
6394void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6395 Primitive::Type input_type = conversion->GetInputType();
6396 Primitive::Type result_type = conversion->GetResultType();
6397 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006398 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006399
6400 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6401 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6402 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6403 }
6404
6405 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006406 if (!isR6 &&
6407 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6408 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006409 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006410 }
6411
6412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6413
6414 if (call_kind == LocationSummary::kNoCall) {
6415 if (Primitive::IsFloatingPointType(input_type)) {
6416 locations->SetInAt(0, Location::RequiresFpuRegister());
6417 } else {
6418 locations->SetInAt(0, Location::RequiresRegister());
6419 }
6420
6421 if (Primitive::IsFloatingPointType(result_type)) {
6422 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6423 } else {
6424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6425 }
6426 } else {
6427 InvokeRuntimeCallingConvention calling_convention;
6428
6429 if (Primitive::IsFloatingPointType(input_type)) {
6430 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6431 } else {
6432 DCHECK_EQ(input_type, Primitive::kPrimLong);
6433 locations->SetInAt(0, Location::RegisterPairLocation(
6434 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6435 }
6436
6437 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6438 }
6439}
6440
6441void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6442 LocationSummary* locations = conversion->GetLocations();
6443 Primitive::Type result_type = conversion->GetResultType();
6444 Primitive::Type input_type = conversion->GetInputType();
6445 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006446 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006447
6448 DCHECK_NE(input_type, result_type);
6449
6450 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6451 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6452 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6453 Register src = locations->InAt(0).AsRegister<Register>();
6454
Alexey Frunzea871ef12016-06-27 15:20:11 -07006455 if (dst_low != src) {
6456 __ Move(dst_low, src);
6457 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006458 __ Sra(dst_high, src, 31);
6459 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6460 Register dst = locations->Out().AsRegister<Register>();
6461 Register src = (input_type == Primitive::kPrimLong)
6462 ? locations->InAt(0).AsRegisterPairLow<Register>()
6463 : locations->InAt(0).AsRegister<Register>();
6464
6465 switch (result_type) {
6466 case Primitive::kPrimChar:
6467 __ Andi(dst, src, 0xFFFF);
6468 break;
6469 case Primitive::kPrimByte:
6470 if (has_sign_extension) {
6471 __ Seb(dst, src);
6472 } else {
6473 __ Sll(dst, src, 24);
6474 __ Sra(dst, dst, 24);
6475 }
6476 break;
6477 case Primitive::kPrimShort:
6478 if (has_sign_extension) {
6479 __ Seh(dst, src);
6480 } else {
6481 __ Sll(dst, src, 16);
6482 __ Sra(dst, dst, 16);
6483 }
6484 break;
6485 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006486 if (dst != src) {
6487 __ Move(dst, src);
6488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006489 break;
6490
6491 default:
6492 LOG(FATAL) << "Unexpected type conversion from " << input_type
6493 << " to " << result_type;
6494 }
6495 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006496 if (input_type == Primitive::kPrimLong) {
6497 if (isR6) {
6498 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6499 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6500 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6501 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6502 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6503 __ Mtc1(src_low, FTMP);
6504 __ Mthc1(src_high, FTMP);
6505 if (result_type == Primitive::kPrimFloat) {
6506 __ Cvtsl(dst, FTMP);
6507 } else {
6508 __ Cvtdl(dst, FTMP);
6509 }
6510 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006511 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6512 : kQuickL2d;
6513 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006514 if (result_type == Primitive::kPrimFloat) {
6515 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6516 } else {
6517 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6518 }
6519 }
6520 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006521 Register src = locations->InAt(0).AsRegister<Register>();
6522 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6523 __ Mtc1(src, FTMP);
6524 if (result_type == Primitive::kPrimFloat) {
6525 __ Cvtsw(dst, FTMP);
6526 } else {
6527 __ Cvtdw(dst, FTMP);
6528 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006529 }
6530 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6531 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006532 if (result_type == Primitive::kPrimLong) {
6533 if (isR6) {
6534 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6535 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6536 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6537 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6538 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6539 MipsLabel truncate;
6540 MipsLabel done;
6541
6542 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6543 // value when the input is either a NaN or is outside of the range of the output type
6544 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6545 // the same result.
6546 //
6547 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6548 // value of the output type if the input is outside of the range after the truncation or
6549 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6550 // results. This matches the desired float/double-to-int/long conversion exactly.
6551 //
6552 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6553 //
6554 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6555 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6556 // even though it must be NAN2008=1 on R6.
6557 //
6558 // The code takes care of the different behaviors by first comparing the input to the
6559 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6560 // If the input is greater than or equal to the minimum, it procedes to the truncate
6561 // instruction, which will handle such an input the same way irrespective of NAN2008.
6562 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6563 // in order to return either zero or the minimum value.
6564 //
6565 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6566 // truncate instruction for MIPS64R6.
6567 if (input_type == Primitive::kPrimFloat) {
6568 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6569 __ LoadConst32(TMP, min_val);
6570 __ Mtc1(TMP, FTMP);
6571 __ CmpLeS(FTMP, FTMP, src);
6572 } else {
6573 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6574 __ LoadConst32(TMP, High32Bits(min_val));
6575 __ Mtc1(ZERO, FTMP);
6576 __ Mthc1(TMP, FTMP);
6577 __ CmpLeD(FTMP, FTMP, src);
6578 }
6579
6580 __ Bc1nez(FTMP, &truncate);
6581
6582 if (input_type == Primitive::kPrimFloat) {
6583 __ CmpEqS(FTMP, src, src);
6584 } else {
6585 __ CmpEqD(FTMP, src, src);
6586 }
6587 __ Move(dst_low, ZERO);
6588 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6589 __ Mfc1(TMP, FTMP);
6590 __ And(dst_high, dst_high, TMP);
6591
6592 __ B(&done);
6593
6594 __ Bind(&truncate);
6595
6596 if (input_type == Primitive::kPrimFloat) {
6597 __ TruncLS(FTMP, src);
6598 } else {
6599 __ TruncLD(FTMP, src);
6600 }
6601 __ Mfc1(dst_low, FTMP);
6602 __ Mfhc1(dst_high, FTMP);
6603
6604 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006606 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6607 : kQuickD2l;
6608 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006609 if (input_type == Primitive::kPrimFloat) {
6610 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6611 } else {
6612 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6613 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006614 }
6615 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006616 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6617 Register dst = locations->Out().AsRegister<Register>();
6618 MipsLabel truncate;
6619 MipsLabel done;
6620
6621 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6622 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6623 // even though it must be NAN2008=1 on R6.
6624 //
6625 // For details see the large comment above for the truncation of float/double to long on R6.
6626 //
6627 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6628 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006629 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006630 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6631 __ LoadConst32(TMP, min_val);
6632 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006633 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006634 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6635 __ LoadConst32(TMP, High32Bits(min_val));
6636 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006637 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006639
6640 if (isR6) {
6641 if (input_type == Primitive::kPrimFloat) {
6642 __ CmpLeS(FTMP, FTMP, src);
6643 } else {
6644 __ CmpLeD(FTMP, FTMP, src);
6645 }
6646 __ Bc1nez(FTMP, &truncate);
6647
6648 if (input_type == Primitive::kPrimFloat) {
6649 __ CmpEqS(FTMP, src, src);
6650 } else {
6651 __ CmpEqD(FTMP, src, src);
6652 }
6653 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6654 __ Mfc1(TMP, FTMP);
6655 __ And(dst, dst, TMP);
6656 } else {
6657 if (input_type == Primitive::kPrimFloat) {
6658 __ ColeS(0, FTMP, src);
6659 } else {
6660 __ ColeD(0, FTMP, src);
6661 }
6662 __ Bc1t(0, &truncate);
6663
6664 if (input_type == Primitive::kPrimFloat) {
6665 __ CeqS(0, src, src);
6666 } else {
6667 __ CeqD(0, src, src);
6668 }
6669 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6670 __ Movf(dst, ZERO, 0);
6671 }
6672
6673 __ B(&done);
6674
6675 __ Bind(&truncate);
6676
6677 if (input_type == Primitive::kPrimFloat) {
6678 __ TruncWS(FTMP, src);
6679 } else {
6680 __ TruncWD(FTMP, src);
6681 }
6682 __ Mfc1(dst, FTMP);
6683
6684 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006685 }
6686 } else if (Primitive::IsFloatingPointType(result_type) &&
6687 Primitive::IsFloatingPointType(input_type)) {
6688 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6689 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6690 if (result_type == Primitive::kPrimFloat) {
6691 __ Cvtsd(dst, src);
6692 } else {
6693 __ Cvtds(dst, src);
6694 }
6695 } else {
6696 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6697 << " to " << result_type;
6698 }
6699}
6700
6701void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6702 HandleShift(ushr);
6703}
6704
6705void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6706 HandleShift(ushr);
6707}
6708
6709void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6710 HandleBinaryOp(instruction);
6711}
6712
6713void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6714 HandleBinaryOp(instruction);
6715}
6716
6717void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6718 // Nothing to do, this should be removed during prepare for register allocator.
6719 LOG(FATAL) << "Unreachable";
6720}
6721
6722void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6723 // Nothing to do, this should be removed during prepare for register allocator.
6724 LOG(FATAL) << "Unreachable";
6725}
6726
6727void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006728 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006729}
6730
6731void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006732 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006733}
6734
6735void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006736 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737}
6738
6739void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006740 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006741}
6742
6743void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006744 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006745}
6746
6747void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006748 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006749}
6750
6751void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006752 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006753}
6754
6755void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006756 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006757}
6758
6759void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006760 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006761}
6762
6763void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006764 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765}
6766
6767void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006768 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006769}
6770
6771void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006772 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006773}
6774
6775void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006776 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006777}
6778
6779void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006780 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006781}
6782
6783void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006784 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006785}
6786
6787void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006788 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006789}
6790
6791void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006792 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006793}
6794
6795void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006796 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797}
6798
6799void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006800 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006801}
6802
6803void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006804 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006805}
6806
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006807void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6808 LocationSummary* locations =
6809 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6810 locations->SetInAt(0, Location::RequiresRegister());
6811}
6812
Alexey Frunze96b66822016-09-10 02:32:44 -07006813void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6814 int32_t lower_bound,
6815 uint32_t num_entries,
6816 HBasicBlock* switch_block,
6817 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006818 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006819 Register temp_reg = TMP;
6820 __ Addiu32(temp_reg, value_reg, -lower_bound);
6821 // Jump to default if index is negative
6822 // Note: We don't check the case that index is positive while value < lower_bound, because in
6823 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6824 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6825
Alexey Frunze96b66822016-09-10 02:32:44 -07006826 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006827 // Jump to successors[0] if value == lower_bound.
6828 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6829 int32_t last_index = 0;
6830 for (; num_entries - last_index > 2; last_index += 2) {
6831 __ Addiu(temp_reg, temp_reg, -2);
6832 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6833 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6834 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6835 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6836 }
6837 if (num_entries - last_index == 2) {
6838 // The last missing case_value.
6839 __ Addiu(temp_reg, temp_reg, -1);
6840 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006841 }
6842
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006843 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006844 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006845 __ B(codegen_->GetLabelOf(default_block));
6846 }
6847}
6848
Alexey Frunze96b66822016-09-10 02:32:44 -07006849void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6850 Register constant_area,
6851 int32_t lower_bound,
6852 uint32_t num_entries,
6853 HBasicBlock* switch_block,
6854 HBasicBlock* default_block) {
6855 // Create a jump table.
6856 std::vector<MipsLabel*> labels(num_entries);
6857 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6858 for (uint32_t i = 0; i < num_entries; i++) {
6859 labels[i] = codegen_->GetLabelOf(successors[i]);
6860 }
6861 JumpTable* table = __ CreateJumpTable(std::move(labels));
6862
6863 // Is the value in range?
6864 __ Addiu32(TMP, value_reg, -lower_bound);
6865 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6866 __ Sltiu(AT, TMP, num_entries);
6867 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6868 } else {
6869 __ LoadConst32(AT, num_entries);
6870 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6871 }
6872
6873 // We are in the range of the table.
6874 // Load the target address from the jump table, indexing by the value.
6875 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6876 __ Sll(TMP, TMP, 2);
6877 __ Addu(TMP, TMP, AT);
6878 __ Lw(TMP, TMP, 0);
6879 // Compute the absolute target address by adding the table start address
6880 // (the table contains offsets to targets relative to its start).
6881 __ Addu(TMP, TMP, AT);
6882 // And jump.
6883 __ Jr(TMP);
6884 __ NopIfNoReordering();
6885}
6886
6887void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6888 int32_t lower_bound = switch_instr->GetStartValue();
6889 uint32_t num_entries = switch_instr->GetNumEntries();
6890 LocationSummary* locations = switch_instr->GetLocations();
6891 Register value_reg = locations->InAt(0).AsRegister<Register>();
6892 HBasicBlock* switch_block = switch_instr->GetBlock();
6893 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6894
6895 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6896 num_entries > kPackedSwitchJumpTableThreshold) {
6897 // R6 uses PC-relative addressing to access the jump table.
6898 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6899 // the jump table and it is implemented by changing HPackedSwitch to
6900 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6901 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6902 GenTableBasedPackedSwitch(value_reg,
6903 ZERO,
6904 lower_bound,
6905 num_entries,
6906 switch_block,
6907 default_block);
6908 } else {
6909 GenPackedSwitchWithCompares(value_reg,
6910 lower_bound,
6911 num_entries,
6912 switch_block,
6913 default_block);
6914 }
6915}
6916
6917void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6918 LocationSummary* locations =
6919 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6920 locations->SetInAt(0, Location::RequiresRegister());
6921 // Constant area pointer (HMipsComputeBaseMethodAddress).
6922 locations->SetInAt(1, Location::RequiresRegister());
6923}
6924
6925void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6926 int32_t lower_bound = switch_instr->GetStartValue();
6927 uint32_t num_entries = switch_instr->GetNumEntries();
6928 LocationSummary* locations = switch_instr->GetLocations();
6929 Register value_reg = locations->InAt(0).AsRegister<Register>();
6930 Register constant_area = locations->InAt(1).AsRegister<Register>();
6931 HBasicBlock* switch_block = switch_instr->GetBlock();
6932 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6933
6934 // This is an R2-only path. HPackedSwitch has been changed to
6935 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6936 // required to address the jump table relative to PC.
6937 GenTableBasedPackedSwitch(value_reg,
6938 constant_area,
6939 lower_bound,
6940 num_entries,
6941 switch_block,
6942 default_block);
6943}
6944
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006945void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6946 HMipsComputeBaseMethodAddress* insn) {
6947 LocationSummary* locations =
6948 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6949 locations->SetOut(Location::RequiresRegister());
6950}
6951
6952void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6953 HMipsComputeBaseMethodAddress* insn) {
6954 LocationSummary* locations = insn->GetLocations();
6955 Register reg = locations->Out().AsRegister<Register>();
6956
6957 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6958
6959 // Generate a dummy PC-relative call to obtain PC.
6960 __ Nal();
6961 // Grab the return address off RA.
6962 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006963 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006964
6965 // Remember this offset (the obtained PC value) for later use with constant area.
6966 __ BindPcRelBaseLabel();
6967}
6968
6969void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6971 locations->SetOut(Location::RequiresRegister());
6972}
6973
6974void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6975 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6976 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6977 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006978 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6979 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006980}
6981
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006982void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6983 // The trampoline uses the same calling convention as dex calling conventions,
6984 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6985 // the method_idx.
6986 HandleInvoke(invoke);
6987}
6988
6989void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6990 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6991}
6992
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006993void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6994 LocationSummary* locations =
6995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6996 locations->SetInAt(0, Location::RequiresRegister());
6997 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006998}
6999
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007000void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7001 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007002 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007003 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007004 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007005 __ LoadFromOffset(kLoadWord,
7006 locations->Out().AsRegister<Register>(),
7007 locations->InAt(0).AsRegister<Register>(),
7008 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007009 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007010 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007011 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007012 __ LoadFromOffset(kLoadWord,
7013 locations->Out().AsRegister<Register>(),
7014 locations->InAt(0).AsRegister<Register>(),
7015 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007016 __ LoadFromOffset(kLoadWord,
7017 locations->Out().AsRegister<Register>(),
7018 locations->Out().AsRegister<Register>(),
7019 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007020 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007021}
7022
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007023#undef __
7024#undef QUICK_ENTRY_POINT
7025
7026} // namespace mips
7027} // namespace art