blob: 5f02a52417d69aa534f921900d0f7f96fdba7df4 [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)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000216 : SlowPathCodeMIPS(at), cls_(cls), 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 {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000221 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200222 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
223
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000228 dex::TypeIndex type_index = cls_->GetTypeIndex();
229 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230
Serban Constantinescufca16662016-07-14 09:21:59 +0100231 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
232 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000233 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 if (do_clinit_) {
235 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
236 } else {
237 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
238 }
239
240 // Move the class to the desired location.
241 Location out = locations->Out();
242 if (out.IsValid()) {
243 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 Primitive::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200245 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
246 }
247
248 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
250 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
251 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
252 DCHECK(out.IsValid());
253 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
254 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
255 // so that we can avoid another calculation here.
256 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
257 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
258 DCHECK_NE(out.AsRegister<Register>(), AT);
259 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000260 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800261 bool reordering = __ SetReorder(false);
262 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
263 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
264 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000265 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200266 __ B(GetExitLabel());
267 }
268
269 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
270
271 private:
272 // The class this slow path will load.
273 HLoadClass* const cls_;
274
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200275 // The dex PC of `at_`.
276 const uint32_t dex_pc_;
277
278 // Whether to initialize the class.
279 const bool do_clinit_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
282};
283
284class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
285 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 LocationSummary* locations = instruction_->GetLocations();
290 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
291 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
292
293 __ Bind(GetEntryLabel());
294 SaveLiveRegisters(codegen, locations);
295
296 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000297 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000298 const dex::StringIndex string_index = load->GetStringIndex();
299 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100300 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
302 Primitive::Type type = instruction_->GetType();
303 mips_codegen->MoveLocation(locations->Out(),
304 calling_convention.GetReturnLocation(type),
305 type);
306
307 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308
309 // Store the resolved String to the BSS entry.
310 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
311 // .bss entry address in the fast path, so that we can avoid another calculation here.
312 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
313 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
314 Register out = locations->Out().AsRegister<Register>();
315 DCHECK_NE(out, AT);
316 CodeGeneratorMIPS::PcRelativePatchInfo* info =
317 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800318 bool reordering = __ SetReorder(false);
319 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
320 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
321 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000322
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200323 __ B(GetExitLabel());
324 }
325
326 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
327
328 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
330};
331
332class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
333 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
338 __ Bind(GetEntryLabel());
339 if (instruction_->CanThrowIntoCatchBlock()) {
340 // Live registers will be restored in the catch block if caught.
341 SaveLiveRegisters(codegen, instruction_->GetLocations());
342 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100343 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344 instruction_,
345 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100346 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
348 }
349
350 bool IsFatal() const OVERRIDE { return true; }
351
352 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
353
354 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
356};
357
358class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
359 public:
360 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000361 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200362
363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
364 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
365 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100366 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200367 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368 if (successor_ == nullptr) {
369 __ B(GetReturnLabel());
370 } else {
371 __ B(mips_codegen->GetLabelOf(successor_));
372 }
373 }
374
375 MipsLabel* GetReturnLabel() {
376 DCHECK(successor_ == nullptr);
377 return &return_label_;
378 }
379
380 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
381
382 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200383 // If not null, the block to branch to after the suspend check.
384 HBasicBlock* const successor_;
385
386 // If `successor_` is null, the label to branch to after the suspend check.
387 MipsLabel return_label_;
388
389 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
390};
391
392class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
393 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800394 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
395 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
398 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 uint32_t dex_pc = instruction_->GetDexPc();
400 DCHECK(instruction_->IsCheckCast()
401 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
402 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
403
404 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800405 if (!is_fatal_) {
406 SaveLiveRegisters(codegen, locations);
407 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200408
409 // We're moving two locations to locations that could overlap, so we need a parallel
410 // move resolver.
411 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800412 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
414 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800415 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
417 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100419 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800420 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 Primitive::Type ret_type = instruction_->GetType();
422 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
423 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200424 } else {
425 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800426 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
427 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 }
429
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800430 if (!is_fatal_) {
431 RestoreLiveRegisters(codegen, locations);
432 __ B(GetExitLabel());
433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 }
435
436 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
437
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800438 bool IsFatal() const OVERRIDE { return is_fatal_; }
439
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800441 const bool is_fatal_;
442
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
444};
445
446class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
447 public:
Aart Bik42249c32016-01-07 15:33:50 -0800448 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000449 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450
451 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800452 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200453 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100454 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000455 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200456 }
457
458 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
459
460 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200461 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
462};
463
464CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
465 const MipsInstructionSetFeatures& isa_features,
466 const CompilerOptions& compiler_options,
467 OptimizingCompilerStats* stats)
468 : CodeGenerator(graph,
469 kNumberOfCoreRegisters,
470 kNumberOfFRegisters,
471 kNumberOfRegisterPairs,
472 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
473 arraysize(kCoreCalleeSaves)),
474 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
475 arraysize(kFpuCalleeSaves)),
476 compiler_options,
477 stats),
478 block_labels_(nullptr),
479 location_builder_(graph, this),
480 instruction_visitor_(graph, this),
481 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100482 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700483 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700484 uint32_literals_(std::less<uint32_t>(),
485 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700486 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
487 boot_image_string_patches_(StringReferenceValueComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
489 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
490 boot_image_type_patches_(TypeReferenceValueComparator(),
491 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
492 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000493 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800494 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
495 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700496 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200497 // Save RA (containing the return address) to mimic Quick.
498 AddAllocatedRegister(Location::RegisterLocation(RA));
499}
500
501#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100502// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
503#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700504#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200505
506void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
507 // Ensure that we fix up branches.
508 __ FinalizeCode();
509
510 // Adjust native pc offsets in stack maps.
511 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800512 uint32_t old_position =
513 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200514 uint32_t new_position = __ GetAdjustedPosition(old_position);
515 DCHECK_GE(new_position, old_position);
516 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
517 }
518
519 // Adjust pc offsets for the disassembly information.
520 if (disasm_info_ != nullptr) {
521 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
522 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
523 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
524 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
525 it.second.start = __ GetAdjustedPosition(it.second.start);
526 it.second.end = __ GetAdjustedPosition(it.second.end);
527 }
528 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
529 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
530 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
531 }
532 }
533
534 CodeGenerator::Finalize(allocator);
535}
536
537MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
538 return codegen_->GetAssembler();
539}
540
541void ParallelMoveResolverMIPS::EmitMove(size_t index) {
542 DCHECK_LT(index, moves_.size());
543 MoveOperands* move = moves_[index];
544 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
545}
546
547void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
548 DCHECK_LT(index, moves_.size());
549 MoveOperands* move = moves_[index];
550 Primitive::Type type = move->GetType();
551 Location loc1 = move->GetDestination();
552 Location loc2 = move->GetSource();
553
554 DCHECK(!loc1.IsConstant());
555 DCHECK(!loc2.IsConstant());
556
557 if (loc1.Equals(loc2)) {
558 return;
559 }
560
561 if (loc1.IsRegister() && loc2.IsRegister()) {
562 // Swap 2 GPRs.
563 Register r1 = loc1.AsRegister<Register>();
564 Register r2 = loc2.AsRegister<Register>();
565 __ Move(TMP, r2);
566 __ Move(r2, r1);
567 __ Move(r1, TMP);
568 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
569 FRegister f1 = loc1.AsFpuRegister<FRegister>();
570 FRegister f2 = loc2.AsFpuRegister<FRegister>();
571 if (type == Primitive::kPrimFloat) {
572 __ MovS(FTMP, f2);
573 __ MovS(f2, f1);
574 __ MovS(f1, FTMP);
575 } else {
576 DCHECK_EQ(type, Primitive::kPrimDouble);
577 __ MovD(FTMP, f2);
578 __ MovD(f2, f1);
579 __ MovD(f1, FTMP);
580 }
581 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
582 (loc1.IsFpuRegister() && loc2.IsRegister())) {
583 // Swap FPR and GPR.
584 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
585 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
586 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200587 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200588 __ Move(TMP, r2);
589 __ Mfc1(r2, f1);
590 __ Mtc1(TMP, f1);
591 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
592 // Swap 2 GPR register pairs.
593 Register r1 = loc1.AsRegisterPairLow<Register>();
594 Register r2 = loc2.AsRegisterPairLow<Register>();
595 __ Move(TMP, r2);
596 __ Move(r2, r1);
597 __ Move(r1, TMP);
598 r1 = loc1.AsRegisterPairHigh<Register>();
599 r2 = loc2.AsRegisterPairHigh<Register>();
600 __ Move(TMP, r2);
601 __ Move(r2, r1);
602 __ Move(r1, TMP);
603 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
604 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
605 // Swap FPR and GPR register pair.
606 DCHECK_EQ(type, Primitive::kPrimDouble);
607 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
608 : loc2.AsFpuRegister<FRegister>();
609 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
610 : loc2.AsRegisterPairLow<Register>();
611 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
612 : loc2.AsRegisterPairHigh<Register>();
613 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
614 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
615 // unpredictable and the following mfch1 will fail.
616 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800617 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200618 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800619 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200620 __ Move(r2_l, TMP);
621 __ Move(r2_h, AT);
622 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
623 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
624 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
625 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000626 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
627 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200628 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
629 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000630 __ Move(TMP, reg);
631 __ LoadFromOffset(kLoadWord, reg, SP, offset);
632 __ StoreToOffset(kStoreWord, TMP, SP, offset);
633 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
634 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
635 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
636 : loc2.AsRegisterPairLow<Register>();
637 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
638 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200639 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000640 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
641 : loc2.GetHighStackIndex(kMipsWordSize);
642 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000643 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000644 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000645 __ Move(TMP, reg_h);
646 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
647 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200648 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
649 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
650 : loc2.AsFpuRegister<FRegister>();
651 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
652 if (type == Primitive::kPrimFloat) {
653 __ MovS(FTMP, reg);
654 __ LoadSFromOffset(reg, SP, offset);
655 __ StoreSToOffset(FTMP, SP, offset);
656 } else {
657 DCHECK_EQ(type, Primitive::kPrimDouble);
658 __ MovD(FTMP, reg);
659 __ LoadDFromOffset(reg, SP, offset);
660 __ StoreDToOffset(FTMP, SP, offset);
661 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200662 } else {
663 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
664 }
665}
666
667void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
668 __ Pop(static_cast<Register>(reg));
669}
670
671void ParallelMoveResolverMIPS::SpillScratch(int reg) {
672 __ Push(static_cast<Register>(reg));
673}
674
675void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
676 // Allocate a scratch register other than TMP, if available.
677 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
678 // automatically unspilled when the scratch scope object is destroyed).
679 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
680 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
681 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
682 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
683 __ LoadFromOffset(kLoadWord,
684 Register(ensure_scratch.GetRegister()),
685 SP,
686 index1 + stack_offset);
687 __ LoadFromOffset(kLoadWord,
688 TMP,
689 SP,
690 index2 + stack_offset);
691 __ StoreToOffset(kStoreWord,
692 Register(ensure_scratch.GetRegister()),
693 SP,
694 index2 + stack_offset);
695 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
696 }
697}
698
Alexey Frunze73296a72016-06-03 22:51:46 -0700699void CodeGeneratorMIPS::ComputeSpillMask() {
700 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
701 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
702 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
703 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
704 // registers, include the ZERO register to force alignment of FPU callee-saved registers
705 // within the stack frame.
706 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
707 core_spill_mask_ |= (1 << ZERO);
708 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700709}
710
711bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700712 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700713 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
714 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
715 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700716 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700717}
718
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200719static dwarf::Reg DWARFReg(Register reg) {
720 return dwarf::Reg::MipsCore(static_cast<int>(reg));
721}
722
723// TODO: mapping of floating-point registers to DWARF.
724
725void CodeGeneratorMIPS::GenerateFrameEntry() {
726 __ Bind(&frame_entry_label_);
727
728 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
729
730 if (do_overflow_check) {
731 __ LoadFromOffset(kLoadWord,
732 ZERO,
733 SP,
734 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
735 RecordPcInfo(nullptr, 0);
736 }
737
738 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700739 CHECK_EQ(fpu_spill_mask_, 0u);
740 CHECK_EQ(core_spill_mask_, 1u << RA);
741 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200742 return;
743 }
744
745 // Make sure the frame size isn't unreasonably large.
746 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
747 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
748 }
749
750 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200751
Alexey Frunze73296a72016-06-03 22:51:46 -0700752 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200753 __ IncreaseFrameSize(ofs);
754
Alexey Frunze73296a72016-06-03 22:51:46 -0700755 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
756 Register reg = static_cast<Register>(MostSignificantBit(mask));
757 mask ^= 1u << reg;
758 ofs -= kMipsWordSize;
759 // The ZERO register is only included for alignment.
760 if (reg != ZERO) {
761 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200762 __ cfi().RelOffset(DWARFReg(reg), ofs);
763 }
764 }
765
Alexey Frunze73296a72016-06-03 22:51:46 -0700766 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
767 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
768 mask ^= 1u << reg;
769 ofs -= kMipsDoublewordSize;
770 __ StoreDToOffset(reg, SP, ofs);
771 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200772 }
773
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100774 // Save the current method if we need it. Note that we do not
775 // do this in HCurrentMethod, as the instruction might have been removed
776 // in the SSA graph.
777 if (RequiresCurrentMethod()) {
778 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
779 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100780
781 if (GetGraph()->HasShouldDeoptimizeFlag()) {
782 // Initialize should deoptimize flag to 0.
783 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
784 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200785}
786
787void CodeGeneratorMIPS::GenerateFrameExit() {
788 __ cfi().RememberState();
789
790 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200791 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200792
Alexey Frunze73296a72016-06-03 22:51:46 -0700793 // For better instruction scheduling restore RA before other registers.
794 uint32_t ofs = GetFrameSize();
795 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
796 Register reg = static_cast<Register>(MostSignificantBit(mask));
797 mask ^= 1u << reg;
798 ofs -= kMipsWordSize;
799 // The ZERO register is only included for alignment.
800 if (reg != ZERO) {
801 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200802 __ cfi().Restore(DWARFReg(reg));
803 }
804 }
805
Alexey Frunze73296a72016-06-03 22:51:46 -0700806 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
807 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
808 mask ^= 1u << reg;
809 ofs -= kMipsDoublewordSize;
810 __ LoadDFromOffset(reg, SP, ofs);
811 // TODO: __ cfi().Restore(DWARFReg(reg));
812 }
813
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700814 size_t frame_size = GetFrameSize();
815 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
816 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
817 bool reordering = __ SetReorder(false);
818 if (exchange) {
819 __ Jr(RA);
820 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
821 } else {
822 __ DecreaseFrameSize(frame_size);
823 __ Jr(RA);
824 __ Nop(); // In delay slot.
825 }
826 __ SetReorder(reordering);
827 } else {
828 __ Jr(RA);
829 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200830 }
831
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200832 __ cfi().RestoreState();
833 __ cfi().DefCFAOffset(GetFrameSize());
834}
835
836void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
837 __ Bind(GetLabelOf(block));
838}
839
840void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
841 if (src.Equals(dst)) {
842 return;
843 }
844
845 if (src.IsConstant()) {
846 MoveConstant(dst, src.GetConstant());
847 } else {
848 if (Primitive::Is64BitType(dst_type)) {
849 Move64(dst, src);
850 } else {
851 Move32(dst, src);
852 }
853 }
854}
855
856void CodeGeneratorMIPS::Move32(Location destination, Location source) {
857 if (source.Equals(destination)) {
858 return;
859 }
860
861 if (destination.IsRegister()) {
862 if (source.IsRegister()) {
863 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
864 } else if (source.IsFpuRegister()) {
865 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
866 } else {
867 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
868 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
869 }
870 } else if (destination.IsFpuRegister()) {
871 if (source.IsRegister()) {
872 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
873 } else if (source.IsFpuRegister()) {
874 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
875 } else {
876 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
877 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
878 }
879 } else {
880 DCHECK(destination.IsStackSlot()) << destination;
881 if (source.IsRegister()) {
882 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
883 } else if (source.IsFpuRegister()) {
884 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
885 } else {
886 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
887 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
888 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
889 }
890 }
891}
892
893void CodeGeneratorMIPS::Move64(Location destination, Location source) {
894 if (source.Equals(destination)) {
895 return;
896 }
897
898 if (destination.IsRegisterPair()) {
899 if (source.IsRegisterPair()) {
900 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
901 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
902 } else if (source.IsFpuRegister()) {
903 Register dst_high = destination.AsRegisterPairHigh<Register>();
904 Register dst_low = destination.AsRegisterPairLow<Register>();
905 FRegister src = source.AsFpuRegister<FRegister>();
906 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800907 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200908 } else {
909 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
910 int32_t off = source.GetStackIndex();
911 Register r = destination.AsRegisterPairLow<Register>();
912 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
913 }
914 } else if (destination.IsFpuRegister()) {
915 if (source.IsRegisterPair()) {
916 FRegister dst = destination.AsFpuRegister<FRegister>();
917 Register src_high = source.AsRegisterPairHigh<Register>();
918 Register src_low = source.AsRegisterPairLow<Register>();
919 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800920 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200921 } else if (source.IsFpuRegister()) {
922 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
923 } else {
924 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
925 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
926 }
927 } else {
928 DCHECK(destination.IsDoubleStackSlot()) << destination;
929 int32_t off = destination.GetStackIndex();
930 if (source.IsRegisterPair()) {
931 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
932 } else if (source.IsFpuRegister()) {
933 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
934 } else {
935 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
936 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
937 __ StoreToOffset(kStoreWord, TMP, SP, off);
938 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
939 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
940 }
941 }
942}
943
944void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
945 if (c->IsIntConstant() || c->IsNullConstant()) {
946 // Move 32 bit constant.
947 int32_t value = GetInt32ValueOf(c);
948 if (destination.IsRegister()) {
949 Register dst = destination.AsRegister<Register>();
950 __ LoadConst32(dst, value);
951 } else {
952 DCHECK(destination.IsStackSlot())
953 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700954 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200955 }
956 } else if (c->IsLongConstant()) {
957 // Move 64 bit constant.
958 int64_t value = GetInt64ValueOf(c);
959 if (destination.IsRegisterPair()) {
960 Register r_h = destination.AsRegisterPairHigh<Register>();
961 Register r_l = destination.AsRegisterPairLow<Register>();
962 __ LoadConst64(r_h, r_l, value);
963 } else {
964 DCHECK(destination.IsDoubleStackSlot())
965 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700966 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200967 }
968 } else if (c->IsFloatConstant()) {
969 // Move 32 bit float constant.
970 int32_t value = GetInt32ValueOf(c);
971 if (destination.IsFpuRegister()) {
972 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
973 } else {
974 DCHECK(destination.IsStackSlot())
975 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700976 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200977 }
978 } else {
979 // Move 64 bit double constant.
980 DCHECK(c->IsDoubleConstant()) << c->DebugName();
981 int64_t value = GetInt64ValueOf(c);
982 if (destination.IsFpuRegister()) {
983 FRegister fd = destination.AsFpuRegister<FRegister>();
984 __ LoadDConst64(fd, value, TMP);
985 } else {
986 DCHECK(destination.IsDoubleStackSlot())
987 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700988 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200989 }
990 }
991}
992
993void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
994 DCHECK(destination.IsRegister());
995 Register dst = destination.AsRegister<Register>();
996 __ LoadConst32(dst, value);
997}
998
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200999void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1000 if (location.IsRegister()) {
1001 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001002 } else if (location.IsRegisterPair()) {
1003 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1004 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001005 } else {
1006 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1007 }
1008}
1009
Vladimir Markoaad75c62016-10-03 08:46:48 +00001010template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1011inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1012 const ArenaDeque<PcRelativePatchInfo>& infos,
1013 ArenaVector<LinkerPatch>* linker_patches) {
1014 for (const PcRelativePatchInfo& info : infos) {
1015 const DexFile& dex_file = info.target_dex_file;
1016 size_t offset_or_index = info.offset_or_index;
1017 DCHECK(info.high_label.IsBound());
1018 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1019 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1020 // the assembler's base label used for PC-relative addressing.
1021 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1022 ? __ GetLabelLocation(&info.pc_rel_label)
1023 : __ GetPcRelBaseLabelLocation();
1024 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1025 }
1026}
1027
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001028void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1029 DCHECK(linker_patches->empty());
1030 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001031 pc_relative_dex_cache_patches_.size() +
1032 pc_relative_string_patches_.size() +
1033 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001034 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001035 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001036 boot_image_type_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001037 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001038 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1039 linker_patches);
1040 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001041 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001042 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1043 linker_patches);
1044 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001045 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1046 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001047 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1048 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001049 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001050 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1051 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001052 for (const auto& entry : boot_image_string_patches_) {
1053 const StringReference& target_string = entry.first;
1054 Literal* literal = entry.second;
1055 DCHECK(literal->GetLabel()->IsBound());
1056 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1057 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1058 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001059 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001060 }
1061 for (const auto& entry : boot_image_type_patches_) {
1062 const TypeReference& target_type = entry.first;
1063 Literal* literal = entry.second;
1064 DCHECK(literal->GetLabel()->IsBound());
1065 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1066 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1067 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001068 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001069 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001070 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001071}
1072
1073CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001074 const DexFile& dex_file, dex::StringIndex string_index) {
1075 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001076}
1077
1078CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001079 const DexFile& dex_file, dex::TypeIndex type_index) {
1080 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001081}
1082
Vladimir Marko1998cd02017-01-13 13:02:58 +00001083CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1084 const DexFile& dex_file, dex::TypeIndex type_index) {
1085 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1086}
1087
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001088CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1089 const DexFile& dex_file, uint32_t element_offset) {
1090 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1091}
1092
1093CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1094 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1095 patches->emplace_back(dex_file, offset_or_index);
1096 return &patches->back();
1097}
1098
Alexey Frunze06a46c42016-07-19 15:00:40 -07001099Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1100 return map->GetOrCreate(
1101 value,
1102 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1103}
1104
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001105Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1106 MethodToLiteralMap* map) {
1107 return map->GetOrCreate(
1108 target_method,
1109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1110}
1111
Alexey Frunze06a46c42016-07-19 15:00:40 -07001112Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001113 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001114 return boot_image_string_patches_.GetOrCreate(
1115 StringReference(&dex_file, string_index),
1116 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1117}
1118
1119Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001120 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001121 return boot_image_type_patches_.GetOrCreate(
1122 TypeReference(&dex_file, type_index),
1123 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1124}
1125
1126Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001127 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001128}
1129
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001130void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1131 Register out,
1132 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001133 if (GetInstructionSetFeatures().IsR6()) {
1134 DCHECK_EQ(base, ZERO);
1135 __ Bind(&info->high_label);
1136 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001137 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001138 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001139 } else {
1140 // If base is ZERO, emit NAL to obtain the actual base.
1141 if (base == ZERO) {
1142 // Generate a dummy PC-relative call to obtain PC.
1143 __ Nal();
1144 }
1145 __ Bind(&info->high_label);
1146 __ Lui(out, /* placeholder */ 0x1234);
1147 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1148 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1149 if (base == ZERO) {
1150 __ Bind(&info->pc_rel_label);
1151 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001152 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001153 __ Addu(out, out, (base == ZERO) ? RA : base);
1154 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001155 // The immediately following instruction will add the sign-extended low half of the 32-bit
1156 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001157}
1158
Alexey Frunze627c1a02017-01-30 19:28:14 -08001159CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1160 const DexFile& dex_file,
1161 dex::StringIndex dex_index,
1162 Handle<mirror::String> handle) {
1163 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1164 reinterpret_cast64<uint64_t>(handle.GetReference()));
1165 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1166 return &jit_string_patches_.back();
1167}
1168
1169CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1170 const DexFile& dex_file,
1171 dex::TypeIndex dex_index,
1172 Handle<mirror::Class> handle) {
1173 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1174 reinterpret_cast64<uint64_t>(handle.GetReference()));
1175 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1176 return &jit_class_patches_.back();
1177}
1178
1179void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1180 const uint8_t* roots_data,
1181 const CodeGeneratorMIPS::JitPatchInfo& info,
1182 uint64_t index_in_table) const {
1183 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1184 uintptr_t address =
1185 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1186 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1187 // lui reg, addr32_high
1188 DCHECK_EQ(code[literal_offset + 0], 0x34);
1189 DCHECK_EQ(code[literal_offset + 1], 0x12);
1190 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1191 DCHECK_EQ(code[literal_offset + 3], 0x3C);
1192 // lw reg, reg, addr32_low
1193 DCHECK_EQ(code[literal_offset + 4], 0x78);
1194 DCHECK_EQ(code[literal_offset + 5], 0x56);
1195 DCHECK_EQ((code[literal_offset + 7] & 0xFC), 0x8C);
1196 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "lw reg, reg, addr32_low".
1197 // lui reg, addr32_high
1198 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1199 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
1200 // lw reg, reg, addr32_low
1201 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1202 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1203}
1204
1205void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1206 for (const JitPatchInfo& info : jit_string_patches_) {
1207 const auto& it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1208 dex::StringIndex(info.index)));
1209 DCHECK(it != jit_string_roots_.end());
1210 PatchJitRootUse(code, roots_data, info, it->second);
1211 }
1212 for (const JitPatchInfo& info : jit_class_patches_) {
1213 const auto& it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1214 dex::TypeIndex(info.index)));
1215 DCHECK(it != jit_class_roots_.end());
1216 PatchJitRootUse(code, roots_data, info, it->second);
1217 }
1218}
1219
Goran Jakovljevice114da22016-12-26 14:21:43 +01001220void CodeGeneratorMIPS::MarkGCCard(Register object,
1221 Register value,
1222 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001223 MipsLabel done;
1224 Register card = AT;
1225 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001226 if (value_can_be_null) {
1227 __ Beqz(value, &done);
1228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001229 __ LoadFromOffset(kLoadWord,
1230 card,
1231 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001232 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1234 __ Addu(temp, card, temp);
1235 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001236 if (value_can_be_null) {
1237 __ Bind(&done);
1238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239}
1240
David Brazdil58282f42016-01-14 12:45:10 +00001241void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1243 blocked_core_registers_[ZERO] = true;
1244 blocked_core_registers_[K0] = true;
1245 blocked_core_registers_[K1] = true;
1246 blocked_core_registers_[GP] = true;
1247 blocked_core_registers_[SP] = true;
1248 blocked_core_registers_[RA] = true;
1249
1250 // AT and TMP(T8) are used as temporary/scratch registers
1251 // (similar to how AT is used by MIPS assemblers).
1252 blocked_core_registers_[AT] = true;
1253 blocked_core_registers_[TMP] = true;
1254 blocked_fpu_registers_[FTMP] = true;
1255
1256 // Reserve suspend and thread registers.
1257 blocked_core_registers_[S0] = true;
1258 blocked_core_registers_[TR] = true;
1259
1260 // Reserve T9 for function calls
1261 blocked_core_registers_[T9] = true;
1262
1263 // Reserve odd-numbered FPU registers.
1264 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1265 blocked_fpu_registers_[i] = true;
1266 }
1267
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001268 if (GetGraph()->IsDebuggable()) {
1269 // Stubs do not save callee-save floating point registers. If the graph
1270 // is debuggable, we need to deal with these registers differently. For
1271 // now, just block them.
1272 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1273 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1274 }
1275 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001276}
1277
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001278size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1279 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1280 return kMipsWordSize;
1281}
1282
1283size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1284 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1285 return kMipsWordSize;
1286}
1287
1288size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1289 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1290 return kMipsDoublewordSize;
1291}
1292
1293size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1294 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1295 return kMipsDoublewordSize;
1296}
1297
1298void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001299 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001300}
1301
1302void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001303 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304}
1305
Serban Constantinescufca16662016-07-14 09:21:59 +01001306constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1307
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1309 HInstruction* instruction,
1310 uint32_t dex_pc,
1311 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001312 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001313 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001314 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001315 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001316 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001317 // Reserve argument space on stack (for $a0-$a3) for
1318 // entrypoints that directly reference native implementations.
1319 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001320 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001321 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001322 } else {
1323 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001325 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001326 if (EntrypointRequiresStackMap(entrypoint)) {
1327 RecordPcInfo(instruction, dex_pc, slow_path);
1328 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329}
1330
1331void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1332 Register class_reg) {
1333 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1334 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1335 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1336 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1337 __ Sync(0);
1338 __ Bind(slow_path->GetExitLabel());
1339}
1340
1341void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1342 __ Sync(0); // Only stype 0 is supported.
1343}
1344
1345void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1346 HBasicBlock* successor) {
1347 SuspendCheckSlowPathMIPS* slow_path =
1348 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1349 codegen_->AddSlowPath(slow_path);
1350
1351 __ LoadFromOffset(kLoadUnsignedHalfword,
1352 TMP,
1353 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001354 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355 if (successor == nullptr) {
1356 __ Bnez(TMP, slow_path->GetEntryLabel());
1357 __ Bind(slow_path->GetReturnLabel());
1358 } else {
1359 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1360 __ B(slow_path->GetEntryLabel());
1361 // slow_path will return to GetLabelOf(successor).
1362 }
1363}
1364
1365InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1366 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001367 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368 assembler_(codegen->GetAssembler()),
1369 codegen_(codegen) {}
1370
1371void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1372 DCHECK_EQ(instruction->InputCount(), 2U);
1373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1374 Primitive::Type type = instruction->GetResultType();
1375 switch (type) {
1376 case Primitive::kPrimInt: {
1377 locations->SetInAt(0, Location::RequiresRegister());
1378 HInstruction* right = instruction->InputAt(1);
1379 bool can_use_imm = false;
1380 if (right->IsConstant()) {
1381 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1382 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1383 can_use_imm = IsUint<16>(imm);
1384 } else if (instruction->IsAdd()) {
1385 can_use_imm = IsInt<16>(imm);
1386 } else {
1387 DCHECK(instruction->IsSub());
1388 can_use_imm = IsInt<16>(-imm);
1389 }
1390 }
1391 if (can_use_imm)
1392 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1393 else
1394 locations->SetInAt(1, Location::RequiresRegister());
1395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1396 break;
1397 }
1398
1399 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001400 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001401 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1402 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403 break;
1404 }
1405
1406 case Primitive::kPrimFloat:
1407 case Primitive::kPrimDouble:
1408 DCHECK(instruction->IsAdd() || instruction->IsSub());
1409 locations->SetInAt(0, Location::RequiresFpuRegister());
1410 locations->SetInAt(1, Location::RequiresFpuRegister());
1411 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1412 break;
1413
1414 default:
1415 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1416 }
1417}
1418
1419void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1420 Primitive::Type type = instruction->GetType();
1421 LocationSummary* locations = instruction->GetLocations();
1422
1423 switch (type) {
1424 case Primitive::kPrimInt: {
1425 Register dst = locations->Out().AsRegister<Register>();
1426 Register lhs = locations->InAt(0).AsRegister<Register>();
1427 Location rhs_location = locations->InAt(1);
1428
1429 Register rhs_reg = ZERO;
1430 int32_t rhs_imm = 0;
1431 bool use_imm = rhs_location.IsConstant();
1432 if (use_imm) {
1433 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1434 } else {
1435 rhs_reg = rhs_location.AsRegister<Register>();
1436 }
1437
1438 if (instruction->IsAnd()) {
1439 if (use_imm)
1440 __ Andi(dst, lhs, rhs_imm);
1441 else
1442 __ And(dst, lhs, rhs_reg);
1443 } else if (instruction->IsOr()) {
1444 if (use_imm)
1445 __ Ori(dst, lhs, rhs_imm);
1446 else
1447 __ Or(dst, lhs, rhs_reg);
1448 } else if (instruction->IsXor()) {
1449 if (use_imm)
1450 __ Xori(dst, lhs, rhs_imm);
1451 else
1452 __ Xor(dst, lhs, rhs_reg);
1453 } else if (instruction->IsAdd()) {
1454 if (use_imm)
1455 __ Addiu(dst, lhs, rhs_imm);
1456 else
1457 __ Addu(dst, lhs, rhs_reg);
1458 } else {
1459 DCHECK(instruction->IsSub());
1460 if (use_imm)
1461 __ Addiu(dst, lhs, -rhs_imm);
1462 else
1463 __ Subu(dst, lhs, rhs_reg);
1464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001469 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1470 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1471 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1472 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001473 Location rhs_location = locations->InAt(1);
1474 bool use_imm = rhs_location.IsConstant();
1475 if (!use_imm) {
1476 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1477 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1478 if (instruction->IsAnd()) {
1479 __ And(dst_low, lhs_low, rhs_low);
1480 __ And(dst_high, lhs_high, rhs_high);
1481 } else if (instruction->IsOr()) {
1482 __ Or(dst_low, lhs_low, rhs_low);
1483 __ Or(dst_high, lhs_high, rhs_high);
1484 } else if (instruction->IsXor()) {
1485 __ Xor(dst_low, lhs_low, rhs_low);
1486 __ Xor(dst_high, lhs_high, rhs_high);
1487 } else if (instruction->IsAdd()) {
1488 if (lhs_low == rhs_low) {
1489 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1490 __ Slt(TMP, lhs_low, ZERO);
1491 __ Addu(dst_low, lhs_low, rhs_low);
1492 } else {
1493 __ Addu(dst_low, lhs_low, rhs_low);
1494 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1495 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1496 }
1497 __ Addu(dst_high, lhs_high, rhs_high);
1498 __ Addu(dst_high, dst_high, TMP);
1499 } else {
1500 DCHECK(instruction->IsSub());
1501 __ Sltu(TMP, lhs_low, rhs_low);
1502 __ Subu(dst_low, lhs_low, rhs_low);
1503 __ Subu(dst_high, lhs_high, rhs_high);
1504 __ Subu(dst_high, dst_high, TMP);
1505 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001506 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001507 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1508 if (instruction->IsOr()) {
1509 uint32_t low = Low32Bits(value);
1510 uint32_t high = High32Bits(value);
1511 if (IsUint<16>(low)) {
1512 if (dst_low != lhs_low || low != 0) {
1513 __ Ori(dst_low, lhs_low, low);
1514 }
1515 } else {
1516 __ LoadConst32(TMP, low);
1517 __ Or(dst_low, lhs_low, TMP);
1518 }
1519 if (IsUint<16>(high)) {
1520 if (dst_high != lhs_high || high != 0) {
1521 __ Ori(dst_high, lhs_high, high);
1522 }
1523 } else {
1524 if (high != low) {
1525 __ LoadConst32(TMP, high);
1526 }
1527 __ Or(dst_high, lhs_high, TMP);
1528 }
1529 } else if (instruction->IsXor()) {
1530 uint32_t low = Low32Bits(value);
1531 uint32_t high = High32Bits(value);
1532 if (IsUint<16>(low)) {
1533 if (dst_low != lhs_low || low != 0) {
1534 __ Xori(dst_low, lhs_low, low);
1535 }
1536 } else {
1537 __ LoadConst32(TMP, low);
1538 __ Xor(dst_low, lhs_low, TMP);
1539 }
1540 if (IsUint<16>(high)) {
1541 if (dst_high != lhs_high || high != 0) {
1542 __ Xori(dst_high, lhs_high, high);
1543 }
1544 } else {
1545 if (high != low) {
1546 __ LoadConst32(TMP, high);
1547 }
1548 __ Xor(dst_high, lhs_high, TMP);
1549 }
1550 } else if (instruction->IsAnd()) {
1551 uint32_t low = Low32Bits(value);
1552 uint32_t high = High32Bits(value);
1553 if (IsUint<16>(low)) {
1554 __ Andi(dst_low, lhs_low, low);
1555 } else if (low != 0xFFFFFFFF) {
1556 __ LoadConst32(TMP, low);
1557 __ And(dst_low, lhs_low, TMP);
1558 } else if (dst_low != lhs_low) {
1559 __ Move(dst_low, lhs_low);
1560 }
1561 if (IsUint<16>(high)) {
1562 __ Andi(dst_high, lhs_high, high);
1563 } else if (high != 0xFFFFFFFF) {
1564 if (high != low) {
1565 __ LoadConst32(TMP, high);
1566 }
1567 __ And(dst_high, lhs_high, TMP);
1568 } else if (dst_high != lhs_high) {
1569 __ Move(dst_high, lhs_high);
1570 }
1571 } else {
1572 if (instruction->IsSub()) {
1573 value = -value;
1574 } else {
1575 DCHECK(instruction->IsAdd());
1576 }
1577 int32_t low = Low32Bits(value);
1578 int32_t high = High32Bits(value);
1579 if (IsInt<16>(low)) {
1580 if (dst_low != lhs_low || low != 0) {
1581 __ Addiu(dst_low, lhs_low, low);
1582 }
1583 if (low != 0) {
1584 __ Sltiu(AT, dst_low, low);
1585 }
1586 } else {
1587 __ LoadConst32(TMP, low);
1588 __ Addu(dst_low, lhs_low, TMP);
1589 __ Sltu(AT, dst_low, TMP);
1590 }
1591 if (IsInt<16>(high)) {
1592 if (dst_high != lhs_high || high != 0) {
1593 __ Addiu(dst_high, lhs_high, high);
1594 }
1595 } else {
1596 if (high != low) {
1597 __ LoadConst32(TMP, high);
1598 }
1599 __ Addu(dst_high, lhs_high, TMP);
1600 }
1601 if (low != 0) {
1602 __ Addu(dst_high, dst_high, AT);
1603 }
1604 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001605 }
1606 break;
1607 }
1608
1609 case Primitive::kPrimFloat:
1610 case Primitive::kPrimDouble: {
1611 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1612 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1613 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1614 if (instruction->IsAdd()) {
1615 if (type == Primitive::kPrimFloat) {
1616 __ AddS(dst, lhs, rhs);
1617 } else {
1618 __ AddD(dst, lhs, rhs);
1619 }
1620 } else {
1621 DCHECK(instruction->IsSub());
1622 if (type == Primitive::kPrimFloat) {
1623 __ SubS(dst, lhs, rhs);
1624 } else {
1625 __ SubD(dst, lhs, rhs);
1626 }
1627 }
1628 break;
1629 }
1630
1631 default:
1632 LOG(FATAL) << "Unexpected binary operation type " << type;
1633 }
1634}
1635
1636void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001637 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001638
1639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1640 Primitive::Type type = instr->GetResultType();
1641 switch (type) {
1642 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001643 locations->SetInAt(0, Location::RequiresRegister());
1644 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1646 break;
1647 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001648 locations->SetInAt(0, Location::RequiresRegister());
1649 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1650 locations->SetOut(Location::RequiresRegister());
1651 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001652 default:
1653 LOG(FATAL) << "Unexpected shift type " << type;
1654 }
1655}
1656
1657static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1658
1659void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001660 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001661 LocationSummary* locations = instr->GetLocations();
1662 Primitive::Type type = instr->GetType();
1663
1664 Location rhs_location = locations->InAt(1);
1665 bool use_imm = rhs_location.IsConstant();
1666 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1667 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001668 const uint32_t shift_mask =
1669 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001670 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001671 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1672 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001673
1674 switch (type) {
1675 case Primitive::kPrimInt: {
1676 Register dst = locations->Out().AsRegister<Register>();
1677 Register lhs = locations->InAt(0).AsRegister<Register>();
1678 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 if (shift_value == 0) {
1680 if (dst != lhs) {
1681 __ Move(dst, lhs);
1682 }
1683 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001684 __ Sll(dst, lhs, shift_value);
1685 } else if (instr->IsShr()) {
1686 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001687 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001688 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001689 } else {
1690 if (has_ins_rotr) {
1691 __ Rotr(dst, lhs, shift_value);
1692 } else {
1693 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1694 __ Srl(dst, lhs, shift_value);
1695 __ Or(dst, dst, TMP);
1696 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001697 }
1698 } else {
1699 if (instr->IsShl()) {
1700 __ Sllv(dst, lhs, rhs_reg);
1701 } else if (instr->IsShr()) {
1702 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001703 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001704 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else {
1706 if (has_ins_rotr) {
1707 __ Rotrv(dst, lhs, rhs_reg);
1708 } else {
1709 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1711 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1712 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1713 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1714 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001715 __ Sllv(TMP, lhs, TMP);
1716 __ Srlv(dst, lhs, rhs_reg);
1717 __ Or(dst, dst, TMP);
1718 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001719 }
1720 }
1721 break;
1722 }
1723
1724 case Primitive::kPrimLong: {
1725 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1726 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1727 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1728 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1729 if (use_imm) {
1730 if (shift_value == 0) {
1731 codegen_->Move64(locations->Out(), locations->InAt(0));
1732 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001733 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001734 if (instr->IsShl()) {
1735 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1736 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1737 __ Sll(dst_low, lhs_low, shift_value);
1738 } else if (instr->IsShr()) {
1739 __ Srl(dst_low, lhs_low, shift_value);
1740 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1741 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001742 } else if (instr->IsUShr()) {
1743 __ Srl(dst_low, lhs_low, shift_value);
1744 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1745 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001746 } else {
1747 __ Srl(dst_low, lhs_low, shift_value);
1748 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1749 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001750 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001751 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001752 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001753 if (instr->IsShl()) {
1754 __ Sll(dst_low, lhs_low, shift_value);
1755 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1756 __ Sll(dst_high, lhs_high, shift_value);
1757 __ Or(dst_high, dst_high, TMP);
1758 } else if (instr->IsShr()) {
1759 __ Sra(dst_high, lhs_high, shift_value);
1760 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1761 __ Srl(dst_low, lhs_low, shift_value);
1762 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001763 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001764 __ Srl(dst_high, lhs_high, shift_value);
1765 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1766 __ Srl(dst_low, lhs_low, shift_value);
1767 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001768 } else {
1769 __ Srl(TMP, lhs_low, shift_value);
1770 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1771 __ Or(dst_low, dst_low, TMP);
1772 __ Srl(TMP, lhs_high, shift_value);
1773 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1774 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001775 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001776 }
1777 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001778 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001779 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001780 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001781 __ Move(dst_low, ZERO);
1782 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001783 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001784 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001785 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001786 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001787 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001788 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001789 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001790 // 64-bit rotation by 32 is just a swap.
1791 __ Move(dst_low, lhs_high);
1792 __ Move(dst_high, lhs_low);
1793 } else {
1794 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001795 __ Srl(dst_low, lhs_high, shift_value_high);
1796 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1797 __ Srl(dst_high, lhs_low, shift_value_high);
1798 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001799 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001800 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1801 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001802 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001803 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1804 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001805 __ Or(dst_high, dst_high, TMP);
1806 }
1807 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001808 }
1809 }
1810 } else {
1811 MipsLabel done;
1812 if (instr->IsShl()) {
1813 __ Sllv(dst_low, lhs_low, rhs_reg);
1814 __ Nor(AT, ZERO, rhs_reg);
1815 __ Srl(TMP, lhs_low, 1);
1816 __ Srlv(TMP, TMP, AT);
1817 __ Sllv(dst_high, lhs_high, rhs_reg);
1818 __ Or(dst_high, dst_high, TMP);
1819 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1820 __ Beqz(TMP, &done);
1821 __ Move(dst_high, dst_low);
1822 __ Move(dst_low, ZERO);
1823 } else if (instr->IsShr()) {
1824 __ Srav(dst_high, lhs_high, rhs_reg);
1825 __ Nor(AT, ZERO, rhs_reg);
1826 __ Sll(TMP, lhs_high, 1);
1827 __ Sllv(TMP, TMP, AT);
1828 __ Srlv(dst_low, lhs_low, rhs_reg);
1829 __ Or(dst_low, dst_low, TMP);
1830 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1831 __ Beqz(TMP, &done);
1832 __ Move(dst_low, dst_high);
1833 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001834 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001835 __ Srlv(dst_high, lhs_high, rhs_reg);
1836 __ Nor(AT, ZERO, rhs_reg);
1837 __ Sll(TMP, lhs_high, 1);
1838 __ Sllv(TMP, TMP, AT);
1839 __ Srlv(dst_low, lhs_low, rhs_reg);
1840 __ Or(dst_low, dst_low, TMP);
1841 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1842 __ Beqz(TMP, &done);
1843 __ Move(dst_low, dst_high);
1844 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001845 } else {
1846 __ Nor(AT, ZERO, rhs_reg);
1847 __ Srlv(TMP, lhs_low, rhs_reg);
1848 __ Sll(dst_low, lhs_high, 1);
1849 __ Sllv(dst_low, dst_low, AT);
1850 __ Or(dst_low, dst_low, TMP);
1851 __ Srlv(TMP, lhs_high, rhs_reg);
1852 __ Sll(dst_high, lhs_low, 1);
1853 __ Sllv(dst_high, dst_high, AT);
1854 __ Or(dst_high, dst_high, TMP);
1855 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1856 __ Beqz(TMP, &done);
1857 __ Move(TMP, dst_high);
1858 __ Move(dst_high, dst_low);
1859 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001860 }
1861 __ Bind(&done);
1862 }
1863 break;
1864 }
1865
1866 default:
1867 LOG(FATAL) << "Unexpected shift operation type " << type;
1868 }
1869}
1870
1871void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1872 HandleBinaryOp(instruction);
1873}
1874
1875void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1876 HandleBinaryOp(instruction);
1877}
1878
1879void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1880 HandleBinaryOp(instruction);
1881}
1882
1883void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1884 HandleBinaryOp(instruction);
1885}
1886
1887void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1890 locations->SetInAt(0, Location::RequiresRegister());
1891 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1892 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1893 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1894 } else {
1895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1896 }
1897}
1898
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001899static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
1900 auto null_checker = [codegen, instruction]() {
1901 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001902 };
1903 return null_checker;
1904}
1905
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1907 LocationSummary* locations = instruction->GetLocations();
1908 Register obj = locations->InAt(0).AsRegister<Register>();
1909 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001910 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001911 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001913 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001914 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1915 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 switch (type) {
1917 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918 Register out = locations->Out().AsRegister<Register>();
1919 if (index.IsConstant()) {
1920 size_t offset =
1921 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001922 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 } else {
1924 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001925 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 }
1927 break;
1928 }
1929
1930 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001931 Register out = locations->Out().AsRegister<Register>();
1932 if (index.IsConstant()) {
1933 size_t offset =
1934 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001935 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936 } else {
1937 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 }
1940 break;
1941 }
1942
1943 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944 Register out = locations->Out().AsRegister<Register>();
1945 if (index.IsConstant()) {
1946 size_t offset =
1947 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001948 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001949 } else {
1950 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1951 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001952 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001953 }
1954 break;
1955 }
1956
1957 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001958 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001959 if (maybe_compressed_char_at) {
1960 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1961 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
1962 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
1963 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1964 "Expecting 0=compressed, 1=uncompressed");
1965 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001966 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001967 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1968 if (maybe_compressed_char_at) {
1969 MipsLabel uncompressed_load, done;
1970 __ Bnez(TMP, &uncompressed_load);
1971 __ LoadFromOffset(kLoadUnsignedByte,
1972 out,
1973 obj,
1974 data_offset + (const_index << TIMES_1));
1975 __ B(&done);
1976 __ Bind(&uncompressed_load);
1977 __ LoadFromOffset(kLoadUnsignedHalfword,
1978 out,
1979 obj,
1980 data_offset + (const_index << TIMES_2));
1981 __ Bind(&done);
1982 } else {
1983 __ LoadFromOffset(kLoadUnsignedHalfword,
1984 out,
1985 obj,
1986 data_offset + (const_index << TIMES_2),
1987 null_checker);
1988 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001989 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001990 Register index_reg = index.AsRegister<Register>();
1991 if (maybe_compressed_char_at) {
1992 MipsLabel uncompressed_load, done;
1993 __ Bnez(TMP, &uncompressed_load);
1994 __ Addu(TMP, obj, index_reg);
1995 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1996 __ B(&done);
1997 __ Bind(&uncompressed_load);
1998 __ Sll(TMP, index_reg, TIMES_2);
1999 __ Addu(TMP, obj, TMP);
2000 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2001 __ Bind(&done);
2002 } else {
2003 __ Sll(TMP, index_reg, TIMES_2);
2004 __ Addu(TMP, obj, TMP);
2005 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2006 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002007 }
2008 break;
2009 }
2010
2011 case Primitive::kPrimInt:
2012 case Primitive::kPrimNot: {
2013 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 Register out = locations->Out().AsRegister<Register>();
2015 if (index.IsConstant()) {
2016 size_t offset =
2017 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002018 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 } else {
2020 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2021 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002022 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002023 }
2024 break;
2025 }
2026
2027 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 Register out = locations->Out().AsRegisterPairLow<Register>();
2029 if (index.IsConstant()) {
2030 size_t offset =
2031 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002032 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002033 } else {
2034 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2035 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002036 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002037 }
2038 break;
2039 }
2040
2041 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2043 if (index.IsConstant()) {
2044 size_t offset =
2045 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002046 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047 } else {
2048 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2049 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002050 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002051 }
2052 break;
2053 }
2054
2055 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002056 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2057 if (index.IsConstant()) {
2058 size_t offset =
2059 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002060 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 } else {
2062 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2063 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002064 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002065 }
2066 break;
2067 }
2068
2069 case Primitive::kPrimVoid:
2070 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2071 UNREACHABLE();
2072 }
Alexey Frunzec061de12017-02-14 13:27:23 -08002073
2074 if (type == Primitive::kPrimNot) {
2075 Register out = locations->Out().AsRegister<Register>();
2076 __ MaybeUnpoisonHeapReference(out);
2077 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078}
2079
2080void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2081 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2084}
2085
2086void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2087 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002088 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 Register obj = locations->InAt(0).AsRegister<Register>();
2090 Register out = locations->Out().AsRegister<Register>();
2091 __ LoadFromOffset(kLoadWord, out, obj, offset);
2092 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002093 // Mask out compression flag from String's array length.
2094 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2095 __ Srl(out, out, 1u);
2096 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002097}
2098
Alexey Frunzef58b2482016-09-02 22:14:06 -07002099Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2100 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2101 ? Location::ConstantLocation(instruction->AsConstant())
2102 : Location::RequiresRegister();
2103}
2104
2105Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2106 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2107 // We can store a non-zero float or double constant without first loading it into the FPU,
2108 // but we should only prefer this if the constant has a single use.
2109 if (instruction->IsConstant() &&
2110 (instruction->AsConstant()->IsZeroBitPattern() ||
2111 instruction->GetUses().HasExactlyOneElement())) {
2112 return Location::ConstantLocation(instruction->AsConstant());
2113 // Otherwise fall through and require an FPU register for the constant.
2114 }
2115 return Location::RequiresFpuRegister();
2116}
2117
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002119 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2121 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002122 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002123 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 InvokeRuntimeCallingConvention calling_convention;
2125 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2126 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2127 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2128 } else {
2129 locations->SetInAt(0, Location::RequiresRegister());
2130 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2131 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002132 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002134 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002135 }
2136 }
2137}
2138
2139void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2140 LocationSummary* locations = instruction->GetLocations();
2141 Register obj = locations->InAt(0).AsRegister<Register>();
2142 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002143 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144 Primitive::Type value_type = instruction->GetComponentType();
2145 bool needs_runtime_call = locations->WillCall();
2146 bool needs_write_barrier =
2147 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002148 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002149 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150
2151 switch (value_type) {
2152 case Primitive::kPrimBoolean:
2153 case Primitive::kPrimByte: {
2154 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002156 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002157 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002158 __ Addu(base_reg, obj, index.AsRegister<Register>());
2159 }
2160 if (value_location.IsConstant()) {
2161 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2162 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2163 } else {
2164 Register value = value_location.AsRegister<Register>();
2165 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002166 }
2167 break;
2168 }
2169
2170 case Primitive::kPrimShort:
2171 case Primitive::kPrimChar: {
2172 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002173 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002174 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002175 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002176 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2177 __ Addu(base_reg, obj, base_reg);
2178 }
2179 if (value_location.IsConstant()) {
2180 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2181 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2182 } else {
2183 Register value = value_location.AsRegister<Register>();
2184 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002185 }
2186 break;
2187 }
2188
2189 case Primitive::kPrimInt:
2190 case Primitive::kPrimNot: {
2191 if (!needs_runtime_call) {
2192 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002193 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002194 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002195 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002196 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2197 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002198 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002199 if (value_location.IsConstant()) {
2200 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2201 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2202 DCHECK(!needs_write_barrier);
2203 } else {
2204 Register value = value_location.AsRegister<Register>();
Alexey Frunzec061de12017-02-14 13:27:23 -08002205 if (kPoisonHeapReferences && needs_write_barrier) {
2206 // Note that in the case where `value` is a null reference,
2207 // we do not enter this block, as a null reference does not
2208 // need poisoning.
2209 DCHECK_EQ(value_type, Primitive::kPrimNot);
2210 // Use Sw() instead of StoreToOffset() in order to be able to
2211 // hold the poisoned reference in AT and thus avoid allocating
2212 // yet another temporary register.
2213 if (index.IsConstant()) {
2214 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
2215 int16_t low = Low16Bits(data_offset);
2216 uint32_t high = data_offset - low;
2217 __ Addiu32(TMP, obj, high);
2218 base_reg = TMP;
2219 data_offset = low;
2220 }
2221 } else {
2222 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
2223 }
2224 __ PoisonHeapReference(AT, value);
2225 __ Sw(AT, base_reg, data_offset);
2226 null_checker();
2227 } else {
2228 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2229 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002230 if (needs_write_barrier) {
2231 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002232 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002233 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002234 }
2235 } else {
2236 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08002237 // Note: if heap poisoning is enabled, pAputObject takes care
2238 // of poisoning the reference.
Serban Constantinescufca16662016-07-14 09:21:59 +01002239 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2241 }
2242 break;
2243 }
2244
2245 case Primitive::kPrimLong: {
2246 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002248 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002249 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002250 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2251 __ Addu(base_reg, obj, base_reg);
2252 }
2253 if (value_location.IsConstant()) {
2254 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2255 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2256 } else {
2257 Register value = value_location.AsRegisterPairLow<Register>();
2258 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002259 }
2260 break;
2261 }
2262
2263 case Primitive::kPrimFloat: {
2264 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002265 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002266 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002268 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2269 __ Addu(base_reg, obj, base_reg);
2270 }
2271 if (value_location.IsConstant()) {
2272 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2273 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2274 } else {
2275 FRegister value = value_location.AsFpuRegister<FRegister>();
2276 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 }
2278 break;
2279 }
2280
2281 case Primitive::kPrimDouble: {
2282 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002283 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002284 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002286 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2287 __ Addu(base_reg, obj, base_reg);
2288 }
2289 if (value_location.IsConstant()) {
2290 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2291 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2292 } else {
2293 FRegister value = value_location.AsFpuRegister<FRegister>();
2294 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 }
2296 break;
2297 }
2298
2299 case Primitive::kPrimVoid:
2300 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2301 UNREACHABLE();
2302 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303}
2304
2305void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002306 RegisterSet caller_saves = RegisterSet::Empty();
2307 InvokeRuntimeCallingConvention calling_convention;
2308 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2309 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2310 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002311 locations->SetInAt(0, Location::RequiresRegister());
2312 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002313}
2314
2315void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2316 LocationSummary* locations = instruction->GetLocations();
2317 BoundsCheckSlowPathMIPS* slow_path =
2318 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2319 codegen_->AddSlowPath(slow_path);
2320
2321 Register index = locations->InAt(0).AsRegister<Register>();
2322 Register length = locations->InAt(1).AsRegister<Register>();
2323
2324 // length is limited by the maximum positive signed 32-bit integer.
2325 // Unsigned comparison of length and index checks for index < 0
2326 // and for length <= index simultaneously.
2327 __ Bgeu(index, length, slow_path->GetEntryLabel());
2328}
2329
2330void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002331 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2332 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2333
2334 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2335 switch (type_check_kind) {
2336 case TypeCheckKind::kExactCheck:
2337 case TypeCheckKind::kAbstractClassCheck:
2338 case TypeCheckKind::kClassHierarchyCheck:
2339 case TypeCheckKind::kArrayObjectCheck:
2340 call_kind = throws_into_catch
2341 ? LocationSummary::kCallOnSlowPath
2342 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2343 break;
2344 case TypeCheckKind::kArrayCheck:
2345 case TypeCheckKind::kUnresolvedCheck:
2346 case TypeCheckKind::kInterfaceCheck:
2347 call_kind = LocationSummary::kCallOnSlowPath;
2348 break;
2349 }
2350
2351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002352 locations->SetInAt(0, Location::RequiresRegister());
2353 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 locations->AddTemp(Location::RequiresRegister());
2355}
2356
2357void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002358 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 LocationSummary* locations = instruction->GetLocations();
2360 Register obj = locations->InAt(0).AsRegister<Register>();
2361 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002362 Register temp = locations->GetTemp(0).AsRegister<Register>();
2363 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2364 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2365 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2366 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2367 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2368 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2369 const uint32_t object_array_data_offset =
2370 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2371 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002373 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2374 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2375 // read barriers is done for performance and code size reasons.
2376 bool is_type_check_slow_path_fatal = false;
2377 if (!kEmitCompilerReadBarrier) {
2378 is_type_check_slow_path_fatal =
2379 (type_check_kind == TypeCheckKind::kExactCheck ||
2380 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2381 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2382 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2383 !instruction->CanThrowIntoCatchBlock();
2384 }
2385 SlowPathCodeMIPS* slow_path =
2386 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
2387 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002388 codegen_->AddSlowPath(slow_path);
2389
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002390 // Avoid this check if we know `obj` is not null.
2391 if (instruction->MustDoNullCheck()) {
2392 __ Beqz(obj, &done);
2393 }
2394
2395 switch (type_check_kind) {
2396 case TypeCheckKind::kExactCheck:
2397 case TypeCheckKind::kArrayCheck: {
2398 // /* HeapReference<Class> */ temp = obj->klass_
2399 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
2400 __ MaybeUnpoisonHeapReference(temp);
2401 // Jump to slow path for throwing the exception or doing a
2402 // more involved array check.
2403 __ Bne(temp, cls, slow_path->GetEntryLabel());
2404 break;
2405 }
2406
2407 case TypeCheckKind::kAbstractClassCheck: {
2408 // /* HeapReference<Class> */ temp = obj->klass_
2409 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
2410 __ MaybeUnpoisonHeapReference(temp);
2411 // If the class is abstract, we eagerly fetch the super class of the
2412 // object to avoid doing a comparison we know will fail.
2413 MipsLabel loop;
2414 __ Bind(&loop);
2415 // /* HeapReference<Class> */ temp = temp->super_class_
2416 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
2417 __ MaybeUnpoisonHeapReference(temp);
2418 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2419 // exception.
2420 __ Beqz(temp, slow_path->GetEntryLabel());
2421 // Otherwise, compare the classes.
2422 __ Bne(temp, cls, &loop);
2423 break;
2424 }
2425
2426 case TypeCheckKind::kClassHierarchyCheck: {
2427 // /* HeapReference<Class> */ temp = obj->klass_
2428 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
2429 __ MaybeUnpoisonHeapReference(temp);
2430 // Walk over the class hierarchy to find a match.
2431 MipsLabel loop;
2432 __ Bind(&loop);
2433 __ Beq(temp, cls, &done);
2434 // /* HeapReference<Class> */ temp = temp->super_class_
2435 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
2436 __ MaybeUnpoisonHeapReference(temp);
2437 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2438 // exception. Otherwise, jump to the beginning of the loop.
2439 __ Bnez(temp, &loop);
2440 __ B(slow_path->GetEntryLabel());
2441 break;
2442 }
2443
2444 case TypeCheckKind::kArrayObjectCheck: {
2445 // /* HeapReference<Class> */ temp = obj->klass_
2446 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
2447 __ MaybeUnpoisonHeapReference(temp);
2448 // Do an exact check.
2449 __ Beq(temp, cls, &done);
2450 // Otherwise, we need to check that the object's class is a non-primitive array.
2451 // /* HeapReference<Class> */ temp = temp->component_type_
2452 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
2453 __ MaybeUnpoisonHeapReference(temp);
2454 // If the component type is null, jump to the slow path to throw the exception.
2455 __ Beqz(temp, slow_path->GetEntryLabel());
2456 // Otherwise, the object is indeed an array, further check that this component
2457 // type is not a primitive type.
2458 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2459 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2460 __ Bnez(temp, slow_path->GetEntryLabel());
2461 break;
2462 }
2463
2464 case TypeCheckKind::kUnresolvedCheck:
2465 // We always go into the type check slow path for the unresolved check case.
2466 // We cannot directly call the CheckCast runtime entry point
2467 // without resorting to a type checking slow path here (i.e. by
2468 // calling InvokeRuntime directly), as it would require to
2469 // assign fixed registers for the inputs of this HInstanceOf
2470 // instruction (following the runtime calling convention), which
2471 // might be cluttered by the potential first read barrier
2472 // emission at the beginning of this method.
2473 __ B(slow_path->GetEntryLabel());
2474 break;
2475
2476 case TypeCheckKind::kInterfaceCheck: {
2477 // Avoid read barriers to improve performance of the fast path. We can not get false
2478 // positives by doing this.
2479 // /* HeapReference<Class> */ temp = obj->klass_
2480 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
2481 __ MaybeUnpoisonHeapReference(temp);
2482 // /* HeapReference<Class> */ temp = temp->iftable_
2483 __ LoadFromOffset(kLoadWord, temp, temp, iftable_offset);
2484 __ MaybeUnpoisonHeapReference(temp);
2485 // Iftable is never null.
2486 __ Lw(TMP, temp, array_length_offset);
2487 // Loop through the iftable and check if any class matches.
2488 MipsLabel loop;
2489 __ Bind(&loop);
2490 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
2491 __ Beqz(TMP, slow_path->GetEntryLabel());
2492 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
2493 __ MaybeUnpoisonHeapReference(AT);
2494 // Go to next interface.
2495 __ Addiu(TMP, TMP, -2);
2496 // Compare the classes and continue the loop if they do not match.
2497 __ Bne(AT, cls, &loop);
2498 break;
2499 }
2500 }
2501
2502 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002503 __ Bind(slow_path->GetExitLabel());
2504}
2505
2506void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2507 LocationSummary* locations =
2508 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2509 locations->SetInAt(0, Location::RequiresRegister());
2510 if (check->HasUses()) {
2511 locations->SetOut(Location::SameAsFirstInput());
2512 }
2513}
2514
2515void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2516 // We assume the class is not null.
2517 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2518 check->GetLoadClass(),
2519 check,
2520 check->GetDexPc(),
2521 true);
2522 codegen_->AddSlowPath(slow_path);
2523 GenerateClassInitializationCheck(slow_path,
2524 check->GetLocations()->InAt(0).AsRegister<Register>());
2525}
2526
2527void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2528 Primitive::Type in_type = compare->InputAt(0)->GetType();
2529
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002530 LocationSummary* locations =
2531 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002532
2533 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002534 case Primitive::kPrimBoolean:
2535 case Primitive::kPrimByte:
2536 case Primitive::kPrimShort:
2537 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002538 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002539 locations->SetInAt(0, Location::RequiresRegister());
2540 locations->SetInAt(1, Location::RequiresRegister());
2541 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2542 break;
2543
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 case Primitive::kPrimLong:
2545 locations->SetInAt(0, Location::RequiresRegister());
2546 locations->SetInAt(1, Location::RequiresRegister());
2547 // Output overlaps because it is written before doing the low comparison.
2548 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2549 break;
2550
2551 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002552 case Primitive::kPrimDouble:
2553 locations->SetInAt(0, Location::RequiresFpuRegister());
2554 locations->SetInAt(1, Location::RequiresFpuRegister());
2555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002557
2558 default:
2559 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2560 }
2561}
2562
2563void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2564 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002565 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002567 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002568
2569 // 0 if: left == right
2570 // 1 if: left > right
2571 // -1 if: left < right
2572 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002573 case Primitive::kPrimBoolean:
2574 case Primitive::kPrimByte:
2575 case Primitive::kPrimShort:
2576 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002577 case Primitive::kPrimInt: {
2578 Register lhs = locations->InAt(0).AsRegister<Register>();
2579 Register rhs = locations->InAt(1).AsRegister<Register>();
2580 __ Slt(TMP, lhs, rhs);
2581 __ Slt(res, rhs, lhs);
2582 __ Subu(res, res, TMP);
2583 break;
2584 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585 case Primitive::kPrimLong: {
2586 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002587 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2588 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2589 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2590 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2591 // TODO: more efficient (direct) comparison with a constant.
2592 __ Slt(TMP, lhs_high, rhs_high);
2593 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2594 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2595 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2596 __ Sltu(TMP, lhs_low, rhs_low);
2597 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2598 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2599 __ Bind(&done);
2600 break;
2601 }
2602
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002603 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002604 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002605 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2606 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2607 MipsLabel done;
2608 if (isR6) {
2609 __ CmpEqS(FTMP, lhs, rhs);
2610 __ LoadConst32(res, 0);
2611 __ Bc1nez(FTMP, &done);
2612 if (gt_bias) {
2613 __ CmpLtS(FTMP, lhs, rhs);
2614 __ LoadConst32(res, -1);
2615 __ Bc1nez(FTMP, &done);
2616 __ LoadConst32(res, 1);
2617 } else {
2618 __ CmpLtS(FTMP, rhs, lhs);
2619 __ LoadConst32(res, 1);
2620 __ Bc1nez(FTMP, &done);
2621 __ LoadConst32(res, -1);
2622 }
2623 } else {
2624 if (gt_bias) {
2625 __ ColtS(0, lhs, rhs);
2626 __ LoadConst32(res, -1);
2627 __ Bc1t(0, &done);
2628 __ CeqS(0, lhs, rhs);
2629 __ LoadConst32(res, 1);
2630 __ Movt(res, ZERO, 0);
2631 } else {
2632 __ ColtS(0, rhs, lhs);
2633 __ LoadConst32(res, 1);
2634 __ Bc1t(0, &done);
2635 __ CeqS(0, lhs, rhs);
2636 __ LoadConst32(res, -1);
2637 __ Movt(res, ZERO, 0);
2638 }
2639 }
2640 __ Bind(&done);
2641 break;
2642 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002644 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002645 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2646 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2647 MipsLabel done;
2648 if (isR6) {
2649 __ CmpEqD(FTMP, lhs, rhs);
2650 __ LoadConst32(res, 0);
2651 __ Bc1nez(FTMP, &done);
2652 if (gt_bias) {
2653 __ CmpLtD(FTMP, lhs, rhs);
2654 __ LoadConst32(res, -1);
2655 __ Bc1nez(FTMP, &done);
2656 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002657 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002658 __ CmpLtD(FTMP, rhs, lhs);
2659 __ LoadConst32(res, 1);
2660 __ Bc1nez(FTMP, &done);
2661 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002662 }
2663 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002664 if (gt_bias) {
2665 __ ColtD(0, lhs, rhs);
2666 __ LoadConst32(res, -1);
2667 __ Bc1t(0, &done);
2668 __ CeqD(0, lhs, rhs);
2669 __ LoadConst32(res, 1);
2670 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002672 __ ColtD(0, rhs, lhs);
2673 __ LoadConst32(res, 1);
2674 __ Bc1t(0, &done);
2675 __ CeqD(0, lhs, rhs);
2676 __ LoadConst32(res, -1);
2677 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002678 }
2679 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002680 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 break;
2682 }
2683
2684 default:
2685 LOG(FATAL) << "Unimplemented compare type " << in_type;
2686 }
2687}
2688
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002689void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002691 switch (instruction->InputAt(0)->GetType()) {
2692 default:
2693 case Primitive::kPrimLong:
2694 locations->SetInAt(0, Location::RequiresRegister());
2695 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2696 break;
2697
2698 case Primitive::kPrimFloat:
2699 case Primitive::kPrimDouble:
2700 locations->SetInAt(0, Location::RequiresFpuRegister());
2701 locations->SetInAt(1, Location::RequiresFpuRegister());
2702 break;
2703 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002704 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2706 }
2707}
2708
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002709void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002710 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 return;
2712 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002713
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002714 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002715 LocationSummary* locations = instruction->GetLocations();
2716 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002717 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002718
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002719 switch (type) {
2720 default:
2721 // Integer case.
2722 GenerateIntCompare(instruction->GetCondition(), locations);
2723 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002724
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002725 case Primitive::kPrimLong:
2726 // TODO: don't use branches.
2727 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002728 break;
2729
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002730 case Primitive::kPrimFloat:
2731 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002732 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2733 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002734 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002735
2736 // Convert the branches into the result.
2737 MipsLabel done;
2738
2739 // False case: result = 0.
2740 __ LoadConst32(dst, 0);
2741 __ B(&done);
2742
2743 // True case: result = 1.
2744 __ Bind(&true_label);
2745 __ LoadConst32(dst, 1);
2746 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002747}
2748
Alexey Frunze7e99e052015-11-24 19:28:01 -08002749void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2750 DCHECK(instruction->IsDiv() || instruction->IsRem());
2751 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2752
2753 LocationSummary* locations = instruction->GetLocations();
2754 Location second = locations->InAt(1);
2755 DCHECK(second.IsConstant());
2756
2757 Register out = locations->Out().AsRegister<Register>();
2758 Register dividend = locations->InAt(0).AsRegister<Register>();
2759 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2760 DCHECK(imm == 1 || imm == -1);
2761
2762 if (instruction->IsRem()) {
2763 __ Move(out, ZERO);
2764 } else {
2765 if (imm == -1) {
2766 __ Subu(out, ZERO, dividend);
2767 } else if (out != dividend) {
2768 __ Move(out, dividend);
2769 }
2770 }
2771}
2772
2773void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2774 DCHECK(instruction->IsDiv() || instruction->IsRem());
2775 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2776
2777 LocationSummary* locations = instruction->GetLocations();
2778 Location second = locations->InAt(1);
2779 DCHECK(second.IsConstant());
2780
2781 Register out = locations->Out().AsRegister<Register>();
2782 Register dividend = locations->InAt(0).AsRegister<Register>();
2783 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002784 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002785 int ctz_imm = CTZ(abs_imm);
2786
2787 if (instruction->IsDiv()) {
2788 if (ctz_imm == 1) {
2789 // Fast path for division by +/-2, which is very common.
2790 __ Srl(TMP, dividend, 31);
2791 } else {
2792 __ Sra(TMP, dividend, 31);
2793 __ Srl(TMP, TMP, 32 - ctz_imm);
2794 }
2795 __ Addu(out, dividend, TMP);
2796 __ Sra(out, out, ctz_imm);
2797 if (imm < 0) {
2798 __ Subu(out, ZERO, out);
2799 }
2800 } else {
2801 if (ctz_imm == 1) {
2802 // Fast path for modulo +/-2, which is very common.
2803 __ Sra(TMP, dividend, 31);
2804 __ Subu(out, dividend, TMP);
2805 __ Andi(out, out, 1);
2806 __ Addu(out, out, TMP);
2807 } else {
2808 __ Sra(TMP, dividend, 31);
2809 __ Srl(TMP, TMP, 32 - ctz_imm);
2810 __ Addu(out, dividend, TMP);
2811 if (IsUint<16>(abs_imm - 1)) {
2812 __ Andi(out, out, abs_imm - 1);
2813 } else {
2814 __ Sll(out, out, 32 - ctz_imm);
2815 __ Srl(out, out, 32 - ctz_imm);
2816 }
2817 __ Subu(out, out, TMP);
2818 }
2819 }
2820}
2821
2822void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2823 DCHECK(instruction->IsDiv() || instruction->IsRem());
2824 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2825
2826 LocationSummary* locations = instruction->GetLocations();
2827 Location second = locations->InAt(1);
2828 DCHECK(second.IsConstant());
2829
2830 Register out = locations->Out().AsRegister<Register>();
2831 Register dividend = locations->InAt(0).AsRegister<Register>();
2832 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2833
2834 int64_t magic;
2835 int shift;
2836 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2837
2838 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2839
2840 __ LoadConst32(TMP, magic);
2841 if (isR6) {
2842 __ MuhR6(TMP, dividend, TMP);
2843 } else {
2844 __ MultR2(dividend, TMP);
2845 __ Mfhi(TMP);
2846 }
2847 if (imm > 0 && magic < 0) {
2848 __ Addu(TMP, TMP, dividend);
2849 } else if (imm < 0 && magic > 0) {
2850 __ Subu(TMP, TMP, dividend);
2851 }
2852
2853 if (shift != 0) {
2854 __ Sra(TMP, TMP, shift);
2855 }
2856
2857 if (instruction->IsDiv()) {
2858 __ Sra(out, TMP, 31);
2859 __ Subu(out, TMP, out);
2860 } else {
2861 __ Sra(AT, TMP, 31);
2862 __ Subu(AT, TMP, AT);
2863 __ LoadConst32(TMP, imm);
2864 if (isR6) {
2865 __ MulR6(TMP, AT, TMP);
2866 } else {
2867 __ MulR2(TMP, AT, TMP);
2868 }
2869 __ Subu(out, dividend, TMP);
2870 }
2871}
2872
2873void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2874 DCHECK(instruction->IsDiv() || instruction->IsRem());
2875 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2876
2877 LocationSummary* locations = instruction->GetLocations();
2878 Register out = locations->Out().AsRegister<Register>();
2879 Location second = locations->InAt(1);
2880
2881 if (second.IsConstant()) {
2882 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2883 if (imm == 0) {
2884 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2885 } else if (imm == 1 || imm == -1) {
2886 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002887 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002888 DivRemByPowerOfTwo(instruction);
2889 } else {
2890 DCHECK(imm <= -2 || imm >= 2);
2891 GenerateDivRemWithAnyConstant(instruction);
2892 }
2893 } else {
2894 Register dividend = locations->InAt(0).AsRegister<Register>();
2895 Register divisor = second.AsRegister<Register>();
2896 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2897 if (instruction->IsDiv()) {
2898 if (isR6) {
2899 __ DivR6(out, dividend, divisor);
2900 } else {
2901 __ DivR2(out, dividend, divisor);
2902 }
2903 } else {
2904 if (isR6) {
2905 __ ModR6(out, dividend, divisor);
2906 } else {
2907 __ ModR2(out, dividend, divisor);
2908 }
2909 }
2910 }
2911}
2912
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2914 Primitive::Type type = div->GetResultType();
2915 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002916 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002917 : LocationSummary::kNoCall;
2918
2919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2920
2921 switch (type) {
2922 case Primitive::kPrimInt:
2923 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002924 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2926 break;
2927
2928 case Primitive::kPrimLong: {
2929 InvokeRuntimeCallingConvention calling_convention;
2930 locations->SetInAt(0, Location::RegisterPairLocation(
2931 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2932 locations->SetInAt(1, Location::RegisterPairLocation(
2933 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2934 locations->SetOut(calling_convention.GetReturnLocation(type));
2935 break;
2936 }
2937
2938 case Primitive::kPrimFloat:
2939 case Primitive::kPrimDouble:
2940 locations->SetInAt(0, Location::RequiresFpuRegister());
2941 locations->SetInAt(1, Location::RequiresFpuRegister());
2942 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2943 break;
2944
2945 default:
2946 LOG(FATAL) << "Unexpected div type " << type;
2947 }
2948}
2949
2950void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2951 Primitive::Type type = instruction->GetType();
2952 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002953
2954 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002955 case Primitive::kPrimInt:
2956 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002957 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002959 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002960 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2961 break;
2962 }
2963 case Primitive::kPrimFloat:
2964 case Primitive::kPrimDouble: {
2965 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2966 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2967 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2968 if (type == Primitive::kPrimFloat) {
2969 __ DivS(dst, lhs, rhs);
2970 } else {
2971 __ DivD(dst, lhs, rhs);
2972 }
2973 break;
2974 }
2975 default:
2976 LOG(FATAL) << "Unexpected div type " << type;
2977 }
2978}
2979
2980void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002981 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002982 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002983}
2984
2985void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2986 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2987 codegen_->AddSlowPath(slow_path);
2988 Location value = instruction->GetLocations()->InAt(0);
2989 Primitive::Type type = instruction->GetType();
2990
2991 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002992 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002993 case Primitive::kPrimByte:
2994 case Primitive::kPrimChar:
2995 case Primitive::kPrimShort:
2996 case Primitive::kPrimInt: {
2997 if (value.IsConstant()) {
2998 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2999 __ B(slow_path->GetEntryLabel());
3000 } else {
3001 // A division by a non-null constant is valid. We don't need to perform
3002 // any check, so simply fall through.
3003 }
3004 } else {
3005 DCHECK(value.IsRegister()) << value;
3006 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3007 }
3008 break;
3009 }
3010 case Primitive::kPrimLong: {
3011 if (value.IsConstant()) {
3012 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3013 __ B(slow_path->GetEntryLabel());
3014 } else {
3015 // A division by a non-null constant is valid. We don't need to perform
3016 // any check, so simply fall through.
3017 }
3018 } else {
3019 DCHECK(value.IsRegisterPair()) << value;
3020 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3021 __ Beqz(TMP, slow_path->GetEntryLabel());
3022 }
3023 break;
3024 }
3025 default:
3026 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3027 }
3028}
3029
3030void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3031 LocationSummary* locations =
3032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3033 locations->SetOut(Location::ConstantLocation(constant));
3034}
3035
3036void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3037 // Will be generated at use site.
3038}
3039
3040void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3041 exit->SetLocations(nullptr);
3042}
3043
3044void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3045}
3046
3047void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3048 LocationSummary* locations =
3049 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3050 locations->SetOut(Location::ConstantLocation(constant));
3051}
3052
3053void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3054 // Will be generated at use site.
3055}
3056
3057void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3058 got->SetLocations(nullptr);
3059}
3060
3061void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3062 DCHECK(!successor->IsExitBlock());
3063 HBasicBlock* block = got->GetBlock();
3064 HInstruction* previous = got->GetPrevious();
3065 HLoopInformation* info = block->GetLoopInformation();
3066
3067 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3068 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3069 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3070 return;
3071 }
3072 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3073 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3074 }
3075 if (!codegen_->GoesToNextBlock(block, successor)) {
3076 __ B(codegen_->GetLabelOf(successor));
3077 }
3078}
3079
3080void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3081 HandleGoto(got, got->GetSuccessor());
3082}
3083
3084void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3085 try_boundary->SetLocations(nullptr);
3086}
3087
3088void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3089 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3090 if (!successor->IsExitBlock()) {
3091 HandleGoto(try_boundary, successor);
3092 }
3093}
3094
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003095void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3096 LocationSummary* locations) {
3097 Register dst = locations->Out().AsRegister<Register>();
3098 Register lhs = locations->InAt(0).AsRegister<Register>();
3099 Location rhs_location = locations->InAt(1);
3100 Register rhs_reg = ZERO;
3101 int64_t rhs_imm = 0;
3102 bool use_imm = rhs_location.IsConstant();
3103 if (use_imm) {
3104 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3105 } else {
3106 rhs_reg = rhs_location.AsRegister<Register>();
3107 }
3108
3109 switch (cond) {
3110 case kCondEQ:
3111 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003112 if (use_imm && IsInt<16>(-rhs_imm)) {
3113 if (rhs_imm == 0) {
3114 if (cond == kCondEQ) {
3115 __ Sltiu(dst, lhs, 1);
3116 } else {
3117 __ Sltu(dst, ZERO, lhs);
3118 }
3119 } else {
3120 __ Addiu(dst, lhs, -rhs_imm);
3121 if (cond == kCondEQ) {
3122 __ Sltiu(dst, dst, 1);
3123 } else {
3124 __ Sltu(dst, ZERO, dst);
3125 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003126 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003127 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003128 if (use_imm && IsUint<16>(rhs_imm)) {
3129 __ Xori(dst, lhs, rhs_imm);
3130 } else {
3131 if (use_imm) {
3132 rhs_reg = TMP;
3133 __ LoadConst32(rhs_reg, rhs_imm);
3134 }
3135 __ Xor(dst, lhs, rhs_reg);
3136 }
3137 if (cond == kCondEQ) {
3138 __ Sltiu(dst, dst, 1);
3139 } else {
3140 __ Sltu(dst, ZERO, dst);
3141 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003142 }
3143 break;
3144
3145 case kCondLT:
3146 case kCondGE:
3147 if (use_imm && IsInt<16>(rhs_imm)) {
3148 __ Slti(dst, lhs, rhs_imm);
3149 } else {
3150 if (use_imm) {
3151 rhs_reg = TMP;
3152 __ LoadConst32(rhs_reg, rhs_imm);
3153 }
3154 __ Slt(dst, lhs, rhs_reg);
3155 }
3156 if (cond == kCondGE) {
3157 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3158 // only the slt instruction but no sge.
3159 __ Xori(dst, dst, 1);
3160 }
3161 break;
3162
3163 case kCondLE:
3164 case kCondGT:
3165 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3166 // Simulate lhs <= rhs via lhs < rhs + 1.
3167 __ Slti(dst, lhs, rhs_imm + 1);
3168 if (cond == kCondGT) {
3169 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3170 // only the slti instruction but no sgti.
3171 __ Xori(dst, dst, 1);
3172 }
3173 } else {
3174 if (use_imm) {
3175 rhs_reg = TMP;
3176 __ LoadConst32(rhs_reg, rhs_imm);
3177 }
3178 __ Slt(dst, rhs_reg, lhs);
3179 if (cond == kCondLE) {
3180 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3181 // only the slt instruction but no sle.
3182 __ Xori(dst, dst, 1);
3183 }
3184 }
3185 break;
3186
3187 case kCondB:
3188 case kCondAE:
3189 if (use_imm && IsInt<16>(rhs_imm)) {
3190 // Sltiu sign-extends its 16-bit immediate operand before
3191 // the comparison and thus lets us compare directly with
3192 // unsigned values in the ranges [0, 0x7fff] and
3193 // [0xffff8000, 0xffffffff].
3194 __ Sltiu(dst, lhs, rhs_imm);
3195 } else {
3196 if (use_imm) {
3197 rhs_reg = TMP;
3198 __ LoadConst32(rhs_reg, rhs_imm);
3199 }
3200 __ Sltu(dst, lhs, rhs_reg);
3201 }
3202 if (cond == kCondAE) {
3203 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3204 // only the sltu instruction but no sgeu.
3205 __ Xori(dst, dst, 1);
3206 }
3207 break;
3208
3209 case kCondBE:
3210 case kCondA:
3211 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3212 // Simulate lhs <= rhs via lhs < rhs + 1.
3213 // Note that this only works if rhs + 1 does not overflow
3214 // to 0, hence the check above.
3215 // Sltiu sign-extends its 16-bit immediate operand before
3216 // the comparison and thus lets us compare directly with
3217 // unsigned values in the ranges [0, 0x7fff] and
3218 // [0xffff8000, 0xffffffff].
3219 __ Sltiu(dst, lhs, rhs_imm + 1);
3220 if (cond == kCondA) {
3221 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3222 // only the sltiu instruction but no sgtiu.
3223 __ Xori(dst, dst, 1);
3224 }
3225 } else {
3226 if (use_imm) {
3227 rhs_reg = TMP;
3228 __ LoadConst32(rhs_reg, rhs_imm);
3229 }
3230 __ Sltu(dst, rhs_reg, lhs);
3231 if (cond == kCondBE) {
3232 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3233 // only the sltu instruction but no sleu.
3234 __ Xori(dst, dst, 1);
3235 }
3236 }
3237 break;
3238 }
3239}
3240
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003241bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3242 LocationSummary* input_locations,
3243 Register dst) {
3244 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3245 Location rhs_location = input_locations->InAt(1);
3246 Register rhs_reg = ZERO;
3247 int64_t rhs_imm = 0;
3248 bool use_imm = rhs_location.IsConstant();
3249 if (use_imm) {
3250 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3251 } else {
3252 rhs_reg = rhs_location.AsRegister<Register>();
3253 }
3254
3255 switch (cond) {
3256 case kCondEQ:
3257 case kCondNE:
3258 if (use_imm && IsInt<16>(-rhs_imm)) {
3259 __ Addiu(dst, lhs, -rhs_imm);
3260 } else if (use_imm && IsUint<16>(rhs_imm)) {
3261 __ Xori(dst, lhs, rhs_imm);
3262 } else {
3263 if (use_imm) {
3264 rhs_reg = TMP;
3265 __ LoadConst32(rhs_reg, rhs_imm);
3266 }
3267 __ Xor(dst, lhs, rhs_reg);
3268 }
3269 return (cond == kCondEQ);
3270
3271 case kCondLT:
3272 case kCondGE:
3273 if (use_imm && IsInt<16>(rhs_imm)) {
3274 __ Slti(dst, lhs, rhs_imm);
3275 } else {
3276 if (use_imm) {
3277 rhs_reg = TMP;
3278 __ LoadConst32(rhs_reg, rhs_imm);
3279 }
3280 __ Slt(dst, lhs, rhs_reg);
3281 }
3282 return (cond == kCondGE);
3283
3284 case kCondLE:
3285 case kCondGT:
3286 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3287 // Simulate lhs <= rhs via lhs < rhs + 1.
3288 __ Slti(dst, lhs, rhs_imm + 1);
3289 return (cond == kCondGT);
3290 } else {
3291 if (use_imm) {
3292 rhs_reg = TMP;
3293 __ LoadConst32(rhs_reg, rhs_imm);
3294 }
3295 __ Slt(dst, rhs_reg, lhs);
3296 return (cond == kCondLE);
3297 }
3298
3299 case kCondB:
3300 case kCondAE:
3301 if (use_imm && IsInt<16>(rhs_imm)) {
3302 // Sltiu sign-extends its 16-bit immediate operand before
3303 // the comparison and thus lets us compare directly with
3304 // unsigned values in the ranges [0, 0x7fff] and
3305 // [0xffff8000, 0xffffffff].
3306 __ Sltiu(dst, lhs, rhs_imm);
3307 } else {
3308 if (use_imm) {
3309 rhs_reg = TMP;
3310 __ LoadConst32(rhs_reg, rhs_imm);
3311 }
3312 __ Sltu(dst, lhs, rhs_reg);
3313 }
3314 return (cond == kCondAE);
3315
3316 case kCondBE:
3317 case kCondA:
3318 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3319 // Simulate lhs <= rhs via lhs < rhs + 1.
3320 // Note that this only works if rhs + 1 does not overflow
3321 // to 0, hence the check above.
3322 // Sltiu sign-extends its 16-bit immediate operand before
3323 // the comparison and thus lets us compare directly with
3324 // unsigned values in the ranges [0, 0x7fff] and
3325 // [0xffff8000, 0xffffffff].
3326 __ Sltiu(dst, lhs, rhs_imm + 1);
3327 return (cond == kCondA);
3328 } else {
3329 if (use_imm) {
3330 rhs_reg = TMP;
3331 __ LoadConst32(rhs_reg, rhs_imm);
3332 }
3333 __ Sltu(dst, rhs_reg, lhs);
3334 return (cond == kCondBE);
3335 }
3336 }
3337}
3338
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003339void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3340 LocationSummary* locations,
3341 MipsLabel* label) {
3342 Register lhs = locations->InAt(0).AsRegister<Register>();
3343 Location rhs_location = locations->InAt(1);
3344 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003345 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003346 bool use_imm = rhs_location.IsConstant();
3347 if (use_imm) {
3348 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3349 } else {
3350 rhs_reg = rhs_location.AsRegister<Register>();
3351 }
3352
3353 if (use_imm && rhs_imm == 0) {
3354 switch (cond) {
3355 case kCondEQ:
3356 case kCondBE: // <= 0 if zero
3357 __ Beqz(lhs, label);
3358 break;
3359 case kCondNE:
3360 case kCondA: // > 0 if non-zero
3361 __ Bnez(lhs, label);
3362 break;
3363 case kCondLT:
3364 __ Bltz(lhs, label);
3365 break;
3366 case kCondGE:
3367 __ Bgez(lhs, label);
3368 break;
3369 case kCondLE:
3370 __ Blez(lhs, label);
3371 break;
3372 case kCondGT:
3373 __ Bgtz(lhs, label);
3374 break;
3375 case kCondB: // always false
3376 break;
3377 case kCondAE: // always true
3378 __ B(label);
3379 break;
3380 }
3381 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003382 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3383 if (isR6 || !use_imm) {
3384 if (use_imm) {
3385 rhs_reg = TMP;
3386 __ LoadConst32(rhs_reg, rhs_imm);
3387 }
3388 switch (cond) {
3389 case kCondEQ:
3390 __ Beq(lhs, rhs_reg, label);
3391 break;
3392 case kCondNE:
3393 __ Bne(lhs, rhs_reg, label);
3394 break;
3395 case kCondLT:
3396 __ Blt(lhs, rhs_reg, label);
3397 break;
3398 case kCondGE:
3399 __ Bge(lhs, rhs_reg, label);
3400 break;
3401 case kCondLE:
3402 __ Bge(rhs_reg, lhs, label);
3403 break;
3404 case kCondGT:
3405 __ Blt(rhs_reg, lhs, label);
3406 break;
3407 case kCondB:
3408 __ Bltu(lhs, rhs_reg, label);
3409 break;
3410 case kCondAE:
3411 __ Bgeu(lhs, rhs_reg, label);
3412 break;
3413 case kCondBE:
3414 __ Bgeu(rhs_reg, lhs, label);
3415 break;
3416 case kCondA:
3417 __ Bltu(rhs_reg, lhs, label);
3418 break;
3419 }
3420 } else {
3421 // Special cases for more efficient comparison with constants on R2.
3422 switch (cond) {
3423 case kCondEQ:
3424 __ LoadConst32(TMP, rhs_imm);
3425 __ Beq(lhs, TMP, label);
3426 break;
3427 case kCondNE:
3428 __ LoadConst32(TMP, rhs_imm);
3429 __ Bne(lhs, TMP, label);
3430 break;
3431 case kCondLT:
3432 if (IsInt<16>(rhs_imm)) {
3433 __ Slti(TMP, lhs, rhs_imm);
3434 __ Bnez(TMP, label);
3435 } else {
3436 __ LoadConst32(TMP, rhs_imm);
3437 __ Blt(lhs, TMP, label);
3438 }
3439 break;
3440 case kCondGE:
3441 if (IsInt<16>(rhs_imm)) {
3442 __ Slti(TMP, lhs, rhs_imm);
3443 __ Beqz(TMP, label);
3444 } else {
3445 __ LoadConst32(TMP, rhs_imm);
3446 __ Bge(lhs, TMP, label);
3447 }
3448 break;
3449 case kCondLE:
3450 if (IsInt<16>(rhs_imm + 1)) {
3451 // Simulate lhs <= rhs via lhs < rhs + 1.
3452 __ Slti(TMP, lhs, rhs_imm + 1);
3453 __ Bnez(TMP, label);
3454 } else {
3455 __ LoadConst32(TMP, rhs_imm);
3456 __ Bge(TMP, lhs, label);
3457 }
3458 break;
3459 case kCondGT:
3460 if (IsInt<16>(rhs_imm + 1)) {
3461 // Simulate lhs > rhs via !(lhs < rhs + 1).
3462 __ Slti(TMP, lhs, rhs_imm + 1);
3463 __ Beqz(TMP, label);
3464 } else {
3465 __ LoadConst32(TMP, rhs_imm);
3466 __ Blt(TMP, lhs, label);
3467 }
3468 break;
3469 case kCondB:
3470 if (IsInt<16>(rhs_imm)) {
3471 __ Sltiu(TMP, lhs, rhs_imm);
3472 __ Bnez(TMP, label);
3473 } else {
3474 __ LoadConst32(TMP, rhs_imm);
3475 __ Bltu(lhs, TMP, label);
3476 }
3477 break;
3478 case kCondAE:
3479 if (IsInt<16>(rhs_imm)) {
3480 __ Sltiu(TMP, lhs, rhs_imm);
3481 __ Beqz(TMP, label);
3482 } else {
3483 __ LoadConst32(TMP, rhs_imm);
3484 __ Bgeu(lhs, TMP, label);
3485 }
3486 break;
3487 case kCondBE:
3488 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3489 // Simulate lhs <= rhs via lhs < rhs + 1.
3490 // Note that this only works if rhs + 1 does not overflow
3491 // to 0, hence the check above.
3492 __ Sltiu(TMP, lhs, rhs_imm + 1);
3493 __ Bnez(TMP, label);
3494 } else {
3495 __ LoadConst32(TMP, rhs_imm);
3496 __ Bgeu(TMP, lhs, label);
3497 }
3498 break;
3499 case kCondA:
3500 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3501 // Simulate lhs > rhs via !(lhs < rhs + 1).
3502 // Note that this only works if rhs + 1 does not overflow
3503 // to 0, hence the check above.
3504 __ Sltiu(TMP, lhs, rhs_imm + 1);
3505 __ Beqz(TMP, label);
3506 } else {
3507 __ LoadConst32(TMP, rhs_imm);
3508 __ Bltu(TMP, lhs, label);
3509 }
3510 break;
3511 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003512 }
3513 }
3514}
3515
3516void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3517 LocationSummary* locations,
3518 MipsLabel* label) {
3519 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3520 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3521 Location rhs_location = locations->InAt(1);
3522 Register rhs_high = ZERO;
3523 Register rhs_low = ZERO;
3524 int64_t imm = 0;
3525 uint32_t imm_high = 0;
3526 uint32_t imm_low = 0;
3527 bool use_imm = rhs_location.IsConstant();
3528 if (use_imm) {
3529 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3530 imm_high = High32Bits(imm);
3531 imm_low = Low32Bits(imm);
3532 } else {
3533 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3534 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3535 }
3536
3537 if (use_imm && imm == 0) {
3538 switch (cond) {
3539 case kCondEQ:
3540 case kCondBE: // <= 0 if zero
3541 __ Or(TMP, lhs_high, lhs_low);
3542 __ Beqz(TMP, label);
3543 break;
3544 case kCondNE:
3545 case kCondA: // > 0 if non-zero
3546 __ Or(TMP, lhs_high, lhs_low);
3547 __ Bnez(TMP, label);
3548 break;
3549 case kCondLT:
3550 __ Bltz(lhs_high, label);
3551 break;
3552 case kCondGE:
3553 __ Bgez(lhs_high, label);
3554 break;
3555 case kCondLE:
3556 __ Or(TMP, lhs_high, lhs_low);
3557 __ Sra(AT, lhs_high, 31);
3558 __ Bgeu(AT, TMP, label);
3559 break;
3560 case kCondGT:
3561 __ Or(TMP, lhs_high, lhs_low);
3562 __ Sra(AT, lhs_high, 31);
3563 __ Bltu(AT, TMP, label);
3564 break;
3565 case kCondB: // always false
3566 break;
3567 case kCondAE: // always true
3568 __ B(label);
3569 break;
3570 }
3571 } else if (use_imm) {
3572 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3573 switch (cond) {
3574 case kCondEQ:
3575 __ LoadConst32(TMP, imm_high);
3576 __ Xor(TMP, TMP, lhs_high);
3577 __ LoadConst32(AT, imm_low);
3578 __ Xor(AT, AT, lhs_low);
3579 __ Or(TMP, TMP, AT);
3580 __ Beqz(TMP, label);
3581 break;
3582 case kCondNE:
3583 __ LoadConst32(TMP, imm_high);
3584 __ Xor(TMP, TMP, lhs_high);
3585 __ LoadConst32(AT, imm_low);
3586 __ Xor(AT, AT, lhs_low);
3587 __ Or(TMP, TMP, AT);
3588 __ Bnez(TMP, label);
3589 break;
3590 case kCondLT:
3591 __ LoadConst32(TMP, imm_high);
3592 __ Blt(lhs_high, TMP, label);
3593 __ Slt(TMP, TMP, lhs_high);
3594 __ LoadConst32(AT, imm_low);
3595 __ Sltu(AT, lhs_low, AT);
3596 __ Blt(TMP, AT, label);
3597 break;
3598 case kCondGE:
3599 __ LoadConst32(TMP, imm_high);
3600 __ Blt(TMP, lhs_high, label);
3601 __ Slt(TMP, lhs_high, TMP);
3602 __ LoadConst32(AT, imm_low);
3603 __ Sltu(AT, lhs_low, AT);
3604 __ Or(TMP, TMP, AT);
3605 __ Beqz(TMP, label);
3606 break;
3607 case kCondLE:
3608 __ LoadConst32(TMP, imm_high);
3609 __ Blt(lhs_high, TMP, label);
3610 __ Slt(TMP, TMP, lhs_high);
3611 __ LoadConst32(AT, imm_low);
3612 __ Sltu(AT, AT, lhs_low);
3613 __ Or(TMP, TMP, AT);
3614 __ Beqz(TMP, label);
3615 break;
3616 case kCondGT:
3617 __ LoadConst32(TMP, imm_high);
3618 __ Blt(TMP, lhs_high, label);
3619 __ Slt(TMP, lhs_high, TMP);
3620 __ LoadConst32(AT, imm_low);
3621 __ Sltu(AT, AT, lhs_low);
3622 __ Blt(TMP, AT, label);
3623 break;
3624 case kCondB:
3625 __ LoadConst32(TMP, imm_high);
3626 __ Bltu(lhs_high, TMP, label);
3627 __ Sltu(TMP, TMP, lhs_high);
3628 __ LoadConst32(AT, imm_low);
3629 __ Sltu(AT, lhs_low, AT);
3630 __ Blt(TMP, AT, label);
3631 break;
3632 case kCondAE:
3633 __ LoadConst32(TMP, imm_high);
3634 __ Bltu(TMP, lhs_high, label);
3635 __ Sltu(TMP, lhs_high, TMP);
3636 __ LoadConst32(AT, imm_low);
3637 __ Sltu(AT, lhs_low, AT);
3638 __ Or(TMP, TMP, AT);
3639 __ Beqz(TMP, label);
3640 break;
3641 case kCondBE:
3642 __ LoadConst32(TMP, imm_high);
3643 __ Bltu(lhs_high, TMP, label);
3644 __ Sltu(TMP, TMP, lhs_high);
3645 __ LoadConst32(AT, imm_low);
3646 __ Sltu(AT, AT, lhs_low);
3647 __ Or(TMP, TMP, AT);
3648 __ Beqz(TMP, label);
3649 break;
3650 case kCondA:
3651 __ LoadConst32(TMP, imm_high);
3652 __ Bltu(TMP, lhs_high, label);
3653 __ Sltu(TMP, lhs_high, TMP);
3654 __ LoadConst32(AT, imm_low);
3655 __ Sltu(AT, AT, lhs_low);
3656 __ Blt(TMP, AT, label);
3657 break;
3658 }
3659 } else {
3660 switch (cond) {
3661 case kCondEQ:
3662 __ Xor(TMP, lhs_high, rhs_high);
3663 __ Xor(AT, lhs_low, rhs_low);
3664 __ Or(TMP, TMP, AT);
3665 __ Beqz(TMP, label);
3666 break;
3667 case kCondNE:
3668 __ Xor(TMP, lhs_high, rhs_high);
3669 __ Xor(AT, lhs_low, rhs_low);
3670 __ Or(TMP, TMP, AT);
3671 __ Bnez(TMP, label);
3672 break;
3673 case kCondLT:
3674 __ Blt(lhs_high, rhs_high, label);
3675 __ Slt(TMP, rhs_high, lhs_high);
3676 __ Sltu(AT, lhs_low, rhs_low);
3677 __ Blt(TMP, AT, label);
3678 break;
3679 case kCondGE:
3680 __ Blt(rhs_high, lhs_high, label);
3681 __ Slt(TMP, lhs_high, rhs_high);
3682 __ Sltu(AT, lhs_low, rhs_low);
3683 __ Or(TMP, TMP, AT);
3684 __ Beqz(TMP, label);
3685 break;
3686 case kCondLE:
3687 __ Blt(lhs_high, rhs_high, label);
3688 __ Slt(TMP, rhs_high, lhs_high);
3689 __ Sltu(AT, rhs_low, lhs_low);
3690 __ Or(TMP, TMP, AT);
3691 __ Beqz(TMP, label);
3692 break;
3693 case kCondGT:
3694 __ Blt(rhs_high, lhs_high, label);
3695 __ Slt(TMP, lhs_high, rhs_high);
3696 __ Sltu(AT, rhs_low, lhs_low);
3697 __ Blt(TMP, AT, label);
3698 break;
3699 case kCondB:
3700 __ Bltu(lhs_high, rhs_high, label);
3701 __ Sltu(TMP, rhs_high, lhs_high);
3702 __ Sltu(AT, lhs_low, rhs_low);
3703 __ Blt(TMP, AT, label);
3704 break;
3705 case kCondAE:
3706 __ Bltu(rhs_high, lhs_high, label);
3707 __ Sltu(TMP, lhs_high, rhs_high);
3708 __ Sltu(AT, lhs_low, rhs_low);
3709 __ Or(TMP, TMP, AT);
3710 __ Beqz(TMP, label);
3711 break;
3712 case kCondBE:
3713 __ Bltu(lhs_high, rhs_high, label);
3714 __ Sltu(TMP, rhs_high, lhs_high);
3715 __ Sltu(AT, rhs_low, lhs_low);
3716 __ Or(TMP, TMP, AT);
3717 __ Beqz(TMP, label);
3718 break;
3719 case kCondA:
3720 __ Bltu(rhs_high, lhs_high, label);
3721 __ Sltu(TMP, lhs_high, rhs_high);
3722 __ Sltu(AT, rhs_low, lhs_low);
3723 __ Blt(TMP, AT, label);
3724 break;
3725 }
3726 }
3727}
3728
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003729void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3730 bool gt_bias,
3731 Primitive::Type type,
3732 LocationSummary* locations) {
3733 Register dst = locations->Out().AsRegister<Register>();
3734 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3735 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3736 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3737 if (type == Primitive::kPrimFloat) {
3738 if (isR6) {
3739 switch (cond) {
3740 case kCondEQ:
3741 __ CmpEqS(FTMP, lhs, rhs);
3742 __ Mfc1(dst, FTMP);
3743 __ Andi(dst, dst, 1);
3744 break;
3745 case kCondNE:
3746 __ CmpEqS(FTMP, lhs, rhs);
3747 __ Mfc1(dst, FTMP);
3748 __ Addiu(dst, dst, 1);
3749 break;
3750 case kCondLT:
3751 if (gt_bias) {
3752 __ CmpLtS(FTMP, lhs, rhs);
3753 } else {
3754 __ CmpUltS(FTMP, lhs, rhs);
3755 }
3756 __ Mfc1(dst, FTMP);
3757 __ Andi(dst, dst, 1);
3758 break;
3759 case kCondLE:
3760 if (gt_bias) {
3761 __ CmpLeS(FTMP, lhs, rhs);
3762 } else {
3763 __ CmpUleS(FTMP, lhs, rhs);
3764 }
3765 __ Mfc1(dst, FTMP);
3766 __ Andi(dst, dst, 1);
3767 break;
3768 case kCondGT:
3769 if (gt_bias) {
3770 __ CmpUltS(FTMP, rhs, lhs);
3771 } else {
3772 __ CmpLtS(FTMP, rhs, lhs);
3773 }
3774 __ Mfc1(dst, FTMP);
3775 __ Andi(dst, dst, 1);
3776 break;
3777 case kCondGE:
3778 if (gt_bias) {
3779 __ CmpUleS(FTMP, rhs, lhs);
3780 } else {
3781 __ CmpLeS(FTMP, rhs, lhs);
3782 }
3783 __ Mfc1(dst, FTMP);
3784 __ Andi(dst, dst, 1);
3785 break;
3786 default:
3787 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3788 UNREACHABLE();
3789 }
3790 } else {
3791 switch (cond) {
3792 case kCondEQ:
3793 __ CeqS(0, lhs, rhs);
3794 __ LoadConst32(dst, 1);
3795 __ Movf(dst, ZERO, 0);
3796 break;
3797 case kCondNE:
3798 __ CeqS(0, lhs, rhs);
3799 __ LoadConst32(dst, 1);
3800 __ Movt(dst, ZERO, 0);
3801 break;
3802 case kCondLT:
3803 if (gt_bias) {
3804 __ ColtS(0, lhs, rhs);
3805 } else {
3806 __ CultS(0, lhs, rhs);
3807 }
3808 __ LoadConst32(dst, 1);
3809 __ Movf(dst, ZERO, 0);
3810 break;
3811 case kCondLE:
3812 if (gt_bias) {
3813 __ ColeS(0, lhs, rhs);
3814 } else {
3815 __ CuleS(0, lhs, rhs);
3816 }
3817 __ LoadConst32(dst, 1);
3818 __ Movf(dst, ZERO, 0);
3819 break;
3820 case kCondGT:
3821 if (gt_bias) {
3822 __ CultS(0, rhs, lhs);
3823 } else {
3824 __ ColtS(0, rhs, lhs);
3825 }
3826 __ LoadConst32(dst, 1);
3827 __ Movf(dst, ZERO, 0);
3828 break;
3829 case kCondGE:
3830 if (gt_bias) {
3831 __ CuleS(0, rhs, lhs);
3832 } else {
3833 __ ColeS(0, rhs, lhs);
3834 }
3835 __ LoadConst32(dst, 1);
3836 __ Movf(dst, ZERO, 0);
3837 break;
3838 default:
3839 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3840 UNREACHABLE();
3841 }
3842 }
3843 } else {
3844 DCHECK_EQ(type, Primitive::kPrimDouble);
3845 if (isR6) {
3846 switch (cond) {
3847 case kCondEQ:
3848 __ CmpEqD(FTMP, lhs, rhs);
3849 __ Mfc1(dst, FTMP);
3850 __ Andi(dst, dst, 1);
3851 break;
3852 case kCondNE:
3853 __ CmpEqD(FTMP, lhs, rhs);
3854 __ Mfc1(dst, FTMP);
3855 __ Addiu(dst, dst, 1);
3856 break;
3857 case kCondLT:
3858 if (gt_bias) {
3859 __ CmpLtD(FTMP, lhs, rhs);
3860 } else {
3861 __ CmpUltD(FTMP, lhs, rhs);
3862 }
3863 __ Mfc1(dst, FTMP);
3864 __ Andi(dst, dst, 1);
3865 break;
3866 case kCondLE:
3867 if (gt_bias) {
3868 __ CmpLeD(FTMP, lhs, rhs);
3869 } else {
3870 __ CmpUleD(FTMP, lhs, rhs);
3871 }
3872 __ Mfc1(dst, FTMP);
3873 __ Andi(dst, dst, 1);
3874 break;
3875 case kCondGT:
3876 if (gt_bias) {
3877 __ CmpUltD(FTMP, rhs, lhs);
3878 } else {
3879 __ CmpLtD(FTMP, rhs, lhs);
3880 }
3881 __ Mfc1(dst, FTMP);
3882 __ Andi(dst, dst, 1);
3883 break;
3884 case kCondGE:
3885 if (gt_bias) {
3886 __ CmpUleD(FTMP, rhs, lhs);
3887 } else {
3888 __ CmpLeD(FTMP, rhs, lhs);
3889 }
3890 __ Mfc1(dst, FTMP);
3891 __ Andi(dst, dst, 1);
3892 break;
3893 default:
3894 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3895 UNREACHABLE();
3896 }
3897 } else {
3898 switch (cond) {
3899 case kCondEQ:
3900 __ CeqD(0, lhs, rhs);
3901 __ LoadConst32(dst, 1);
3902 __ Movf(dst, ZERO, 0);
3903 break;
3904 case kCondNE:
3905 __ CeqD(0, lhs, rhs);
3906 __ LoadConst32(dst, 1);
3907 __ Movt(dst, ZERO, 0);
3908 break;
3909 case kCondLT:
3910 if (gt_bias) {
3911 __ ColtD(0, lhs, rhs);
3912 } else {
3913 __ CultD(0, lhs, rhs);
3914 }
3915 __ LoadConst32(dst, 1);
3916 __ Movf(dst, ZERO, 0);
3917 break;
3918 case kCondLE:
3919 if (gt_bias) {
3920 __ ColeD(0, lhs, rhs);
3921 } else {
3922 __ CuleD(0, lhs, rhs);
3923 }
3924 __ LoadConst32(dst, 1);
3925 __ Movf(dst, ZERO, 0);
3926 break;
3927 case kCondGT:
3928 if (gt_bias) {
3929 __ CultD(0, rhs, lhs);
3930 } else {
3931 __ ColtD(0, rhs, lhs);
3932 }
3933 __ LoadConst32(dst, 1);
3934 __ Movf(dst, ZERO, 0);
3935 break;
3936 case kCondGE:
3937 if (gt_bias) {
3938 __ CuleD(0, rhs, lhs);
3939 } else {
3940 __ ColeD(0, rhs, lhs);
3941 }
3942 __ LoadConst32(dst, 1);
3943 __ Movf(dst, ZERO, 0);
3944 break;
3945 default:
3946 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3947 UNREACHABLE();
3948 }
3949 }
3950 }
3951}
3952
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003953bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3954 bool gt_bias,
3955 Primitive::Type type,
3956 LocationSummary* input_locations,
3957 int cc) {
3958 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3959 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3960 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3961 if (type == Primitive::kPrimFloat) {
3962 switch (cond) {
3963 case kCondEQ:
3964 __ CeqS(cc, lhs, rhs);
3965 return false;
3966 case kCondNE:
3967 __ CeqS(cc, lhs, rhs);
3968 return true;
3969 case kCondLT:
3970 if (gt_bias) {
3971 __ ColtS(cc, lhs, rhs);
3972 } else {
3973 __ CultS(cc, lhs, rhs);
3974 }
3975 return false;
3976 case kCondLE:
3977 if (gt_bias) {
3978 __ ColeS(cc, lhs, rhs);
3979 } else {
3980 __ CuleS(cc, lhs, rhs);
3981 }
3982 return false;
3983 case kCondGT:
3984 if (gt_bias) {
3985 __ CultS(cc, rhs, lhs);
3986 } else {
3987 __ ColtS(cc, rhs, lhs);
3988 }
3989 return false;
3990 case kCondGE:
3991 if (gt_bias) {
3992 __ CuleS(cc, rhs, lhs);
3993 } else {
3994 __ ColeS(cc, rhs, lhs);
3995 }
3996 return false;
3997 default:
3998 LOG(FATAL) << "Unexpected non-floating-point condition";
3999 UNREACHABLE();
4000 }
4001 } else {
4002 DCHECK_EQ(type, Primitive::kPrimDouble);
4003 switch (cond) {
4004 case kCondEQ:
4005 __ CeqD(cc, lhs, rhs);
4006 return false;
4007 case kCondNE:
4008 __ CeqD(cc, lhs, rhs);
4009 return true;
4010 case kCondLT:
4011 if (gt_bias) {
4012 __ ColtD(cc, lhs, rhs);
4013 } else {
4014 __ CultD(cc, lhs, rhs);
4015 }
4016 return false;
4017 case kCondLE:
4018 if (gt_bias) {
4019 __ ColeD(cc, lhs, rhs);
4020 } else {
4021 __ CuleD(cc, lhs, rhs);
4022 }
4023 return false;
4024 case kCondGT:
4025 if (gt_bias) {
4026 __ CultD(cc, rhs, lhs);
4027 } else {
4028 __ ColtD(cc, rhs, lhs);
4029 }
4030 return false;
4031 case kCondGE:
4032 if (gt_bias) {
4033 __ CuleD(cc, rhs, lhs);
4034 } else {
4035 __ ColeD(cc, rhs, lhs);
4036 }
4037 return false;
4038 default:
4039 LOG(FATAL) << "Unexpected non-floating-point condition";
4040 UNREACHABLE();
4041 }
4042 }
4043}
4044
4045bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4046 bool gt_bias,
4047 Primitive::Type type,
4048 LocationSummary* input_locations,
4049 FRegister dst) {
4050 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4051 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4052 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4053 if (type == Primitive::kPrimFloat) {
4054 switch (cond) {
4055 case kCondEQ:
4056 __ CmpEqS(dst, lhs, rhs);
4057 return false;
4058 case kCondNE:
4059 __ CmpEqS(dst, lhs, rhs);
4060 return true;
4061 case kCondLT:
4062 if (gt_bias) {
4063 __ CmpLtS(dst, lhs, rhs);
4064 } else {
4065 __ CmpUltS(dst, lhs, rhs);
4066 }
4067 return false;
4068 case kCondLE:
4069 if (gt_bias) {
4070 __ CmpLeS(dst, lhs, rhs);
4071 } else {
4072 __ CmpUleS(dst, lhs, rhs);
4073 }
4074 return false;
4075 case kCondGT:
4076 if (gt_bias) {
4077 __ CmpUltS(dst, rhs, lhs);
4078 } else {
4079 __ CmpLtS(dst, rhs, lhs);
4080 }
4081 return false;
4082 case kCondGE:
4083 if (gt_bias) {
4084 __ CmpUleS(dst, rhs, lhs);
4085 } else {
4086 __ CmpLeS(dst, rhs, lhs);
4087 }
4088 return false;
4089 default:
4090 LOG(FATAL) << "Unexpected non-floating-point condition";
4091 UNREACHABLE();
4092 }
4093 } else {
4094 DCHECK_EQ(type, Primitive::kPrimDouble);
4095 switch (cond) {
4096 case kCondEQ:
4097 __ CmpEqD(dst, lhs, rhs);
4098 return false;
4099 case kCondNE:
4100 __ CmpEqD(dst, lhs, rhs);
4101 return true;
4102 case kCondLT:
4103 if (gt_bias) {
4104 __ CmpLtD(dst, lhs, rhs);
4105 } else {
4106 __ CmpUltD(dst, lhs, rhs);
4107 }
4108 return false;
4109 case kCondLE:
4110 if (gt_bias) {
4111 __ CmpLeD(dst, lhs, rhs);
4112 } else {
4113 __ CmpUleD(dst, lhs, rhs);
4114 }
4115 return false;
4116 case kCondGT:
4117 if (gt_bias) {
4118 __ CmpUltD(dst, rhs, lhs);
4119 } else {
4120 __ CmpLtD(dst, rhs, lhs);
4121 }
4122 return false;
4123 case kCondGE:
4124 if (gt_bias) {
4125 __ CmpUleD(dst, rhs, lhs);
4126 } else {
4127 __ CmpLeD(dst, rhs, lhs);
4128 }
4129 return false;
4130 default:
4131 LOG(FATAL) << "Unexpected non-floating-point condition";
4132 UNREACHABLE();
4133 }
4134 }
4135}
4136
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004137void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
4138 bool gt_bias,
4139 Primitive::Type type,
4140 LocationSummary* locations,
4141 MipsLabel* label) {
4142 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4143 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4144 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4145 if (type == Primitive::kPrimFloat) {
4146 if (isR6) {
4147 switch (cond) {
4148 case kCondEQ:
4149 __ CmpEqS(FTMP, lhs, rhs);
4150 __ Bc1nez(FTMP, label);
4151 break;
4152 case kCondNE:
4153 __ CmpEqS(FTMP, lhs, rhs);
4154 __ Bc1eqz(FTMP, label);
4155 break;
4156 case kCondLT:
4157 if (gt_bias) {
4158 __ CmpLtS(FTMP, lhs, rhs);
4159 } else {
4160 __ CmpUltS(FTMP, lhs, rhs);
4161 }
4162 __ Bc1nez(FTMP, label);
4163 break;
4164 case kCondLE:
4165 if (gt_bias) {
4166 __ CmpLeS(FTMP, lhs, rhs);
4167 } else {
4168 __ CmpUleS(FTMP, lhs, rhs);
4169 }
4170 __ Bc1nez(FTMP, label);
4171 break;
4172 case kCondGT:
4173 if (gt_bias) {
4174 __ CmpUltS(FTMP, rhs, lhs);
4175 } else {
4176 __ CmpLtS(FTMP, rhs, lhs);
4177 }
4178 __ Bc1nez(FTMP, label);
4179 break;
4180 case kCondGE:
4181 if (gt_bias) {
4182 __ CmpUleS(FTMP, rhs, lhs);
4183 } else {
4184 __ CmpLeS(FTMP, rhs, lhs);
4185 }
4186 __ Bc1nez(FTMP, label);
4187 break;
4188 default:
4189 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004190 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004191 }
4192 } else {
4193 switch (cond) {
4194 case kCondEQ:
4195 __ CeqS(0, lhs, rhs);
4196 __ Bc1t(0, label);
4197 break;
4198 case kCondNE:
4199 __ CeqS(0, lhs, rhs);
4200 __ Bc1f(0, label);
4201 break;
4202 case kCondLT:
4203 if (gt_bias) {
4204 __ ColtS(0, lhs, rhs);
4205 } else {
4206 __ CultS(0, lhs, rhs);
4207 }
4208 __ Bc1t(0, label);
4209 break;
4210 case kCondLE:
4211 if (gt_bias) {
4212 __ ColeS(0, lhs, rhs);
4213 } else {
4214 __ CuleS(0, lhs, rhs);
4215 }
4216 __ Bc1t(0, label);
4217 break;
4218 case kCondGT:
4219 if (gt_bias) {
4220 __ CultS(0, rhs, lhs);
4221 } else {
4222 __ ColtS(0, rhs, lhs);
4223 }
4224 __ Bc1t(0, label);
4225 break;
4226 case kCondGE:
4227 if (gt_bias) {
4228 __ CuleS(0, rhs, lhs);
4229 } else {
4230 __ ColeS(0, rhs, lhs);
4231 }
4232 __ Bc1t(0, label);
4233 break;
4234 default:
4235 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004236 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004237 }
4238 }
4239 } else {
4240 DCHECK_EQ(type, Primitive::kPrimDouble);
4241 if (isR6) {
4242 switch (cond) {
4243 case kCondEQ:
4244 __ CmpEqD(FTMP, lhs, rhs);
4245 __ Bc1nez(FTMP, label);
4246 break;
4247 case kCondNE:
4248 __ CmpEqD(FTMP, lhs, rhs);
4249 __ Bc1eqz(FTMP, label);
4250 break;
4251 case kCondLT:
4252 if (gt_bias) {
4253 __ CmpLtD(FTMP, lhs, rhs);
4254 } else {
4255 __ CmpUltD(FTMP, lhs, rhs);
4256 }
4257 __ Bc1nez(FTMP, label);
4258 break;
4259 case kCondLE:
4260 if (gt_bias) {
4261 __ CmpLeD(FTMP, lhs, rhs);
4262 } else {
4263 __ CmpUleD(FTMP, lhs, rhs);
4264 }
4265 __ Bc1nez(FTMP, label);
4266 break;
4267 case kCondGT:
4268 if (gt_bias) {
4269 __ CmpUltD(FTMP, rhs, lhs);
4270 } else {
4271 __ CmpLtD(FTMP, rhs, lhs);
4272 }
4273 __ Bc1nez(FTMP, label);
4274 break;
4275 case kCondGE:
4276 if (gt_bias) {
4277 __ CmpUleD(FTMP, rhs, lhs);
4278 } else {
4279 __ CmpLeD(FTMP, rhs, lhs);
4280 }
4281 __ Bc1nez(FTMP, label);
4282 break;
4283 default:
4284 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004285 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004286 }
4287 } else {
4288 switch (cond) {
4289 case kCondEQ:
4290 __ CeqD(0, lhs, rhs);
4291 __ Bc1t(0, label);
4292 break;
4293 case kCondNE:
4294 __ CeqD(0, lhs, rhs);
4295 __ Bc1f(0, label);
4296 break;
4297 case kCondLT:
4298 if (gt_bias) {
4299 __ ColtD(0, lhs, rhs);
4300 } else {
4301 __ CultD(0, lhs, rhs);
4302 }
4303 __ Bc1t(0, label);
4304 break;
4305 case kCondLE:
4306 if (gt_bias) {
4307 __ ColeD(0, lhs, rhs);
4308 } else {
4309 __ CuleD(0, lhs, rhs);
4310 }
4311 __ Bc1t(0, label);
4312 break;
4313 case kCondGT:
4314 if (gt_bias) {
4315 __ CultD(0, rhs, lhs);
4316 } else {
4317 __ ColtD(0, rhs, lhs);
4318 }
4319 __ Bc1t(0, label);
4320 break;
4321 case kCondGE:
4322 if (gt_bias) {
4323 __ CuleD(0, rhs, lhs);
4324 } else {
4325 __ ColeD(0, rhs, lhs);
4326 }
4327 __ Bc1t(0, label);
4328 break;
4329 default:
4330 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004331 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004332 }
4333 }
4334 }
4335}
4336
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004337void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004338 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004339 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004340 MipsLabel* false_target) {
4341 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004342
David Brazdil0debae72015-11-12 18:37:00 +00004343 if (true_target == nullptr && false_target == nullptr) {
4344 // Nothing to do. The code always falls through.
4345 return;
4346 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004347 // Constant condition, statically compared against "true" (integer value 1).
4348 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004349 if (true_target != nullptr) {
4350 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004351 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004352 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004353 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004354 if (false_target != nullptr) {
4355 __ B(false_target);
4356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004357 }
David Brazdil0debae72015-11-12 18:37:00 +00004358 return;
4359 }
4360
4361 // The following code generates these patterns:
4362 // (1) true_target == nullptr && false_target != nullptr
4363 // - opposite condition true => branch to false_target
4364 // (2) true_target != nullptr && false_target == nullptr
4365 // - condition true => branch to true_target
4366 // (3) true_target != nullptr && false_target != nullptr
4367 // - condition true => branch to true_target
4368 // - branch to false_target
4369 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004370 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004371 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004372 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004373 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004374 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4375 } else {
4376 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4377 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004378 } else {
4379 // The condition instruction has not been materialized, use its inputs as
4380 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004381 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004382 Primitive::Type type = condition->InputAt(0)->GetType();
4383 LocationSummary* locations = cond->GetLocations();
4384 IfCondition if_cond = condition->GetCondition();
4385 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004386
David Brazdil0debae72015-11-12 18:37:00 +00004387 if (true_target == nullptr) {
4388 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004389 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004390 }
4391
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004392 switch (type) {
4393 default:
4394 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4395 break;
4396 case Primitive::kPrimLong:
4397 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4398 break;
4399 case Primitive::kPrimFloat:
4400 case Primitive::kPrimDouble:
4401 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4402 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004403 }
4404 }
David Brazdil0debae72015-11-12 18:37:00 +00004405
4406 // If neither branch falls through (case 3), the conditional branch to `true_target`
4407 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4408 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004409 __ B(false_target);
4410 }
4411}
4412
4413void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004415 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004416 locations->SetInAt(0, Location::RequiresRegister());
4417 }
4418}
4419
4420void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004421 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4422 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4423 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4424 nullptr : codegen_->GetLabelOf(true_successor);
4425 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4426 nullptr : codegen_->GetLabelOf(false_successor);
4427 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004428}
4429
4430void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4431 LocationSummary* locations = new (GetGraph()->GetArena())
4432 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004433 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004434 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004435 locations->SetInAt(0, Location::RequiresRegister());
4436 }
4437}
4438
4439void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004440 SlowPathCodeMIPS* slow_path =
4441 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004442 GenerateTestAndBranch(deoptimize,
4443 /* condition_input_index */ 0,
4444 slow_path->GetEntryLabel(),
4445 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004446}
4447
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004448// This function returns true if a conditional move can be generated for HSelect.
4449// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4450// branches and regular moves.
4451//
4452// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4453//
4454// While determining feasibility of a conditional move and setting inputs/outputs
4455// are two distinct tasks, this function does both because they share quite a bit
4456// of common logic.
4457static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4458 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4459 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4460 HCondition* condition = cond->AsCondition();
4461
4462 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4463 Primitive::Type dst_type = select->GetType();
4464
4465 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4466 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4467 bool is_true_value_zero_constant =
4468 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4469 bool is_false_value_zero_constant =
4470 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4471
4472 bool can_move_conditionally = false;
4473 bool use_const_for_false_in = false;
4474 bool use_const_for_true_in = false;
4475
4476 if (!cond->IsConstant()) {
4477 switch (cond_type) {
4478 default:
4479 switch (dst_type) {
4480 default:
4481 // Moving int on int condition.
4482 if (is_r6) {
4483 if (is_true_value_zero_constant) {
4484 // seleqz out_reg, false_reg, cond_reg
4485 can_move_conditionally = true;
4486 use_const_for_true_in = true;
4487 } else if (is_false_value_zero_constant) {
4488 // selnez out_reg, true_reg, cond_reg
4489 can_move_conditionally = true;
4490 use_const_for_false_in = true;
4491 } else if (materialized) {
4492 // Not materializing unmaterialized int conditions
4493 // to keep the instruction count low.
4494 // selnez AT, true_reg, cond_reg
4495 // seleqz TMP, false_reg, cond_reg
4496 // or out_reg, AT, TMP
4497 can_move_conditionally = true;
4498 }
4499 } else {
4500 // movn out_reg, true_reg/ZERO, cond_reg
4501 can_move_conditionally = true;
4502 use_const_for_true_in = is_true_value_zero_constant;
4503 }
4504 break;
4505 case Primitive::kPrimLong:
4506 // Moving long on int condition.
4507 if (is_r6) {
4508 if (is_true_value_zero_constant) {
4509 // seleqz out_reg_lo, false_reg_lo, cond_reg
4510 // seleqz out_reg_hi, false_reg_hi, cond_reg
4511 can_move_conditionally = true;
4512 use_const_for_true_in = true;
4513 } else if (is_false_value_zero_constant) {
4514 // selnez out_reg_lo, true_reg_lo, cond_reg
4515 // selnez out_reg_hi, true_reg_hi, cond_reg
4516 can_move_conditionally = true;
4517 use_const_for_false_in = true;
4518 }
4519 // Other long conditional moves would generate 6+ instructions,
4520 // which is too many.
4521 } else {
4522 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4523 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4524 can_move_conditionally = true;
4525 use_const_for_true_in = is_true_value_zero_constant;
4526 }
4527 break;
4528 case Primitive::kPrimFloat:
4529 case Primitive::kPrimDouble:
4530 // Moving float/double on int condition.
4531 if (is_r6) {
4532 if (materialized) {
4533 // Not materializing unmaterialized int conditions
4534 // to keep the instruction count low.
4535 can_move_conditionally = true;
4536 if (is_true_value_zero_constant) {
4537 // sltu TMP, ZERO, cond_reg
4538 // mtc1 TMP, temp_cond_reg
4539 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4540 use_const_for_true_in = true;
4541 } else if (is_false_value_zero_constant) {
4542 // sltu TMP, ZERO, cond_reg
4543 // mtc1 TMP, temp_cond_reg
4544 // selnez.fmt out_reg, true_reg, temp_cond_reg
4545 use_const_for_false_in = true;
4546 } else {
4547 // sltu TMP, ZERO, cond_reg
4548 // mtc1 TMP, temp_cond_reg
4549 // sel.fmt temp_cond_reg, false_reg, true_reg
4550 // mov.fmt out_reg, temp_cond_reg
4551 }
4552 }
4553 } else {
4554 // movn.fmt out_reg, true_reg, cond_reg
4555 can_move_conditionally = true;
4556 }
4557 break;
4558 }
4559 break;
4560 case Primitive::kPrimLong:
4561 // We don't materialize long comparison now
4562 // and use conditional branches instead.
4563 break;
4564 case Primitive::kPrimFloat:
4565 case Primitive::kPrimDouble:
4566 switch (dst_type) {
4567 default:
4568 // Moving int on float/double condition.
4569 if (is_r6) {
4570 if (is_true_value_zero_constant) {
4571 // mfc1 TMP, temp_cond_reg
4572 // seleqz out_reg, false_reg, TMP
4573 can_move_conditionally = true;
4574 use_const_for_true_in = true;
4575 } else if (is_false_value_zero_constant) {
4576 // mfc1 TMP, temp_cond_reg
4577 // selnez out_reg, true_reg, TMP
4578 can_move_conditionally = true;
4579 use_const_for_false_in = true;
4580 } else {
4581 // mfc1 TMP, temp_cond_reg
4582 // selnez AT, true_reg, TMP
4583 // seleqz TMP, false_reg, TMP
4584 // or out_reg, AT, TMP
4585 can_move_conditionally = true;
4586 }
4587 } else {
4588 // movt out_reg, true_reg/ZERO, cc
4589 can_move_conditionally = true;
4590 use_const_for_true_in = is_true_value_zero_constant;
4591 }
4592 break;
4593 case Primitive::kPrimLong:
4594 // Moving long on float/double condition.
4595 if (is_r6) {
4596 if (is_true_value_zero_constant) {
4597 // mfc1 TMP, temp_cond_reg
4598 // seleqz out_reg_lo, false_reg_lo, TMP
4599 // seleqz out_reg_hi, false_reg_hi, TMP
4600 can_move_conditionally = true;
4601 use_const_for_true_in = true;
4602 } else if (is_false_value_zero_constant) {
4603 // mfc1 TMP, temp_cond_reg
4604 // selnez out_reg_lo, true_reg_lo, TMP
4605 // selnez out_reg_hi, true_reg_hi, TMP
4606 can_move_conditionally = true;
4607 use_const_for_false_in = true;
4608 }
4609 // Other long conditional moves would generate 6+ instructions,
4610 // which is too many.
4611 } else {
4612 // movt out_reg_lo, true_reg_lo/ZERO, cc
4613 // movt out_reg_hi, true_reg_hi/ZERO, cc
4614 can_move_conditionally = true;
4615 use_const_for_true_in = is_true_value_zero_constant;
4616 }
4617 break;
4618 case Primitive::kPrimFloat:
4619 case Primitive::kPrimDouble:
4620 // Moving float/double on float/double condition.
4621 if (is_r6) {
4622 can_move_conditionally = true;
4623 if (is_true_value_zero_constant) {
4624 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4625 use_const_for_true_in = true;
4626 } else if (is_false_value_zero_constant) {
4627 // selnez.fmt out_reg, true_reg, temp_cond_reg
4628 use_const_for_false_in = true;
4629 } else {
4630 // sel.fmt temp_cond_reg, false_reg, true_reg
4631 // mov.fmt out_reg, temp_cond_reg
4632 }
4633 } else {
4634 // movt.fmt out_reg, true_reg, cc
4635 can_move_conditionally = true;
4636 }
4637 break;
4638 }
4639 break;
4640 }
4641 }
4642
4643 if (can_move_conditionally) {
4644 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4645 } else {
4646 DCHECK(!use_const_for_false_in);
4647 DCHECK(!use_const_for_true_in);
4648 }
4649
4650 if (locations_to_set != nullptr) {
4651 if (use_const_for_false_in) {
4652 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4653 } else {
4654 locations_to_set->SetInAt(0,
4655 Primitive::IsFloatingPointType(dst_type)
4656 ? Location::RequiresFpuRegister()
4657 : Location::RequiresRegister());
4658 }
4659 if (use_const_for_true_in) {
4660 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4661 } else {
4662 locations_to_set->SetInAt(1,
4663 Primitive::IsFloatingPointType(dst_type)
4664 ? Location::RequiresFpuRegister()
4665 : Location::RequiresRegister());
4666 }
4667 if (materialized) {
4668 locations_to_set->SetInAt(2, Location::RequiresRegister());
4669 }
4670 // On R6 we don't require the output to be the same as the
4671 // first input for conditional moves unlike on R2.
4672 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4673 if (is_out_same_as_first_in) {
4674 locations_to_set->SetOut(Location::SameAsFirstInput());
4675 } else {
4676 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4677 ? Location::RequiresFpuRegister()
4678 : Location::RequiresRegister());
4679 }
4680 }
4681
4682 return can_move_conditionally;
4683}
4684
4685void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4686 LocationSummary* locations = select->GetLocations();
4687 Location dst = locations->Out();
4688 Location src = locations->InAt(1);
4689 Register src_reg = ZERO;
4690 Register src_reg_high = ZERO;
4691 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4692 Register cond_reg = TMP;
4693 int cond_cc = 0;
4694 Primitive::Type cond_type = Primitive::kPrimInt;
4695 bool cond_inverted = false;
4696 Primitive::Type dst_type = select->GetType();
4697
4698 if (IsBooleanValueOrMaterializedCondition(cond)) {
4699 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4700 } else {
4701 HCondition* condition = cond->AsCondition();
4702 LocationSummary* cond_locations = cond->GetLocations();
4703 IfCondition if_cond = condition->GetCondition();
4704 cond_type = condition->InputAt(0)->GetType();
4705 switch (cond_type) {
4706 default:
4707 DCHECK_NE(cond_type, Primitive::kPrimLong);
4708 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4709 break;
4710 case Primitive::kPrimFloat:
4711 case Primitive::kPrimDouble:
4712 cond_inverted = MaterializeFpCompareR2(if_cond,
4713 condition->IsGtBias(),
4714 cond_type,
4715 cond_locations,
4716 cond_cc);
4717 break;
4718 }
4719 }
4720
4721 DCHECK(dst.Equals(locations->InAt(0)));
4722 if (src.IsRegister()) {
4723 src_reg = src.AsRegister<Register>();
4724 } else if (src.IsRegisterPair()) {
4725 src_reg = src.AsRegisterPairLow<Register>();
4726 src_reg_high = src.AsRegisterPairHigh<Register>();
4727 } else if (src.IsConstant()) {
4728 DCHECK(src.GetConstant()->IsZeroBitPattern());
4729 }
4730
4731 switch (cond_type) {
4732 default:
4733 switch (dst_type) {
4734 default:
4735 if (cond_inverted) {
4736 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4737 } else {
4738 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4739 }
4740 break;
4741 case Primitive::kPrimLong:
4742 if (cond_inverted) {
4743 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4744 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4745 } else {
4746 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4747 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4748 }
4749 break;
4750 case Primitive::kPrimFloat:
4751 if (cond_inverted) {
4752 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4753 } else {
4754 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4755 }
4756 break;
4757 case Primitive::kPrimDouble:
4758 if (cond_inverted) {
4759 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4760 } else {
4761 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4762 }
4763 break;
4764 }
4765 break;
4766 case Primitive::kPrimLong:
4767 LOG(FATAL) << "Unreachable";
4768 UNREACHABLE();
4769 case Primitive::kPrimFloat:
4770 case Primitive::kPrimDouble:
4771 switch (dst_type) {
4772 default:
4773 if (cond_inverted) {
4774 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4775 } else {
4776 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4777 }
4778 break;
4779 case Primitive::kPrimLong:
4780 if (cond_inverted) {
4781 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4782 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4783 } else {
4784 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4785 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4786 }
4787 break;
4788 case Primitive::kPrimFloat:
4789 if (cond_inverted) {
4790 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4791 } else {
4792 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4793 }
4794 break;
4795 case Primitive::kPrimDouble:
4796 if (cond_inverted) {
4797 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4798 } else {
4799 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4800 }
4801 break;
4802 }
4803 break;
4804 }
4805}
4806
4807void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4808 LocationSummary* locations = select->GetLocations();
4809 Location dst = locations->Out();
4810 Location false_src = locations->InAt(0);
4811 Location true_src = locations->InAt(1);
4812 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4813 Register cond_reg = TMP;
4814 FRegister fcond_reg = FTMP;
4815 Primitive::Type cond_type = Primitive::kPrimInt;
4816 bool cond_inverted = false;
4817 Primitive::Type dst_type = select->GetType();
4818
4819 if (IsBooleanValueOrMaterializedCondition(cond)) {
4820 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4821 } else {
4822 HCondition* condition = cond->AsCondition();
4823 LocationSummary* cond_locations = cond->GetLocations();
4824 IfCondition if_cond = condition->GetCondition();
4825 cond_type = condition->InputAt(0)->GetType();
4826 switch (cond_type) {
4827 default:
4828 DCHECK_NE(cond_type, Primitive::kPrimLong);
4829 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4830 break;
4831 case Primitive::kPrimFloat:
4832 case Primitive::kPrimDouble:
4833 cond_inverted = MaterializeFpCompareR6(if_cond,
4834 condition->IsGtBias(),
4835 cond_type,
4836 cond_locations,
4837 fcond_reg);
4838 break;
4839 }
4840 }
4841
4842 if (true_src.IsConstant()) {
4843 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4844 }
4845 if (false_src.IsConstant()) {
4846 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4847 }
4848
4849 switch (dst_type) {
4850 default:
4851 if (Primitive::IsFloatingPointType(cond_type)) {
4852 __ Mfc1(cond_reg, fcond_reg);
4853 }
4854 if (true_src.IsConstant()) {
4855 if (cond_inverted) {
4856 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4857 } else {
4858 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4859 }
4860 } else if (false_src.IsConstant()) {
4861 if (cond_inverted) {
4862 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4863 } else {
4864 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4865 }
4866 } else {
4867 DCHECK_NE(cond_reg, AT);
4868 if (cond_inverted) {
4869 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4870 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4871 } else {
4872 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4873 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4874 }
4875 __ Or(dst.AsRegister<Register>(), AT, TMP);
4876 }
4877 break;
4878 case Primitive::kPrimLong: {
4879 if (Primitive::IsFloatingPointType(cond_type)) {
4880 __ Mfc1(cond_reg, fcond_reg);
4881 }
4882 Register dst_lo = dst.AsRegisterPairLow<Register>();
4883 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4884 if (true_src.IsConstant()) {
4885 Register src_lo = false_src.AsRegisterPairLow<Register>();
4886 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4887 if (cond_inverted) {
4888 __ Selnez(dst_lo, src_lo, cond_reg);
4889 __ Selnez(dst_hi, src_hi, cond_reg);
4890 } else {
4891 __ Seleqz(dst_lo, src_lo, cond_reg);
4892 __ Seleqz(dst_hi, src_hi, cond_reg);
4893 }
4894 } else {
4895 DCHECK(false_src.IsConstant());
4896 Register src_lo = true_src.AsRegisterPairLow<Register>();
4897 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4898 if (cond_inverted) {
4899 __ Seleqz(dst_lo, src_lo, cond_reg);
4900 __ Seleqz(dst_hi, src_hi, cond_reg);
4901 } else {
4902 __ Selnez(dst_lo, src_lo, cond_reg);
4903 __ Selnez(dst_hi, src_hi, cond_reg);
4904 }
4905 }
4906 break;
4907 }
4908 case Primitive::kPrimFloat: {
4909 if (!Primitive::IsFloatingPointType(cond_type)) {
4910 // sel*.fmt tests bit 0 of the condition register, account for that.
4911 __ Sltu(TMP, ZERO, cond_reg);
4912 __ Mtc1(TMP, fcond_reg);
4913 }
4914 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4915 if (true_src.IsConstant()) {
4916 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4917 if (cond_inverted) {
4918 __ SelnezS(dst_reg, src_reg, fcond_reg);
4919 } else {
4920 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4921 }
4922 } else if (false_src.IsConstant()) {
4923 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4924 if (cond_inverted) {
4925 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4926 } else {
4927 __ SelnezS(dst_reg, src_reg, fcond_reg);
4928 }
4929 } else {
4930 if (cond_inverted) {
4931 __ SelS(fcond_reg,
4932 true_src.AsFpuRegister<FRegister>(),
4933 false_src.AsFpuRegister<FRegister>());
4934 } else {
4935 __ SelS(fcond_reg,
4936 false_src.AsFpuRegister<FRegister>(),
4937 true_src.AsFpuRegister<FRegister>());
4938 }
4939 __ MovS(dst_reg, fcond_reg);
4940 }
4941 break;
4942 }
4943 case Primitive::kPrimDouble: {
4944 if (!Primitive::IsFloatingPointType(cond_type)) {
4945 // sel*.fmt tests bit 0 of the condition register, account for that.
4946 __ Sltu(TMP, ZERO, cond_reg);
4947 __ Mtc1(TMP, fcond_reg);
4948 }
4949 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4950 if (true_src.IsConstant()) {
4951 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4952 if (cond_inverted) {
4953 __ SelnezD(dst_reg, src_reg, fcond_reg);
4954 } else {
4955 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4956 }
4957 } else if (false_src.IsConstant()) {
4958 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4959 if (cond_inverted) {
4960 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4961 } else {
4962 __ SelnezD(dst_reg, src_reg, fcond_reg);
4963 }
4964 } else {
4965 if (cond_inverted) {
4966 __ SelD(fcond_reg,
4967 true_src.AsFpuRegister<FRegister>(),
4968 false_src.AsFpuRegister<FRegister>());
4969 } else {
4970 __ SelD(fcond_reg,
4971 false_src.AsFpuRegister<FRegister>(),
4972 true_src.AsFpuRegister<FRegister>());
4973 }
4974 __ MovD(dst_reg, fcond_reg);
4975 }
4976 break;
4977 }
4978 }
4979}
4980
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004981void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4982 LocationSummary* locations = new (GetGraph()->GetArena())
4983 LocationSummary(flag, LocationSummary::kNoCall);
4984 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004985}
4986
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004987void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4988 __ LoadFromOffset(kLoadWord,
4989 flag->GetLocations()->Out().AsRegister<Register>(),
4990 SP,
4991 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004992}
4993
David Brazdil74eb1b22015-12-14 11:44:01 +00004994void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004996 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004997}
4998
4999void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005000 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5001 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5002 if (is_r6) {
5003 GenConditionalMoveR6(select);
5004 } else {
5005 GenConditionalMoveR2(select);
5006 }
5007 } else {
5008 LocationSummary* locations = select->GetLocations();
5009 MipsLabel false_target;
5010 GenerateTestAndBranch(select,
5011 /* condition_input_index */ 2,
5012 /* true_target */ nullptr,
5013 &false_target);
5014 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5015 __ Bind(&false_target);
5016 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005017}
5018
David Srbecky0cf44932015-12-09 14:09:59 +00005019void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5020 new (GetGraph()->GetArena()) LocationSummary(info);
5021}
5022
David Srbeckyd28f4a02016-03-14 17:14:24 +00005023void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5024 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005025}
5026
5027void CodeGeneratorMIPS::GenerateNop() {
5028 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005029}
5030
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005031void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5032 Primitive::Type field_type = field_info.GetFieldType();
5033 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5034 bool generate_volatile = field_info.IsVolatile() && is_wide;
5035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005036 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005037
5038 locations->SetInAt(0, Location::RequiresRegister());
5039 if (generate_volatile) {
5040 InvokeRuntimeCallingConvention calling_convention;
5041 // need A0 to hold base + offset
5042 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5043 if (field_type == Primitive::kPrimLong) {
5044 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5045 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005046 // Use Location::Any() to prevent situations when running out of available fp registers.
5047 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005048 // Need some temp core regs since FP results are returned in core registers
5049 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5050 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5051 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5052 }
5053 } else {
5054 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5055 locations->SetOut(Location::RequiresFpuRegister());
5056 } else {
5057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5058 }
5059 }
5060}
5061
5062void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
5063 const FieldInfo& field_info,
5064 uint32_t dex_pc) {
5065 Primitive::Type type = field_info.GetFieldType();
5066 LocationSummary* locations = instruction->GetLocations();
5067 Register obj = locations->InAt(0).AsRegister<Register>();
5068 LoadOperandType load_type = kLoadUnsignedByte;
5069 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005070 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005071 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005072
5073 switch (type) {
5074 case Primitive::kPrimBoolean:
5075 load_type = kLoadUnsignedByte;
5076 break;
5077 case Primitive::kPrimByte:
5078 load_type = kLoadSignedByte;
5079 break;
5080 case Primitive::kPrimShort:
5081 load_type = kLoadSignedHalfword;
5082 break;
5083 case Primitive::kPrimChar:
5084 load_type = kLoadUnsignedHalfword;
5085 break;
5086 case Primitive::kPrimInt:
5087 case Primitive::kPrimFloat:
5088 case Primitive::kPrimNot:
5089 load_type = kLoadWord;
5090 break;
5091 case Primitive::kPrimLong:
5092 case Primitive::kPrimDouble:
5093 load_type = kLoadDoubleword;
5094 break;
5095 case Primitive::kPrimVoid:
5096 LOG(FATAL) << "Unreachable type " << type;
5097 UNREACHABLE();
5098 }
5099
5100 if (is_volatile && load_type == kLoadDoubleword) {
5101 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005102 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005103 // Do implicit Null check
5104 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5105 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01005106 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005107 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
5108 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005109 // FP results are returned in core registers. Need to move them.
5110 Location out = locations->Out();
5111 if (out.IsFpuRegister()) {
5112 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
5113 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
5114 out.AsFpuRegister<FRegister>());
5115 } else {
5116 DCHECK(out.IsDoubleStackSlot());
5117 __ StoreToOffset(kStoreWord,
5118 locations->GetTemp(1).AsRegister<Register>(),
5119 SP,
5120 out.GetStackIndex());
5121 __ StoreToOffset(kStoreWord,
5122 locations->GetTemp(2).AsRegister<Register>(),
5123 SP,
5124 out.GetStackIndex() + 4);
5125 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126 }
5127 } else {
5128 if (!Primitive::IsFloatingPointType(type)) {
5129 Register dst;
5130 if (type == Primitive::kPrimLong) {
5131 DCHECK(locations->Out().IsRegisterPair());
5132 dst = locations->Out().AsRegisterPairLow<Register>();
5133 } else {
5134 DCHECK(locations->Out().IsRegister());
5135 dst = locations->Out().AsRegister<Register>();
5136 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005137 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08005138 if (type == Primitive::kPrimNot) {
5139 __ MaybeUnpoisonHeapReference(dst);
5140 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141 } else {
5142 DCHECK(locations->Out().IsFpuRegister());
5143 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5144 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005145 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005147 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005148 }
5149 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005150 }
5151
5152 if (is_volatile) {
5153 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5154 }
5155}
5156
5157void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5158 Primitive::Type field_type = field_info.GetFieldType();
5159 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5160 bool generate_volatile = field_info.IsVolatile() && is_wide;
5161 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005162 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005163
5164 locations->SetInAt(0, Location::RequiresRegister());
5165 if (generate_volatile) {
5166 InvokeRuntimeCallingConvention calling_convention;
5167 // need A0 to hold base + offset
5168 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5169 if (field_type == Primitive::kPrimLong) {
5170 locations->SetInAt(1, Location::RegisterPairLocation(
5171 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5172 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005173 // Use Location::Any() to prevent situations when running out of available fp registers.
5174 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005175 // Pass FP parameters in core registers.
5176 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5177 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
5178 }
5179 } else {
5180 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005181 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005182 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005183 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005184 }
5185 }
5186}
5187
5188void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
5189 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01005190 uint32_t dex_pc,
5191 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005192 Primitive::Type type = field_info.GetFieldType();
5193 LocationSummary* locations = instruction->GetLocations();
5194 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07005195 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005196 StoreOperandType store_type = kStoreByte;
5197 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005198 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08005199 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005200 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005201
5202 switch (type) {
5203 case Primitive::kPrimBoolean:
5204 case Primitive::kPrimByte:
5205 store_type = kStoreByte;
5206 break;
5207 case Primitive::kPrimShort:
5208 case Primitive::kPrimChar:
5209 store_type = kStoreHalfword;
5210 break;
5211 case Primitive::kPrimInt:
5212 case Primitive::kPrimFloat:
5213 case Primitive::kPrimNot:
5214 store_type = kStoreWord;
5215 break;
5216 case Primitive::kPrimLong:
5217 case Primitive::kPrimDouble:
5218 store_type = kStoreDoubleword;
5219 break;
5220 case Primitive::kPrimVoid:
5221 LOG(FATAL) << "Unreachable type " << type;
5222 UNREACHABLE();
5223 }
5224
5225 if (is_volatile) {
5226 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5227 }
5228
5229 if (is_volatile && store_type == kStoreDoubleword) {
5230 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005231 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005232 // Do implicit Null check.
5233 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5234 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5235 if (type == Primitive::kPrimDouble) {
5236 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07005237 if (value_location.IsFpuRegister()) {
5238 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
5239 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005240 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07005241 value_location.AsFpuRegister<FRegister>());
5242 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005243 __ LoadFromOffset(kLoadWord,
5244 locations->GetTemp(1).AsRegister<Register>(),
5245 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005246 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005247 __ LoadFromOffset(kLoadWord,
5248 locations->GetTemp(2).AsRegister<Register>(),
5249 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005250 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005251 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005252 DCHECK(value_location.IsConstant());
5253 DCHECK(value_location.GetConstant()->IsDoubleConstant());
5254 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005255 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
5256 locations->GetTemp(1).AsRegister<Register>(),
5257 value);
5258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005259 }
Serban Constantinescufca16662016-07-14 09:21:59 +01005260 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005261 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
5262 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005263 if (value_location.IsConstant()) {
5264 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
5265 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
5266 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005267 Register src;
5268 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005269 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005270 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005271 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005272 }
Alexey Frunzec061de12017-02-14 13:27:23 -08005273 if (kPoisonHeapReferences && needs_write_barrier) {
5274 // Note that in the case where `value` is a null reference,
5275 // we do not enter this block, as a null reference does not
5276 // need poisoning.
5277 DCHECK_EQ(type, Primitive::kPrimNot);
5278 __ PoisonHeapReference(TMP, src);
5279 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
5280 } else {
5281 __ StoreToOffset(store_type, src, obj, offset, null_checker);
5282 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005283 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005284 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005285 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005286 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005287 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005288 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005289 }
5290 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005291 }
5292
5293 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08005294 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005295 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01005296 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005297 }
5298
5299 if (is_volatile) {
5300 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5301 }
5302}
5303
5304void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5305 HandleFieldGet(instruction, instruction->GetFieldInfo());
5306}
5307
5308void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5309 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5310}
5311
5312void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5313 HandleFieldSet(instruction, instruction->GetFieldInfo());
5314}
5315
5316void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005317 HandleFieldSet(instruction,
5318 instruction->GetFieldInfo(),
5319 instruction->GetDexPc(),
5320 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005321}
5322
Alexey Frunze06a46c42016-07-19 15:00:40 -07005323void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5324 HInstruction* instruction ATTRIBUTE_UNUSED,
5325 Location root,
5326 Register obj,
5327 uint32_t offset) {
5328 Register root_reg = root.AsRegister<Register>();
5329 if (kEmitCompilerReadBarrier) {
5330 UNIMPLEMENTED(FATAL) << "for read barrier";
5331 } else {
5332 // Plain GC root load with no read barrier.
5333 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5334 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5335 // Note that GC roots are not affected by heap poisoning, thus we
5336 // do not have to unpoison `root_reg` here.
5337 }
5338}
5339
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005340void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005341 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5342 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5343 switch (type_check_kind) {
5344 case TypeCheckKind::kExactCheck:
5345 case TypeCheckKind::kAbstractClassCheck:
5346 case TypeCheckKind::kClassHierarchyCheck:
5347 case TypeCheckKind::kArrayObjectCheck:
5348 call_kind = LocationSummary::kNoCall;
5349 break;
5350 case TypeCheckKind::kArrayCheck:
5351 case TypeCheckKind::kUnresolvedCheck:
5352 case TypeCheckKind::kInterfaceCheck:
5353 call_kind = LocationSummary::kCallOnSlowPath;
5354 break;
5355 }
5356
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5358 locations->SetInAt(0, Location::RequiresRegister());
5359 locations->SetInAt(1, Location::RequiresRegister());
5360 // The output does overlap inputs.
5361 // Note that TypeCheckSlowPathMIPS uses this register too.
5362 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5363}
5364
5365void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005366 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005367 LocationSummary* locations = instruction->GetLocations();
5368 Register obj = locations->InAt(0).AsRegister<Register>();
5369 Register cls = locations->InAt(1).AsRegister<Register>();
5370 Register out = locations->Out().AsRegister<Register>();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005371 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5372 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5373 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5374 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005375 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005376 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005377
5378 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005379 // Avoid this check if we know `obj` is not null.
5380 if (instruction->MustDoNullCheck()) {
5381 __ Move(out, ZERO);
5382 __ Beqz(obj, &done);
5383 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005384
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005385 switch (type_check_kind) {
5386 case TypeCheckKind::kExactCheck: {
5387 // /* HeapReference<Class> */ out = obj->klass_
5388 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5389 __ MaybeUnpoisonHeapReference(out);
5390 // Classes must be equal for the instanceof to succeed.
5391 __ Xor(out, out, cls);
5392 __ Sltiu(out, out, 1);
5393 break;
5394 }
5395
5396 case TypeCheckKind::kAbstractClassCheck: {
5397 // /* HeapReference<Class> */ out = obj->klass_
5398 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5399 __ MaybeUnpoisonHeapReference(out);
5400 // If the class is abstract, we eagerly fetch the super class of the
5401 // object to avoid doing a comparison we know will fail.
5402 MipsLabel loop;
5403 __ Bind(&loop);
5404 // /* HeapReference<Class> */ out = out->super_class_
5405 __ LoadFromOffset(kLoadWord, out, out, super_offset);
5406 __ MaybeUnpoisonHeapReference(out);
5407 // If `out` is null, we use it for the result, and jump to `done`.
5408 __ Beqz(out, &done);
5409 __ Bne(out, cls, &loop);
5410 __ LoadConst32(out, 1);
5411 break;
5412 }
5413
5414 case TypeCheckKind::kClassHierarchyCheck: {
5415 // /* HeapReference<Class> */ out = obj->klass_
5416 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5417 __ MaybeUnpoisonHeapReference(out);
5418 // Walk over the class hierarchy to find a match.
5419 MipsLabel loop, success;
5420 __ Bind(&loop);
5421 __ Beq(out, cls, &success);
5422 // /* HeapReference<Class> */ out = out->super_class_
5423 __ LoadFromOffset(kLoadWord, out, out, super_offset);
5424 __ MaybeUnpoisonHeapReference(out);
5425 __ Bnez(out, &loop);
5426 // If `out` is null, we use it for the result, and jump to `done`.
5427 __ B(&done);
5428 __ Bind(&success);
5429 __ LoadConst32(out, 1);
5430 break;
5431 }
5432
5433 case TypeCheckKind::kArrayObjectCheck: {
5434 // /* HeapReference<Class> */ out = obj->klass_
5435 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5436 __ MaybeUnpoisonHeapReference(out);
5437 // Do an exact check.
5438 MipsLabel success;
5439 __ Beq(out, cls, &success);
5440 // Otherwise, we need to check that the object's class is a non-primitive array.
5441 // /* HeapReference<Class> */ out = out->component_type_
5442 __ LoadFromOffset(kLoadWord, out, out, component_offset);
5443 __ MaybeUnpoisonHeapReference(out);
5444 // If `out` is null, we use it for the result, and jump to `done`.
5445 __ Beqz(out, &done);
5446 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5447 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5448 __ Sltiu(out, out, 1);
5449 __ B(&done);
5450 __ Bind(&success);
5451 __ LoadConst32(out, 1);
5452 break;
5453 }
5454
5455 case TypeCheckKind::kArrayCheck: {
5456 // No read barrier since the slow path will retry upon failure.
5457 // /* HeapReference<Class> */ out = obj->klass_
5458 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5459 __ MaybeUnpoisonHeapReference(out);
5460 DCHECK(locations->OnlyCallsOnSlowPath());
5461 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
5462 /* is_fatal */ false);
5463 codegen_->AddSlowPath(slow_path);
5464 __ Bne(out, cls, slow_path->GetEntryLabel());
5465 __ LoadConst32(out, 1);
5466 break;
5467 }
5468
5469 case TypeCheckKind::kUnresolvedCheck:
5470 case TypeCheckKind::kInterfaceCheck: {
5471 // Note that we indeed only call on slow path, but we always go
5472 // into the slow path for the unresolved and interface check
5473 // cases.
5474 //
5475 // We cannot directly call the InstanceofNonTrivial runtime
5476 // entry point without resorting to a type checking slow path
5477 // here (i.e. by calling InvokeRuntime directly), as it would
5478 // require to assign fixed registers for the inputs of this
5479 // HInstanceOf instruction (following the runtime calling
5480 // convention), which might be cluttered by the potential first
5481 // read barrier emission at the beginning of this method.
5482 //
5483 // TODO: Introduce a new runtime entry point taking the object
5484 // to test (instead of its class) as argument, and let it deal
5485 // with the read barrier issues. This will let us refactor this
5486 // case of the `switch` code as it was previously (with a direct
5487 // call to the runtime not using a type checking slow path).
5488 // This should also be beneficial for the other cases above.
5489 DCHECK(locations->OnlyCallsOnSlowPath());
5490 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
5491 /* is_fatal */ false);
5492 codegen_->AddSlowPath(slow_path);
5493 __ B(slow_path->GetEntryLabel());
5494 break;
5495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005496 }
5497
5498 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005499
5500 if (slow_path != nullptr) {
5501 __ Bind(slow_path->GetExitLabel());
5502 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005503}
5504
5505void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5507 locations->SetOut(Location::ConstantLocation(constant));
5508}
5509
5510void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5511 // Will be generated at use site.
5512}
5513
5514void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5516 locations->SetOut(Location::ConstantLocation(constant));
5517}
5518
5519void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5520 // Will be generated at use site.
5521}
5522
5523void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5524 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5525 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5526}
5527
5528void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5529 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005530 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005531 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005532 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005533}
5534
5535void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5536 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5537 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005538 Location receiver = invoke->GetLocations()->InAt(0);
5539 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005540 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005541
5542 // Set the hidden argument.
5543 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5544 invoke->GetDexMethodIndex());
5545
5546 // temp = object->GetClass();
5547 if (receiver.IsStackSlot()) {
5548 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5549 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5550 } else {
5551 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5552 }
5553 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005554 // Instead of simply (possibly) unpoisoning `temp` here, we should
5555 // emit a read barrier for the previous class reference load.
5556 // However this is not required in practice, as this is an
5557 // intermediate/temporary reference and because the current
5558 // concurrent copying collector keeps the from-space memory
5559 // intact/accessible until the end of the marking phase (the
5560 // concurrent copying collector may not in the future).
5561 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005562 __ LoadFromOffset(kLoadWord, temp, temp,
5563 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5564 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005565 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005566 // temp = temp->GetImtEntryAt(method_offset);
5567 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5568 // T9 = temp->GetEntryPoint();
5569 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5570 // T9();
5571 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005572 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005573 DCHECK(!codegen_->IsLeafMethod());
5574 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5575}
5576
5577void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005578 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5579 if (intrinsic.TryDispatch(invoke)) {
5580 return;
5581 }
5582
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005583 HandleInvoke(invoke);
5584}
5585
5586void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005587 // Explicit clinit checks triggered by static invokes must have been pruned by
5588 // art::PrepareForRegisterAllocation.
5589 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005590
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005591 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5592 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005593
Chris Larsen701566a2015-10-27 15:29:13 -07005594 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5595 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005596 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5597 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5598 }
Chris Larsen701566a2015-10-27 15:29:13 -07005599 return;
5600 }
5601
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005602 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005603
5604 // Add the extra input register if either the dex cache array base register
5605 // or the PC-relative base register for accessing literals is needed.
5606 if (has_extra_input) {
5607 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5608 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005609}
5610
Orion Hodsonac141392017-01-13 11:53:47 +00005611void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5612 HandleInvoke(invoke);
5613}
5614
5615void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5616 codegen_->GenerateInvokePolymorphicCall(invoke);
5617}
5618
Chris Larsen701566a2015-10-27 15:29:13 -07005619static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005620 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005621 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5622 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005623 return true;
5624 }
5625 return false;
5626}
5627
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005628HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005629 HLoadString::LoadKind desired_string_load_kind) {
5630 if (kEmitCompilerReadBarrier) {
5631 UNIMPLEMENTED(FATAL) << "for read barrier";
5632 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005633 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005634 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005635 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5636 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005637 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005638 bool is_r6 = GetInstructionSetFeatures().IsR6();
5639 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005640 switch (desired_string_load_kind) {
5641 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5642 DCHECK(!GetCompilerOptions().GetCompilePic());
5643 break;
5644 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5645 DCHECK(GetCompilerOptions().GetCompilePic());
5646 break;
5647 case HLoadString::LoadKind::kBootImageAddress:
5648 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005649 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005650 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005651 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005652 case HLoadString::LoadKind::kJitTableAddress:
5653 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005654 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005655 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005656 case HLoadString::LoadKind::kDexCacheViaMethod:
5657 fallback_load = false;
5658 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005659 }
5660 if (fallback_load) {
5661 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5662 }
5663 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005664}
5665
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005666HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5667 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005668 if (kEmitCompilerReadBarrier) {
5669 UNIMPLEMENTED(FATAL) << "for read barrier";
5670 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005671 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005672 // is incompatible with it.
5673 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005674 bool is_r6 = GetInstructionSetFeatures().IsR6();
5675 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005676 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005677 case HLoadClass::LoadKind::kInvalid:
5678 LOG(FATAL) << "UNREACHABLE";
5679 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005680 case HLoadClass::LoadKind::kReferrersClass:
5681 fallback_load = false;
5682 break;
5683 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5684 DCHECK(!GetCompilerOptions().GetCompilePic());
5685 break;
5686 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5687 DCHECK(GetCompilerOptions().GetCompilePic());
5688 break;
5689 case HLoadClass::LoadKind::kBootImageAddress:
5690 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005691 case HLoadClass::LoadKind::kBssEntry:
5692 DCHECK(!Runtime::Current()->UseJitCompilation());
5693 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005694 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005695 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005696 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005697 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005698 case HLoadClass::LoadKind::kDexCacheViaMethod:
5699 fallback_load = false;
5700 break;
5701 }
5702 if (fallback_load) {
5703 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5704 }
5705 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005706}
5707
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005708Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5709 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005710 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005711 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5712 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5713 if (!invoke->GetLocations()->Intrinsified()) {
5714 return location.AsRegister<Register>();
5715 }
5716 // For intrinsics we allow any location, so it may be on the stack.
5717 if (!location.IsRegister()) {
5718 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5719 return temp;
5720 }
5721 // For register locations, check if the register was saved. If so, get it from the stack.
5722 // Note: There is a chance that the register was saved but not overwritten, so we could
5723 // save one load. However, since this is just an intrinsic slow path we prefer this
5724 // simple and more robust approach rather that trying to determine if that's the case.
5725 SlowPathCode* slow_path = GetCurrentSlowPath();
5726 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5727 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5728 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5729 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5730 return temp;
5731 }
5732 return location.AsRegister<Register>();
5733}
5734
Vladimir Markodc151b22015-10-15 18:02:30 +01005735HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5736 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005737 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005738 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005739 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005740 // is incompatible with it.
5741 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005742 bool is_r6 = GetInstructionSetFeatures().IsR6();
5743 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005744 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005745 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005746 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005747 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005748 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005749 break;
5750 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005751 if (fallback_load) {
5752 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5753 dispatch_info.method_load_data = 0;
5754 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005755 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005756}
5757
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005758void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5759 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005760 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005761 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5762 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005763 bool is_r6 = GetInstructionSetFeatures().IsR6();
5764 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005765 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5766 : ZERO;
5767
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005768 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005769 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005770 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005771 uint32_t offset =
5772 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005773 __ LoadFromOffset(kLoadWord,
5774 temp.AsRegister<Register>(),
5775 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005776 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005777 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005778 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005779 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005780 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005781 break;
5782 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5783 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5784 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005785 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5786 if (is_r6) {
5787 uint32_t offset = invoke->GetDexCacheArrayOffset();
5788 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5789 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
5790 bool reordering = __ SetReorder(false);
5791 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
5792 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
5793 __ SetReorder(reordering);
5794 } else {
5795 HMipsDexCacheArraysBase* base =
5796 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5797 int32_t offset =
5798 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5799 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5800 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005801 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005802 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005803 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005804 Register reg = temp.AsRegister<Register>();
5805 Register method_reg;
5806 if (current_method.IsRegister()) {
5807 method_reg = current_method.AsRegister<Register>();
5808 } else {
5809 // TODO: use the appropriate DCHECK() here if possible.
5810 // DCHECK(invoke->GetLocations()->Intrinsified());
5811 DCHECK(!current_method.IsValid());
5812 method_reg = reg;
5813 __ Lw(reg, SP, kCurrentMethodStackOffset);
5814 }
5815
5816 // temp = temp->dex_cache_resolved_methods_;
5817 __ LoadFromOffset(kLoadWord,
5818 reg,
5819 method_reg,
5820 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005821 // temp = temp[index_in_cache];
5822 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5823 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005824 __ LoadFromOffset(kLoadWord,
5825 reg,
5826 reg,
5827 CodeGenerator::GetCachePointerOffset(index_in_cache));
5828 break;
5829 }
5830 }
5831
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005832 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005833 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005834 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005835 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5837 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005838 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005839 T9,
5840 callee_method.AsRegister<Register>(),
5841 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005842 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005843 // T9()
5844 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005845 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005846 break;
5847 }
5848 DCHECK(!IsLeafMethod());
5849}
5850
5851void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005852 // Explicit clinit checks triggered by static invokes must have been pruned by
5853 // art::PrepareForRegisterAllocation.
5854 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005855
5856 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5857 return;
5858 }
5859
5860 LocationSummary* locations = invoke->GetLocations();
5861 codegen_->GenerateStaticOrDirectCall(invoke,
5862 locations->HasTemps()
5863 ? locations->GetTemp(0)
5864 : Location::NoLocation());
5865 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5866}
5867
Chris Larsen3acee732015-11-18 13:31:08 -08005868void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005869 // Use the calling convention instead of the location of the receiver, as
5870 // intrinsics may have put the receiver in a different register. In the intrinsics
5871 // slow path, the arguments have been moved to the right place, so here we are
5872 // guaranteed that the receiver is the first register of the calling convention.
5873 InvokeDexCallingConvention calling_convention;
5874 Register receiver = calling_convention.GetRegisterAt(0);
5875
Chris Larsen3acee732015-11-18 13:31:08 -08005876 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005877 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5878 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5879 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005880 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005881
5882 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005883 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005884 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005885 // Instead of simply (possibly) unpoisoning `temp` here, we should
5886 // emit a read barrier for the previous class reference load.
5887 // However this is not required in practice, as this is an
5888 // intermediate/temporary reference and because the current
5889 // concurrent copying collector keeps the from-space memory
5890 // intact/accessible until the end of the marking phase (the
5891 // concurrent copying collector may not in the future).
5892 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005893 // temp = temp->GetMethodAt(method_offset);
5894 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5895 // T9 = temp->GetEntryPoint();
5896 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5897 // T9();
5898 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005899 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005900}
5901
5902void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5903 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5904 return;
5905 }
5906
5907 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005908 DCHECK(!codegen_->IsLeafMethod());
5909 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5910}
5911
5912void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005913 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5914 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005915 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005916 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005917 cls,
5918 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005919 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005920 return;
5921 }
Vladimir Marko41559982017-01-06 14:04:23 +00005922 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005923
5924 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5925 ? LocationSummary::kCallOnSlowPath
5926 : LocationSummary::kNoCall;
5927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005928 switch (load_kind) {
5929 // We need an extra register for PC-relative literals on R2.
5930 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005931 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005932 case HLoadClass::LoadKind::kBootImageAddress:
5933 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005934 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5935 break;
5936 }
5937 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005938 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005939 locations->SetInAt(0, Location::RequiresRegister());
5940 break;
5941 default:
5942 break;
5943 }
5944 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005945}
5946
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005947// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5948// move.
5949void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005950 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5951 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5952 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005953 return;
5954 }
Vladimir Marko41559982017-01-06 14:04:23 +00005955 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005956
Vladimir Marko41559982017-01-06 14:04:23 +00005957 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005958 Location out_loc = locations->Out();
5959 Register out = out_loc.AsRegister<Register>();
5960 Register base_or_current_method_reg;
5961 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5962 switch (load_kind) {
5963 // We need an extra register for PC-relative literals on R2.
5964 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005965 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005966 case HLoadClass::LoadKind::kBootImageAddress:
5967 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005968 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5969 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005970 case HLoadClass::LoadKind::kReferrersClass:
5971 case HLoadClass::LoadKind::kDexCacheViaMethod:
5972 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5973 break;
5974 default:
5975 base_or_current_method_reg = ZERO;
5976 break;
5977 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005978
Alexey Frunze06a46c42016-07-19 15:00:40 -07005979 bool generate_null_check = false;
5980 switch (load_kind) {
5981 case HLoadClass::LoadKind::kReferrersClass: {
5982 DCHECK(!cls->CanCallRuntime());
5983 DCHECK(!cls->MustGenerateClinitCheck());
5984 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5985 GenerateGcRootFieldLoad(cls,
5986 out_loc,
5987 base_or_current_method_reg,
5988 ArtMethod::DeclaringClassOffset().Int32Value());
5989 break;
5990 }
5991 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005992 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005993 __ LoadLiteral(out,
5994 base_or_current_method_reg,
5995 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5996 cls->GetTypeIndex()));
5997 break;
5998 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005999 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006000 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6001 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006002 bool reordering = __ SetReorder(false);
6003 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
6004 __ Addiu(out, out, /* placeholder */ 0x5678);
6005 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006006 break;
6007 }
6008 case HLoadClass::LoadKind::kBootImageAddress: {
6009 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006010 uint32_t address = dchecked_integral_cast<uint32_t>(
6011 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6012 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006013 __ LoadLiteral(out,
6014 base_or_current_method_reg,
6015 codegen_->DeduplicateBootImageAddressLiteral(address));
6016 break;
6017 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006018 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006019 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006020 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006021 bool reordering = __ SetReorder(false);
6022 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08006023 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006024 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006025 generate_null_check = true;
6026 break;
6027 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006028 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08006029 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
6030 cls->GetTypeIndex(),
6031 cls->GetClass());
6032 bool reordering = __ SetReorder(false);
6033 __ Bind(&info->high_label);
6034 __ Lui(out, /* placeholder */ 0x1234);
6035 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
6036 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006037 break;
6038 }
Vladimir Marko41559982017-01-06 14:04:23 +00006039 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006040 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006041 LOG(FATAL) << "UNREACHABLE";
6042 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006043 }
6044
6045 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6046 DCHECK(cls->CanCallRuntime());
6047 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
6048 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6049 codegen_->AddSlowPath(slow_path);
6050 if (generate_null_check) {
6051 __ Beqz(out, slow_path->GetEntryLabel());
6052 }
6053 if (cls->MustGenerateClinitCheck()) {
6054 GenerateClassInitializationCheck(slow_path, out);
6055 } else {
6056 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006057 }
6058 }
6059}
6060
6061static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006062 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006063}
6064
6065void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
6066 LocationSummary* locations =
6067 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6068 locations->SetOut(Location::RequiresRegister());
6069}
6070
6071void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
6072 Register out = load->GetLocations()->Out().AsRegister<Register>();
6073 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6074}
6075
6076void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
6077 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6078}
6079
6080void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6081 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
6082}
6083
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006084void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006085 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006086 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006087 HLoadString::LoadKind load_kind = load->GetLoadKind();
6088 switch (load_kind) {
6089 // We need an extra register for PC-relative literals on R2.
6090 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6091 case HLoadString::LoadKind::kBootImageAddress:
6092 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006093 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006094 if (codegen_->GetInstructionSetFeatures().IsR6()) {
6095 break;
6096 }
6097 FALLTHROUGH_INTENDED;
6098 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006099 case HLoadString::LoadKind::kDexCacheViaMethod:
6100 locations->SetInAt(0, Location::RequiresRegister());
6101 break;
6102 default:
6103 break;
6104 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07006105 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6106 InvokeRuntimeCallingConvention calling_convention;
6107 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
6108 } else {
6109 locations->SetOut(Location::RequiresRegister());
6110 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006111}
6112
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006113// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6114// move.
6115void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006116 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006117 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006118 Location out_loc = locations->Out();
6119 Register out = out_loc.AsRegister<Register>();
6120 Register base_or_current_method_reg;
6121 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6122 switch (load_kind) {
6123 // We need an extra register for PC-relative literals on R2.
6124 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6125 case HLoadString::LoadKind::kBootImageAddress:
6126 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006127 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006128 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
6129 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006130 default:
6131 base_or_current_method_reg = ZERO;
6132 break;
6133 }
6134
6135 switch (load_kind) {
6136 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006137 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006138 __ LoadLiteral(out,
6139 base_or_current_method_reg,
6140 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6141 load->GetStringIndex()));
6142 return; // No dex cache slow path.
6143 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006144 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006145 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006146 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006147 bool reordering = __ SetReorder(false);
6148 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
6149 __ Addiu(out, out, /* placeholder */ 0x5678);
6150 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006151 return; // No dex cache slow path.
6152 }
6153 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006154 uint32_t address = dchecked_integral_cast<uint32_t>(
6155 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6156 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006157 __ LoadLiteral(out,
6158 base_or_current_method_reg,
6159 codegen_->DeduplicateBootImageAddressLiteral(address));
6160 return; // No dex cache slow path.
6161 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006162 case HLoadString::LoadKind::kBssEntry: {
6163 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6164 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006165 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006166 bool reordering = __ SetReorder(false);
6167 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08006168 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006169 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006170 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
6171 codegen_->AddSlowPath(slow_path);
6172 __ Beqz(out, slow_path->GetEntryLabel());
6173 __ Bind(slow_path->GetExitLabel());
6174 return;
6175 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006176 case HLoadString::LoadKind::kJitTableAddress: {
6177 CodeGeneratorMIPS::JitPatchInfo* info =
6178 codegen_->NewJitRootStringPatch(load->GetDexFile(),
6179 load->GetStringIndex(),
6180 load->GetString());
6181 bool reordering = __ SetReorder(false);
6182 __ Bind(&info->high_label);
6183 __ Lui(out, /* placeholder */ 0x1234);
6184 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
6185 __ SetReorder(reordering);
6186 return;
6187 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006188 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006189 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006190 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006191
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006192 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006193 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6194 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006195 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006196 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6197 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006198}
6199
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006200void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
6201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6202 locations->SetOut(Location::ConstantLocation(constant));
6203}
6204
6205void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6206 // Will be generated at use site.
6207}
6208
6209void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
6210 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006212 InvokeRuntimeCallingConvention calling_convention;
6213 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6214}
6215
6216void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
6217 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006218 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006219 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6220 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006221 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006222 }
6223 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6224}
6225
6226void LocationsBuilderMIPS::VisitMul(HMul* mul) {
6227 LocationSummary* locations =
6228 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
6229 switch (mul->GetResultType()) {
6230 case Primitive::kPrimInt:
6231 case Primitive::kPrimLong:
6232 locations->SetInAt(0, Location::RequiresRegister());
6233 locations->SetInAt(1, Location::RequiresRegister());
6234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6235 break;
6236
6237 case Primitive::kPrimFloat:
6238 case Primitive::kPrimDouble:
6239 locations->SetInAt(0, Location::RequiresFpuRegister());
6240 locations->SetInAt(1, Location::RequiresFpuRegister());
6241 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6242 break;
6243
6244 default:
6245 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6246 }
6247}
6248
6249void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
6250 Primitive::Type type = instruction->GetType();
6251 LocationSummary* locations = instruction->GetLocations();
6252 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6253
6254 switch (type) {
6255 case Primitive::kPrimInt: {
6256 Register dst = locations->Out().AsRegister<Register>();
6257 Register lhs = locations->InAt(0).AsRegister<Register>();
6258 Register rhs = locations->InAt(1).AsRegister<Register>();
6259
6260 if (isR6) {
6261 __ MulR6(dst, lhs, rhs);
6262 } else {
6263 __ MulR2(dst, lhs, rhs);
6264 }
6265 break;
6266 }
6267 case Primitive::kPrimLong: {
6268 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6269 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6270 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6271 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
6272 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
6273 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
6274
6275 // Extra checks to protect caused by the existance of A1_A2.
6276 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
6277 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
6278 DCHECK_NE(dst_high, lhs_low);
6279 DCHECK_NE(dst_high, rhs_low);
6280
6281 // A_B * C_D
6282 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
6283 // dst_lo: [ low(B*D) ]
6284 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
6285
6286 if (isR6) {
6287 __ MulR6(TMP, lhs_high, rhs_low);
6288 __ MulR6(dst_high, lhs_low, rhs_high);
6289 __ Addu(dst_high, dst_high, TMP);
6290 __ MuhuR6(TMP, lhs_low, rhs_low);
6291 __ Addu(dst_high, dst_high, TMP);
6292 __ MulR6(dst_low, lhs_low, rhs_low);
6293 } else {
6294 __ MulR2(TMP, lhs_high, rhs_low);
6295 __ MulR2(dst_high, lhs_low, rhs_high);
6296 __ Addu(dst_high, dst_high, TMP);
6297 __ MultuR2(lhs_low, rhs_low);
6298 __ Mfhi(TMP);
6299 __ Addu(dst_high, dst_high, TMP);
6300 __ Mflo(dst_low);
6301 }
6302 break;
6303 }
6304 case Primitive::kPrimFloat:
6305 case Primitive::kPrimDouble: {
6306 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6307 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
6308 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
6309 if (type == Primitive::kPrimFloat) {
6310 __ MulS(dst, lhs, rhs);
6311 } else {
6312 __ MulD(dst, lhs, rhs);
6313 }
6314 break;
6315 }
6316 default:
6317 LOG(FATAL) << "Unexpected mul type " << type;
6318 }
6319}
6320
6321void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
6322 LocationSummary* locations =
6323 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6324 switch (neg->GetResultType()) {
6325 case Primitive::kPrimInt:
6326 case Primitive::kPrimLong:
6327 locations->SetInAt(0, Location::RequiresRegister());
6328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6329 break;
6330
6331 case Primitive::kPrimFloat:
6332 case Primitive::kPrimDouble:
6333 locations->SetInAt(0, Location::RequiresFpuRegister());
6334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6335 break;
6336
6337 default:
6338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6339 }
6340}
6341
6342void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
6343 Primitive::Type type = instruction->GetType();
6344 LocationSummary* locations = instruction->GetLocations();
6345
6346 switch (type) {
6347 case Primitive::kPrimInt: {
6348 Register dst = locations->Out().AsRegister<Register>();
6349 Register src = locations->InAt(0).AsRegister<Register>();
6350 __ Subu(dst, ZERO, src);
6351 break;
6352 }
6353 case Primitive::kPrimLong: {
6354 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6355 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6356 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6357 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6358 __ Subu(dst_low, ZERO, src_low);
6359 __ Sltu(TMP, ZERO, dst_low);
6360 __ Subu(dst_high, ZERO, src_high);
6361 __ Subu(dst_high, dst_high, TMP);
6362 break;
6363 }
6364 case Primitive::kPrimFloat:
6365 case Primitive::kPrimDouble: {
6366 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6367 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6368 if (type == Primitive::kPrimFloat) {
6369 __ NegS(dst, src);
6370 } else {
6371 __ NegD(dst, src);
6372 }
6373 break;
6374 }
6375 default:
6376 LOG(FATAL) << "Unexpected neg type " << type;
6377 }
6378}
6379
6380void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
6381 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006382 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006383 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006384 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006385 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6386 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387}
6388
6389void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006390 // Note: if heap poisoning is enabled, the entry point takes care
6391 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006392 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
6393 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394}
6395
6396void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
6397 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006399 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006400 if (instruction->IsStringAlloc()) {
6401 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6402 } else {
6403 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006405 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6406}
6407
6408void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006409 // Note: if heap poisoning is enabled, the entry point takes care
6410 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006411 if (instruction->IsStringAlloc()) {
6412 // String is allocated through StringFactory. Call NewEmptyString entry point.
6413 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006414 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006415 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6416 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6417 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006418 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006419 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6420 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006421 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006422 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006424}
6425
6426void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6428 locations->SetInAt(0, Location::RequiresRegister());
6429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6430}
6431
6432void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6433 Primitive::Type type = instruction->GetType();
6434 LocationSummary* locations = instruction->GetLocations();
6435
6436 switch (type) {
6437 case Primitive::kPrimInt: {
6438 Register dst = locations->Out().AsRegister<Register>();
6439 Register src = locations->InAt(0).AsRegister<Register>();
6440 __ Nor(dst, src, ZERO);
6441 break;
6442 }
6443
6444 case Primitive::kPrimLong: {
6445 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6446 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6447 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6448 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6449 __ Nor(dst_high, src_high, ZERO);
6450 __ Nor(dst_low, src_low, ZERO);
6451 break;
6452 }
6453
6454 default:
6455 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6456 }
6457}
6458
6459void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6461 locations->SetInAt(0, Location::RequiresRegister());
6462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6463}
6464
6465void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6466 LocationSummary* locations = instruction->GetLocations();
6467 __ Xori(locations->Out().AsRegister<Register>(),
6468 locations->InAt(0).AsRegister<Register>(),
6469 1);
6470}
6471
6472void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006473 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6474 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006475}
6476
Calin Juravle2ae48182016-03-16 14:05:09 +00006477void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6478 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006479 return;
6480 }
6481 Location obj = instruction->GetLocations()->InAt(0);
6482
6483 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006484 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006485}
6486
Calin Juravle2ae48182016-03-16 14:05:09 +00006487void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006488 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006489 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006490
6491 Location obj = instruction->GetLocations()->InAt(0);
6492
6493 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6494}
6495
6496void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006497 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006498}
6499
6500void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6501 HandleBinaryOp(instruction);
6502}
6503
6504void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6505 HandleBinaryOp(instruction);
6506}
6507
6508void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6509 LOG(FATAL) << "Unreachable";
6510}
6511
6512void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6513 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6514}
6515
6516void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6518 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6519 if (location.IsStackSlot()) {
6520 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6521 } else if (location.IsDoubleStackSlot()) {
6522 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6523 }
6524 locations->SetOut(location);
6525}
6526
6527void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6528 ATTRIBUTE_UNUSED) {
6529 // Nothing to do, the parameter is already at its location.
6530}
6531
6532void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6533 LocationSummary* locations =
6534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6535 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6536}
6537
6538void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6539 ATTRIBUTE_UNUSED) {
6540 // Nothing to do, the method is already at its location.
6541}
6542
6543void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006545 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546 locations->SetInAt(i, Location::Any());
6547 }
6548 locations->SetOut(Location::Any());
6549}
6550
6551void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6552 LOG(FATAL) << "Unreachable";
6553}
6554
6555void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6556 Primitive::Type type = rem->GetResultType();
6557 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006558 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6560
6561 switch (type) {
6562 case Primitive::kPrimInt:
6563 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006564 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6566 break;
6567
6568 case Primitive::kPrimLong: {
6569 InvokeRuntimeCallingConvention calling_convention;
6570 locations->SetInAt(0, Location::RegisterPairLocation(
6571 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6572 locations->SetInAt(1, Location::RegisterPairLocation(
6573 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6574 locations->SetOut(calling_convention.GetReturnLocation(type));
6575 break;
6576 }
6577
6578 case Primitive::kPrimFloat:
6579 case Primitive::kPrimDouble: {
6580 InvokeRuntimeCallingConvention calling_convention;
6581 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6582 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6583 locations->SetOut(calling_convention.GetReturnLocation(type));
6584 break;
6585 }
6586
6587 default:
6588 LOG(FATAL) << "Unexpected rem type " << type;
6589 }
6590}
6591
6592void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6593 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006594
6595 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006596 case Primitive::kPrimInt:
6597 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006598 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006599 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006600 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006601 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6602 break;
6603 }
6604 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006605 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006606 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006607 break;
6608 }
6609 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006610 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006611 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006612 break;
6613 }
6614 default:
6615 LOG(FATAL) << "Unexpected rem type " << type;
6616 }
6617}
6618
6619void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6620 memory_barrier->SetLocations(nullptr);
6621}
6622
6623void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6624 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6625}
6626
6627void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6628 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6629 Primitive::Type return_type = ret->InputAt(0)->GetType();
6630 locations->SetInAt(0, MipsReturnLocation(return_type));
6631}
6632
6633void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6634 codegen_->GenerateFrameExit();
6635}
6636
6637void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6638 ret->SetLocations(nullptr);
6639}
6640
6641void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6642 codegen_->GenerateFrameExit();
6643}
6644
Alexey Frunze92d90602015-12-18 18:16:36 -08006645void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6646 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006647}
6648
Alexey Frunze92d90602015-12-18 18:16:36 -08006649void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6650 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006651}
6652
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006653void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6654 HandleShift(shl);
6655}
6656
6657void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6658 HandleShift(shl);
6659}
6660
6661void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6662 HandleShift(shr);
6663}
6664
6665void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6666 HandleShift(shr);
6667}
6668
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006669void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6670 HandleBinaryOp(instruction);
6671}
6672
6673void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6674 HandleBinaryOp(instruction);
6675}
6676
6677void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6678 HandleFieldGet(instruction, instruction->GetFieldInfo());
6679}
6680
6681void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6682 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6683}
6684
6685void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6686 HandleFieldSet(instruction, instruction->GetFieldInfo());
6687}
6688
6689void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006690 HandleFieldSet(instruction,
6691 instruction->GetFieldInfo(),
6692 instruction->GetDexPc(),
6693 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006694}
6695
6696void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6697 HUnresolvedInstanceFieldGet* instruction) {
6698 FieldAccessCallingConventionMIPS calling_convention;
6699 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6700 instruction->GetFieldType(),
6701 calling_convention);
6702}
6703
6704void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6705 HUnresolvedInstanceFieldGet* instruction) {
6706 FieldAccessCallingConventionMIPS calling_convention;
6707 codegen_->GenerateUnresolvedFieldAccess(instruction,
6708 instruction->GetFieldType(),
6709 instruction->GetFieldIndex(),
6710 instruction->GetDexPc(),
6711 calling_convention);
6712}
6713
6714void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6715 HUnresolvedInstanceFieldSet* instruction) {
6716 FieldAccessCallingConventionMIPS calling_convention;
6717 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6718 instruction->GetFieldType(),
6719 calling_convention);
6720}
6721
6722void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6723 HUnresolvedInstanceFieldSet* instruction) {
6724 FieldAccessCallingConventionMIPS calling_convention;
6725 codegen_->GenerateUnresolvedFieldAccess(instruction,
6726 instruction->GetFieldType(),
6727 instruction->GetFieldIndex(),
6728 instruction->GetDexPc(),
6729 calling_convention);
6730}
6731
6732void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6733 HUnresolvedStaticFieldGet* instruction) {
6734 FieldAccessCallingConventionMIPS calling_convention;
6735 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6736 instruction->GetFieldType(),
6737 calling_convention);
6738}
6739
6740void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6741 HUnresolvedStaticFieldGet* instruction) {
6742 FieldAccessCallingConventionMIPS calling_convention;
6743 codegen_->GenerateUnresolvedFieldAccess(instruction,
6744 instruction->GetFieldType(),
6745 instruction->GetFieldIndex(),
6746 instruction->GetDexPc(),
6747 calling_convention);
6748}
6749
6750void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6751 HUnresolvedStaticFieldSet* instruction) {
6752 FieldAccessCallingConventionMIPS calling_convention;
6753 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6754 instruction->GetFieldType(),
6755 calling_convention);
6756}
6757
6758void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6759 HUnresolvedStaticFieldSet* instruction) {
6760 FieldAccessCallingConventionMIPS calling_convention;
6761 codegen_->GenerateUnresolvedFieldAccess(instruction,
6762 instruction->GetFieldType(),
6763 instruction->GetFieldIndex(),
6764 instruction->GetDexPc(),
6765 calling_convention);
6766}
6767
6768void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006769 LocationSummary* locations =
6770 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006771 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006772}
6773
6774void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6775 HBasicBlock* block = instruction->GetBlock();
6776 if (block->GetLoopInformation() != nullptr) {
6777 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6778 // The back edge will generate the suspend check.
6779 return;
6780 }
6781 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6782 // The goto will generate the suspend check.
6783 return;
6784 }
6785 GenerateSuspendCheck(instruction, nullptr);
6786}
6787
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006788void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6789 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006791 InvokeRuntimeCallingConvention calling_convention;
6792 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6793}
6794
6795void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006796 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6798}
6799
6800void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6801 Primitive::Type input_type = conversion->GetInputType();
6802 Primitive::Type result_type = conversion->GetResultType();
6803 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006804 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006805
6806 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6807 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6808 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6809 }
6810
6811 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006812 if (!isR6 &&
6813 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6814 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006815 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006816 }
6817
6818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6819
6820 if (call_kind == LocationSummary::kNoCall) {
6821 if (Primitive::IsFloatingPointType(input_type)) {
6822 locations->SetInAt(0, Location::RequiresFpuRegister());
6823 } else {
6824 locations->SetInAt(0, Location::RequiresRegister());
6825 }
6826
6827 if (Primitive::IsFloatingPointType(result_type)) {
6828 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6829 } else {
6830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6831 }
6832 } else {
6833 InvokeRuntimeCallingConvention calling_convention;
6834
6835 if (Primitive::IsFloatingPointType(input_type)) {
6836 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6837 } else {
6838 DCHECK_EQ(input_type, Primitive::kPrimLong);
6839 locations->SetInAt(0, Location::RegisterPairLocation(
6840 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6841 }
6842
6843 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6844 }
6845}
6846
6847void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6848 LocationSummary* locations = conversion->GetLocations();
6849 Primitive::Type result_type = conversion->GetResultType();
6850 Primitive::Type input_type = conversion->GetInputType();
6851 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006852 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006853
6854 DCHECK_NE(input_type, result_type);
6855
6856 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6857 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6858 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6859 Register src = locations->InAt(0).AsRegister<Register>();
6860
Alexey Frunzea871ef12016-06-27 15:20:11 -07006861 if (dst_low != src) {
6862 __ Move(dst_low, src);
6863 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006864 __ Sra(dst_high, src, 31);
6865 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6866 Register dst = locations->Out().AsRegister<Register>();
6867 Register src = (input_type == Primitive::kPrimLong)
6868 ? locations->InAt(0).AsRegisterPairLow<Register>()
6869 : locations->InAt(0).AsRegister<Register>();
6870
6871 switch (result_type) {
6872 case Primitive::kPrimChar:
6873 __ Andi(dst, src, 0xFFFF);
6874 break;
6875 case Primitive::kPrimByte:
6876 if (has_sign_extension) {
6877 __ Seb(dst, src);
6878 } else {
6879 __ Sll(dst, src, 24);
6880 __ Sra(dst, dst, 24);
6881 }
6882 break;
6883 case Primitive::kPrimShort:
6884 if (has_sign_extension) {
6885 __ Seh(dst, src);
6886 } else {
6887 __ Sll(dst, src, 16);
6888 __ Sra(dst, dst, 16);
6889 }
6890 break;
6891 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006892 if (dst != src) {
6893 __ Move(dst, src);
6894 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006895 break;
6896
6897 default:
6898 LOG(FATAL) << "Unexpected type conversion from " << input_type
6899 << " to " << result_type;
6900 }
6901 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006902 if (input_type == Primitive::kPrimLong) {
6903 if (isR6) {
6904 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6905 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6906 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6907 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6908 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6909 __ Mtc1(src_low, FTMP);
6910 __ Mthc1(src_high, FTMP);
6911 if (result_type == Primitive::kPrimFloat) {
6912 __ Cvtsl(dst, FTMP);
6913 } else {
6914 __ Cvtdl(dst, FTMP);
6915 }
6916 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006917 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6918 : kQuickL2d;
6919 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006920 if (result_type == Primitive::kPrimFloat) {
6921 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6922 } else {
6923 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6924 }
6925 }
6926 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006927 Register src = locations->InAt(0).AsRegister<Register>();
6928 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6929 __ Mtc1(src, FTMP);
6930 if (result_type == Primitive::kPrimFloat) {
6931 __ Cvtsw(dst, FTMP);
6932 } else {
6933 __ Cvtdw(dst, FTMP);
6934 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006935 }
6936 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6937 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006938 if (result_type == Primitive::kPrimLong) {
6939 if (isR6) {
6940 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6941 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6942 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6943 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6944 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6945 MipsLabel truncate;
6946 MipsLabel done;
6947
6948 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6949 // value when the input is either a NaN or is outside of the range of the output type
6950 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6951 // the same result.
6952 //
6953 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6954 // value of the output type if the input is outside of the range after the truncation or
6955 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6956 // results. This matches the desired float/double-to-int/long conversion exactly.
6957 //
6958 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6959 //
6960 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6961 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6962 // even though it must be NAN2008=1 on R6.
6963 //
6964 // The code takes care of the different behaviors by first comparing the input to the
6965 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6966 // If the input is greater than or equal to the minimum, it procedes to the truncate
6967 // instruction, which will handle such an input the same way irrespective of NAN2008.
6968 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6969 // in order to return either zero or the minimum value.
6970 //
6971 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6972 // truncate instruction for MIPS64R6.
6973 if (input_type == Primitive::kPrimFloat) {
6974 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6975 __ LoadConst32(TMP, min_val);
6976 __ Mtc1(TMP, FTMP);
6977 __ CmpLeS(FTMP, FTMP, src);
6978 } else {
6979 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6980 __ LoadConst32(TMP, High32Bits(min_val));
6981 __ Mtc1(ZERO, FTMP);
6982 __ Mthc1(TMP, FTMP);
6983 __ CmpLeD(FTMP, FTMP, src);
6984 }
6985
6986 __ Bc1nez(FTMP, &truncate);
6987
6988 if (input_type == Primitive::kPrimFloat) {
6989 __ CmpEqS(FTMP, src, src);
6990 } else {
6991 __ CmpEqD(FTMP, src, src);
6992 }
6993 __ Move(dst_low, ZERO);
6994 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6995 __ Mfc1(TMP, FTMP);
6996 __ And(dst_high, dst_high, TMP);
6997
6998 __ B(&done);
6999
7000 __ Bind(&truncate);
7001
7002 if (input_type == Primitive::kPrimFloat) {
7003 __ TruncLS(FTMP, src);
7004 } else {
7005 __ TruncLD(FTMP, src);
7006 }
7007 __ Mfc1(dst_low, FTMP);
7008 __ Mfhc1(dst_high, FTMP);
7009
7010 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007011 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007012 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
7013 : kQuickD2l;
7014 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007015 if (input_type == Primitive::kPrimFloat) {
7016 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
7017 } else {
7018 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
7019 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007020 }
7021 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007022 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7023 Register dst = locations->Out().AsRegister<Register>();
7024 MipsLabel truncate;
7025 MipsLabel done;
7026
7027 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
7028 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
7029 // even though it must be NAN2008=1 on R6.
7030 //
7031 // For details see the large comment above for the truncation of float/double to long on R6.
7032 //
7033 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
7034 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007035 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007036 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
7037 __ LoadConst32(TMP, min_val);
7038 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007039 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007040 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
7041 __ LoadConst32(TMP, High32Bits(min_val));
7042 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07007043 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007044 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007045
7046 if (isR6) {
7047 if (input_type == Primitive::kPrimFloat) {
7048 __ CmpLeS(FTMP, FTMP, src);
7049 } else {
7050 __ CmpLeD(FTMP, FTMP, src);
7051 }
7052 __ Bc1nez(FTMP, &truncate);
7053
7054 if (input_type == Primitive::kPrimFloat) {
7055 __ CmpEqS(FTMP, src, src);
7056 } else {
7057 __ CmpEqD(FTMP, src, src);
7058 }
7059 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
7060 __ Mfc1(TMP, FTMP);
7061 __ And(dst, dst, TMP);
7062 } else {
7063 if (input_type == Primitive::kPrimFloat) {
7064 __ ColeS(0, FTMP, src);
7065 } else {
7066 __ ColeD(0, FTMP, src);
7067 }
7068 __ Bc1t(0, &truncate);
7069
7070 if (input_type == Primitive::kPrimFloat) {
7071 __ CeqS(0, src, src);
7072 } else {
7073 __ CeqD(0, src, src);
7074 }
7075 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
7076 __ Movf(dst, ZERO, 0);
7077 }
7078
7079 __ B(&done);
7080
7081 __ Bind(&truncate);
7082
7083 if (input_type == Primitive::kPrimFloat) {
7084 __ TruncWS(FTMP, src);
7085 } else {
7086 __ TruncWD(FTMP, src);
7087 }
7088 __ Mfc1(dst, FTMP);
7089
7090 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007091 }
7092 } else if (Primitive::IsFloatingPointType(result_type) &&
7093 Primitive::IsFloatingPointType(input_type)) {
7094 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7095 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7096 if (result_type == Primitive::kPrimFloat) {
7097 __ Cvtsd(dst, src);
7098 } else {
7099 __ Cvtds(dst, src);
7100 }
7101 } else {
7102 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7103 << " to " << result_type;
7104 }
7105}
7106
7107void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
7108 HandleShift(ushr);
7109}
7110
7111void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
7112 HandleShift(ushr);
7113}
7114
7115void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
7116 HandleBinaryOp(instruction);
7117}
7118
7119void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
7120 HandleBinaryOp(instruction);
7121}
7122
7123void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7124 // Nothing to do, this should be removed during prepare for register allocator.
7125 LOG(FATAL) << "Unreachable";
7126}
7127
7128void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7129 // Nothing to do, this should be removed during prepare for register allocator.
7130 LOG(FATAL) << "Unreachable";
7131}
7132
7133void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007134 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007135}
7136
7137void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007138 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007139}
7140
7141void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007142 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007143}
7144
7145void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007146 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007147}
7148
7149void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007150 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007151}
7152
7153void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007154 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007155}
7156
7157void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007158 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007159}
7160
7161void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007162 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007163}
7164
7165void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007166 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007167}
7168
7169void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007170 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007171}
7172
7173void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007174 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007175}
7176
7177void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007178 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007179}
7180
7181void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007182 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007183}
7184
7185void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007186 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007187}
7188
7189void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007190 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007191}
7192
7193void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007194 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007195}
7196
7197void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007198 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199}
7200
7201void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007202 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007203}
7204
7205void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007206 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007207}
7208
7209void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007210 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007211}
7212
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007213void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7214 LocationSummary* locations =
7215 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7216 locations->SetInAt(0, Location::RequiresRegister());
7217}
7218
Alexey Frunze96b66822016-09-10 02:32:44 -07007219void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
7220 int32_t lower_bound,
7221 uint32_t num_entries,
7222 HBasicBlock* switch_block,
7223 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007224 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007225 Register temp_reg = TMP;
7226 __ Addiu32(temp_reg, value_reg, -lower_bound);
7227 // Jump to default if index is negative
7228 // Note: We don't check the case that index is positive while value < lower_bound, because in
7229 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7230 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
7231
Alexey Frunze96b66822016-09-10 02:32:44 -07007232 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007233 // Jump to successors[0] if value == lower_bound.
7234 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
7235 int32_t last_index = 0;
7236 for (; num_entries - last_index > 2; last_index += 2) {
7237 __ Addiu(temp_reg, temp_reg, -2);
7238 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7239 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7240 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7241 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7242 }
7243 if (num_entries - last_index == 2) {
7244 // The last missing case_value.
7245 __ Addiu(temp_reg, temp_reg, -1);
7246 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007247 }
7248
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007249 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07007250 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007251 __ B(codegen_->GetLabelOf(default_block));
7252 }
7253}
7254
Alexey Frunze96b66822016-09-10 02:32:44 -07007255void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
7256 Register constant_area,
7257 int32_t lower_bound,
7258 uint32_t num_entries,
7259 HBasicBlock* switch_block,
7260 HBasicBlock* default_block) {
7261 // Create a jump table.
7262 std::vector<MipsLabel*> labels(num_entries);
7263 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7264 for (uint32_t i = 0; i < num_entries; i++) {
7265 labels[i] = codegen_->GetLabelOf(successors[i]);
7266 }
7267 JumpTable* table = __ CreateJumpTable(std::move(labels));
7268
7269 // Is the value in range?
7270 __ Addiu32(TMP, value_reg, -lower_bound);
7271 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
7272 __ Sltiu(AT, TMP, num_entries);
7273 __ Beqz(AT, codegen_->GetLabelOf(default_block));
7274 } else {
7275 __ LoadConst32(AT, num_entries);
7276 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
7277 }
7278
7279 // We are in the range of the table.
7280 // Load the target address from the jump table, indexing by the value.
7281 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
7282 __ Sll(TMP, TMP, 2);
7283 __ Addu(TMP, TMP, AT);
7284 __ Lw(TMP, TMP, 0);
7285 // Compute the absolute target address by adding the table start address
7286 // (the table contains offsets to targets relative to its start).
7287 __ Addu(TMP, TMP, AT);
7288 // And jump.
7289 __ Jr(TMP);
7290 __ NopIfNoReordering();
7291}
7292
7293void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7294 int32_t lower_bound = switch_instr->GetStartValue();
7295 uint32_t num_entries = switch_instr->GetNumEntries();
7296 LocationSummary* locations = switch_instr->GetLocations();
7297 Register value_reg = locations->InAt(0).AsRegister<Register>();
7298 HBasicBlock* switch_block = switch_instr->GetBlock();
7299 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7300
7301 if (codegen_->GetInstructionSetFeatures().IsR6() &&
7302 num_entries > kPackedSwitchJumpTableThreshold) {
7303 // R6 uses PC-relative addressing to access the jump table.
7304 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
7305 // the jump table and it is implemented by changing HPackedSwitch to
7306 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
7307 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
7308 GenTableBasedPackedSwitch(value_reg,
7309 ZERO,
7310 lower_bound,
7311 num_entries,
7312 switch_block,
7313 default_block);
7314 } else {
7315 GenPackedSwitchWithCompares(value_reg,
7316 lower_bound,
7317 num_entries,
7318 switch_block,
7319 default_block);
7320 }
7321}
7322
7323void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7324 LocationSummary* locations =
7325 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7326 locations->SetInAt(0, Location::RequiresRegister());
7327 // Constant area pointer (HMipsComputeBaseMethodAddress).
7328 locations->SetInAt(1, Location::RequiresRegister());
7329}
7330
7331void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7332 int32_t lower_bound = switch_instr->GetStartValue();
7333 uint32_t num_entries = switch_instr->GetNumEntries();
7334 LocationSummary* locations = switch_instr->GetLocations();
7335 Register value_reg = locations->InAt(0).AsRegister<Register>();
7336 Register constant_area = locations->InAt(1).AsRegister<Register>();
7337 HBasicBlock* switch_block = switch_instr->GetBlock();
7338 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7339
7340 // This is an R2-only path. HPackedSwitch has been changed to
7341 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
7342 // required to address the jump table relative to PC.
7343 GenTableBasedPackedSwitch(value_reg,
7344 constant_area,
7345 lower_bound,
7346 num_entries,
7347 switch_block,
7348 default_block);
7349}
7350
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007351void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
7352 HMipsComputeBaseMethodAddress* insn) {
7353 LocationSummary* locations =
7354 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7355 locations->SetOut(Location::RequiresRegister());
7356}
7357
7358void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
7359 HMipsComputeBaseMethodAddress* insn) {
7360 LocationSummary* locations = insn->GetLocations();
7361 Register reg = locations->Out().AsRegister<Register>();
7362
7363 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7364
7365 // Generate a dummy PC-relative call to obtain PC.
7366 __ Nal();
7367 // Grab the return address off RA.
7368 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007369 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007370
7371 // Remember this offset (the obtained PC value) for later use with constant area.
7372 __ BindPcRelBaseLabel();
7373}
7374
7375void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7377 locations->SetOut(Location::RequiresRegister());
7378}
7379
7380void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7381 Register reg = base->GetLocations()->Out().AsRegister<Register>();
7382 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7383 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007384 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7385 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007386 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007387 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
7388 __ Addiu(reg, reg, /* placeholder */ 0x5678);
7389 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007390}
7391
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007392void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7393 // The trampoline uses the same calling convention as dex calling conventions,
7394 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
7395 // the method_idx.
7396 HandleInvoke(invoke);
7397}
7398
7399void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7400 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
7401}
7402
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007403void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7404 LocationSummary* locations =
7405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7406 locations->SetInAt(0, Location::RequiresRegister());
7407 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007408}
7409
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007410void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7411 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007412 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007413 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007414 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007415 __ LoadFromOffset(kLoadWord,
7416 locations->Out().AsRegister<Register>(),
7417 locations->InAt(0).AsRegister<Register>(),
7418 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007419 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007420 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007421 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007422 __ LoadFromOffset(kLoadWord,
7423 locations->Out().AsRegister<Register>(),
7424 locations->InAt(0).AsRegister<Register>(),
7425 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007426 __ LoadFromOffset(kLoadWord,
7427 locations->Out().AsRegister<Register>(),
7428 locations->Out().AsRegister<Register>(),
7429 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007430 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007431}
7432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007433#undef __
7434#undef QUICK_ENTRY_POINT
7435
7436} // namespace mips
7437} // namespace art