blob: 24ab4b19527d958b377e1a1ac6cd28d85dbdadeb [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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
397 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398 uint32_t dex_pc = instruction_->GetDexPc();
399 DCHECK(instruction_->IsCheckCast()
400 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
401 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
402
403 __ Bind(GetEntryLabel());
404 SaveLiveRegisters(codegen, locations);
405
406 // We're moving two locations to locations that could overlap, so we need a parallel
407 // move resolver.
408 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
411 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800412 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
414 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100416 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800417 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 Primitive::Type ret_type = instruction_->GetType();
419 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
420 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 } else {
422 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800423 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
424 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 }
426
427 RestoreLiveRegisters(codegen, locations);
428 __ B(GetExitLabel());
429 }
430
431 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
432
433 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
435};
436
437class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
438 public:
Aart Bik42249c32016-01-07 15:33:50 -0800439 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000440 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441
442 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800443 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100445 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000446 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 }
448
449 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
450
451 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
453};
454
455CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
456 const MipsInstructionSetFeatures& isa_features,
457 const CompilerOptions& compiler_options,
458 OptimizingCompilerStats* stats)
459 : CodeGenerator(graph,
460 kNumberOfCoreRegisters,
461 kNumberOfFRegisters,
462 kNumberOfRegisterPairs,
463 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
464 arraysize(kCoreCalleeSaves)),
465 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
466 arraysize(kFpuCalleeSaves)),
467 compiler_options,
468 stats),
469 block_labels_(nullptr),
470 location_builder_(graph, this),
471 instruction_visitor_(graph, this),
472 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100473 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700474 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700475 uint32_literals_(std::less<uint32_t>(),
476 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700477 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
478 boot_image_string_patches_(StringReferenceValueComparator(),
479 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
480 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
481 boot_image_type_patches_(TypeReferenceValueComparator(),
482 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
483 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000484 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700485 boot_image_address_patches_(std::less<uint32_t>(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800487 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
488 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700489 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490 // Save RA (containing the return address) to mimic Quick.
491 AddAllocatedRegister(Location::RegisterLocation(RA));
492}
493
494#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100495// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
496#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700497#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498
499void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
500 // Ensure that we fix up branches.
501 __ FinalizeCode();
502
503 // Adjust native pc offsets in stack maps.
504 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800505 uint32_t old_position =
506 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200507 uint32_t new_position = __ GetAdjustedPosition(old_position);
508 DCHECK_GE(new_position, old_position);
509 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
510 }
511
512 // Adjust pc offsets for the disassembly information.
513 if (disasm_info_ != nullptr) {
514 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
515 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
516 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
517 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
518 it.second.start = __ GetAdjustedPosition(it.second.start);
519 it.second.end = __ GetAdjustedPosition(it.second.end);
520 }
521 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
522 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
523 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
524 }
525 }
526
527 CodeGenerator::Finalize(allocator);
528}
529
530MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
531 return codegen_->GetAssembler();
532}
533
534void ParallelMoveResolverMIPS::EmitMove(size_t index) {
535 DCHECK_LT(index, moves_.size());
536 MoveOperands* move = moves_[index];
537 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
538}
539
540void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
541 DCHECK_LT(index, moves_.size());
542 MoveOperands* move = moves_[index];
543 Primitive::Type type = move->GetType();
544 Location loc1 = move->GetDestination();
545 Location loc2 = move->GetSource();
546
547 DCHECK(!loc1.IsConstant());
548 DCHECK(!loc2.IsConstant());
549
550 if (loc1.Equals(loc2)) {
551 return;
552 }
553
554 if (loc1.IsRegister() && loc2.IsRegister()) {
555 // Swap 2 GPRs.
556 Register r1 = loc1.AsRegister<Register>();
557 Register r2 = loc2.AsRegister<Register>();
558 __ Move(TMP, r2);
559 __ Move(r2, r1);
560 __ Move(r1, TMP);
561 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
562 FRegister f1 = loc1.AsFpuRegister<FRegister>();
563 FRegister f2 = loc2.AsFpuRegister<FRegister>();
564 if (type == Primitive::kPrimFloat) {
565 __ MovS(FTMP, f2);
566 __ MovS(f2, f1);
567 __ MovS(f1, FTMP);
568 } else {
569 DCHECK_EQ(type, Primitive::kPrimDouble);
570 __ MovD(FTMP, f2);
571 __ MovD(f2, f1);
572 __ MovD(f1, FTMP);
573 }
574 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
575 (loc1.IsFpuRegister() && loc2.IsRegister())) {
576 // Swap FPR and GPR.
577 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
578 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
579 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200580 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200581 __ Move(TMP, r2);
582 __ Mfc1(r2, f1);
583 __ Mtc1(TMP, f1);
584 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
585 // Swap 2 GPR register pairs.
586 Register r1 = loc1.AsRegisterPairLow<Register>();
587 Register r2 = loc2.AsRegisterPairLow<Register>();
588 __ Move(TMP, r2);
589 __ Move(r2, r1);
590 __ Move(r1, TMP);
591 r1 = loc1.AsRegisterPairHigh<Register>();
592 r2 = loc2.AsRegisterPairHigh<Register>();
593 __ Move(TMP, r2);
594 __ Move(r2, r1);
595 __ Move(r1, TMP);
596 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
597 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
598 // Swap FPR and GPR register pair.
599 DCHECK_EQ(type, Primitive::kPrimDouble);
600 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
601 : loc2.AsFpuRegister<FRegister>();
602 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
603 : loc2.AsRegisterPairLow<Register>();
604 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
605 : loc2.AsRegisterPairHigh<Register>();
606 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
607 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
608 // unpredictable and the following mfch1 will fail.
609 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800610 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200611 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800612 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200613 __ Move(r2_l, TMP);
614 __ Move(r2_h, AT);
615 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
617 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
618 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000619 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
620 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200621 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
622 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000623 __ Move(TMP, reg);
624 __ LoadFromOffset(kLoadWord, reg, SP, offset);
625 __ StoreToOffset(kStoreWord, TMP, SP, offset);
626 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
627 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
628 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
629 : loc2.AsRegisterPairLow<Register>();
630 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
631 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200632 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000633 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
634 : loc2.GetHighStackIndex(kMipsWordSize);
635 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000636 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000637 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000638 __ Move(TMP, reg_h);
639 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
640 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200641 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
642 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
643 : loc2.AsFpuRegister<FRegister>();
644 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
645 if (type == Primitive::kPrimFloat) {
646 __ MovS(FTMP, reg);
647 __ LoadSFromOffset(reg, SP, offset);
648 __ StoreSToOffset(FTMP, SP, offset);
649 } else {
650 DCHECK_EQ(type, Primitive::kPrimDouble);
651 __ MovD(FTMP, reg);
652 __ LoadDFromOffset(reg, SP, offset);
653 __ StoreDToOffset(FTMP, SP, offset);
654 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200655 } else {
656 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
657 }
658}
659
660void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
661 __ Pop(static_cast<Register>(reg));
662}
663
664void ParallelMoveResolverMIPS::SpillScratch(int reg) {
665 __ Push(static_cast<Register>(reg));
666}
667
668void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
669 // Allocate a scratch register other than TMP, if available.
670 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
671 // automatically unspilled when the scratch scope object is destroyed).
672 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
673 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
674 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
675 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
676 __ LoadFromOffset(kLoadWord,
677 Register(ensure_scratch.GetRegister()),
678 SP,
679 index1 + stack_offset);
680 __ LoadFromOffset(kLoadWord,
681 TMP,
682 SP,
683 index2 + stack_offset);
684 __ StoreToOffset(kStoreWord,
685 Register(ensure_scratch.GetRegister()),
686 SP,
687 index2 + stack_offset);
688 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
689 }
690}
691
Alexey Frunze73296a72016-06-03 22:51:46 -0700692void CodeGeneratorMIPS::ComputeSpillMask() {
693 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
694 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
695 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
696 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
697 // registers, include the ZERO register to force alignment of FPU callee-saved registers
698 // within the stack frame.
699 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
700 core_spill_mask_ |= (1 << ZERO);
701 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700702}
703
704bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700705 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700706 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
707 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
708 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700709 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700710}
711
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200712static dwarf::Reg DWARFReg(Register reg) {
713 return dwarf::Reg::MipsCore(static_cast<int>(reg));
714}
715
716// TODO: mapping of floating-point registers to DWARF.
717
718void CodeGeneratorMIPS::GenerateFrameEntry() {
719 __ Bind(&frame_entry_label_);
720
721 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
722
723 if (do_overflow_check) {
724 __ LoadFromOffset(kLoadWord,
725 ZERO,
726 SP,
727 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
728 RecordPcInfo(nullptr, 0);
729 }
730
731 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700732 CHECK_EQ(fpu_spill_mask_, 0u);
733 CHECK_EQ(core_spill_mask_, 1u << RA);
734 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200735 return;
736 }
737
738 // Make sure the frame size isn't unreasonably large.
739 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
740 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
741 }
742
743 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200746 __ IncreaseFrameSize(ofs);
747
Alexey Frunze73296a72016-06-03 22:51:46 -0700748 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
749 Register reg = static_cast<Register>(MostSignificantBit(mask));
750 mask ^= 1u << reg;
751 ofs -= kMipsWordSize;
752 // The ZERO register is only included for alignment.
753 if (reg != ZERO) {
754 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200755 __ cfi().RelOffset(DWARFReg(reg), ofs);
756 }
757 }
758
Alexey Frunze73296a72016-06-03 22:51:46 -0700759 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
760 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
761 mask ^= 1u << reg;
762 ofs -= kMipsDoublewordSize;
763 __ StoreDToOffset(reg, SP, ofs);
764 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200765 }
766
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100767 // Save the current method if we need it. Note that we do not
768 // do this in HCurrentMethod, as the instruction might have been removed
769 // in the SSA graph.
770 if (RequiresCurrentMethod()) {
771 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
772 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100773
774 if (GetGraph()->HasShouldDeoptimizeFlag()) {
775 // Initialize should deoptimize flag to 0.
776 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
777 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200778}
779
780void CodeGeneratorMIPS::GenerateFrameExit() {
781 __ cfi().RememberState();
782
783 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200784 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200785
Alexey Frunze73296a72016-06-03 22:51:46 -0700786 // For better instruction scheduling restore RA before other registers.
787 uint32_t ofs = GetFrameSize();
788 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
789 Register reg = static_cast<Register>(MostSignificantBit(mask));
790 mask ^= 1u << reg;
791 ofs -= kMipsWordSize;
792 // The ZERO register is only included for alignment.
793 if (reg != ZERO) {
794 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200795 __ cfi().Restore(DWARFReg(reg));
796 }
797 }
798
Alexey Frunze73296a72016-06-03 22:51:46 -0700799 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
800 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
801 mask ^= 1u << reg;
802 ofs -= kMipsDoublewordSize;
803 __ LoadDFromOffset(reg, SP, ofs);
804 // TODO: __ cfi().Restore(DWARFReg(reg));
805 }
806
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700807 size_t frame_size = GetFrameSize();
808 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
809 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
810 bool reordering = __ SetReorder(false);
811 if (exchange) {
812 __ Jr(RA);
813 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
814 } else {
815 __ DecreaseFrameSize(frame_size);
816 __ Jr(RA);
817 __ Nop(); // In delay slot.
818 }
819 __ SetReorder(reordering);
820 } else {
821 __ Jr(RA);
822 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200823 }
824
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200825 __ cfi().RestoreState();
826 __ cfi().DefCFAOffset(GetFrameSize());
827}
828
829void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
830 __ Bind(GetLabelOf(block));
831}
832
833void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
834 if (src.Equals(dst)) {
835 return;
836 }
837
838 if (src.IsConstant()) {
839 MoveConstant(dst, src.GetConstant());
840 } else {
841 if (Primitive::Is64BitType(dst_type)) {
842 Move64(dst, src);
843 } else {
844 Move32(dst, src);
845 }
846 }
847}
848
849void CodeGeneratorMIPS::Move32(Location destination, Location source) {
850 if (source.Equals(destination)) {
851 return;
852 }
853
854 if (destination.IsRegister()) {
855 if (source.IsRegister()) {
856 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
857 } else if (source.IsFpuRegister()) {
858 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
859 } else {
860 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
861 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
862 }
863 } else if (destination.IsFpuRegister()) {
864 if (source.IsRegister()) {
865 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
866 } else if (source.IsFpuRegister()) {
867 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
868 } else {
869 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
870 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
871 }
872 } else {
873 DCHECK(destination.IsStackSlot()) << destination;
874 if (source.IsRegister()) {
875 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
876 } else if (source.IsFpuRegister()) {
877 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
878 } else {
879 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
880 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
881 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
882 }
883 }
884}
885
886void CodeGeneratorMIPS::Move64(Location destination, Location source) {
887 if (source.Equals(destination)) {
888 return;
889 }
890
891 if (destination.IsRegisterPair()) {
892 if (source.IsRegisterPair()) {
893 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
894 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
895 } else if (source.IsFpuRegister()) {
896 Register dst_high = destination.AsRegisterPairHigh<Register>();
897 Register dst_low = destination.AsRegisterPairLow<Register>();
898 FRegister src = source.AsFpuRegister<FRegister>();
899 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800900 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200901 } else {
902 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
903 int32_t off = source.GetStackIndex();
904 Register r = destination.AsRegisterPairLow<Register>();
905 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
906 }
907 } else if (destination.IsFpuRegister()) {
908 if (source.IsRegisterPair()) {
909 FRegister dst = destination.AsFpuRegister<FRegister>();
910 Register src_high = source.AsRegisterPairHigh<Register>();
911 Register src_low = source.AsRegisterPairLow<Register>();
912 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800913 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200914 } else if (source.IsFpuRegister()) {
915 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
916 } else {
917 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
918 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
919 }
920 } else {
921 DCHECK(destination.IsDoubleStackSlot()) << destination;
922 int32_t off = destination.GetStackIndex();
923 if (source.IsRegisterPair()) {
924 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
925 } else if (source.IsFpuRegister()) {
926 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
927 } else {
928 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
929 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
930 __ StoreToOffset(kStoreWord, TMP, SP, off);
931 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
932 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
933 }
934 }
935}
936
937void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
938 if (c->IsIntConstant() || c->IsNullConstant()) {
939 // Move 32 bit constant.
940 int32_t value = GetInt32ValueOf(c);
941 if (destination.IsRegister()) {
942 Register dst = destination.AsRegister<Register>();
943 __ LoadConst32(dst, value);
944 } else {
945 DCHECK(destination.IsStackSlot())
946 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700947 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200948 }
949 } else if (c->IsLongConstant()) {
950 // Move 64 bit constant.
951 int64_t value = GetInt64ValueOf(c);
952 if (destination.IsRegisterPair()) {
953 Register r_h = destination.AsRegisterPairHigh<Register>();
954 Register r_l = destination.AsRegisterPairLow<Register>();
955 __ LoadConst64(r_h, r_l, value);
956 } else {
957 DCHECK(destination.IsDoubleStackSlot())
958 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700959 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200960 }
961 } else if (c->IsFloatConstant()) {
962 // Move 32 bit float constant.
963 int32_t value = GetInt32ValueOf(c);
964 if (destination.IsFpuRegister()) {
965 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
966 } else {
967 DCHECK(destination.IsStackSlot())
968 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700969 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200970 }
971 } else {
972 // Move 64 bit double constant.
973 DCHECK(c->IsDoubleConstant()) << c->DebugName();
974 int64_t value = GetInt64ValueOf(c);
975 if (destination.IsFpuRegister()) {
976 FRegister fd = destination.AsFpuRegister<FRegister>();
977 __ LoadDConst64(fd, value, TMP);
978 } else {
979 DCHECK(destination.IsDoubleStackSlot())
980 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700981 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200982 }
983 }
984}
985
986void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
987 DCHECK(destination.IsRegister());
988 Register dst = destination.AsRegister<Register>();
989 __ LoadConst32(dst, value);
990}
991
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200992void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
993 if (location.IsRegister()) {
994 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700995 } else if (location.IsRegisterPair()) {
996 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
997 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200998 } else {
999 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1000 }
1001}
1002
Vladimir Markoaad75c62016-10-03 08:46:48 +00001003template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1004inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1005 const ArenaDeque<PcRelativePatchInfo>& infos,
1006 ArenaVector<LinkerPatch>* linker_patches) {
1007 for (const PcRelativePatchInfo& info : infos) {
1008 const DexFile& dex_file = info.target_dex_file;
1009 size_t offset_or_index = info.offset_or_index;
1010 DCHECK(info.high_label.IsBound());
1011 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1012 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1013 // the assembler's base label used for PC-relative addressing.
1014 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1015 ? __ GetLabelLocation(&info.pc_rel_label)
1016 : __ GetPcRelBaseLabelLocation();
1017 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1018 }
1019}
1020
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001021void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1022 DCHECK(linker_patches->empty());
1023 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001024 pc_relative_dex_cache_patches_.size() +
1025 pc_relative_string_patches_.size() +
1026 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001027 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 boot_image_string_patches_.size() +
1029 boot_image_type_patches_.size() +
1030 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001031 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001032 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1033 linker_patches);
1034 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001035 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001036 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1037 linker_patches);
1038 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001039 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1040 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001041 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1042 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001043 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001044 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1045 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001046 for (const auto& entry : boot_image_string_patches_) {
1047 const StringReference& target_string = entry.first;
1048 Literal* literal = entry.second;
1049 DCHECK(literal->GetLabel()->IsBound());
1050 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1051 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1052 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001053 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001054 }
1055 for (const auto& entry : boot_image_type_patches_) {
1056 const TypeReference& target_type = entry.first;
1057 Literal* literal = entry.second;
1058 DCHECK(literal->GetLabel()->IsBound());
1059 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1060 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1061 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001062 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001063 }
1064 for (const auto& entry : boot_image_address_patches_) {
1065 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1066 Literal* literal = entry.second;
1067 DCHECK(literal->GetLabel()->IsBound());
1068 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1069 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1070 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001071 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001072}
1073
1074CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001075 const DexFile& dex_file, dex::StringIndex string_index) {
1076 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001077}
1078
1079CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001080 const DexFile& dex_file, dex::TypeIndex type_index) {
1081 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001082}
1083
Vladimir Marko1998cd02017-01-13 13:02:58 +00001084CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1085 const DexFile& dex_file, dex::TypeIndex type_index) {
1086 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1087}
1088
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001089CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1090 const DexFile& dex_file, uint32_t element_offset) {
1091 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1092}
1093
1094CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1095 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1096 patches->emplace_back(dex_file, offset_or_index);
1097 return &patches->back();
1098}
1099
Alexey Frunze06a46c42016-07-19 15:00:40 -07001100Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1101 return map->GetOrCreate(
1102 value,
1103 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1104}
1105
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001106Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1107 MethodToLiteralMap* map) {
1108 return map->GetOrCreate(
1109 target_method,
1110 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1111}
1112
Alexey Frunze06a46c42016-07-19 15:00:40 -07001113Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001114 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001115 return boot_image_string_patches_.GetOrCreate(
1116 StringReference(&dex_file, string_index),
1117 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1118}
1119
1120Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001121 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001122 return boot_image_type_patches_.GetOrCreate(
1123 TypeReference(&dex_file, type_index),
1124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1125}
1126
1127Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1128 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1129 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1130 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1131}
1132
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001133void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1134 Register out,
1135 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001136 if (GetInstructionSetFeatures().IsR6()) {
1137 DCHECK_EQ(base, ZERO);
1138 __ Bind(&info->high_label);
1139 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001140 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001141 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001142 } else {
1143 // If base is ZERO, emit NAL to obtain the actual base.
1144 if (base == ZERO) {
1145 // Generate a dummy PC-relative call to obtain PC.
1146 __ Nal();
1147 }
1148 __ Bind(&info->high_label);
1149 __ Lui(out, /* placeholder */ 0x1234);
1150 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1151 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1152 if (base == ZERO) {
1153 __ Bind(&info->pc_rel_label);
1154 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001155 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001156 __ Addu(out, out, (base == ZERO) ? RA : base);
1157 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001158 // The immediately following instruction will add the sign-extended low half of the 32-bit
1159 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001160}
1161
Alexey Frunze627c1a02017-01-30 19:28:14 -08001162CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1163 const DexFile& dex_file,
1164 dex::StringIndex dex_index,
1165 Handle<mirror::String> handle) {
1166 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1167 reinterpret_cast64<uint64_t>(handle.GetReference()));
1168 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1169 return &jit_string_patches_.back();
1170}
1171
1172CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1173 const DexFile& dex_file,
1174 dex::TypeIndex dex_index,
1175 Handle<mirror::Class> handle) {
1176 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1177 reinterpret_cast64<uint64_t>(handle.GetReference()));
1178 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1179 return &jit_class_patches_.back();
1180}
1181
1182void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1183 const uint8_t* roots_data,
1184 const CodeGeneratorMIPS::JitPatchInfo& info,
1185 uint64_t index_in_table) const {
1186 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1187 uintptr_t address =
1188 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1189 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1190 // lui reg, addr32_high
1191 DCHECK_EQ(code[literal_offset + 0], 0x34);
1192 DCHECK_EQ(code[literal_offset + 1], 0x12);
1193 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1194 DCHECK_EQ(code[literal_offset + 3], 0x3C);
1195 // lw reg, reg, addr32_low
1196 DCHECK_EQ(code[literal_offset + 4], 0x78);
1197 DCHECK_EQ(code[literal_offset + 5], 0x56);
1198 DCHECK_EQ((code[literal_offset + 7] & 0xFC), 0x8C);
1199 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "lw reg, reg, addr32_low".
1200 // lui reg, addr32_high
1201 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1202 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
1203 // lw reg, reg, addr32_low
1204 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1205 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1206}
1207
1208void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1209 for (const JitPatchInfo& info : jit_string_patches_) {
1210 const auto& it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1211 dex::StringIndex(info.index)));
1212 DCHECK(it != jit_string_roots_.end());
1213 PatchJitRootUse(code, roots_data, info, it->second);
1214 }
1215 for (const JitPatchInfo& info : jit_class_patches_) {
1216 const auto& it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1217 dex::TypeIndex(info.index)));
1218 DCHECK(it != jit_class_roots_.end());
1219 PatchJitRootUse(code, roots_data, info, it->second);
1220 }
1221}
1222
Goran Jakovljevice114da22016-12-26 14:21:43 +01001223void CodeGeneratorMIPS::MarkGCCard(Register object,
1224 Register value,
1225 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001226 MipsLabel done;
1227 Register card = AT;
1228 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001229 if (value_can_be_null) {
1230 __ Beqz(value, &done);
1231 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232 __ LoadFromOffset(kLoadWord,
1233 card,
1234 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001235 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1237 __ Addu(temp, card, temp);
1238 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001239 if (value_can_be_null) {
1240 __ Bind(&done);
1241 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242}
1243
David Brazdil58282f42016-01-14 12:45:10 +00001244void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1246 blocked_core_registers_[ZERO] = true;
1247 blocked_core_registers_[K0] = true;
1248 blocked_core_registers_[K1] = true;
1249 blocked_core_registers_[GP] = true;
1250 blocked_core_registers_[SP] = true;
1251 blocked_core_registers_[RA] = true;
1252
1253 // AT and TMP(T8) are used as temporary/scratch registers
1254 // (similar to how AT is used by MIPS assemblers).
1255 blocked_core_registers_[AT] = true;
1256 blocked_core_registers_[TMP] = true;
1257 blocked_fpu_registers_[FTMP] = true;
1258
1259 // Reserve suspend and thread registers.
1260 blocked_core_registers_[S0] = true;
1261 blocked_core_registers_[TR] = true;
1262
1263 // Reserve T9 for function calls
1264 blocked_core_registers_[T9] = true;
1265
1266 // Reserve odd-numbered FPU registers.
1267 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1268 blocked_fpu_registers_[i] = true;
1269 }
1270
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001271 if (GetGraph()->IsDebuggable()) {
1272 // Stubs do not save callee-save floating point registers. If the graph
1273 // is debuggable, we need to deal with these registers differently. For
1274 // now, just block them.
1275 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1276 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1277 }
1278 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001279}
1280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1282 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1283 return kMipsWordSize;
1284}
1285
1286size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1287 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1288 return kMipsWordSize;
1289}
1290
1291size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1292 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1293 return kMipsDoublewordSize;
1294}
1295
1296size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1297 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1298 return kMipsDoublewordSize;
1299}
1300
1301void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001302 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001303}
1304
1305void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001306 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001307}
1308
Serban Constantinescufca16662016-07-14 09:21:59 +01001309constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1310
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1312 HInstruction* instruction,
1313 uint32_t dex_pc,
1314 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001315 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001316 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001317 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001318 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001319 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001320 // Reserve argument space on stack (for $a0-$a3) for
1321 // entrypoints that directly reference native implementations.
1322 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001323 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001325 } else {
1326 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001327 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001328 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001329 if (EntrypointRequiresStackMap(entrypoint)) {
1330 RecordPcInfo(instruction, dex_pc, slow_path);
1331 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332}
1333
1334void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1335 Register class_reg) {
1336 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1337 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1338 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1339 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1340 __ Sync(0);
1341 __ Bind(slow_path->GetExitLabel());
1342}
1343
1344void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1345 __ Sync(0); // Only stype 0 is supported.
1346}
1347
1348void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1349 HBasicBlock* successor) {
1350 SuspendCheckSlowPathMIPS* slow_path =
1351 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1352 codegen_->AddSlowPath(slow_path);
1353
1354 __ LoadFromOffset(kLoadUnsignedHalfword,
1355 TMP,
1356 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001357 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001358 if (successor == nullptr) {
1359 __ Bnez(TMP, slow_path->GetEntryLabel());
1360 __ Bind(slow_path->GetReturnLabel());
1361 } else {
1362 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1363 __ B(slow_path->GetEntryLabel());
1364 // slow_path will return to GetLabelOf(successor).
1365 }
1366}
1367
1368InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1369 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001370 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371 assembler_(codegen->GetAssembler()),
1372 codegen_(codegen) {}
1373
1374void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1375 DCHECK_EQ(instruction->InputCount(), 2U);
1376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1377 Primitive::Type type = instruction->GetResultType();
1378 switch (type) {
1379 case Primitive::kPrimInt: {
1380 locations->SetInAt(0, Location::RequiresRegister());
1381 HInstruction* right = instruction->InputAt(1);
1382 bool can_use_imm = false;
1383 if (right->IsConstant()) {
1384 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1385 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1386 can_use_imm = IsUint<16>(imm);
1387 } else if (instruction->IsAdd()) {
1388 can_use_imm = IsInt<16>(imm);
1389 } else {
1390 DCHECK(instruction->IsSub());
1391 can_use_imm = IsInt<16>(-imm);
1392 }
1393 }
1394 if (can_use_imm)
1395 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1396 else
1397 locations->SetInAt(1, Location::RequiresRegister());
1398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1399 break;
1400 }
1401
1402 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001404 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 break;
1407 }
1408
1409 case Primitive::kPrimFloat:
1410 case Primitive::kPrimDouble:
1411 DCHECK(instruction->IsAdd() || instruction->IsSub());
1412 locations->SetInAt(0, Location::RequiresFpuRegister());
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1415 break;
1416
1417 default:
1418 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1419 }
1420}
1421
1422void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1423 Primitive::Type type = instruction->GetType();
1424 LocationSummary* locations = instruction->GetLocations();
1425
1426 switch (type) {
1427 case Primitive::kPrimInt: {
1428 Register dst = locations->Out().AsRegister<Register>();
1429 Register lhs = locations->InAt(0).AsRegister<Register>();
1430 Location rhs_location = locations->InAt(1);
1431
1432 Register rhs_reg = ZERO;
1433 int32_t rhs_imm = 0;
1434 bool use_imm = rhs_location.IsConstant();
1435 if (use_imm) {
1436 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1437 } else {
1438 rhs_reg = rhs_location.AsRegister<Register>();
1439 }
1440
1441 if (instruction->IsAnd()) {
1442 if (use_imm)
1443 __ Andi(dst, lhs, rhs_imm);
1444 else
1445 __ And(dst, lhs, rhs_reg);
1446 } else if (instruction->IsOr()) {
1447 if (use_imm)
1448 __ Ori(dst, lhs, rhs_imm);
1449 else
1450 __ Or(dst, lhs, rhs_reg);
1451 } else if (instruction->IsXor()) {
1452 if (use_imm)
1453 __ Xori(dst, lhs, rhs_imm);
1454 else
1455 __ Xor(dst, lhs, rhs_reg);
1456 } else if (instruction->IsAdd()) {
1457 if (use_imm)
1458 __ Addiu(dst, lhs, rhs_imm);
1459 else
1460 __ Addu(dst, lhs, rhs_reg);
1461 } else {
1462 DCHECK(instruction->IsSub());
1463 if (use_imm)
1464 __ Addiu(dst, lhs, -rhs_imm);
1465 else
1466 __ Subu(dst, lhs, rhs_reg);
1467 }
1468 break;
1469 }
1470
1471 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001472 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1473 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1474 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1475 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001476 Location rhs_location = locations->InAt(1);
1477 bool use_imm = rhs_location.IsConstant();
1478 if (!use_imm) {
1479 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1480 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1481 if (instruction->IsAnd()) {
1482 __ And(dst_low, lhs_low, rhs_low);
1483 __ And(dst_high, lhs_high, rhs_high);
1484 } else if (instruction->IsOr()) {
1485 __ Or(dst_low, lhs_low, rhs_low);
1486 __ Or(dst_high, lhs_high, rhs_high);
1487 } else if (instruction->IsXor()) {
1488 __ Xor(dst_low, lhs_low, rhs_low);
1489 __ Xor(dst_high, lhs_high, rhs_high);
1490 } else if (instruction->IsAdd()) {
1491 if (lhs_low == rhs_low) {
1492 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1493 __ Slt(TMP, lhs_low, ZERO);
1494 __ Addu(dst_low, lhs_low, rhs_low);
1495 } else {
1496 __ Addu(dst_low, lhs_low, rhs_low);
1497 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1498 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1499 }
1500 __ Addu(dst_high, lhs_high, rhs_high);
1501 __ Addu(dst_high, dst_high, TMP);
1502 } else {
1503 DCHECK(instruction->IsSub());
1504 __ Sltu(TMP, lhs_low, rhs_low);
1505 __ Subu(dst_low, lhs_low, rhs_low);
1506 __ Subu(dst_high, lhs_high, rhs_high);
1507 __ Subu(dst_high, dst_high, TMP);
1508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001509 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001510 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1511 if (instruction->IsOr()) {
1512 uint32_t low = Low32Bits(value);
1513 uint32_t high = High32Bits(value);
1514 if (IsUint<16>(low)) {
1515 if (dst_low != lhs_low || low != 0) {
1516 __ Ori(dst_low, lhs_low, low);
1517 }
1518 } else {
1519 __ LoadConst32(TMP, low);
1520 __ Or(dst_low, lhs_low, TMP);
1521 }
1522 if (IsUint<16>(high)) {
1523 if (dst_high != lhs_high || high != 0) {
1524 __ Ori(dst_high, lhs_high, high);
1525 }
1526 } else {
1527 if (high != low) {
1528 __ LoadConst32(TMP, high);
1529 }
1530 __ Or(dst_high, lhs_high, TMP);
1531 }
1532 } else if (instruction->IsXor()) {
1533 uint32_t low = Low32Bits(value);
1534 uint32_t high = High32Bits(value);
1535 if (IsUint<16>(low)) {
1536 if (dst_low != lhs_low || low != 0) {
1537 __ Xori(dst_low, lhs_low, low);
1538 }
1539 } else {
1540 __ LoadConst32(TMP, low);
1541 __ Xor(dst_low, lhs_low, TMP);
1542 }
1543 if (IsUint<16>(high)) {
1544 if (dst_high != lhs_high || high != 0) {
1545 __ Xori(dst_high, lhs_high, high);
1546 }
1547 } else {
1548 if (high != low) {
1549 __ LoadConst32(TMP, high);
1550 }
1551 __ Xor(dst_high, lhs_high, TMP);
1552 }
1553 } else if (instruction->IsAnd()) {
1554 uint32_t low = Low32Bits(value);
1555 uint32_t high = High32Bits(value);
1556 if (IsUint<16>(low)) {
1557 __ Andi(dst_low, lhs_low, low);
1558 } else if (low != 0xFFFFFFFF) {
1559 __ LoadConst32(TMP, low);
1560 __ And(dst_low, lhs_low, TMP);
1561 } else if (dst_low != lhs_low) {
1562 __ Move(dst_low, lhs_low);
1563 }
1564 if (IsUint<16>(high)) {
1565 __ Andi(dst_high, lhs_high, high);
1566 } else if (high != 0xFFFFFFFF) {
1567 if (high != low) {
1568 __ LoadConst32(TMP, high);
1569 }
1570 __ And(dst_high, lhs_high, TMP);
1571 } else if (dst_high != lhs_high) {
1572 __ Move(dst_high, lhs_high);
1573 }
1574 } else {
1575 if (instruction->IsSub()) {
1576 value = -value;
1577 } else {
1578 DCHECK(instruction->IsAdd());
1579 }
1580 int32_t low = Low32Bits(value);
1581 int32_t high = High32Bits(value);
1582 if (IsInt<16>(low)) {
1583 if (dst_low != lhs_low || low != 0) {
1584 __ Addiu(dst_low, lhs_low, low);
1585 }
1586 if (low != 0) {
1587 __ Sltiu(AT, dst_low, low);
1588 }
1589 } else {
1590 __ LoadConst32(TMP, low);
1591 __ Addu(dst_low, lhs_low, TMP);
1592 __ Sltu(AT, dst_low, TMP);
1593 }
1594 if (IsInt<16>(high)) {
1595 if (dst_high != lhs_high || high != 0) {
1596 __ Addiu(dst_high, lhs_high, high);
1597 }
1598 } else {
1599 if (high != low) {
1600 __ LoadConst32(TMP, high);
1601 }
1602 __ Addu(dst_high, lhs_high, TMP);
1603 }
1604 if (low != 0) {
1605 __ Addu(dst_high, dst_high, AT);
1606 }
1607 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001608 }
1609 break;
1610 }
1611
1612 case Primitive::kPrimFloat:
1613 case Primitive::kPrimDouble: {
1614 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1615 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1616 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1617 if (instruction->IsAdd()) {
1618 if (type == Primitive::kPrimFloat) {
1619 __ AddS(dst, lhs, rhs);
1620 } else {
1621 __ AddD(dst, lhs, rhs);
1622 }
1623 } else {
1624 DCHECK(instruction->IsSub());
1625 if (type == Primitive::kPrimFloat) {
1626 __ SubS(dst, lhs, rhs);
1627 } else {
1628 __ SubD(dst, lhs, rhs);
1629 }
1630 }
1631 break;
1632 }
1633
1634 default:
1635 LOG(FATAL) << "Unexpected binary operation type " << type;
1636 }
1637}
1638
1639void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001640 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641
1642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1643 Primitive::Type type = instr->GetResultType();
1644 switch (type) {
1645 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1649 break;
1650 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001651 locations->SetInAt(0, Location::RequiresRegister());
1652 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1653 locations->SetOut(Location::RequiresRegister());
1654 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001655 default:
1656 LOG(FATAL) << "Unexpected shift type " << type;
1657 }
1658}
1659
1660static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1661
1662void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001663 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001664 LocationSummary* locations = instr->GetLocations();
1665 Primitive::Type type = instr->GetType();
1666
1667 Location rhs_location = locations->InAt(1);
1668 bool use_imm = rhs_location.IsConstant();
1669 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1670 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001671 const uint32_t shift_mask =
1672 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001673 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001674 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1675 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001676
1677 switch (type) {
1678 case Primitive::kPrimInt: {
1679 Register dst = locations->Out().AsRegister<Register>();
1680 Register lhs = locations->InAt(0).AsRegister<Register>();
1681 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001682 if (shift_value == 0) {
1683 if (dst != lhs) {
1684 __ Move(dst, lhs);
1685 }
1686 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001687 __ Sll(dst, lhs, shift_value);
1688 } else if (instr->IsShr()) {
1689 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001690 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 } else {
1693 if (has_ins_rotr) {
1694 __ Rotr(dst, lhs, shift_value);
1695 } else {
1696 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1697 __ Srl(dst, lhs, shift_value);
1698 __ Or(dst, dst, TMP);
1699 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001700 }
1701 } else {
1702 if (instr->IsShl()) {
1703 __ Sllv(dst, lhs, rhs_reg);
1704 } else if (instr->IsShr()) {
1705 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001706 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001707 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001708 } else {
1709 if (has_ins_rotr) {
1710 __ Rotrv(dst, lhs, rhs_reg);
1711 } else {
1712 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001713 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1714 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1715 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1716 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1717 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001718 __ Sllv(TMP, lhs, TMP);
1719 __ Srlv(dst, lhs, rhs_reg);
1720 __ Or(dst, dst, TMP);
1721 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001722 }
1723 }
1724 break;
1725 }
1726
1727 case Primitive::kPrimLong: {
1728 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1729 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1730 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1731 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1732 if (use_imm) {
1733 if (shift_value == 0) {
1734 codegen_->Move64(locations->Out(), locations->InAt(0));
1735 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001736 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001737 if (instr->IsShl()) {
1738 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1739 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1740 __ Sll(dst_low, lhs_low, shift_value);
1741 } else if (instr->IsShr()) {
1742 __ Srl(dst_low, lhs_low, shift_value);
1743 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1744 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001745 } else if (instr->IsUShr()) {
1746 __ Srl(dst_low, lhs_low, shift_value);
1747 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1748 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001749 } else {
1750 __ Srl(dst_low, lhs_low, shift_value);
1751 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1752 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001753 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001754 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001755 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001756 if (instr->IsShl()) {
1757 __ Sll(dst_low, lhs_low, shift_value);
1758 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1759 __ Sll(dst_high, lhs_high, shift_value);
1760 __ Or(dst_high, dst_high, TMP);
1761 } else if (instr->IsShr()) {
1762 __ Sra(dst_high, lhs_high, shift_value);
1763 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1764 __ Srl(dst_low, lhs_low, shift_value);
1765 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001766 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001767 __ Srl(dst_high, lhs_high, shift_value);
1768 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1769 __ Srl(dst_low, lhs_low, shift_value);
1770 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001771 } else {
1772 __ Srl(TMP, lhs_low, shift_value);
1773 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1774 __ Or(dst_low, dst_low, TMP);
1775 __ Srl(TMP, lhs_high, shift_value);
1776 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1777 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001778 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001779 }
1780 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001781 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001782 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001783 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001784 __ Move(dst_low, ZERO);
1785 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001786 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001787 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001788 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001789 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001790 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001791 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001792 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001793 // 64-bit rotation by 32 is just a swap.
1794 __ Move(dst_low, lhs_high);
1795 __ Move(dst_high, lhs_low);
1796 } else {
1797 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001798 __ Srl(dst_low, lhs_high, shift_value_high);
1799 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1800 __ Srl(dst_high, lhs_low, shift_value_high);
1801 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001802 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001803 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1804 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001805 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001806 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1807 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001808 __ Or(dst_high, dst_high, TMP);
1809 }
1810 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001811 }
1812 }
1813 } else {
1814 MipsLabel done;
1815 if (instr->IsShl()) {
1816 __ Sllv(dst_low, lhs_low, rhs_reg);
1817 __ Nor(AT, ZERO, rhs_reg);
1818 __ Srl(TMP, lhs_low, 1);
1819 __ Srlv(TMP, TMP, AT);
1820 __ Sllv(dst_high, lhs_high, rhs_reg);
1821 __ Or(dst_high, dst_high, TMP);
1822 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1823 __ Beqz(TMP, &done);
1824 __ Move(dst_high, dst_low);
1825 __ Move(dst_low, ZERO);
1826 } else if (instr->IsShr()) {
1827 __ Srav(dst_high, lhs_high, rhs_reg);
1828 __ Nor(AT, ZERO, rhs_reg);
1829 __ Sll(TMP, lhs_high, 1);
1830 __ Sllv(TMP, TMP, AT);
1831 __ Srlv(dst_low, lhs_low, rhs_reg);
1832 __ Or(dst_low, dst_low, TMP);
1833 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1834 __ Beqz(TMP, &done);
1835 __ Move(dst_low, dst_high);
1836 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001837 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001838 __ Srlv(dst_high, lhs_high, rhs_reg);
1839 __ Nor(AT, ZERO, rhs_reg);
1840 __ Sll(TMP, lhs_high, 1);
1841 __ Sllv(TMP, TMP, AT);
1842 __ Srlv(dst_low, lhs_low, rhs_reg);
1843 __ Or(dst_low, dst_low, TMP);
1844 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1845 __ Beqz(TMP, &done);
1846 __ Move(dst_low, dst_high);
1847 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001848 } else {
1849 __ Nor(AT, ZERO, rhs_reg);
1850 __ Srlv(TMP, lhs_low, rhs_reg);
1851 __ Sll(dst_low, lhs_high, 1);
1852 __ Sllv(dst_low, dst_low, AT);
1853 __ Or(dst_low, dst_low, TMP);
1854 __ Srlv(TMP, lhs_high, rhs_reg);
1855 __ Sll(dst_high, lhs_low, 1);
1856 __ Sllv(dst_high, dst_high, AT);
1857 __ Or(dst_high, dst_high, TMP);
1858 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1859 __ Beqz(TMP, &done);
1860 __ Move(TMP, dst_high);
1861 __ Move(dst_high, dst_low);
1862 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 }
1864 __ Bind(&done);
1865 }
1866 break;
1867 }
1868
1869 default:
1870 LOG(FATAL) << "Unexpected shift operation type " << type;
1871 }
1872}
1873
1874void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1875 HandleBinaryOp(instruction);
1876}
1877
1878void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1879 HandleBinaryOp(instruction);
1880}
1881
1882void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1883 HandleBinaryOp(instruction);
1884}
1885
1886void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1887 HandleBinaryOp(instruction);
1888}
1889
1890void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1891 LocationSummary* locations =
1892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1895 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1896 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1897 } else {
1898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1899 }
1900}
1901
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001902static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
1903 auto null_checker = [codegen, instruction]() {
1904 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001905 };
1906 return null_checker;
1907}
1908
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1910 LocationSummary* locations = instruction->GetLocations();
1911 Register obj = locations->InAt(0).AsRegister<Register>();
1912 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001913 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001914 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001916 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001917 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1918 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 switch (type) {
1920 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 Register out = locations->Out().AsRegister<Register>();
1922 if (index.IsConstant()) {
1923 size_t offset =
1924 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001925 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 } else {
1927 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001928 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 }
1930 break;
1931 }
1932
1933 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 Register out = locations->Out().AsRegister<Register>();
1935 if (index.IsConstant()) {
1936 size_t offset =
1937 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 } else {
1940 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001941 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 }
1943 break;
1944 }
1945
1946 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 Register out = locations->Out().AsRegister<Register>();
1948 if (index.IsConstant()) {
1949 size_t offset =
1950 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001951 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 } else {
1953 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1954 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001955 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 }
1957 break;
1958 }
1959
1960 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001962 if (maybe_compressed_char_at) {
1963 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1964 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
1965 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
1966 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1967 "Expecting 0=compressed, 1=uncompressed");
1968 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001970 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1971 if (maybe_compressed_char_at) {
1972 MipsLabel uncompressed_load, done;
1973 __ Bnez(TMP, &uncompressed_load);
1974 __ LoadFromOffset(kLoadUnsignedByte,
1975 out,
1976 obj,
1977 data_offset + (const_index << TIMES_1));
1978 __ B(&done);
1979 __ Bind(&uncompressed_load);
1980 __ LoadFromOffset(kLoadUnsignedHalfword,
1981 out,
1982 obj,
1983 data_offset + (const_index << TIMES_2));
1984 __ Bind(&done);
1985 } else {
1986 __ LoadFromOffset(kLoadUnsignedHalfword,
1987 out,
1988 obj,
1989 data_offset + (const_index << TIMES_2),
1990 null_checker);
1991 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001992 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001993 Register index_reg = index.AsRegister<Register>();
1994 if (maybe_compressed_char_at) {
1995 MipsLabel uncompressed_load, done;
1996 __ Bnez(TMP, &uncompressed_load);
1997 __ Addu(TMP, obj, index_reg);
1998 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1999 __ B(&done);
2000 __ Bind(&uncompressed_load);
2001 __ Sll(TMP, index_reg, TIMES_2);
2002 __ Addu(TMP, obj, TMP);
2003 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2004 __ Bind(&done);
2005 } else {
2006 __ Sll(TMP, index_reg, TIMES_2);
2007 __ Addu(TMP, obj, TMP);
2008 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2009 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 }
2011 break;
2012 }
2013
2014 case Primitive::kPrimInt:
2015 case Primitive::kPrimNot: {
2016 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 Register out = locations->Out().AsRegister<Register>();
2018 if (index.IsConstant()) {
2019 size_t offset =
2020 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002021 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 } else {
2023 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2024 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002025 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 break;
2028 }
2029
2030 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031 Register out = locations->Out().AsRegisterPairLow<Register>();
2032 if (index.IsConstant()) {
2033 size_t offset =
2034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002035 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 } else {
2037 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2038 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 }
2041 break;
2042 }
2043
2044 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2046 if (index.IsConstant()) {
2047 size_t offset =
2048 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002049 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 } else {
2051 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2052 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002053 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002054 }
2055 break;
2056 }
2057
2058 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2060 if (index.IsConstant()) {
2061 size_t offset =
2062 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002063 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 } else {
2065 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2066 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002067 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 }
2069 break;
2070 }
2071
2072 case Primitive::kPrimVoid:
2073 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2074 UNREACHABLE();
2075 }
Alexey Frunzec061de12017-02-14 13:27:23 -08002076
2077 if (type == Primitive::kPrimNot) {
2078 Register out = locations->Out().AsRegister<Register>();
2079 __ MaybeUnpoisonHeapReference(out);
2080 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002081}
2082
2083void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2085 locations->SetInAt(0, Location::RequiresRegister());
2086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2087}
2088
2089void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2090 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002091 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 Register obj = locations->InAt(0).AsRegister<Register>();
2093 Register out = locations->Out().AsRegister<Register>();
2094 __ LoadFromOffset(kLoadWord, out, obj, offset);
2095 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002096 // Mask out compression flag from String's array length.
2097 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2098 __ Srl(out, out, 1u);
2099 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002100}
2101
Alexey Frunzef58b2482016-09-02 22:14:06 -07002102Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2103 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2104 ? Location::ConstantLocation(instruction->AsConstant())
2105 : Location::RequiresRegister();
2106}
2107
2108Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2109 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2110 // We can store a non-zero float or double constant without first loading it into the FPU,
2111 // but we should only prefer this if the constant has a single use.
2112 if (instruction->IsConstant() &&
2113 (instruction->AsConstant()->IsZeroBitPattern() ||
2114 instruction->GetUses().HasExactlyOneElement())) {
2115 return Location::ConstantLocation(instruction->AsConstant());
2116 // Otherwise fall through and require an FPU register for the constant.
2117 }
2118 return Location::RequiresFpuRegister();
2119}
2120
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002121void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002122 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2124 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002125 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002126 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002127 InvokeRuntimeCallingConvention calling_convention;
2128 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2129 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2130 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2131 } else {
2132 locations->SetInAt(0, Location::RequiresRegister());
2133 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2134 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002135 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002137 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002138 }
2139 }
2140}
2141
2142void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2143 LocationSummary* locations = instruction->GetLocations();
2144 Register obj = locations->InAt(0).AsRegister<Register>();
2145 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002146 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002147 Primitive::Type value_type = instruction->GetComponentType();
2148 bool needs_runtime_call = locations->WillCall();
2149 bool needs_write_barrier =
2150 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002151 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002152 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002153
2154 switch (value_type) {
2155 case Primitive::kPrimBoolean:
2156 case Primitive::kPrimByte: {
2157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002158 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002159 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002161 __ Addu(base_reg, obj, index.AsRegister<Register>());
2162 }
2163 if (value_location.IsConstant()) {
2164 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2165 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2166 } else {
2167 Register value = value_location.AsRegister<Register>();
2168 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002169 }
2170 break;
2171 }
2172
2173 case Primitive::kPrimShort:
2174 case Primitive::kPrimChar: {
2175 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002176 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002177 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002178 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002179 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2180 __ Addu(base_reg, obj, base_reg);
2181 }
2182 if (value_location.IsConstant()) {
2183 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2184 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2185 } else {
2186 Register value = value_location.AsRegister<Register>();
2187 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002188 }
2189 break;
2190 }
2191
2192 case Primitive::kPrimInt:
2193 case Primitive::kPrimNot: {
2194 if (!needs_runtime_call) {
2195 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002196 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002197 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002198 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002199 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2200 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002202 if (value_location.IsConstant()) {
2203 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2204 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2205 DCHECK(!needs_write_barrier);
2206 } else {
2207 Register value = value_location.AsRegister<Register>();
Alexey Frunzec061de12017-02-14 13:27:23 -08002208 if (kPoisonHeapReferences && needs_write_barrier) {
2209 // Note that in the case where `value` is a null reference,
2210 // we do not enter this block, as a null reference does not
2211 // need poisoning.
2212 DCHECK_EQ(value_type, Primitive::kPrimNot);
2213 // Use Sw() instead of StoreToOffset() in order to be able to
2214 // hold the poisoned reference in AT and thus avoid allocating
2215 // yet another temporary register.
2216 if (index.IsConstant()) {
2217 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
2218 int16_t low = Low16Bits(data_offset);
2219 uint32_t high = data_offset - low;
2220 __ Addiu32(TMP, obj, high);
2221 base_reg = TMP;
2222 data_offset = low;
2223 }
2224 } else {
2225 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
2226 }
2227 __ PoisonHeapReference(AT, value);
2228 __ Sw(AT, base_reg, data_offset);
2229 null_checker();
2230 } else {
2231 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2232 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002233 if (needs_write_barrier) {
2234 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002235 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 }
2238 } else {
2239 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08002240 // Note: if heap poisoning is enabled, pAputObject takes care
2241 // of poisoning the reference.
Serban Constantinescufca16662016-07-14 09:21:59 +01002242 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2244 }
2245 break;
2246 }
2247
2248 case Primitive::kPrimLong: {
2249 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002251 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002252 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002253 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2254 __ Addu(base_reg, obj, base_reg);
2255 }
2256 if (value_location.IsConstant()) {
2257 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2258 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2259 } else {
2260 Register value = value_location.AsRegisterPairLow<Register>();
2261 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 }
2263 break;
2264 }
2265
2266 case Primitive::kPrimFloat: {
2267 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002269 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002270 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002271 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2272 __ Addu(base_reg, obj, base_reg);
2273 }
2274 if (value_location.IsConstant()) {
2275 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2276 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2277 } else {
2278 FRegister value = value_location.AsFpuRegister<FRegister>();
2279 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002280 }
2281 break;
2282 }
2283
2284 case Primitive::kPrimDouble: {
2285 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002286 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002287 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002289 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2290 __ Addu(base_reg, obj, base_reg);
2291 }
2292 if (value_location.IsConstant()) {
2293 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2294 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2295 } else {
2296 FRegister value = value_location.AsFpuRegister<FRegister>();
2297 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 }
2299 break;
2300 }
2301
2302 case Primitive::kPrimVoid:
2303 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2304 UNREACHABLE();
2305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306}
2307
2308void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002309 RegisterSet caller_saves = RegisterSet::Empty();
2310 InvokeRuntimeCallingConvention calling_convention;
2311 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2312 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2313 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 locations->SetInAt(0, Location::RequiresRegister());
2315 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316}
2317
2318void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2319 LocationSummary* locations = instruction->GetLocations();
2320 BoundsCheckSlowPathMIPS* slow_path =
2321 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2322 codegen_->AddSlowPath(slow_path);
2323
2324 Register index = locations->InAt(0).AsRegister<Register>();
2325 Register length = locations->InAt(1).AsRegister<Register>();
2326
2327 // length is limited by the maximum positive signed 32-bit integer.
2328 // Unsigned comparison of length and index checks for index < 0
2329 // and for length <= index simultaneously.
2330 __ Bgeu(index, length, slow_path->GetEntryLabel());
2331}
2332
2333void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2335 instruction,
2336 LocationSummary::kCallOnSlowPath);
2337 locations->SetInAt(0, Location::RequiresRegister());
2338 locations->SetInAt(1, Location::RequiresRegister());
2339 // Note that TypeCheckSlowPathMIPS uses this register too.
2340 locations->AddTemp(Location::RequiresRegister());
2341}
2342
2343void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2344 LocationSummary* locations = instruction->GetLocations();
2345 Register obj = locations->InAt(0).AsRegister<Register>();
2346 Register cls = locations->InAt(1).AsRegister<Register>();
2347 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2348
2349 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2350 codegen_->AddSlowPath(slow_path);
2351
2352 // TODO: avoid this check if we know obj is not null.
2353 __ Beqz(obj, slow_path->GetExitLabel());
2354 // Compare the class of `obj` with `cls`.
2355 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08002356 __ MaybeUnpoisonHeapReference(obj_cls);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2358 __ Bind(slow_path->GetExitLabel());
2359}
2360
2361void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2362 LocationSummary* locations =
2363 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2364 locations->SetInAt(0, Location::RequiresRegister());
2365 if (check->HasUses()) {
2366 locations->SetOut(Location::SameAsFirstInput());
2367 }
2368}
2369
2370void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2371 // We assume the class is not null.
2372 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2373 check->GetLoadClass(),
2374 check,
2375 check->GetDexPc(),
2376 true);
2377 codegen_->AddSlowPath(slow_path);
2378 GenerateClassInitializationCheck(slow_path,
2379 check->GetLocations()->InAt(0).AsRegister<Register>());
2380}
2381
2382void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2383 Primitive::Type in_type = compare->InputAt(0)->GetType();
2384
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002385 LocationSummary* locations =
2386 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002387
2388 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002389 case Primitive::kPrimBoolean:
2390 case Primitive::kPrimByte:
2391 case Primitive::kPrimShort:
2392 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002393 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002394 locations->SetInAt(0, Location::RequiresRegister());
2395 locations->SetInAt(1, Location::RequiresRegister());
2396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2397 break;
2398
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399 case Primitive::kPrimLong:
2400 locations->SetInAt(0, Location::RequiresRegister());
2401 locations->SetInAt(1, Location::RequiresRegister());
2402 // Output overlaps because it is written before doing the low comparison.
2403 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2404 break;
2405
2406 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002407 case Primitive::kPrimDouble:
2408 locations->SetInAt(0, Location::RequiresFpuRegister());
2409 locations->SetInAt(1, Location::RequiresFpuRegister());
2410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002412
2413 default:
2414 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2415 }
2416}
2417
2418void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2419 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002420 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002422 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002423
2424 // 0 if: left == right
2425 // 1 if: left > right
2426 // -1 if: left < right
2427 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002428 case Primitive::kPrimBoolean:
2429 case Primitive::kPrimByte:
2430 case Primitive::kPrimShort:
2431 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002432 case Primitive::kPrimInt: {
2433 Register lhs = locations->InAt(0).AsRegister<Register>();
2434 Register rhs = locations->InAt(1).AsRegister<Register>();
2435 __ Slt(TMP, lhs, rhs);
2436 __ Slt(res, rhs, lhs);
2437 __ Subu(res, res, TMP);
2438 break;
2439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 case Primitive::kPrimLong: {
2441 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2443 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2444 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2445 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2446 // TODO: more efficient (direct) comparison with a constant.
2447 __ Slt(TMP, lhs_high, rhs_high);
2448 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2449 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2450 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2451 __ Sltu(TMP, lhs_low, rhs_low);
2452 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2453 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2454 __ Bind(&done);
2455 break;
2456 }
2457
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002458 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002459 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002460 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2461 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2462 MipsLabel done;
2463 if (isR6) {
2464 __ CmpEqS(FTMP, lhs, rhs);
2465 __ LoadConst32(res, 0);
2466 __ Bc1nez(FTMP, &done);
2467 if (gt_bias) {
2468 __ CmpLtS(FTMP, lhs, rhs);
2469 __ LoadConst32(res, -1);
2470 __ Bc1nez(FTMP, &done);
2471 __ LoadConst32(res, 1);
2472 } else {
2473 __ CmpLtS(FTMP, rhs, lhs);
2474 __ LoadConst32(res, 1);
2475 __ Bc1nez(FTMP, &done);
2476 __ LoadConst32(res, -1);
2477 }
2478 } else {
2479 if (gt_bias) {
2480 __ ColtS(0, lhs, rhs);
2481 __ LoadConst32(res, -1);
2482 __ Bc1t(0, &done);
2483 __ CeqS(0, lhs, rhs);
2484 __ LoadConst32(res, 1);
2485 __ Movt(res, ZERO, 0);
2486 } else {
2487 __ ColtS(0, rhs, lhs);
2488 __ LoadConst32(res, 1);
2489 __ Bc1t(0, &done);
2490 __ CeqS(0, lhs, rhs);
2491 __ LoadConst32(res, -1);
2492 __ Movt(res, ZERO, 0);
2493 }
2494 }
2495 __ Bind(&done);
2496 break;
2497 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002498 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002499 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002500 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2501 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2502 MipsLabel done;
2503 if (isR6) {
2504 __ CmpEqD(FTMP, lhs, rhs);
2505 __ LoadConst32(res, 0);
2506 __ Bc1nez(FTMP, &done);
2507 if (gt_bias) {
2508 __ CmpLtD(FTMP, lhs, rhs);
2509 __ LoadConst32(res, -1);
2510 __ Bc1nez(FTMP, &done);
2511 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002512 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002513 __ CmpLtD(FTMP, rhs, lhs);
2514 __ LoadConst32(res, 1);
2515 __ Bc1nez(FTMP, &done);
2516 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002517 }
2518 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002519 if (gt_bias) {
2520 __ ColtD(0, lhs, rhs);
2521 __ LoadConst32(res, -1);
2522 __ Bc1t(0, &done);
2523 __ CeqD(0, lhs, rhs);
2524 __ LoadConst32(res, 1);
2525 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002526 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002527 __ ColtD(0, rhs, lhs);
2528 __ LoadConst32(res, 1);
2529 __ Bc1t(0, &done);
2530 __ CeqD(0, lhs, rhs);
2531 __ LoadConst32(res, -1);
2532 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002533 }
2534 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002535 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002536 break;
2537 }
2538
2539 default:
2540 LOG(FATAL) << "Unimplemented compare type " << in_type;
2541 }
2542}
2543
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002544void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002546 switch (instruction->InputAt(0)->GetType()) {
2547 default:
2548 case Primitive::kPrimLong:
2549 locations->SetInAt(0, Location::RequiresRegister());
2550 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2551 break;
2552
2553 case Primitive::kPrimFloat:
2554 case Primitive::kPrimDouble:
2555 locations->SetInAt(0, Location::RequiresFpuRegister());
2556 locations->SetInAt(1, Location::RequiresFpuRegister());
2557 break;
2558 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002559 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2561 }
2562}
2563
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002564void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002565 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 return;
2567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002568
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002569 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002570 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002572 switch (type) {
2573 default:
2574 // Integer case.
2575 GenerateIntCompare(instruction->GetCondition(), locations);
2576 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002577
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002578 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01002579 GenerateLongCompare(instruction->GetCondition(), locations);
2580 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002581
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002582 case Primitive::kPrimFloat:
2583 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002584 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2585 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002586 }
2587}
2588
Alexey Frunze7e99e052015-11-24 19:28:01 -08002589void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2590 DCHECK(instruction->IsDiv() || instruction->IsRem());
2591 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2592
2593 LocationSummary* locations = instruction->GetLocations();
2594 Location second = locations->InAt(1);
2595 DCHECK(second.IsConstant());
2596
2597 Register out = locations->Out().AsRegister<Register>();
2598 Register dividend = locations->InAt(0).AsRegister<Register>();
2599 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2600 DCHECK(imm == 1 || imm == -1);
2601
2602 if (instruction->IsRem()) {
2603 __ Move(out, ZERO);
2604 } else {
2605 if (imm == -1) {
2606 __ Subu(out, ZERO, dividend);
2607 } else if (out != dividend) {
2608 __ Move(out, dividend);
2609 }
2610 }
2611}
2612
2613void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2614 DCHECK(instruction->IsDiv() || instruction->IsRem());
2615 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2616
2617 LocationSummary* locations = instruction->GetLocations();
2618 Location second = locations->InAt(1);
2619 DCHECK(second.IsConstant());
2620
2621 Register out = locations->Out().AsRegister<Register>();
2622 Register dividend = locations->InAt(0).AsRegister<Register>();
2623 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002624 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002625 int ctz_imm = CTZ(abs_imm);
2626
2627 if (instruction->IsDiv()) {
2628 if (ctz_imm == 1) {
2629 // Fast path for division by +/-2, which is very common.
2630 __ Srl(TMP, dividend, 31);
2631 } else {
2632 __ Sra(TMP, dividend, 31);
2633 __ Srl(TMP, TMP, 32 - ctz_imm);
2634 }
2635 __ Addu(out, dividend, TMP);
2636 __ Sra(out, out, ctz_imm);
2637 if (imm < 0) {
2638 __ Subu(out, ZERO, out);
2639 }
2640 } else {
2641 if (ctz_imm == 1) {
2642 // Fast path for modulo +/-2, which is very common.
2643 __ Sra(TMP, dividend, 31);
2644 __ Subu(out, dividend, TMP);
2645 __ Andi(out, out, 1);
2646 __ Addu(out, out, TMP);
2647 } else {
2648 __ Sra(TMP, dividend, 31);
2649 __ Srl(TMP, TMP, 32 - ctz_imm);
2650 __ Addu(out, dividend, TMP);
2651 if (IsUint<16>(abs_imm - 1)) {
2652 __ Andi(out, out, abs_imm - 1);
2653 } else {
2654 __ Sll(out, out, 32 - ctz_imm);
2655 __ Srl(out, out, 32 - ctz_imm);
2656 }
2657 __ Subu(out, out, TMP);
2658 }
2659 }
2660}
2661
2662void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2663 DCHECK(instruction->IsDiv() || instruction->IsRem());
2664 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2665
2666 LocationSummary* locations = instruction->GetLocations();
2667 Location second = locations->InAt(1);
2668 DCHECK(second.IsConstant());
2669
2670 Register out = locations->Out().AsRegister<Register>();
2671 Register dividend = locations->InAt(0).AsRegister<Register>();
2672 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2673
2674 int64_t magic;
2675 int shift;
2676 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2677
2678 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2679
2680 __ LoadConst32(TMP, magic);
2681 if (isR6) {
2682 __ MuhR6(TMP, dividend, TMP);
2683 } else {
2684 __ MultR2(dividend, TMP);
2685 __ Mfhi(TMP);
2686 }
2687 if (imm > 0 && magic < 0) {
2688 __ Addu(TMP, TMP, dividend);
2689 } else if (imm < 0 && magic > 0) {
2690 __ Subu(TMP, TMP, dividend);
2691 }
2692
2693 if (shift != 0) {
2694 __ Sra(TMP, TMP, shift);
2695 }
2696
2697 if (instruction->IsDiv()) {
2698 __ Sra(out, TMP, 31);
2699 __ Subu(out, TMP, out);
2700 } else {
2701 __ Sra(AT, TMP, 31);
2702 __ Subu(AT, TMP, AT);
2703 __ LoadConst32(TMP, imm);
2704 if (isR6) {
2705 __ MulR6(TMP, AT, TMP);
2706 } else {
2707 __ MulR2(TMP, AT, TMP);
2708 }
2709 __ Subu(out, dividend, TMP);
2710 }
2711}
2712
2713void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2714 DCHECK(instruction->IsDiv() || instruction->IsRem());
2715 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2716
2717 LocationSummary* locations = instruction->GetLocations();
2718 Register out = locations->Out().AsRegister<Register>();
2719 Location second = locations->InAt(1);
2720
2721 if (second.IsConstant()) {
2722 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2723 if (imm == 0) {
2724 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2725 } else if (imm == 1 || imm == -1) {
2726 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002727 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002728 DivRemByPowerOfTwo(instruction);
2729 } else {
2730 DCHECK(imm <= -2 || imm >= 2);
2731 GenerateDivRemWithAnyConstant(instruction);
2732 }
2733 } else {
2734 Register dividend = locations->InAt(0).AsRegister<Register>();
2735 Register divisor = second.AsRegister<Register>();
2736 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2737 if (instruction->IsDiv()) {
2738 if (isR6) {
2739 __ DivR6(out, dividend, divisor);
2740 } else {
2741 __ DivR2(out, dividend, divisor);
2742 }
2743 } else {
2744 if (isR6) {
2745 __ ModR6(out, dividend, divisor);
2746 } else {
2747 __ ModR2(out, dividend, divisor);
2748 }
2749 }
2750 }
2751}
2752
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002753void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2754 Primitive::Type type = div->GetResultType();
2755 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002756 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002757 : LocationSummary::kNoCall;
2758
2759 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2760
2761 switch (type) {
2762 case Primitive::kPrimInt:
2763 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002764 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2766 break;
2767
2768 case Primitive::kPrimLong: {
2769 InvokeRuntimeCallingConvention calling_convention;
2770 locations->SetInAt(0, Location::RegisterPairLocation(
2771 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2772 locations->SetInAt(1, Location::RegisterPairLocation(
2773 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2774 locations->SetOut(calling_convention.GetReturnLocation(type));
2775 break;
2776 }
2777
2778 case Primitive::kPrimFloat:
2779 case Primitive::kPrimDouble:
2780 locations->SetInAt(0, Location::RequiresFpuRegister());
2781 locations->SetInAt(1, Location::RequiresFpuRegister());
2782 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2783 break;
2784
2785 default:
2786 LOG(FATAL) << "Unexpected div type " << type;
2787 }
2788}
2789
2790void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2791 Primitive::Type type = instruction->GetType();
2792 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002793
2794 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002795 case Primitive::kPrimInt:
2796 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002798 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002799 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2801 break;
2802 }
2803 case Primitive::kPrimFloat:
2804 case Primitive::kPrimDouble: {
2805 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2806 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2807 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2808 if (type == Primitive::kPrimFloat) {
2809 __ DivS(dst, lhs, rhs);
2810 } else {
2811 __ DivD(dst, lhs, rhs);
2812 }
2813 break;
2814 }
2815 default:
2816 LOG(FATAL) << "Unexpected div type " << type;
2817 }
2818}
2819
2820void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002821 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823}
2824
2825void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2826 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2827 codegen_->AddSlowPath(slow_path);
2828 Location value = instruction->GetLocations()->InAt(0);
2829 Primitive::Type type = instruction->GetType();
2830
2831 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002832 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833 case Primitive::kPrimByte:
2834 case Primitive::kPrimChar:
2835 case Primitive::kPrimShort:
2836 case Primitive::kPrimInt: {
2837 if (value.IsConstant()) {
2838 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2839 __ B(slow_path->GetEntryLabel());
2840 } else {
2841 // A division by a non-null constant is valid. We don't need to perform
2842 // any check, so simply fall through.
2843 }
2844 } else {
2845 DCHECK(value.IsRegister()) << value;
2846 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2847 }
2848 break;
2849 }
2850 case Primitive::kPrimLong: {
2851 if (value.IsConstant()) {
2852 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2853 __ B(slow_path->GetEntryLabel());
2854 } else {
2855 // A division by a non-null constant is valid. We don't need to perform
2856 // any check, so simply fall through.
2857 }
2858 } else {
2859 DCHECK(value.IsRegisterPair()) << value;
2860 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2861 __ Beqz(TMP, slow_path->GetEntryLabel());
2862 }
2863 break;
2864 }
2865 default:
2866 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2867 }
2868}
2869
2870void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2871 LocationSummary* locations =
2872 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2873 locations->SetOut(Location::ConstantLocation(constant));
2874}
2875
2876void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2877 // Will be generated at use site.
2878}
2879
2880void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2881 exit->SetLocations(nullptr);
2882}
2883
2884void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2885}
2886
2887void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2888 LocationSummary* locations =
2889 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2890 locations->SetOut(Location::ConstantLocation(constant));
2891}
2892
2893void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2894 // Will be generated at use site.
2895}
2896
2897void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2898 got->SetLocations(nullptr);
2899}
2900
2901void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2902 DCHECK(!successor->IsExitBlock());
2903 HBasicBlock* block = got->GetBlock();
2904 HInstruction* previous = got->GetPrevious();
2905 HLoopInformation* info = block->GetLoopInformation();
2906
2907 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2908 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2909 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2910 return;
2911 }
2912 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2913 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2914 }
2915 if (!codegen_->GoesToNextBlock(block, successor)) {
2916 __ B(codegen_->GetLabelOf(successor));
2917 }
2918}
2919
2920void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2921 HandleGoto(got, got->GetSuccessor());
2922}
2923
2924void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2925 try_boundary->SetLocations(nullptr);
2926}
2927
2928void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2929 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2930 if (!successor->IsExitBlock()) {
2931 HandleGoto(try_boundary, successor);
2932 }
2933}
2934
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002935void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2936 LocationSummary* locations) {
2937 Register dst = locations->Out().AsRegister<Register>();
2938 Register lhs = locations->InAt(0).AsRegister<Register>();
2939 Location rhs_location = locations->InAt(1);
2940 Register rhs_reg = ZERO;
2941 int64_t rhs_imm = 0;
2942 bool use_imm = rhs_location.IsConstant();
2943 if (use_imm) {
2944 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2945 } else {
2946 rhs_reg = rhs_location.AsRegister<Register>();
2947 }
2948
2949 switch (cond) {
2950 case kCondEQ:
2951 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002952 if (use_imm && IsInt<16>(-rhs_imm)) {
2953 if (rhs_imm == 0) {
2954 if (cond == kCondEQ) {
2955 __ Sltiu(dst, lhs, 1);
2956 } else {
2957 __ Sltu(dst, ZERO, lhs);
2958 }
2959 } else {
2960 __ Addiu(dst, lhs, -rhs_imm);
2961 if (cond == kCondEQ) {
2962 __ Sltiu(dst, dst, 1);
2963 } else {
2964 __ Sltu(dst, ZERO, dst);
2965 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002966 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002967 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002968 if (use_imm && IsUint<16>(rhs_imm)) {
2969 __ Xori(dst, lhs, rhs_imm);
2970 } else {
2971 if (use_imm) {
2972 rhs_reg = TMP;
2973 __ LoadConst32(rhs_reg, rhs_imm);
2974 }
2975 __ Xor(dst, lhs, rhs_reg);
2976 }
2977 if (cond == kCondEQ) {
2978 __ Sltiu(dst, dst, 1);
2979 } else {
2980 __ Sltu(dst, ZERO, dst);
2981 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002982 }
2983 break;
2984
2985 case kCondLT:
2986 case kCondGE:
2987 if (use_imm && IsInt<16>(rhs_imm)) {
2988 __ Slti(dst, lhs, rhs_imm);
2989 } else {
2990 if (use_imm) {
2991 rhs_reg = TMP;
2992 __ LoadConst32(rhs_reg, rhs_imm);
2993 }
2994 __ Slt(dst, lhs, rhs_reg);
2995 }
2996 if (cond == kCondGE) {
2997 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2998 // only the slt instruction but no sge.
2999 __ Xori(dst, dst, 1);
3000 }
3001 break;
3002
3003 case kCondLE:
3004 case kCondGT:
3005 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3006 // Simulate lhs <= rhs via lhs < rhs + 1.
3007 __ Slti(dst, lhs, rhs_imm + 1);
3008 if (cond == kCondGT) {
3009 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3010 // only the slti instruction but no sgti.
3011 __ Xori(dst, dst, 1);
3012 }
3013 } else {
3014 if (use_imm) {
3015 rhs_reg = TMP;
3016 __ LoadConst32(rhs_reg, rhs_imm);
3017 }
3018 __ Slt(dst, rhs_reg, lhs);
3019 if (cond == kCondLE) {
3020 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3021 // only the slt instruction but no sle.
3022 __ Xori(dst, dst, 1);
3023 }
3024 }
3025 break;
3026
3027 case kCondB:
3028 case kCondAE:
3029 if (use_imm && IsInt<16>(rhs_imm)) {
3030 // Sltiu sign-extends its 16-bit immediate operand before
3031 // the comparison and thus lets us compare directly with
3032 // unsigned values in the ranges [0, 0x7fff] and
3033 // [0xffff8000, 0xffffffff].
3034 __ Sltiu(dst, lhs, rhs_imm);
3035 } else {
3036 if (use_imm) {
3037 rhs_reg = TMP;
3038 __ LoadConst32(rhs_reg, rhs_imm);
3039 }
3040 __ Sltu(dst, lhs, rhs_reg);
3041 }
3042 if (cond == kCondAE) {
3043 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3044 // only the sltu instruction but no sgeu.
3045 __ Xori(dst, dst, 1);
3046 }
3047 break;
3048
3049 case kCondBE:
3050 case kCondA:
3051 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3052 // Simulate lhs <= rhs via lhs < rhs + 1.
3053 // Note that this only works if rhs + 1 does not overflow
3054 // to 0, hence the check above.
3055 // Sltiu sign-extends its 16-bit immediate operand before
3056 // the comparison and thus lets us compare directly with
3057 // unsigned values in the ranges [0, 0x7fff] and
3058 // [0xffff8000, 0xffffffff].
3059 __ Sltiu(dst, lhs, rhs_imm + 1);
3060 if (cond == kCondA) {
3061 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3062 // only the sltiu instruction but no sgtiu.
3063 __ Xori(dst, dst, 1);
3064 }
3065 } else {
3066 if (use_imm) {
3067 rhs_reg = TMP;
3068 __ LoadConst32(rhs_reg, rhs_imm);
3069 }
3070 __ Sltu(dst, rhs_reg, lhs);
3071 if (cond == kCondBE) {
3072 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3073 // only the sltu instruction but no sleu.
3074 __ Xori(dst, dst, 1);
3075 }
3076 }
3077 break;
3078 }
3079}
3080
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003081bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3082 LocationSummary* input_locations,
3083 Register dst) {
3084 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3085 Location rhs_location = input_locations->InAt(1);
3086 Register rhs_reg = ZERO;
3087 int64_t rhs_imm = 0;
3088 bool use_imm = rhs_location.IsConstant();
3089 if (use_imm) {
3090 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3091 } else {
3092 rhs_reg = rhs_location.AsRegister<Register>();
3093 }
3094
3095 switch (cond) {
3096 case kCondEQ:
3097 case kCondNE:
3098 if (use_imm && IsInt<16>(-rhs_imm)) {
3099 __ Addiu(dst, lhs, -rhs_imm);
3100 } else if (use_imm && IsUint<16>(rhs_imm)) {
3101 __ Xori(dst, lhs, rhs_imm);
3102 } else {
3103 if (use_imm) {
3104 rhs_reg = TMP;
3105 __ LoadConst32(rhs_reg, rhs_imm);
3106 }
3107 __ Xor(dst, lhs, rhs_reg);
3108 }
3109 return (cond == kCondEQ);
3110
3111 case kCondLT:
3112 case kCondGE:
3113 if (use_imm && IsInt<16>(rhs_imm)) {
3114 __ Slti(dst, lhs, rhs_imm);
3115 } else {
3116 if (use_imm) {
3117 rhs_reg = TMP;
3118 __ LoadConst32(rhs_reg, rhs_imm);
3119 }
3120 __ Slt(dst, lhs, rhs_reg);
3121 }
3122 return (cond == kCondGE);
3123
3124 case kCondLE:
3125 case kCondGT:
3126 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3127 // Simulate lhs <= rhs via lhs < rhs + 1.
3128 __ Slti(dst, lhs, rhs_imm + 1);
3129 return (cond == kCondGT);
3130 } else {
3131 if (use_imm) {
3132 rhs_reg = TMP;
3133 __ LoadConst32(rhs_reg, rhs_imm);
3134 }
3135 __ Slt(dst, rhs_reg, lhs);
3136 return (cond == kCondLE);
3137 }
3138
3139 case kCondB:
3140 case kCondAE:
3141 if (use_imm && IsInt<16>(rhs_imm)) {
3142 // Sltiu sign-extends its 16-bit immediate operand before
3143 // the comparison and thus lets us compare directly with
3144 // unsigned values in the ranges [0, 0x7fff] and
3145 // [0xffff8000, 0xffffffff].
3146 __ Sltiu(dst, lhs, rhs_imm);
3147 } else {
3148 if (use_imm) {
3149 rhs_reg = TMP;
3150 __ LoadConst32(rhs_reg, rhs_imm);
3151 }
3152 __ Sltu(dst, lhs, rhs_reg);
3153 }
3154 return (cond == kCondAE);
3155
3156 case kCondBE:
3157 case kCondA:
3158 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3159 // Simulate lhs <= rhs via lhs < rhs + 1.
3160 // Note that this only works if rhs + 1 does not overflow
3161 // to 0, hence the check above.
3162 // Sltiu sign-extends its 16-bit immediate operand before
3163 // the comparison and thus lets us compare directly with
3164 // unsigned values in the ranges [0, 0x7fff] and
3165 // [0xffff8000, 0xffffffff].
3166 __ Sltiu(dst, lhs, rhs_imm + 1);
3167 return (cond == kCondA);
3168 } else {
3169 if (use_imm) {
3170 rhs_reg = TMP;
3171 __ LoadConst32(rhs_reg, rhs_imm);
3172 }
3173 __ Sltu(dst, rhs_reg, lhs);
3174 return (cond == kCondBE);
3175 }
3176 }
3177}
3178
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003179void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3180 LocationSummary* locations,
3181 MipsLabel* label) {
3182 Register lhs = locations->InAt(0).AsRegister<Register>();
3183 Location rhs_location = locations->InAt(1);
3184 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003185 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003186 bool use_imm = rhs_location.IsConstant();
3187 if (use_imm) {
3188 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3189 } else {
3190 rhs_reg = rhs_location.AsRegister<Register>();
3191 }
3192
3193 if (use_imm && rhs_imm == 0) {
3194 switch (cond) {
3195 case kCondEQ:
3196 case kCondBE: // <= 0 if zero
3197 __ Beqz(lhs, label);
3198 break;
3199 case kCondNE:
3200 case kCondA: // > 0 if non-zero
3201 __ Bnez(lhs, label);
3202 break;
3203 case kCondLT:
3204 __ Bltz(lhs, label);
3205 break;
3206 case kCondGE:
3207 __ Bgez(lhs, label);
3208 break;
3209 case kCondLE:
3210 __ Blez(lhs, label);
3211 break;
3212 case kCondGT:
3213 __ Bgtz(lhs, label);
3214 break;
3215 case kCondB: // always false
3216 break;
3217 case kCondAE: // always true
3218 __ B(label);
3219 break;
3220 }
3221 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003222 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3223 if (isR6 || !use_imm) {
3224 if (use_imm) {
3225 rhs_reg = TMP;
3226 __ LoadConst32(rhs_reg, rhs_imm);
3227 }
3228 switch (cond) {
3229 case kCondEQ:
3230 __ Beq(lhs, rhs_reg, label);
3231 break;
3232 case kCondNE:
3233 __ Bne(lhs, rhs_reg, label);
3234 break;
3235 case kCondLT:
3236 __ Blt(lhs, rhs_reg, label);
3237 break;
3238 case kCondGE:
3239 __ Bge(lhs, rhs_reg, label);
3240 break;
3241 case kCondLE:
3242 __ Bge(rhs_reg, lhs, label);
3243 break;
3244 case kCondGT:
3245 __ Blt(rhs_reg, lhs, label);
3246 break;
3247 case kCondB:
3248 __ Bltu(lhs, rhs_reg, label);
3249 break;
3250 case kCondAE:
3251 __ Bgeu(lhs, rhs_reg, label);
3252 break;
3253 case kCondBE:
3254 __ Bgeu(rhs_reg, lhs, label);
3255 break;
3256 case kCondA:
3257 __ Bltu(rhs_reg, lhs, label);
3258 break;
3259 }
3260 } else {
3261 // Special cases for more efficient comparison with constants on R2.
3262 switch (cond) {
3263 case kCondEQ:
3264 __ LoadConst32(TMP, rhs_imm);
3265 __ Beq(lhs, TMP, label);
3266 break;
3267 case kCondNE:
3268 __ LoadConst32(TMP, rhs_imm);
3269 __ Bne(lhs, TMP, label);
3270 break;
3271 case kCondLT:
3272 if (IsInt<16>(rhs_imm)) {
3273 __ Slti(TMP, lhs, rhs_imm);
3274 __ Bnez(TMP, label);
3275 } else {
3276 __ LoadConst32(TMP, rhs_imm);
3277 __ Blt(lhs, TMP, label);
3278 }
3279 break;
3280 case kCondGE:
3281 if (IsInt<16>(rhs_imm)) {
3282 __ Slti(TMP, lhs, rhs_imm);
3283 __ Beqz(TMP, label);
3284 } else {
3285 __ LoadConst32(TMP, rhs_imm);
3286 __ Bge(lhs, TMP, label);
3287 }
3288 break;
3289 case kCondLE:
3290 if (IsInt<16>(rhs_imm + 1)) {
3291 // Simulate lhs <= rhs via lhs < rhs + 1.
3292 __ Slti(TMP, lhs, rhs_imm + 1);
3293 __ Bnez(TMP, label);
3294 } else {
3295 __ LoadConst32(TMP, rhs_imm);
3296 __ Bge(TMP, lhs, label);
3297 }
3298 break;
3299 case kCondGT:
3300 if (IsInt<16>(rhs_imm + 1)) {
3301 // Simulate lhs > rhs via !(lhs < rhs + 1).
3302 __ Slti(TMP, lhs, rhs_imm + 1);
3303 __ Beqz(TMP, label);
3304 } else {
3305 __ LoadConst32(TMP, rhs_imm);
3306 __ Blt(TMP, lhs, label);
3307 }
3308 break;
3309 case kCondB:
3310 if (IsInt<16>(rhs_imm)) {
3311 __ Sltiu(TMP, lhs, rhs_imm);
3312 __ Bnez(TMP, label);
3313 } else {
3314 __ LoadConst32(TMP, rhs_imm);
3315 __ Bltu(lhs, TMP, label);
3316 }
3317 break;
3318 case kCondAE:
3319 if (IsInt<16>(rhs_imm)) {
3320 __ Sltiu(TMP, lhs, rhs_imm);
3321 __ Beqz(TMP, label);
3322 } else {
3323 __ LoadConst32(TMP, rhs_imm);
3324 __ Bgeu(lhs, TMP, label);
3325 }
3326 break;
3327 case kCondBE:
3328 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3329 // Simulate lhs <= rhs via lhs < rhs + 1.
3330 // Note that this only works if rhs + 1 does not overflow
3331 // to 0, hence the check above.
3332 __ Sltiu(TMP, lhs, rhs_imm + 1);
3333 __ Bnez(TMP, label);
3334 } else {
3335 __ LoadConst32(TMP, rhs_imm);
3336 __ Bgeu(TMP, lhs, label);
3337 }
3338 break;
3339 case kCondA:
3340 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3341 // Simulate lhs > rhs via !(lhs < rhs + 1).
3342 // Note that this only works if rhs + 1 does not overflow
3343 // to 0, hence the check above.
3344 __ Sltiu(TMP, lhs, rhs_imm + 1);
3345 __ Beqz(TMP, label);
3346 } else {
3347 __ LoadConst32(TMP, rhs_imm);
3348 __ Bltu(TMP, lhs, label);
3349 }
3350 break;
3351 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003352 }
3353 }
3354}
3355
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003356void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
3357 LocationSummary* locations) {
3358 Register dst = locations->Out().AsRegister<Register>();
3359 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3360 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3361 Location rhs_location = locations->InAt(1);
3362 Register rhs_high = ZERO;
3363 Register rhs_low = ZERO;
3364 int64_t imm = 0;
3365 uint32_t imm_high = 0;
3366 uint32_t imm_low = 0;
3367 bool use_imm = rhs_location.IsConstant();
3368 if (use_imm) {
3369 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3370 imm_high = High32Bits(imm);
3371 imm_low = Low32Bits(imm);
3372 } else {
3373 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3374 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3375 }
3376 if (use_imm && imm == 0) {
3377 switch (cond) {
3378 case kCondEQ:
3379 case kCondBE: // <= 0 if zero
3380 __ Or(dst, lhs_high, lhs_low);
3381 __ Sltiu(dst, dst, 1);
3382 break;
3383 case kCondNE:
3384 case kCondA: // > 0 if non-zero
3385 __ Or(dst, lhs_high, lhs_low);
3386 __ Sltu(dst, ZERO, dst);
3387 break;
3388 case kCondLT:
3389 __ Slt(dst, lhs_high, ZERO);
3390 break;
3391 case kCondGE:
3392 __ Slt(dst, lhs_high, ZERO);
3393 __ Xori(dst, dst, 1);
3394 break;
3395 case kCondLE:
3396 __ Or(TMP, lhs_high, lhs_low);
3397 __ Sra(AT, lhs_high, 31);
3398 __ Sltu(dst, AT, TMP);
3399 __ Xori(dst, dst, 1);
3400 break;
3401 case kCondGT:
3402 __ Or(TMP, lhs_high, lhs_low);
3403 __ Sra(AT, lhs_high, 31);
3404 __ Sltu(dst, AT, TMP);
3405 break;
3406 case kCondB: // always false
3407 __ Andi(dst, dst, 0);
3408 break;
3409 case kCondAE: // always true
3410 __ Ori(dst, ZERO, 1);
3411 break;
3412 }
3413 } else if (use_imm) {
3414 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3415 switch (cond) {
3416 case kCondEQ:
3417 __ LoadConst32(TMP, imm_high);
3418 __ Xor(TMP, TMP, lhs_high);
3419 __ LoadConst32(AT, imm_low);
3420 __ Xor(AT, AT, lhs_low);
3421 __ Or(dst, TMP, AT);
3422 __ Sltiu(dst, dst, 1);
3423 break;
3424 case kCondNE:
3425 __ LoadConst32(TMP, imm_high);
3426 __ Xor(TMP, TMP, lhs_high);
3427 __ LoadConst32(AT, imm_low);
3428 __ Xor(AT, AT, lhs_low);
3429 __ Or(dst, TMP, AT);
3430 __ Sltu(dst, ZERO, dst);
3431 break;
3432 case kCondLT:
3433 case kCondGE:
3434 if (dst == lhs_low) {
3435 __ LoadConst32(TMP, imm_low);
3436 __ Sltu(dst, lhs_low, TMP);
3437 }
3438 __ LoadConst32(TMP, imm_high);
3439 __ Slt(AT, lhs_high, TMP);
3440 __ Slt(TMP, TMP, lhs_high);
3441 if (dst != lhs_low) {
3442 __ LoadConst32(dst, imm_low);
3443 __ Sltu(dst, lhs_low, dst);
3444 }
3445 __ Slt(dst, TMP, dst);
3446 __ Or(dst, dst, AT);
3447 if (cond == kCondGE) {
3448 __ Xori(dst, dst, 1);
3449 }
3450 break;
3451 case kCondGT:
3452 case kCondLE:
3453 if (dst == lhs_low) {
3454 __ LoadConst32(TMP, imm_low);
3455 __ Sltu(dst, TMP, lhs_low);
3456 }
3457 __ LoadConst32(TMP, imm_high);
3458 __ Slt(AT, TMP, lhs_high);
3459 __ Slt(TMP, lhs_high, TMP);
3460 if (dst != lhs_low) {
3461 __ LoadConst32(dst, imm_low);
3462 __ Sltu(dst, dst, lhs_low);
3463 }
3464 __ Slt(dst, TMP, dst);
3465 __ Or(dst, dst, AT);
3466 if (cond == kCondLE) {
3467 __ Xori(dst, dst, 1);
3468 }
3469 break;
3470 case kCondB:
3471 case kCondAE:
3472 if (dst == lhs_low) {
3473 __ LoadConst32(TMP, imm_low);
3474 __ Sltu(dst, lhs_low, TMP);
3475 }
3476 __ LoadConst32(TMP, imm_high);
3477 __ Sltu(AT, lhs_high, TMP);
3478 __ Sltu(TMP, TMP, lhs_high);
3479 if (dst != lhs_low) {
3480 __ LoadConst32(dst, imm_low);
3481 __ Sltu(dst, lhs_low, dst);
3482 }
3483 __ Slt(dst, TMP, dst);
3484 __ Or(dst, dst, AT);
3485 if (cond == kCondAE) {
3486 __ Xori(dst, dst, 1);
3487 }
3488 break;
3489 case kCondA:
3490 case kCondBE:
3491 if (dst == lhs_low) {
3492 __ LoadConst32(TMP, imm_low);
3493 __ Sltu(dst, TMP, lhs_low);
3494 }
3495 __ LoadConst32(TMP, imm_high);
3496 __ Sltu(AT, TMP, lhs_high);
3497 __ Sltu(TMP, lhs_high, TMP);
3498 if (dst != lhs_low) {
3499 __ LoadConst32(dst, imm_low);
3500 __ Sltu(dst, dst, lhs_low);
3501 }
3502 __ Slt(dst, TMP, dst);
3503 __ Or(dst, dst, AT);
3504 if (cond == kCondBE) {
3505 __ Xori(dst, dst, 1);
3506 }
3507 break;
3508 }
3509 } else {
3510 switch (cond) {
3511 case kCondEQ:
3512 __ Xor(TMP, lhs_high, rhs_high);
3513 __ Xor(AT, lhs_low, rhs_low);
3514 __ Or(dst, TMP, AT);
3515 __ Sltiu(dst, dst, 1);
3516 break;
3517 case kCondNE:
3518 __ Xor(TMP, lhs_high, rhs_high);
3519 __ Xor(AT, lhs_low, rhs_low);
3520 __ Or(dst, TMP, AT);
3521 __ Sltu(dst, ZERO, dst);
3522 break;
3523 case kCondLT:
3524 case kCondGE:
3525 __ Slt(TMP, rhs_high, lhs_high);
3526 __ Sltu(AT, lhs_low, rhs_low);
3527 __ Slt(TMP, TMP, AT);
3528 __ Slt(AT, lhs_high, rhs_high);
3529 __ Or(dst, AT, TMP);
3530 if (cond == kCondGE) {
3531 __ Xori(dst, dst, 1);
3532 }
3533 break;
3534 case kCondGT:
3535 case kCondLE:
3536 __ Slt(TMP, lhs_high, rhs_high);
3537 __ Sltu(AT, rhs_low, lhs_low);
3538 __ Slt(TMP, TMP, AT);
3539 __ Slt(AT, rhs_high, lhs_high);
3540 __ Or(dst, AT, TMP);
3541 if (cond == kCondLE) {
3542 __ Xori(dst, dst, 1);
3543 }
3544 break;
3545 case kCondB:
3546 case kCondAE:
3547 __ Sltu(TMP, rhs_high, lhs_high);
3548 __ Sltu(AT, lhs_low, rhs_low);
3549 __ Slt(TMP, TMP, AT);
3550 __ Sltu(AT, lhs_high, rhs_high);
3551 __ Or(dst, AT, TMP);
3552 if (cond == kCondAE) {
3553 __ Xori(dst, dst, 1);
3554 }
3555 break;
3556 case kCondA:
3557 case kCondBE:
3558 __ Sltu(TMP, lhs_high, rhs_high);
3559 __ Sltu(AT, rhs_low, lhs_low);
3560 __ Slt(TMP, TMP, AT);
3561 __ Sltu(AT, rhs_high, lhs_high);
3562 __ Or(dst, AT, TMP);
3563 if (cond == kCondBE) {
3564 __ Xori(dst, dst, 1);
3565 }
3566 break;
3567 }
3568 }
3569}
3570
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3572 LocationSummary* locations,
3573 MipsLabel* label) {
3574 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3575 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3576 Location rhs_location = locations->InAt(1);
3577 Register rhs_high = ZERO;
3578 Register rhs_low = ZERO;
3579 int64_t imm = 0;
3580 uint32_t imm_high = 0;
3581 uint32_t imm_low = 0;
3582 bool use_imm = rhs_location.IsConstant();
3583 if (use_imm) {
3584 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3585 imm_high = High32Bits(imm);
3586 imm_low = Low32Bits(imm);
3587 } else {
3588 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3589 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3590 }
3591
3592 if (use_imm && imm == 0) {
3593 switch (cond) {
3594 case kCondEQ:
3595 case kCondBE: // <= 0 if zero
3596 __ Or(TMP, lhs_high, lhs_low);
3597 __ Beqz(TMP, label);
3598 break;
3599 case kCondNE:
3600 case kCondA: // > 0 if non-zero
3601 __ Or(TMP, lhs_high, lhs_low);
3602 __ Bnez(TMP, label);
3603 break;
3604 case kCondLT:
3605 __ Bltz(lhs_high, label);
3606 break;
3607 case kCondGE:
3608 __ Bgez(lhs_high, label);
3609 break;
3610 case kCondLE:
3611 __ Or(TMP, lhs_high, lhs_low);
3612 __ Sra(AT, lhs_high, 31);
3613 __ Bgeu(AT, TMP, label);
3614 break;
3615 case kCondGT:
3616 __ Or(TMP, lhs_high, lhs_low);
3617 __ Sra(AT, lhs_high, 31);
3618 __ Bltu(AT, TMP, label);
3619 break;
3620 case kCondB: // always false
3621 break;
3622 case kCondAE: // always true
3623 __ B(label);
3624 break;
3625 }
3626 } else if (use_imm) {
3627 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3628 switch (cond) {
3629 case kCondEQ:
3630 __ LoadConst32(TMP, imm_high);
3631 __ Xor(TMP, TMP, lhs_high);
3632 __ LoadConst32(AT, imm_low);
3633 __ Xor(AT, AT, lhs_low);
3634 __ Or(TMP, TMP, AT);
3635 __ Beqz(TMP, label);
3636 break;
3637 case kCondNE:
3638 __ LoadConst32(TMP, imm_high);
3639 __ Xor(TMP, TMP, lhs_high);
3640 __ LoadConst32(AT, imm_low);
3641 __ Xor(AT, AT, lhs_low);
3642 __ Or(TMP, TMP, AT);
3643 __ Bnez(TMP, label);
3644 break;
3645 case kCondLT:
3646 __ LoadConst32(TMP, imm_high);
3647 __ Blt(lhs_high, TMP, label);
3648 __ Slt(TMP, TMP, lhs_high);
3649 __ LoadConst32(AT, imm_low);
3650 __ Sltu(AT, lhs_low, AT);
3651 __ Blt(TMP, AT, label);
3652 break;
3653 case kCondGE:
3654 __ LoadConst32(TMP, imm_high);
3655 __ Blt(TMP, lhs_high, label);
3656 __ Slt(TMP, lhs_high, TMP);
3657 __ LoadConst32(AT, imm_low);
3658 __ Sltu(AT, lhs_low, AT);
3659 __ Or(TMP, TMP, AT);
3660 __ Beqz(TMP, label);
3661 break;
3662 case kCondLE:
3663 __ LoadConst32(TMP, imm_high);
3664 __ Blt(lhs_high, TMP, label);
3665 __ Slt(TMP, TMP, lhs_high);
3666 __ LoadConst32(AT, imm_low);
3667 __ Sltu(AT, AT, lhs_low);
3668 __ Or(TMP, TMP, AT);
3669 __ Beqz(TMP, label);
3670 break;
3671 case kCondGT:
3672 __ LoadConst32(TMP, imm_high);
3673 __ Blt(TMP, lhs_high, label);
3674 __ Slt(TMP, lhs_high, TMP);
3675 __ LoadConst32(AT, imm_low);
3676 __ Sltu(AT, AT, lhs_low);
3677 __ Blt(TMP, AT, label);
3678 break;
3679 case kCondB:
3680 __ LoadConst32(TMP, imm_high);
3681 __ Bltu(lhs_high, TMP, label);
3682 __ Sltu(TMP, TMP, lhs_high);
3683 __ LoadConst32(AT, imm_low);
3684 __ Sltu(AT, lhs_low, AT);
3685 __ Blt(TMP, AT, label);
3686 break;
3687 case kCondAE:
3688 __ LoadConst32(TMP, imm_high);
3689 __ Bltu(TMP, lhs_high, label);
3690 __ Sltu(TMP, lhs_high, TMP);
3691 __ LoadConst32(AT, imm_low);
3692 __ Sltu(AT, lhs_low, AT);
3693 __ Or(TMP, TMP, AT);
3694 __ Beqz(TMP, label);
3695 break;
3696 case kCondBE:
3697 __ LoadConst32(TMP, imm_high);
3698 __ Bltu(lhs_high, TMP, label);
3699 __ Sltu(TMP, TMP, lhs_high);
3700 __ LoadConst32(AT, imm_low);
3701 __ Sltu(AT, AT, lhs_low);
3702 __ Or(TMP, TMP, AT);
3703 __ Beqz(TMP, label);
3704 break;
3705 case kCondA:
3706 __ LoadConst32(TMP, imm_high);
3707 __ Bltu(TMP, lhs_high, label);
3708 __ Sltu(TMP, lhs_high, TMP);
3709 __ LoadConst32(AT, imm_low);
3710 __ Sltu(AT, AT, lhs_low);
3711 __ Blt(TMP, AT, label);
3712 break;
3713 }
3714 } else {
3715 switch (cond) {
3716 case kCondEQ:
3717 __ Xor(TMP, lhs_high, rhs_high);
3718 __ Xor(AT, lhs_low, rhs_low);
3719 __ Or(TMP, TMP, AT);
3720 __ Beqz(TMP, label);
3721 break;
3722 case kCondNE:
3723 __ Xor(TMP, lhs_high, rhs_high);
3724 __ Xor(AT, lhs_low, rhs_low);
3725 __ Or(TMP, TMP, AT);
3726 __ Bnez(TMP, label);
3727 break;
3728 case kCondLT:
3729 __ Blt(lhs_high, rhs_high, label);
3730 __ Slt(TMP, rhs_high, lhs_high);
3731 __ Sltu(AT, lhs_low, rhs_low);
3732 __ Blt(TMP, AT, label);
3733 break;
3734 case kCondGE:
3735 __ Blt(rhs_high, lhs_high, label);
3736 __ Slt(TMP, lhs_high, rhs_high);
3737 __ Sltu(AT, lhs_low, rhs_low);
3738 __ Or(TMP, TMP, AT);
3739 __ Beqz(TMP, label);
3740 break;
3741 case kCondLE:
3742 __ Blt(lhs_high, rhs_high, label);
3743 __ Slt(TMP, rhs_high, lhs_high);
3744 __ Sltu(AT, rhs_low, lhs_low);
3745 __ Or(TMP, TMP, AT);
3746 __ Beqz(TMP, label);
3747 break;
3748 case kCondGT:
3749 __ Blt(rhs_high, lhs_high, label);
3750 __ Slt(TMP, lhs_high, rhs_high);
3751 __ Sltu(AT, rhs_low, lhs_low);
3752 __ Blt(TMP, AT, label);
3753 break;
3754 case kCondB:
3755 __ Bltu(lhs_high, rhs_high, label);
3756 __ Sltu(TMP, rhs_high, lhs_high);
3757 __ Sltu(AT, lhs_low, rhs_low);
3758 __ Blt(TMP, AT, label);
3759 break;
3760 case kCondAE:
3761 __ Bltu(rhs_high, lhs_high, label);
3762 __ Sltu(TMP, lhs_high, rhs_high);
3763 __ Sltu(AT, lhs_low, rhs_low);
3764 __ Or(TMP, TMP, AT);
3765 __ Beqz(TMP, label);
3766 break;
3767 case kCondBE:
3768 __ Bltu(lhs_high, rhs_high, label);
3769 __ Sltu(TMP, rhs_high, lhs_high);
3770 __ Sltu(AT, rhs_low, lhs_low);
3771 __ Or(TMP, TMP, AT);
3772 __ Beqz(TMP, label);
3773 break;
3774 case kCondA:
3775 __ Bltu(rhs_high, lhs_high, label);
3776 __ Sltu(TMP, lhs_high, rhs_high);
3777 __ Sltu(AT, rhs_low, lhs_low);
3778 __ Blt(TMP, AT, label);
3779 break;
3780 }
3781 }
3782}
3783
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003784void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3785 bool gt_bias,
3786 Primitive::Type type,
3787 LocationSummary* locations) {
3788 Register dst = locations->Out().AsRegister<Register>();
3789 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3790 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3791 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3792 if (type == Primitive::kPrimFloat) {
3793 if (isR6) {
3794 switch (cond) {
3795 case kCondEQ:
3796 __ CmpEqS(FTMP, lhs, rhs);
3797 __ Mfc1(dst, FTMP);
3798 __ Andi(dst, dst, 1);
3799 break;
3800 case kCondNE:
3801 __ CmpEqS(FTMP, lhs, rhs);
3802 __ Mfc1(dst, FTMP);
3803 __ Addiu(dst, dst, 1);
3804 break;
3805 case kCondLT:
3806 if (gt_bias) {
3807 __ CmpLtS(FTMP, lhs, rhs);
3808 } else {
3809 __ CmpUltS(FTMP, lhs, rhs);
3810 }
3811 __ Mfc1(dst, FTMP);
3812 __ Andi(dst, dst, 1);
3813 break;
3814 case kCondLE:
3815 if (gt_bias) {
3816 __ CmpLeS(FTMP, lhs, rhs);
3817 } else {
3818 __ CmpUleS(FTMP, lhs, rhs);
3819 }
3820 __ Mfc1(dst, FTMP);
3821 __ Andi(dst, dst, 1);
3822 break;
3823 case kCondGT:
3824 if (gt_bias) {
3825 __ CmpUltS(FTMP, rhs, lhs);
3826 } else {
3827 __ CmpLtS(FTMP, rhs, lhs);
3828 }
3829 __ Mfc1(dst, FTMP);
3830 __ Andi(dst, dst, 1);
3831 break;
3832 case kCondGE:
3833 if (gt_bias) {
3834 __ CmpUleS(FTMP, rhs, lhs);
3835 } else {
3836 __ CmpLeS(FTMP, rhs, lhs);
3837 }
3838 __ Mfc1(dst, FTMP);
3839 __ Andi(dst, dst, 1);
3840 break;
3841 default:
3842 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3843 UNREACHABLE();
3844 }
3845 } else {
3846 switch (cond) {
3847 case kCondEQ:
3848 __ CeqS(0, lhs, rhs);
3849 __ LoadConst32(dst, 1);
3850 __ Movf(dst, ZERO, 0);
3851 break;
3852 case kCondNE:
3853 __ CeqS(0, lhs, rhs);
3854 __ LoadConst32(dst, 1);
3855 __ Movt(dst, ZERO, 0);
3856 break;
3857 case kCondLT:
3858 if (gt_bias) {
3859 __ ColtS(0, lhs, rhs);
3860 } else {
3861 __ CultS(0, lhs, rhs);
3862 }
3863 __ LoadConst32(dst, 1);
3864 __ Movf(dst, ZERO, 0);
3865 break;
3866 case kCondLE:
3867 if (gt_bias) {
3868 __ ColeS(0, lhs, rhs);
3869 } else {
3870 __ CuleS(0, lhs, rhs);
3871 }
3872 __ LoadConst32(dst, 1);
3873 __ Movf(dst, ZERO, 0);
3874 break;
3875 case kCondGT:
3876 if (gt_bias) {
3877 __ CultS(0, rhs, lhs);
3878 } else {
3879 __ ColtS(0, rhs, lhs);
3880 }
3881 __ LoadConst32(dst, 1);
3882 __ Movf(dst, ZERO, 0);
3883 break;
3884 case kCondGE:
3885 if (gt_bias) {
3886 __ CuleS(0, rhs, lhs);
3887 } else {
3888 __ ColeS(0, rhs, lhs);
3889 }
3890 __ LoadConst32(dst, 1);
3891 __ Movf(dst, ZERO, 0);
3892 break;
3893 default:
3894 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3895 UNREACHABLE();
3896 }
3897 }
3898 } else {
3899 DCHECK_EQ(type, Primitive::kPrimDouble);
3900 if (isR6) {
3901 switch (cond) {
3902 case kCondEQ:
3903 __ CmpEqD(FTMP, lhs, rhs);
3904 __ Mfc1(dst, FTMP);
3905 __ Andi(dst, dst, 1);
3906 break;
3907 case kCondNE:
3908 __ CmpEqD(FTMP, lhs, rhs);
3909 __ Mfc1(dst, FTMP);
3910 __ Addiu(dst, dst, 1);
3911 break;
3912 case kCondLT:
3913 if (gt_bias) {
3914 __ CmpLtD(FTMP, lhs, rhs);
3915 } else {
3916 __ CmpUltD(FTMP, lhs, rhs);
3917 }
3918 __ Mfc1(dst, FTMP);
3919 __ Andi(dst, dst, 1);
3920 break;
3921 case kCondLE:
3922 if (gt_bias) {
3923 __ CmpLeD(FTMP, lhs, rhs);
3924 } else {
3925 __ CmpUleD(FTMP, lhs, rhs);
3926 }
3927 __ Mfc1(dst, FTMP);
3928 __ Andi(dst, dst, 1);
3929 break;
3930 case kCondGT:
3931 if (gt_bias) {
3932 __ CmpUltD(FTMP, rhs, lhs);
3933 } else {
3934 __ CmpLtD(FTMP, rhs, lhs);
3935 }
3936 __ Mfc1(dst, FTMP);
3937 __ Andi(dst, dst, 1);
3938 break;
3939 case kCondGE:
3940 if (gt_bias) {
3941 __ CmpUleD(FTMP, rhs, lhs);
3942 } else {
3943 __ CmpLeD(FTMP, rhs, lhs);
3944 }
3945 __ Mfc1(dst, FTMP);
3946 __ Andi(dst, dst, 1);
3947 break;
3948 default:
3949 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3950 UNREACHABLE();
3951 }
3952 } else {
3953 switch (cond) {
3954 case kCondEQ:
3955 __ CeqD(0, lhs, rhs);
3956 __ LoadConst32(dst, 1);
3957 __ Movf(dst, ZERO, 0);
3958 break;
3959 case kCondNE:
3960 __ CeqD(0, lhs, rhs);
3961 __ LoadConst32(dst, 1);
3962 __ Movt(dst, ZERO, 0);
3963 break;
3964 case kCondLT:
3965 if (gt_bias) {
3966 __ ColtD(0, lhs, rhs);
3967 } else {
3968 __ CultD(0, lhs, rhs);
3969 }
3970 __ LoadConst32(dst, 1);
3971 __ Movf(dst, ZERO, 0);
3972 break;
3973 case kCondLE:
3974 if (gt_bias) {
3975 __ ColeD(0, lhs, rhs);
3976 } else {
3977 __ CuleD(0, lhs, rhs);
3978 }
3979 __ LoadConst32(dst, 1);
3980 __ Movf(dst, ZERO, 0);
3981 break;
3982 case kCondGT:
3983 if (gt_bias) {
3984 __ CultD(0, rhs, lhs);
3985 } else {
3986 __ ColtD(0, rhs, lhs);
3987 }
3988 __ LoadConst32(dst, 1);
3989 __ Movf(dst, ZERO, 0);
3990 break;
3991 case kCondGE:
3992 if (gt_bias) {
3993 __ CuleD(0, rhs, lhs);
3994 } else {
3995 __ ColeD(0, rhs, lhs);
3996 }
3997 __ LoadConst32(dst, 1);
3998 __ Movf(dst, ZERO, 0);
3999 break;
4000 default:
4001 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4002 UNREACHABLE();
4003 }
4004 }
4005 }
4006}
4007
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004008bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4009 bool gt_bias,
4010 Primitive::Type type,
4011 LocationSummary* input_locations,
4012 int cc) {
4013 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4014 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4015 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4016 if (type == Primitive::kPrimFloat) {
4017 switch (cond) {
4018 case kCondEQ:
4019 __ CeqS(cc, lhs, rhs);
4020 return false;
4021 case kCondNE:
4022 __ CeqS(cc, lhs, rhs);
4023 return true;
4024 case kCondLT:
4025 if (gt_bias) {
4026 __ ColtS(cc, lhs, rhs);
4027 } else {
4028 __ CultS(cc, lhs, rhs);
4029 }
4030 return false;
4031 case kCondLE:
4032 if (gt_bias) {
4033 __ ColeS(cc, lhs, rhs);
4034 } else {
4035 __ CuleS(cc, lhs, rhs);
4036 }
4037 return false;
4038 case kCondGT:
4039 if (gt_bias) {
4040 __ CultS(cc, rhs, lhs);
4041 } else {
4042 __ ColtS(cc, rhs, lhs);
4043 }
4044 return false;
4045 case kCondGE:
4046 if (gt_bias) {
4047 __ CuleS(cc, rhs, lhs);
4048 } else {
4049 __ ColeS(cc, rhs, lhs);
4050 }
4051 return false;
4052 default:
4053 LOG(FATAL) << "Unexpected non-floating-point condition";
4054 UNREACHABLE();
4055 }
4056 } else {
4057 DCHECK_EQ(type, Primitive::kPrimDouble);
4058 switch (cond) {
4059 case kCondEQ:
4060 __ CeqD(cc, lhs, rhs);
4061 return false;
4062 case kCondNE:
4063 __ CeqD(cc, lhs, rhs);
4064 return true;
4065 case kCondLT:
4066 if (gt_bias) {
4067 __ ColtD(cc, lhs, rhs);
4068 } else {
4069 __ CultD(cc, lhs, rhs);
4070 }
4071 return false;
4072 case kCondLE:
4073 if (gt_bias) {
4074 __ ColeD(cc, lhs, rhs);
4075 } else {
4076 __ CuleD(cc, lhs, rhs);
4077 }
4078 return false;
4079 case kCondGT:
4080 if (gt_bias) {
4081 __ CultD(cc, rhs, lhs);
4082 } else {
4083 __ ColtD(cc, rhs, lhs);
4084 }
4085 return false;
4086 case kCondGE:
4087 if (gt_bias) {
4088 __ CuleD(cc, rhs, lhs);
4089 } else {
4090 __ ColeD(cc, rhs, lhs);
4091 }
4092 return false;
4093 default:
4094 LOG(FATAL) << "Unexpected non-floating-point condition";
4095 UNREACHABLE();
4096 }
4097 }
4098}
4099
4100bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4101 bool gt_bias,
4102 Primitive::Type type,
4103 LocationSummary* input_locations,
4104 FRegister dst) {
4105 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4106 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4107 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4108 if (type == Primitive::kPrimFloat) {
4109 switch (cond) {
4110 case kCondEQ:
4111 __ CmpEqS(dst, lhs, rhs);
4112 return false;
4113 case kCondNE:
4114 __ CmpEqS(dst, lhs, rhs);
4115 return true;
4116 case kCondLT:
4117 if (gt_bias) {
4118 __ CmpLtS(dst, lhs, rhs);
4119 } else {
4120 __ CmpUltS(dst, lhs, rhs);
4121 }
4122 return false;
4123 case kCondLE:
4124 if (gt_bias) {
4125 __ CmpLeS(dst, lhs, rhs);
4126 } else {
4127 __ CmpUleS(dst, lhs, rhs);
4128 }
4129 return false;
4130 case kCondGT:
4131 if (gt_bias) {
4132 __ CmpUltS(dst, rhs, lhs);
4133 } else {
4134 __ CmpLtS(dst, rhs, lhs);
4135 }
4136 return false;
4137 case kCondGE:
4138 if (gt_bias) {
4139 __ CmpUleS(dst, rhs, lhs);
4140 } else {
4141 __ CmpLeS(dst, rhs, lhs);
4142 }
4143 return false;
4144 default:
4145 LOG(FATAL) << "Unexpected non-floating-point condition";
4146 UNREACHABLE();
4147 }
4148 } else {
4149 DCHECK_EQ(type, Primitive::kPrimDouble);
4150 switch (cond) {
4151 case kCondEQ:
4152 __ CmpEqD(dst, lhs, rhs);
4153 return false;
4154 case kCondNE:
4155 __ CmpEqD(dst, lhs, rhs);
4156 return true;
4157 case kCondLT:
4158 if (gt_bias) {
4159 __ CmpLtD(dst, lhs, rhs);
4160 } else {
4161 __ CmpUltD(dst, lhs, rhs);
4162 }
4163 return false;
4164 case kCondLE:
4165 if (gt_bias) {
4166 __ CmpLeD(dst, lhs, rhs);
4167 } else {
4168 __ CmpUleD(dst, lhs, rhs);
4169 }
4170 return false;
4171 case kCondGT:
4172 if (gt_bias) {
4173 __ CmpUltD(dst, rhs, lhs);
4174 } else {
4175 __ CmpLtD(dst, rhs, lhs);
4176 }
4177 return false;
4178 case kCondGE:
4179 if (gt_bias) {
4180 __ CmpUleD(dst, rhs, lhs);
4181 } else {
4182 __ CmpLeD(dst, rhs, lhs);
4183 }
4184 return false;
4185 default:
4186 LOG(FATAL) << "Unexpected non-floating-point condition";
4187 UNREACHABLE();
4188 }
4189 }
4190}
4191
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004192void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
4193 bool gt_bias,
4194 Primitive::Type type,
4195 LocationSummary* locations,
4196 MipsLabel* label) {
4197 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4198 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4199 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4200 if (type == Primitive::kPrimFloat) {
4201 if (isR6) {
4202 switch (cond) {
4203 case kCondEQ:
4204 __ CmpEqS(FTMP, lhs, rhs);
4205 __ Bc1nez(FTMP, label);
4206 break;
4207 case kCondNE:
4208 __ CmpEqS(FTMP, lhs, rhs);
4209 __ Bc1eqz(FTMP, label);
4210 break;
4211 case kCondLT:
4212 if (gt_bias) {
4213 __ CmpLtS(FTMP, lhs, rhs);
4214 } else {
4215 __ CmpUltS(FTMP, lhs, rhs);
4216 }
4217 __ Bc1nez(FTMP, label);
4218 break;
4219 case kCondLE:
4220 if (gt_bias) {
4221 __ CmpLeS(FTMP, lhs, rhs);
4222 } else {
4223 __ CmpUleS(FTMP, lhs, rhs);
4224 }
4225 __ Bc1nez(FTMP, label);
4226 break;
4227 case kCondGT:
4228 if (gt_bias) {
4229 __ CmpUltS(FTMP, rhs, lhs);
4230 } else {
4231 __ CmpLtS(FTMP, rhs, lhs);
4232 }
4233 __ Bc1nez(FTMP, label);
4234 break;
4235 case kCondGE:
4236 if (gt_bias) {
4237 __ CmpUleS(FTMP, rhs, lhs);
4238 } else {
4239 __ CmpLeS(FTMP, rhs, lhs);
4240 }
4241 __ Bc1nez(FTMP, label);
4242 break;
4243 default:
4244 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004245 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004246 }
4247 } else {
4248 switch (cond) {
4249 case kCondEQ:
4250 __ CeqS(0, lhs, rhs);
4251 __ Bc1t(0, label);
4252 break;
4253 case kCondNE:
4254 __ CeqS(0, lhs, rhs);
4255 __ Bc1f(0, label);
4256 break;
4257 case kCondLT:
4258 if (gt_bias) {
4259 __ ColtS(0, lhs, rhs);
4260 } else {
4261 __ CultS(0, lhs, rhs);
4262 }
4263 __ Bc1t(0, label);
4264 break;
4265 case kCondLE:
4266 if (gt_bias) {
4267 __ ColeS(0, lhs, rhs);
4268 } else {
4269 __ CuleS(0, lhs, rhs);
4270 }
4271 __ Bc1t(0, label);
4272 break;
4273 case kCondGT:
4274 if (gt_bias) {
4275 __ CultS(0, rhs, lhs);
4276 } else {
4277 __ ColtS(0, rhs, lhs);
4278 }
4279 __ Bc1t(0, label);
4280 break;
4281 case kCondGE:
4282 if (gt_bias) {
4283 __ CuleS(0, rhs, lhs);
4284 } else {
4285 __ ColeS(0, rhs, lhs);
4286 }
4287 __ Bc1t(0, label);
4288 break;
4289 default:
4290 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004291 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004292 }
4293 }
4294 } else {
4295 DCHECK_EQ(type, Primitive::kPrimDouble);
4296 if (isR6) {
4297 switch (cond) {
4298 case kCondEQ:
4299 __ CmpEqD(FTMP, lhs, rhs);
4300 __ Bc1nez(FTMP, label);
4301 break;
4302 case kCondNE:
4303 __ CmpEqD(FTMP, lhs, rhs);
4304 __ Bc1eqz(FTMP, label);
4305 break;
4306 case kCondLT:
4307 if (gt_bias) {
4308 __ CmpLtD(FTMP, lhs, rhs);
4309 } else {
4310 __ CmpUltD(FTMP, lhs, rhs);
4311 }
4312 __ Bc1nez(FTMP, label);
4313 break;
4314 case kCondLE:
4315 if (gt_bias) {
4316 __ CmpLeD(FTMP, lhs, rhs);
4317 } else {
4318 __ CmpUleD(FTMP, lhs, rhs);
4319 }
4320 __ Bc1nez(FTMP, label);
4321 break;
4322 case kCondGT:
4323 if (gt_bias) {
4324 __ CmpUltD(FTMP, rhs, lhs);
4325 } else {
4326 __ CmpLtD(FTMP, rhs, lhs);
4327 }
4328 __ Bc1nez(FTMP, label);
4329 break;
4330 case kCondGE:
4331 if (gt_bias) {
4332 __ CmpUleD(FTMP, rhs, lhs);
4333 } else {
4334 __ CmpLeD(FTMP, rhs, lhs);
4335 }
4336 __ Bc1nez(FTMP, label);
4337 break;
4338 default:
4339 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004340 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004341 }
4342 } else {
4343 switch (cond) {
4344 case kCondEQ:
4345 __ CeqD(0, lhs, rhs);
4346 __ Bc1t(0, label);
4347 break;
4348 case kCondNE:
4349 __ CeqD(0, lhs, rhs);
4350 __ Bc1f(0, label);
4351 break;
4352 case kCondLT:
4353 if (gt_bias) {
4354 __ ColtD(0, lhs, rhs);
4355 } else {
4356 __ CultD(0, lhs, rhs);
4357 }
4358 __ Bc1t(0, label);
4359 break;
4360 case kCondLE:
4361 if (gt_bias) {
4362 __ ColeD(0, lhs, rhs);
4363 } else {
4364 __ CuleD(0, lhs, rhs);
4365 }
4366 __ Bc1t(0, label);
4367 break;
4368 case kCondGT:
4369 if (gt_bias) {
4370 __ CultD(0, rhs, lhs);
4371 } else {
4372 __ ColtD(0, rhs, lhs);
4373 }
4374 __ Bc1t(0, label);
4375 break;
4376 case kCondGE:
4377 if (gt_bias) {
4378 __ CuleD(0, rhs, lhs);
4379 } else {
4380 __ ColeD(0, rhs, lhs);
4381 }
4382 __ Bc1t(0, label);
4383 break;
4384 default:
4385 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004386 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004387 }
4388 }
4389 }
4390}
4391
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004392void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004393 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004394 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004395 MipsLabel* false_target) {
4396 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004397
David Brazdil0debae72015-11-12 18:37:00 +00004398 if (true_target == nullptr && false_target == nullptr) {
4399 // Nothing to do. The code always falls through.
4400 return;
4401 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004402 // Constant condition, statically compared against "true" (integer value 1).
4403 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004404 if (true_target != nullptr) {
4405 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004406 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004407 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004408 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004409 if (false_target != nullptr) {
4410 __ B(false_target);
4411 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004412 }
David Brazdil0debae72015-11-12 18:37:00 +00004413 return;
4414 }
4415
4416 // The following code generates these patterns:
4417 // (1) true_target == nullptr && false_target != nullptr
4418 // - opposite condition true => branch to false_target
4419 // (2) true_target != nullptr && false_target == nullptr
4420 // - condition true => branch to true_target
4421 // (3) true_target != nullptr && false_target != nullptr
4422 // - condition true => branch to true_target
4423 // - branch to false_target
4424 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004425 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004426 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004427 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004428 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004429 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4430 } else {
4431 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4432 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004433 } else {
4434 // The condition instruction has not been materialized, use its inputs as
4435 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004436 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004437 Primitive::Type type = condition->InputAt(0)->GetType();
4438 LocationSummary* locations = cond->GetLocations();
4439 IfCondition if_cond = condition->GetCondition();
4440 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004441
David Brazdil0debae72015-11-12 18:37:00 +00004442 if (true_target == nullptr) {
4443 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004444 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004445 }
4446
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004447 switch (type) {
4448 default:
4449 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4450 break;
4451 case Primitive::kPrimLong:
4452 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4453 break;
4454 case Primitive::kPrimFloat:
4455 case Primitive::kPrimDouble:
4456 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4457 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004458 }
4459 }
David Brazdil0debae72015-11-12 18:37:00 +00004460
4461 // If neither branch falls through (case 3), the conditional branch to `true_target`
4462 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4463 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004464 __ B(false_target);
4465 }
4466}
4467
4468void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004470 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004471 locations->SetInAt(0, Location::RequiresRegister());
4472 }
4473}
4474
4475void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004476 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4477 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4478 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4479 nullptr : codegen_->GetLabelOf(true_successor);
4480 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4481 nullptr : codegen_->GetLabelOf(false_successor);
4482 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004483}
4484
4485void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4486 LocationSummary* locations = new (GetGraph()->GetArena())
4487 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004488 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004489 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004490 locations->SetInAt(0, Location::RequiresRegister());
4491 }
4492}
4493
4494void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004495 SlowPathCodeMIPS* slow_path =
4496 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004497 GenerateTestAndBranch(deoptimize,
4498 /* condition_input_index */ 0,
4499 slow_path->GetEntryLabel(),
4500 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004501}
4502
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004503// This function returns true if a conditional move can be generated for HSelect.
4504// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4505// branches and regular moves.
4506//
4507// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4508//
4509// While determining feasibility of a conditional move and setting inputs/outputs
4510// are two distinct tasks, this function does both because they share quite a bit
4511// of common logic.
4512static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4513 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4514 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4515 HCondition* condition = cond->AsCondition();
4516
4517 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4518 Primitive::Type dst_type = select->GetType();
4519
4520 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4521 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4522 bool is_true_value_zero_constant =
4523 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4524 bool is_false_value_zero_constant =
4525 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4526
4527 bool can_move_conditionally = false;
4528 bool use_const_for_false_in = false;
4529 bool use_const_for_true_in = false;
4530
4531 if (!cond->IsConstant()) {
4532 switch (cond_type) {
4533 default:
4534 switch (dst_type) {
4535 default:
4536 // Moving int on int condition.
4537 if (is_r6) {
4538 if (is_true_value_zero_constant) {
4539 // seleqz out_reg, false_reg, cond_reg
4540 can_move_conditionally = true;
4541 use_const_for_true_in = true;
4542 } else if (is_false_value_zero_constant) {
4543 // selnez out_reg, true_reg, cond_reg
4544 can_move_conditionally = true;
4545 use_const_for_false_in = true;
4546 } else if (materialized) {
4547 // Not materializing unmaterialized int conditions
4548 // to keep the instruction count low.
4549 // selnez AT, true_reg, cond_reg
4550 // seleqz TMP, false_reg, cond_reg
4551 // or out_reg, AT, TMP
4552 can_move_conditionally = true;
4553 }
4554 } else {
4555 // movn out_reg, true_reg/ZERO, cond_reg
4556 can_move_conditionally = true;
4557 use_const_for_true_in = is_true_value_zero_constant;
4558 }
4559 break;
4560 case Primitive::kPrimLong:
4561 // Moving long on int condition.
4562 if (is_r6) {
4563 if (is_true_value_zero_constant) {
4564 // seleqz out_reg_lo, false_reg_lo, cond_reg
4565 // seleqz out_reg_hi, false_reg_hi, cond_reg
4566 can_move_conditionally = true;
4567 use_const_for_true_in = true;
4568 } else if (is_false_value_zero_constant) {
4569 // selnez out_reg_lo, true_reg_lo, cond_reg
4570 // selnez out_reg_hi, true_reg_hi, cond_reg
4571 can_move_conditionally = true;
4572 use_const_for_false_in = true;
4573 }
4574 // Other long conditional moves would generate 6+ instructions,
4575 // which is too many.
4576 } else {
4577 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4578 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4579 can_move_conditionally = true;
4580 use_const_for_true_in = is_true_value_zero_constant;
4581 }
4582 break;
4583 case Primitive::kPrimFloat:
4584 case Primitive::kPrimDouble:
4585 // Moving float/double on int condition.
4586 if (is_r6) {
4587 if (materialized) {
4588 // Not materializing unmaterialized int conditions
4589 // to keep the instruction count low.
4590 can_move_conditionally = true;
4591 if (is_true_value_zero_constant) {
4592 // sltu TMP, ZERO, cond_reg
4593 // mtc1 TMP, temp_cond_reg
4594 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4595 use_const_for_true_in = true;
4596 } else if (is_false_value_zero_constant) {
4597 // sltu TMP, ZERO, cond_reg
4598 // mtc1 TMP, temp_cond_reg
4599 // selnez.fmt out_reg, true_reg, temp_cond_reg
4600 use_const_for_false_in = true;
4601 } else {
4602 // sltu TMP, ZERO, cond_reg
4603 // mtc1 TMP, temp_cond_reg
4604 // sel.fmt temp_cond_reg, false_reg, true_reg
4605 // mov.fmt out_reg, temp_cond_reg
4606 }
4607 }
4608 } else {
4609 // movn.fmt out_reg, true_reg, cond_reg
4610 can_move_conditionally = true;
4611 }
4612 break;
4613 }
4614 break;
4615 case Primitive::kPrimLong:
4616 // We don't materialize long comparison now
4617 // and use conditional branches instead.
4618 break;
4619 case Primitive::kPrimFloat:
4620 case Primitive::kPrimDouble:
4621 switch (dst_type) {
4622 default:
4623 // Moving int on float/double condition.
4624 if (is_r6) {
4625 if (is_true_value_zero_constant) {
4626 // mfc1 TMP, temp_cond_reg
4627 // seleqz out_reg, false_reg, TMP
4628 can_move_conditionally = true;
4629 use_const_for_true_in = true;
4630 } else if (is_false_value_zero_constant) {
4631 // mfc1 TMP, temp_cond_reg
4632 // selnez out_reg, true_reg, TMP
4633 can_move_conditionally = true;
4634 use_const_for_false_in = true;
4635 } else {
4636 // mfc1 TMP, temp_cond_reg
4637 // selnez AT, true_reg, TMP
4638 // seleqz TMP, false_reg, TMP
4639 // or out_reg, AT, TMP
4640 can_move_conditionally = true;
4641 }
4642 } else {
4643 // movt out_reg, true_reg/ZERO, cc
4644 can_move_conditionally = true;
4645 use_const_for_true_in = is_true_value_zero_constant;
4646 }
4647 break;
4648 case Primitive::kPrimLong:
4649 // Moving long on float/double condition.
4650 if (is_r6) {
4651 if (is_true_value_zero_constant) {
4652 // mfc1 TMP, temp_cond_reg
4653 // seleqz out_reg_lo, false_reg_lo, TMP
4654 // seleqz out_reg_hi, false_reg_hi, TMP
4655 can_move_conditionally = true;
4656 use_const_for_true_in = true;
4657 } else if (is_false_value_zero_constant) {
4658 // mfc1 TMP, temp_cond_reg
4659 // selnez out_reg_lo, true_reg_lo, TMP
4660 // selnez out_reg_hi, true_reg_hi, TMP
4661 can_move_conditionally = true;
4662 use_const_for_false_in = true;
4663 }
4664 // Other long conditional moves would generate 6+ instructions,
4665 // which is too many.
4666 } else {
4667 // movt out_reg_lo, true_reg_lo/ZERO, cc
4668 // movt out_reg_hi, true_reg_hi/ZERO, cc
4669 can_move_conditionally = true;
4670 use_const_for_true_in = is_true_value_zero_constant;
4671 }
4672 break;
4673 case Primitive::kPrimFloat:
4674 case Primitive::kPrimDouble:
4675 // Moving float/double on float/double condition.
4676 if (is_r6) {
4677 can_move_conditionally = true;
4678 if (is_true_value_zero_constant) {
4679 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4680 use_const_for_true_in = true;
4681 } else if (is_false_value_zero_constant) {
4682 // selnez.fmt out_reg, true_reg, temp_cond_reg
4683 use_const_for_false_in = true;
4684 } else {
4685 // sel.fmt temp_cond_reg, false_reg, true_reg
4686 // mov.fmt out_reg, temp_cond_reg
4687 }
4688 } else {
4689 // movt.fmt out_reg, true_reg, cc
4690 can_move_conditionally = true;
4691 }
4692 break;
4693 }
4694 break;
4695 }
4696 }
4697
4698 if (can_move_conditionally) {
4699 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4700 } else {
4701 DCHECK(!use_const_for_false_in);
4702 DCHECK(!use_const_for_true_in);
4703 }
4704
4705 if (locations_to_set != nullptr) {
4706 if (use_const_for_false_in) {
4707 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4708 } else {
4709 locations_to_set->SetInAt(0,
4710 Primitive::IsFloatingPointType(dst_type)
4711 ? Location::RequiresFpuRegister()
4712 : Location::RequiresRegister());
4713 }
4714 if (use_const_for_true_in) {
4715 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4716 } else {
4717 locations_to_set->SetInAt(1,
4718 Primitive::IsFloatingPointType(dst_type)
4719 ? Location::RequiresFpuRegister()
4720 : Location::RequiresRegister());
4721 }
4722 if (materialized) {
4723 locations_to_set->SetInAt(2, Location::RequiresRegister());
4724 }
4725 // On R6 we don't require the output to be the same as the
4726 // first input for conditional moves unlike on R2.
4727 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4728 if (is_out_same_as_first_in) {
4729 locations_to_set->SetOut(Location::SameAsFirstInput());
4730 } else {
4731 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4732 ? Location::RequiresFpuRegister()
4733 : Location::RequiresRegister());
4734 }
4735 }
4736
4737 return can_move_conditionally;
4738}
4739
4740void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4741 LocationSummary* locations = select->GetLocations();
4742 Location dst = locations->Out();
4743 Location src = locations->InAt(1);
4744 Register src_reg = ZERO;
4745 Register src_reg_high = ZERO;
4746 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4747 Register cond_reg = TMP;
4748 int cond_cc = 0;
4749 Primitive::Type cond_type = Primitive::kPrimInt;
4750 bool cond_inverted = false;
4751 Primitive::Type dst_type = select->GetType();
4752
4753 if (IsBooleanValueOrMaterializedCondition(cond)) {
4754 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4755 } else {
4756 HCondition* condition = cond->AsCondition();
4757 LocationSummary* cond_locations = cond->GetLocations();
4758 IfCondition if_cond = condition->GetCondition();
4759 cond_type = condition->InputAt(0)->GetType();
4760 switch (cond_type) {
4761 default:
4762 DCHECK_NE(cond_type, Primitive::kPrimLong);
4763 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4764 break;
4765 case Primitive::kPrimFloat:
4766 case Primitive::kPrimDouble:
4767 cond_inverted = MaterializeFpCompareR2(if_cond,
4768 condition->IsGtBias(),
4769 cond_type,
4770 cond_locations,
4771 cond_cc);
4772 break;
4773 }
4774 }
4775
4776 DCHECK(dst.Equals(locations->InAt(0)));
4777 if (src.IsRegister()) {
4778 src_reg = src.AsRegister<Register>();
4779 } else if (src.IsRegisterPair()) {
4780 src_reg = src.AsRegisterPairLow<Register>();
4781 src_reg_high = src.AsRegisterPairHigh<Register>();
4782 } else if (src.IsConstant()) {
4783 DCHECK(src.GetConstant()->IsZeroBitPattern());
4784 }
4785
4786 switch (cond_type) {
4787 default:
4788 switch (dst_type) {
4789 default:
4790 if (cond_inverted) {
4791 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4792 } else {
4793 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4794 }
4795 break;
4796 case Primitive::kPrimLong:
4797 if (cond_inverted) {
4798 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4799 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4800 } else {
4801 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4802 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4803 }
4804 break;
4805 case Primitive::kPrimFloat:
4806 if (cond_inverted) {
4807 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4808 } else {
4809 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4810 }
4811 break;
4812 case Primitive::kPrimDouble:
4813 if (cond_inverted) {
4814 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4815 } else {
4816 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4817 }
4818 break;
4819 }
4820 break;
4821 case Primitive::kPrimLong:
4822 LOG(FATAL) << "Unreachable";
4823 UNREACHABLE();
4824 case Primitive::kPrimFloat:
4825 case Primitive::kPrimDouble:
4826 switch (dst_type) {
4827 default:
4828 if (cond_inverted) {
4829 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4830 } else {
4831 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4832 }
4833 break;
4834 case Primitive::kPrimLong:
4835 if (cond_inverted) {
4836 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4837 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4838 } else {
4839 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4840 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4841 }
4842 break;
4843 case Primitive::kPrimFloat:
4844 if (cond_inverted) {
4845 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4846 } else {
4847 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4848 }
4849 break;
4850 case Primitive::kPrimDouble:
4851 if (cond_inverted) {
4852 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4853 } else {
4854 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4855 }
4856 break;
4857 }
4858 break;
4859 }
4860}
4861
4862void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4863 LocationSummary* locations = select->GetLocations();
4864 Location dst = locations->Out();
4865 Location false_src = locations->InAt(0);
4866 Location true_src = locations->InAt(1);
4867 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4868 Register cond_reg = TMP;
4869 FRegister fcond_reg = FTMP;
4870 Primitive::Type cond_type = Primitive::kPrimInt;
4871 bool cond_inverted = false;
4872 Primitive::Type dst_type = select->GetType();
4873
4874 if (IsBooleanValueOrMaterializedCondition(cond)) {
4875 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4876 } else {
4877 HCondition* condition = cond->AsCondition();
4878 LocationSummary* cond_locations = cond->GetLocations();
4879 IfCondition if_cond = condition->GetCondition();
4880 cond_type = condition->InputAt(0)->GetType();
4881 switch (cond_type) {
4882 default:
4883 DCHECK_NE(cond_type, Primitive::kPrimLong);
4884 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4885 break;
4886 case Primitive::kPrimFloat:
4887 case Primitive::kPrimDouble:
4888 cond_inverted = MaterializeFpCompareR6(if_cond,
4889 condition->IsGtBias(),
4890 cond_type,
4891 cond_locations,
4892 fcond_reg);
4893 break;
4894 }
4895 }
4896
4897 if (true_src.IsConstant()) {
4898 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4899 }
4900 if (false_src.IsConstant()) {
4901 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4902 }
4903
4904 switch (dst_type) {
4905 default:
4906 if (Primitive::IsFloatingPointType(cond_type)) {
4907 __ Mfc1(cond_reg, fcond_reg);
4908 }
4909 if (true_src.IsConstant()) {
4910 if (cond_inverted) {
4911 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4912 } else {
4913 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4914 }
4915 } else if (false_src.IsConstant()) {
4916 if (cond_inverted) {
4917 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4918 } else {
4919 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4920 }
4921 } else {
4922 DCHECK_NE(cond_reg, AT);
4923 if (cond_inverted) {
4924 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4925 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4926 } else {
4927 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4928 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4929 }
4930 __ Or(dst.AsRegister<Register>(), AT, TMP);
4931 }
4932 break;
4933 case Primitive::kPrimLong: {
4934 if (Primitive::IsFloatingPointType(cond_type)) {
4935 __ Mfc1(cond_reg, fcond_reg);
4936 }
4937 Register dst_lo = dst.AsRegisterPairLow<Register>();
4938 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4939 if (true_src.IsConstant()) {
4940 Register src_lo = false_src.AsRegisterPairLow<Register>();
4941 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4942 if (cond_inverted) {
4943 __ Selnez(dst_lo, src_lo, cond_reg);
4944 __ Selnez(dst_hi, src_hi, cond_reg);
4945 } else {
4946 __ Seleqz(dst_lo, src_lo, cond_reg);
4947 __ Seleqz(dst_hi, src_hi, cond_reg);
4948 }
4949 } else {
4950 DCHECK(false_src.IsConstant());
4951 Register src_lo = true_src.AsRegisterPairLow<Register>();
4952 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4953 if (cond_inverted) {
4954 __ Seleqz(dst_lo, src_lo, cond_reg);
4955 __ Seleqz(dst_hi, src_hi, cond_reg);
4956 } else {
4957 __ Selnez(dst_lo, src_lo, cond_reg);
4958 __ Selnez(dst_hi, src_hi, cond_reg);
4959 }
4960 }
4961 break;
4962 }
4963 case Primitive::kPrimFloat: {
4964 if (!Primitive::IsFloatingPointType(cond_type)) {
4965 // sel*.fmt tests bit 0 of the condition register, account for that.
4966 __ Sltu(TMP, ZERO, cond_reg);
4967 __ Mtc1(TMP, fcond_reg);
4968 }
4969 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4970 if (true_src.IsConstant()) {
4971 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4972 if (cond_inverted) {
4973 __ SelnezS(dst_reg, src_reg, fcond_reg);
4974 } else {
4975 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4976 }
4977 } else if (false_src.IsConstant()) {
4978 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4979 if (cond_inverted) {
4980 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4981 } else {
4982 __ SelnezS(dst_reg, src_reg, fcond_reg);
4983 }
4984 } else {
4985 if (cond_inverted) {
4986 __ SelS(fcond_reg,
4987 true_src.AsFpuRegister<FRegister>(),
4988 false_src.AsFpuRegister<FRegister>());
4989 } else {
4990 __ SelS(fcond_reg,
4991 false_src.AsFpuRegister<FRegister>(),
4992 true_src.AsFpuRegister<FRegister>());
4993 }
4994 __ MovS(dst_reg, fcond_reg);
4995 }
4996 break;
4997 }
4998 case Primitive::kPrimDouble: {
4999 if (!Primitive::IsFloatingPointType(cond_type)) {
5000 // sel*.fmt tests bit 0 of the condition register, account for that.
5001 __ Sltu(TMP, ZERO, cond_reg);
5002 __ Mtc1(TMP, fcond_reg);
5003 }
5004 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5005 if (true_src.IsConstant()) {
5006 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5007 if (cond_inverted) {
5008 __ SelnezD(dst_reg, src_reg, fcond_reg);
5009 } else {
5010 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5011 }
5012 } else if (false_src.IsConstant()) {
5013 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5014 if (cond_inverted) {
5015 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5016 } else {
5017 __ SelnezD(dst_reg, src_reg, fcond_reg);
5018 }
5019 } else {
5020 if (cond_inverted) {
5021 __ SelD(fcond_reg,
5022 true_src.AsFpuRegister<FRegister>(),
5023 false_src.AsFpuRegister<FRegister>());
5024 } else {
5025 __ SelD(fcond_reg,
5026 false_src.AsFpuRegister<FRegister>(),
5027 true_src.AsFpuRegister<FRegister>());
5028 }
5029 __ MovD(dst_reg, fcond_reg);
5030 }
5031 break;
5032 }
5033 }
5034}
5035
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005036void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5037 LocationSummary* locations = new (GetGraph()->GetArena())
5038 LocationSummary(flag, LocationSummary::kNoCall);
5039 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005040}
5041
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005042void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5043 __ LoadFromOffset(kLoadWord,
5044 flag->GetLocations()->Out().AsRegister<Register>(),
5045 SP,
5046 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005047}
5048
David Brazdil74eb1b22015-12-14 11:44:01 +00005049void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5050 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005051 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005052}
5053
5054void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005055 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5056 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5057 if (is_r6) {
5058 GenConditionalMoveR6(select);
5059 } else {
5060 GenConditionalMoveR2(select);
5061 }
5062 } else {
5063 LocationSummary* locations = select->GetLocations();
5064 MipsLabel false_target;
5065 GenerateTestAndBranch(select,
5066 /* condition_input_index */ 2,
5067 /* true_target */ nullptr,
5068 &false_target);
5069 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5070 __ Bind(&false_target);
5071 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005072}
5073
David Srbecky0cf44932015-12-09 14:09:59 +00005074void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5075 new (GetGraph()->GetArena()) LocationSummary(info);
5076}
5077
David Srbeckyd28f4a02016-03-14 17:14:24 +00005078void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5079 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005080}
5081
5082void CodeGeneratorMIPS::GenerateNop() {
5083 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005084}
5085
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005086void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5087 Primitive::Type field_type = field_info.GetFieldType();
5088 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5089 bool generate_volatile = field_info.IsVolatile() && is_wide;
5090 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005091 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092
5093 locations->SetInAt(0, Location::RequiresRegister());
5094 if (generate_volatile) {
5095 InvokeRuntimeCallingConvention calling_convention;
5096 // need A0 to hold base + offset
5097 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5098 if (field_type == Primitive::kPrimLong) {
5099 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5100 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005101 // Use Location::Any() to prevent situations when running out of available fp registers.
5102 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005103 // Need some temp core regs since FP results are returned in core registers
5104 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5105 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5106 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5107 }
5108 } else {
5109 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5110 locations->SetOut(Location::RequiresFpuRegister());
5111 } else {
5112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5113 }
5114 }
5115}
5116
5117void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
5118 const FieldInfo& field_info,
5119 uint32_t dex_pc) {
5120 Primitive::Type type = field_info.GetFieldType();
5121 LocationSummary* locations = instruction->GetLocations();
5122 Register obj = locations->InAt(0).AsRegister<Register>();
5123 LoadOperandType load_type = kLoadUnsignedByte;
5124 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005125 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005126 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005127
5128 switch (type) {
5129 case Primitive::kPrimBoolean:
5130 load_type = kLoadUnsignedByte;
5131 break;
5132 case Primitive::kPrimByte:
5133 load_type = kLoadSignedByte;
5134 break;
5135 case Primitive::kPrimShort:
5136 load_type = kLoadSignedHalfword;
5137 break;
5138 case Primitive::kPrimChar:
5139 load_type = kLoadUnsignedHalfword;
5140 break;
5141 case Primitive::kPrimInt:
5142 case Primitive::kPrimFloat:
5143 case Primitive::kPrimNot:
5144 load_type = kLoadWord;
5145 break;
5146 case Primitive::kPrimLong:
5147 case Primitive::kPrimDouble:
5148 load_type = kLoadDoubleword;
5149 break;
5150 case Primitive::kPrimVoid:
5151 LOG(FATAL) << "Unreachable type " << type;
5152 UNREACHABLE();
5153 }
5154
5155 if (is_volatile && load_type == kLoadDoubleword) {
5156 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005157 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005158 // Do implicit Null check
5159 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5160 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01005161 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005162 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
5163 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005164 // FP results are returned in core registers. Need to move them.
5165 Location out = locations->Out();
5166 if (out.IsFpuRegister()) {
5167 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
5168 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
5169 out.AsFpuRegister<FRegister>());
5170 } else {
5171 DCHECK(out.IsDoubleStackSlot());
5172 __ StoreToOffset(kStoreWord,
5173 locations->GetTemp(1).AsRegister<Register>(),
5174 SP,
5175 out.GetStackIndex());
5176 __ StoreToOffset(kStoreWord,
5177 locations->GetTemp(2).AsRegister<Register>(),
5178 SP,
5179 out.GetStackIndex() + 4);
5180 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005181 }
5182 } else {
5183 if (!Primitive::IsFloatingPointType(type)) {
5184 Register dst;
5185 if (type == Primitive::kPrimLong) {
5186 DCHECK(locations->Out().IsRegisterPair());
5187 dst = locations->Out().AsRegisterPairLow<Register>();
5188 } else {
5189 DCHECK(locations->Out().IsRegister());
5190 dst = locations->Out().AsRegister<Register>();
5191 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005192 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08005193 if (type == Primitive::kPrimNot) {
5194 __ MaybeUnpoisonHeapReference(dst);
5195 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005196 } else {
5197 DCHECK(locations->Out().IsFpuRegister());
5198 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5199 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005200 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005201 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005202 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005203 }
5204 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005205 }
5206
5207 if (is_volatile) {
5208 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5209 }
5210}
5211
5212void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5213 Primitive::Type field_type = field_info.GetFieldType();
5214 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5215 bool generate_volatile = field_info.IsVolatile() && is_wide;
5216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005217 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005218
5219 locations->SetInAt(0, Location::RequiresRegister());
5220 if (generate_volatile) {
5221 InvokeRuntimeCallingConvention calling_convention;
5222 // need A0 to hold base + offset
5223 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5224 if (field_type == Primitive::kPrimLong) {
5225 locations->SetInAt(1, Location::RegisterPairLocation(
5226 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5227 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005228 // Use Location::Any() to prevent situations when running out of available fp registers.
5229 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005230 // Pass FP parameters in core registers.
5231 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5232 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
5233 }
5234 } else {
5235 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005236 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005237 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005238 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005239 }
5240 }
5241}
5242
5243void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
5244 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01005245 uint32_t dex_pc,
5246 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005247 Primitive::Type type = field_info.GetFieldType();
5248 LocationSummary* locations = instruction->GetLocations();
5249 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07005250 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005251 StoreOperandType store_type = kStoreByte;
5252 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005253 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08005254 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005255 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005256
5257 switch (type) {
5258 case Primitive::kPrimBoolean:
5259 case Primitive::kPrimByte:
5260 store_type = kStoreByte;
5261 break;
5262 case Primitive::kPrimShort:
5263 case Primitive::kPrimChar:
5264 store_type = kStoreHalfword;
5265 break;
5266 case Primitive::kPrimInt:
5267 case Primitive::kPrimFloat:
5268 case Primitive::kPrimNot:
5269 store_type = kStoreWord;
5270 break;
5271 case Primitive::kPrimLong:
5272 case Primitive::kPrimDouble:
5273 store_type = kStoreDoubleword;
5274 break;
5275 case Primitive::kPrimVoid:
5276 LOG(FATAL) << "Unreachable type " << type;
5277 UNREACHABLE();
5278 }
5279
5280 if (is_volatile) {
5281 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5282 }
5283
5284 if (is_volatile && store_type == kStoreDoubleword) {
5285 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005286 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005287 // Do implicit Null check.
5288 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5289 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5290 if (type == Primitive::kPrimDouble) {
5291 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07005292 if (value_location.IsFpuRegister()) {
5293 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
5294 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005295 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07005296 value_location.AsFpuRegister<FRegister>());
5297 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005298 __ LoadFromOffset(kLoadWord,
5299 locations->GetTemp(1).AsRegister<Register>(),
5300 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005301 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005302 __ LoadFromOffset(kLoadWord,
5303 locations->GetTemp(2).AsRegister<Register>(),
5304 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005305 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005306 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005307 DCHECK(value_location.IsConstant());
5308 DCHECK(value_location.GetConstant()->IsDoubleConstant());
5309 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005310 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
5311 locations->GetTemp(1).AsRegister<Register>(),
5312 value);
5313 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005314 }
Serban Constantinescufca16662016-07-14 09:21:59 +01005315 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005316 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
5317 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005318 if (value_location.IsConstant()) {
5319 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
5320 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
5321 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005322 Register src;
5323 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005324 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005325 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005326 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005327 }
Alexey Frunzec061de12017-02-14 13:27:23 -08005328 if (kPoisonHeapReferences && needs_write_barrier) {
5329 // Note that in the case where `value` is a null reference,
5330 // we do not enter this block, as a null reference does not
5331 // need poisoning.
5332 DCHECK_EQ(type, Primitive::kPrimNot);
5333 __ PoisonHeapReference(TMP, src);
5334 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
5335 } else {
5336 __ StoreToOffset(store_type, src, obj, offset, null_checker);
5337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005338 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005339 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005340 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005341 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005342 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005343 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005344 }
5345 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005346 }
5347
5348 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08005349 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005350 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01005351 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005352 }
5353
5354 if (is_volatile) {
5355 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5356 }
5357}
5358
5359void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5360 HandleFieldGet(instruction, instruction->GetFieldInfo());
5361}
5362
5363void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5364 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5365}
5366
5367void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5368 HandleFieldSet(instruction, instruction->GetFieldInfo());
5369}
5370
5371void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005372 HandleFieldSet(instruction,
5373 instruction->GetFieldInfo(),
5374 instruction->GetDexPc(),
5375 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005376}
5377
Alexey Frunze06a46c42016-07-19 15:00:40 -07005378void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5379 HInstruction* instruction ATTRIBUTE_UNUSED,
5380 Location root,
5381 Register obj,
5382 uint32_t offset) {
5383 Register root_reg = root.AsRegister<Register>();
5384 if (kEmitCompilerReadBarrier) {
5385 UNIMPLEMENTED(FATAL) << "for read barrier";
5386 } else {
5387 // Plain GC root load with no read barrier.
5388 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5389 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5390 // Note that GC roots are not affected by heap poisoning, thus we
5391 // do not have to unpoison `root_reg` here.
5392 }
5393}
5394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005395void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5396 LocationSummary::CallKind call_kind =
5397 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5399 locations->SetInAt(0, Location::RequiresRegister());
5400 locations->SetInAt(1, Location::RequiresRegister());
5401 // The output does overlap inputs.
5402 // Note that TypeCheckSlowPathMIPS uses this register too.
5403 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5404}
5405
5406void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5407 LocationSummary* locations = instruction->GetLocations();
5408 Register obj = locations->InAt(0).AsRegister<Register>();
5409 Register cls = locations->InAt(1).AsRegister<Register>();
5410 Register out = locations->Out().AsRegister<Register>();
5411
5412 MipsLabel done;
5413
5414 // Return 0 if `obj` is null.
5415 // TODO: Avoid this check if we know `obj` is not null.
5416 __ Move(out, ZERO);
5417 __ Beqz(obj, &done);
5418
5419 // Compare the class of `obj` with `cls`.
5420 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08005421 __ MaybeUnpoisonHeapReference(out);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422 if (instruction->IsExactCheck()) {
5423 // Classes must be equal for the instanceof to succeed.
5424 __ Xor(out, out, cls);
5425 __ Sltiu(out, out, 1);
5426 } else {
5427 // If the classes are not equal, we go into a slow path.
5428 DCHECK(locations->OnlyCallsOnSlowPath());
5429 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5430 codegen_->AddSlowPath(slow_path);
5431 __ Bne(out, cls, slow_path->GetEntryLabel());
5432 __ LoadConst32(out, 1);
5433 __ Bind(slow_path->GetExitLabel());
5434 }
5435
5436 __ Bind(&done);
5437}
5438
5439void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5440 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5441 locations->SetOut(Location::ConstantLocation(constant));
5442}
5443
5444void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5445 // Will be generated at use site.
5446}
5447
5448void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5450 locations->SetOut(Location::ConstantLocation(constant));
5451}
5452
5453void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5454 // Will be generated at use site.
5455}
5456
5457void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5458 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5459 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5460}
5461
5462void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5463 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005464 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005465 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005466 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005467}
5468
5469void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5470 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5471 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005472 Location receiver = invoke->GetLocations()->InAt(0);
5473 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005474 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005475
5476 // Set the hidden argument.
5477 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5478 invoke->GetDexMethodIndex());
5479
5480 // temp = object->GetClass();
5481 if (receiver.IsStackSlot()) {
5482 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5483 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5484 } else {
5485 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5486 }
5487 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005488 // Instead of simply (possibly) unpoisoning `temp` here, we should
5489 // emit a read barrier for the previous class reference load.
5490 // However this is not required in practice, as this is an
5491 // intermediate/temporary reference and because the current
5492 // concurrent copying collector keeps the from-space memory
5493 // intact/accessible until the end of the marking phase (the
5494 // concurrent copying collector may not in the future).
5495 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005496 __ LoadFromOffset(kLoadWord, temp, temp,
5497 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5498 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005499 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005500 // temp = temp->GetImtEntryAt(method_offset);
5501 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5502 // T9 = temp->GetEntryPoint();
5503 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5504 // T9();
5505 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005506 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005507 DCHECK(!codegen_->IsLeafMethod());
5508 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5509}
5510
5511void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005512 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5513 if (intrinsic.TryDispatch(invoke)) {
5514 return;
5515 }
5516
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005517 HandleInvoke(invoke);
5518}
5519
5520void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005521 // Explicit clinit checks triggered by static invokes must have been pruned by
5522 // art::PrepareForRegisterAllocation.
5523 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005524
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005525 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5526 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005527
Chris Larsen701566a2015-10-27 15:29:13 -07005528 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5529 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005530 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5531 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5532 }
Chris Larsen701566a2015-10-27 15:29:13 -07005533 return;
5534 }
5535
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005537
5538 // Add the extra input register if either the dex cache array base register
5539 // or the PC-relative base register for accessing literals is needed.
5540 if (has_extra_input) {
5541 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5542 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005543}
5544
Orion Hodsonac141392017-01-13 11:53:47 +00005545void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5546 HandleInvoke(invoke);
5547}
5548
5549void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5550 codegen_->GenerateInvokePolymorphicCall(invoke);
5551}
5552
Chris Larsen701566a2015-10-27 15:29:13 -07005553static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005554 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005555 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5556 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557 return true;
5558 }
5559 return false;
5560}
5561
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005562HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005563 HLoadString::LoadKind desired_string_load_kind) {
5564 if (kEmitCompilerReadBarrier) {
5565 UNIMPLEMENTED(FATAL) << "for read barrier";
5566 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005567 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005568 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005569 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5570 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005571 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005572 bool is_r6 = GetInstructionSetFeatures().IsR6();
5573 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005574 switch (desired_string_load_kind) {
5575 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5576 DCHECK(!GetCompilerOptions().GetCompilePic());
5577 break;
5578 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5579 DCHECK(GetCompilerOptions().GetCompilePic());
5580 break;
5581 case HLoadString::LoadKind::kBootImageAddress:
5582 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005583 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005584 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005585 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005586 case HLoadString::LoadKind::kJitTableAddress:
5587 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005588 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005589 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005590 case HLoadString::LoadKind::kDexCacheViaMethod:
5591 fallback_load = false;
5592 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005593 }
5594 if (fallback_load) {
5595 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5596 }
5597 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005598}
5599
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005600HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5601 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005602 if (kEmitCompilerReadBarrier) {
5603 UNIMPLEMENTED(FATAL) << "for read barrier";
5604 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005605 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005606 // is incompatible with it.
5607 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005608 bool is_r6 = GetInstructionSetFeatures().IsR6();
5609 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005610 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005611 case HLoadClass::LoadKind::kInvalid:
5612 LOG(FATAL) << "UNREACHABLE";
5613 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005614 case HLoadClass::LoadKind::kReferrersClass:
5615 fallback_load = false;
5616 break;
5617 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5618 DCHECK(!GetCompilerOptions().GetCompilePic());
5619 break;
5620 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5621 DCHECK(GetCompilerOptions().GetCompilePic());
5622 break;
5623 case HLoadClass::LoadKind::kBootImageAddress:
5624 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005625 case HLoadClass::LoadKind::kBssEntry:
5626 DCHECK(!Runtime::Current()->UseJitCompilation());
5627 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005628 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005629 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005630 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005631 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005632 case HLoadClass::LoadKind::kDexCacheViaMethod:
5633 fallback_load = false;
5634 break;
5635 }
5636 if (fallback_load) {
5637 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5638 }
5639 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005640}
5641
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005642Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5643 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005644 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005645 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5646 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5647 if (!invoke->GetLocations()->Intrinsified()) {
5648 return location.AsRegister<Register>();
5649 }
5650 // For intrinsics we allow any location, so it may be on the stack.
5651 if (!location.IsRegister()) {
5652 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5653 return temp;
5654 }
5655 // For register locations, check if the register was saved. If so, get it from the stack.
5656 // Note: There is a chance that the register was saved but not overwritten, so we could
5657 // save one load. However, since this is just an intrinsic slow path we prefer this
5658 // simple and more robust approach rather that trying to determine if that's the case.
5659 SlowPathCode* slow_path = GetCurrentSlowPath();
5660 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5661 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5662 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5663 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5664 return temp;
5665 }
5666 return location.AsRegister<Register>();
5667}
5668
Vladimir Markodc151b22015-10-15 18:02:30 +01005669HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5670 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005671 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005672 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005673 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005674 // is incompatible with it.
5675 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005676 bool is_r6 = GetInstructionSetFeatures().IsR6();
5677 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005678 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005679 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005680 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005681 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005682 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005683 break;
5684 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005685 if (fallback_load) {
5686 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5687 dispatch_info.method_load_data = 0;
5688 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005689 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005690}
5691
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5693 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005694 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005695 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5696 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005697 bool is_r6 = GetInstructionSetFeatures().IsR6();
5698 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005699 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5700 : ZERO;
5701
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005702 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005703 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005704 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005705 uint32_t offset =
5706 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005707 __ LoadFromOffset(kLoadWord,
5708 temp.AsRegister<Register>(),
5709 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005710 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005711 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005712 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005713 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005714 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005715 break;
5716 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5717 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5718 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005719 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5720 if (is_r6) {
5721 uint32_t offset = invoke->GetDexCacheArrayOffset();
5722 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5723 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
5724 bool reordering = __ SetReorder(false);
5725 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
5726 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
5727 __ SetReorder(reordering);
5728 } else {
5729 HMipsDexCacheArraysBase* base =
5730 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5731 int32_t offset =
5732 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5733 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5734 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005735 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005737 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005738 Register reg = temp.AsRegister<Register>();
5739 Register method_reg;
5740 if (current_method.IsRegister()) {
5741 method_reg = current_method.AsRegister<Register>();
5742 } else {
5743 // TODO: use the appropriate DCHECK() here if possible.
5744 // DCHECK(invoke->GetLocations()->Intrinsified());
5745 DCHECK(!current_method.IsValid());
5746 method_reg = reg;
5747 __ Lw(reg, SP, kCurrentMethodStackOffset);
5748 }
5749
5750 // temp = temp->dex_cache_resolved_methods_;
5751 __ LoadFromOffset(kLoadWord,
5752 reg,
5753 method_reg,
5754 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005755 // temp = temp[index_in_cache];
5756 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5757 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005758 __ LoadFromOffset(kLoadWord,
5759 reg,
5760 reg,
5761 CodeGenerator::GetCachePointerOffset(index_in_cache));
5762 break;
5763 }
5764 }
5765
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005766 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005767 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005768 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005769 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005770 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5771 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005772 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005773 T9,
5774 callee_method.AsRegister<Register>(),
5775 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005776 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005777 // T9()
5778 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005779 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005780 break;
5781 }
5782 DCHECK(!IsLeafMethod());
5783}
5784
5785void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005786 // Explicit clinit checks triggered by static invokes must have been pruned by
5787 // art::PrepareForRegisterAllocation.
5788 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005789
5790 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5791 return;
5792 }
5793
5794 LocationSummary* locations = invoke->GetLocations();
5795 codegen_->GenerateStaticOrDirectCall(invoke,
5796 locations->HasTemps()
5797 ? locations->GetTemp(0)
5798 : Location::NoLocation());
5799 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5800}
5801
Chris Larsen3acee732015-11-18 13:31:08 -08005802void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005803 // Use the calling convention instead of the location of the receiver, as
5804 // intrinsics may have put the receiver in a different register. In the intrinsics
5805 // slow path, the arguments have been moved to the right place, so here we are
5806 // guaranteed that the receiver is the first register of the calling convention.
5807 InvokeDexCallingConvention calling_convention;
5808 Register receiver = calling_convention.GetRegisterAt(0);
5809
Chris Larsen3acee732015-11-18 13:31:08 -08005810 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005811 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5812 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5813 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005814 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005815
5816 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005817 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005818 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005819 // Instead of simply (possibly) unpoisoning `temp` here, we should
5820 // emit a read barrier for the previous class reference load.
5821 // However this is not required in practice, as this is an
5822 // intermediate/temporary reference and because the current
5823 // concurrent copying collector keeps the from-space memory
5824 // intact/accessible until the end of the marking phase (the
5825 // concurrent copying collector may not in the future).
5826 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005827 // temp = temp->GetMethodAt(method_offset);
5828 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5829 // T9 = temp->GetEntryPoint();
5830 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5831 // T9();
5832 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005833 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005834}
5835
5836void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5837 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5838 return;
5839 }
5840
5841 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005842 DCHECK(!codegen_->IsLeafMethod());
5843 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5844}
5845
5846void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005847 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5848 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005849 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005850 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005851 cls,
5852 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005853 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005854 return;
5855 }
Vladimir Marko41559982017-01-06 14:04:23 +00005856 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005857
5858 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5859 ? LocationSummary::kCallOnSlowPath
5860 : LocationSummary::kNoCall;
5861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005862 switch (load_kind) {
5863 // We need an extra register for PC-relative literals on R2.
5864 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005865 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005866 case HLoadClass::LoadKind::kBootImageAddress:
5867 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005868 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5869 break;
5870 }
5871 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005872 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005873 locations->SetInAt(0, Location::RequiresRegister());
5874 break;
5875 default:
5876 break;
5877 }
5878 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005879}
5880
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005881// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5882// move.
5883void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005884 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5885 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5886 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005887 return;
5888 }
Vladimir Marko41559982017-01-06 14:04:23 +00005889 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005890
Vladimir Marko41559982017-01-06 14:04:23 +00005891 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005892 Location out_loc = locations->Out();
5893 Register out = out_loc.AsRegister<Register>();
5894 Register base_or_current_method_reg;
5895 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5896 switch (load_kind) {
5897 // We need an extra register for PC-relative literals on R2.
5898 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005899 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005900 case HLoadClass::LoadKind::kBootImageAddress:
5901 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005902 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5903 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005904 case HLoadClass::LoadKind::kReferrersClass:
5905 case HLoadClass::LoadKind::kDexCacheViaMethod:
5906 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5907 break;
5908 default:
5909 base_or_current_method_reg = ZERO;
5910 break;
5911 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005912
Alexey Frunze06a46c42016-07-19 15:00:40 -07005913 bool generate_null_check = false;
5914 switch (load_kind) {
5915 case HLoadClass::LoadKind::kReferrersClass: {
5916 DCHECK(!cls->CanCallRuntime());
5917 DCHECK(!cls->MustGenerateClinitCheck());
5918 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5919 GenerateGcRootFieldLoad(cls,
5920 out_loc,
5921 base_or_current_method_reg,
5922 ArtMethod::DeclaringClassOffset().Int32Value());
5923 break;
5924 }
5925 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005926 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005927 __ LoadLiteral(out,
5928 base_or_current_method_reg,
5929 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5930 cls->GetTypeIndex()));
5931 break;
5932 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005933 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005934 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5935 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005936 bool reordering = __ SetReorder(false);
5937 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5938 __ Addiu(out, out, /* placeholder */ 0x5678);
5939 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005940 break;
5941 }
5942 case HLoadClass::LoadKind::kBootImageAddress: {
5943 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005944 uint32_t address = dchecked_integral_cast<uint32_t>(
5945 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5946 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005947 __ LoadLiteral(out,
5948 base_or_current_method_reg,
5949 codegen_->DeduplicateBootImageAddressLiteral(address));
5950 break;
5951 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005952 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005953 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005954 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005955 bool reordering = __ SetReorder(false);
5956 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08005957 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005958 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005959 generate_null_check = true;
5960 break;
5961 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005962 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08005963 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
5964 cls->GetTypeIndex(),
5965 cls->GetClass());
5966 bool reordering = __ SetReorder(false);
5967 __ Bind(&info->high_label);
5968 __ Lui(out, /* placeholder */ 0x1234);
5969 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
5970 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005971 break;
5972 }
Vladimir Marko41559982017-01-06 14:04:23 +00005973 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005974 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005975 LOG(FATAL) << "UNREACHABLE";
5976 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005977 }
5978
5979 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5980 DCHECK(cls->CanCallRuntime());
5981 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5982 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5983 codegen_->AddSlowPath(slow_path);
5984 if (generate_null_check) {
5985 __ Beqz(out, slow_path->GetEntryLabel());
5986 }
5987 if (cls->MustGenerateClinitCheck()) {
5988 GenerateClassInitializationCheck(slow_path, out);
5989 } else {
5990 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005991 }
5992 }
5993}
5994
5995static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005996 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005997}
5998
5999void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
6000 LocationSummary* locations =
6001 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6002 locations->SetOut(Location::RequiresRegister());
6003}
6004
6005void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
6006 Register out = load->GetLocations()->Out().AsRegister<Register>();
6007 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6008}
6009
6010void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
6011 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6012}
6013
6014void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6015 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
6016}
6017
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006018void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006019 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006021 HLoadString::LoadKind load_kind = load->GetLoadKind();
6022 switch (load_kind) {
6023 // We need an extra register for PC-relative literals on R2.
6024 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6025 case HLoadString::LoadKind::kBootImageAddress:
6026 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006027 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006028 if (codegen_->GetInstructionSetFeatures().IsR6()) {
6029 break;
6030 }
6031 FALLTHROUGH_INTENDED;
6032 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006033 case HLoadString::LoadKind::kDexCacheViaMethod:
6034 locations->SetInAt(0, Location::RequiresRegister());
6035 break;
6036 default:
6037 break;
6038 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07006039 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6040 InvokeRuntimeCallingConvention calling_convention;
6041 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
6042 } else {
6043 locations->SetOut(Location::RequiresRegister());
6044 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006045}
6046
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006047// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6048// move.
6049void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006050 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006051 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006052 Location out_loc = locations->Out();
6053 Register out = out_loc.AsRegister<Register>();
6054 Register base_or_current_method_reg;
6055 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6056 switch (load_kind) {
6057 // We need an extra register for PC-relative literals on R2.
6058 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6059 case HLoadString::LoadKind::kBootImageAddress:
6060 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006061 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006062 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
6063 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006064 default:
6065 base_or_current_method_reg = ZERO;
6066 break;
6067 }
6068
6069 switch (load_kind) {
6070 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006071 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006072 __ LoadLiteral(out,
6073 base_or_current_method_reg,
6074 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6075 load->GetStringIndex()));
6076 return; // No dex cache slow path.
6077 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006078 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006079 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006080 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006081 bool reordering = __ SetReorder(false);
6082 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
6083 __ Addiu(out, out, /* placeholder */ 0x5678);
6084 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006085 return; // No dex cache slow path.
6086 }
6087 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006088 uint32_t address = dchecked_integral_cast<uint32_t>(
6089 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6090 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006091 __ LoadLiteral(out,
6092 base_or_current_method_reg,
6093 codegen_->DeduplicateBootImageAddressLiteral(address));
6094 return; // No dex cache slow path.
6095 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006096 case HLoadString::LoadKind::kBssEntry: {
6097 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6098 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006099 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006100 bool reordering = __ SetReorder(false);
6101 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08006102 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006103 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006104 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
6105 codegen_->AddSlowPath(slow_path);
6106 __ Beqz(out, slow_path->GetEntryLabel());
6107 __ Bind(slow_path->GetExitLabel());
6108 return;
6109 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006110 case HLoadString::LoadKind::kJitTableAddress: {
6111 CodeGeneratorMIPS::JitPatchInfo* info =
6112 codegen_->NewJitRootStringPatch(load->GetDexFile(),
6113 load->GetStringIndex(),
6114 load->GetString());
6115 bool reordering = __ SetReorder(false);
6116 __ Bind(&info->high_label);
6117 __ Lui(out, /* placeholder */ 0x1234);
6118 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
6119 __ SetReorder(reordering);
6120 return;
6121 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006122 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006123 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006124 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006125
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006126 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006127 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6128 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006129 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006130 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6131 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006132}
6133
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006134void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
6135 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6136 locations->SetOut(Location::ConstantLocation(constant));
6137}
6138
6139void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6140 // Will be generated at use site.
6141}
6142
6143void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
6144 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006145 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006146 InvokeRuntimeCallingConvention calling_convention;
6147 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6148}
6149
6150void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
6151 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006152 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6154 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006155 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006156 }
6157 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6158}
6159
6160void LocationsBuilderMIPS::VisitMul(HMul* mul) {
6161 LocationSummary* locations =
6162 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
6163 switch (mul->GetResultType()) {
6164 case Primitive::kPrimInt:
6165 case Primitive::kPrimLong:
6166 locations->SetInAt(0, Location::RequiresRegister());
6167 locations->SetInAt(1, Location::RequiresRegister());
6168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6169 break;
6170
6171 case Primitive::kPrimFloat:
6172 case Primitive::kPrimDouble:
6173 locations->SetInAt(0, Location::RequiresFpuRegister());
6174 locations->SetInAt(1, Location::RequiresFpuRegister());
6175 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6176 break;
6177
6178 default:
6179 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6180 }
6181}
6182
6183void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
6184 Primitive::Type type = instruction->GetType();
6185 LocationSummary* locations = instruction->GetLocations();
6186 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6187
6188 switch (type) {
6189 case Primitive::kPrimInt: {
6190 Register dst = locations->Out().AsRegister<Register>();
6191 Register lhs = locations->InAt(0).AsRegister<Register>();
6192 Register rhs = locations->InAt(1).AsRegister<Register>();
6193
6194 if (isR6) {
6195 __ MulR6(dst, lhs, rhs);
6196 } else {
6197 __ MulR2(dst, lhs, rhs);
6198 }
6199 break;
6200 }
6201 case Primitive::kPrimLong: {
6202 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6203 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6204 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6205 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
6206 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
6207 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
6208
6209 // Extra checks to protect caused by the existance of A1_A2.
6210 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
6211 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
6212 DCHECK_NE(dst_high, lhs_low);
6213 DCHECK_NE(dst_high, rhs_low);
6214
6215 // A_B * C_D
6216 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
6217 // dst_lo: [ low(B*D) ]
6218 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
6219
6220 if (isR6) {
6221 __ MulR6(TMP, lhs_high, rhs_low);
6222 __ MulR6(dst_high, lhs_low, rhs_high);
6223 __ Addu(dst_high, dst_high, TMP);
6224 __ MuhuR6(TMP, lhs_low, rhs_low);
6225 __ Addu(dst_high, dst_high, TMP);
6226 __ MulR6(dst_low, lhs_low, rhs_low);
6227 } else {
6228 __ MulR2(TMP, lhs_high, rhs_low);
6229 __ MulR2(dst_high, lhs_low, rhs_high);
6230 __ Addu(dst_high, dst_high, TMP);
6231 __ MultuR2(lhs_low, rhs_low);
6232 __ Mfhi(TMP);
6233 __ Addu(dst_high, dst_high, TMP);
6234 __ Mflo(dst_low);
6235 }
6236 break;
6237 }
6238 case Primitive::kPrimFloat:
6239 case Primitive::kPrimDouble: {
6240 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6241 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
6242 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
6243 if (type == Primitive::kPrimFloat) {
6244 __ MulS(dst, lhs, rhs);
6245 } else {
6246 __ MulD(dst, lhs, rhs);
6247 }
6248 break;
6249 }
6250 default:
6251 LOG(FATAL) << "Unexpected mul type " << type;
6252 }
6253}
6254
6255void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
6256 LocationSummary* locations =
6257 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6258 switch (neg->GetResultType()) {
6259 case Primitive::kPrimInt:
6260 case Primitive::kPrimLong:
6261 locations->SetInAt(0, Location::RequiresRegister());
6262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6263 break;
6264
6265 case Primitive::kPrimFloat:
6266 case Primitive::kPrimDouble:
6267 locations->SetInAt(0, Location::RequiresFpuRegister());
6268 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6269 break;
6270
6271 default:
6272 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6273 }
6274}
6275
6276void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
6277 Primitive::Type type = instruction->GetType();
6278 LocationSummary* locations = instruction->GetLocations();
6279
6280 switch (type) {
6281 case Primitive::kPrimInt: {
6282 Register dst = locations->Out().AsRegister<Register>();
6283 Register src = locations->InAt(0).AsRegister<Register>();
6284 __ Subu(dst, ZERO, src);
6285 break;
6286 }
6287 case Primitive::kPrimLong: {
6288 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6289 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6290 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6291 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6292 __ Subu(dst_low, ZERO, src_low);
6293 __ Sltu(TMP, ZERO, dst_low);
6294 __ Subu(dst_high, ZERO, src_high);
6295 __ Subu(dst_high, dst_high, TMP);
6296 break;
6297 }
6298 case Primitive::kPrimFloat:
6299 case Primitive::kPrimDouble: {
6300 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6301 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6302 if (type == Primitive::kPrimFloat) {
6303 __ NegS(dst, src);
6304 } else {
6305 __ NegD(dst, src);
6306 }
6307 break;
6308 }
6309 default:
6310 LOG(FATAL) << "Unexpected neg type " << type;
6311 }
6312}
6313
6314void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
6315 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006316 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006317 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006318 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006319 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6320 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006321}
6322
6323void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006324 // Note: if heap poisoning is enabled, the entry point takes care
6325 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006326 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
6327 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006328}
6329
6330void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
6331 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006333 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006334 if (instruction->IsStringAlloc()) {
6335 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6336 } else {
6337 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006338 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006339 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6340}
6341
6342void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006343 // Note: if heap poisoning is enabled, the entry point takes care
6344 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006345 if (instruction->IsStringAlloc()) {
6346 // String is allocated through StringFactory. Call NewEmptyString entry point.
6347 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006348 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006349 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6350 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6351 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006352 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006353 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6354 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006355 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006356 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006357 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006358}
6359
6360void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6362 locations->SetInAt(0, Location::RequiresRegister());
6363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6364}
6365
6366void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6367 Primitive::Type type = instruction->GetType();
6368 LocationSummary* locations = instruction->GetLocations();
6369
6370 switch (type) {
6371 case Primitive::kPrimInt: {
6372 Register dst = locations->Out().AsRegister<Register>();
6373 Register src = locations->InAt(0).AsRegister<Register>();
6374 __ Nor(dst, src, ZERO);
6375 break;
6376 }
6377
6378 case Primitive::kPrimLong: {
6379 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6380 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6381 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6382 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6383 __ Nor(dst_high, src_high, ZERO);
6384 __ Nor(dst_low, src_low, ZERO);
6385 break;
6386 }
6387
6388 default:
6389 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6390 }
6391}
6392
6393void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6395 locations->SetInAt(0, Location::RequiresRegister());
6396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6397}
6398
6399void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6400 LocationSummary* locations = instruction->GetLocations();
6401 __ Xori(locations->Out().AsRegister<Register>(),
6402 locations->InAt(0).AsRegister<Register>(),
6403 1);
6404}
6405
6406void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006407 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6408 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006409}
6410
Calin Juravle2ae48182016-03-16 14:05:09 +00006411void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6412 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006413 return;
6414 }
6415 Location obj = instruction->GetLocations()->InAt(0);
6416
6417 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006418 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006419}
6420
Calin Juravle2ae48182016-03-16 14:05:09 +00006421void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006422 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006423 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006424
6425 Location obj = instruction->GetLocations()->InAt(0);
6426
6427 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6428}
6429
6430void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006431 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006432}
6433
6434void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6435 HandleBinaryOp(instruction);
6436}
6437
6438void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6439 HandleBinaryOp(instruction);
6440}
6441
6442void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6443 LOG(FATAL) << "Unreachable";
6444}
6445
6446void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6447 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6448}
6449
6450void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6452 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6453 if (location.IsStackSlot()) {
6454 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6455 } else if (location.IsDoubleStackSlot()) {
6456 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6457 }
6458 locations->SetOut(location);
6459}
6460
6461void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6462 ATTRIBUTE_UNUSED) {
6463 // Nothing to do, the parameter is already at its location.
6464}
6465
6466void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6467 LocationSummary* locations =
6468 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6469 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6470}
6471
6472void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6473 ATTRIBUTE_UNUSED) {
6474 // Nothing to do, the method is already at its location.
6475}
6476
6477void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006479 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 locations->SetInAt(i, Location::Any());
6481 }
6482 locations->SetOut(Location::Any());
6483}
6484
6485void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6486 LOG(FATAL) << "Unreachable";
6487}
6488
6489void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6490 Primitive::Type type = rem->GetResultType();
6491 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006492 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6494
6495 switch (type) {
6496 case Primitive::kPrimInt:
6497 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006498 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6500 break;
6501
6502 case Primitive::kPrimLong: {
6503 InvokeRuntimeCallingConvention calling_convention;
6504 locations->SetInAt(0, Location::RegisterPairLocation(
6505 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6506 locations->SetInAt(1, Location::RegisterPairLocation(
6507 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6508 locations->SetOut(calling_convention.GetReturnLocation(type));
6509 break;
6510 }
6511
6512 case Primitive::kPrimFloat:
6513 case Primitive::kPrimDouble: {
6514 InvokeRuntimeCallingConvention calling_convention;
6515 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6516 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6517 locations->SetOut(calling_convention.GetReturnLocation(type));
6518 break;
6519 }
6520
6521 default:
6522 LOG(FATAL) << "Unexpected rem type " << type;
6523 }
6524}
6525
6526void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6527 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006528
6529 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006530 case Primitive::kPrimInt:
6531 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006532 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006533 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006534 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006535 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6536 break;
6537 }
6538 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006539 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006540 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006541 break;
6542 }
6543 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006544 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006545 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546 break;
6547 }
6548 default:
6549 LOG(FATAL) << "Unexpected rem type " << type;
6550 }
6551}
6552
6553void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6554 memory_barrier->SetLocations(nullptr);
6555}
6556
6557void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6558 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6559}
6560
6561void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6563 Primitive::Type return_type = ret->InputAt(0)->GetType();
6564 locations->SetInAt(0, MipsReturnLocation(return_type));
6565}
6566
6567void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6568 codegen_->GenerateFrameExit();
6569}
6570
6571void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6572 ret->SetLocations(nullptr);
6573}
6574
6575void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6576 codegen_->GenerateFrameExit();
6577}
6578
Alexey Frunze92d90602015-12-18 18:16:36 -08006579void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6580 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006581}
6582
Alexey Frunze92d90602015-12-18 18:16:36 -08006583void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6584 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006585}
6586
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006587void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6588 HandleShift(shl);
6589}
6590
6591void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6592 HandleShift(shl);
6593}
6594
6595void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6596 HandleShift(shr);
6597}
6598
6599void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6600 HandleShift(shr);
6601}
6602
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006603void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6604 HandleBinaryOp(instruction);
6605}
6606
6607void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6608 HandleBinaryOp(instruction);
6609}
6610
6611void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6612 HandleFieldGet(instruction, instruction->GetFieldInfo());
6613}
6614
6615void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6616 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6617}
6618
6619void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6620 HandleFieldSet(instruction, instruction->GetFieldInfo());
6621}
6622
6623void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006624 HandleFieldSet(instruction,
6625 instruction->GetFieldInfo(),
6626 instruction->GetDexPc(),
6627 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006628}
6629
6630void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6631 HUnresolvedInstanceFieldGet* instruction) {
6632 FieldAccessCallingConventionMIPS calling_convention;
6633 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6634 instruction->GetFieldType(),
6635 calling_convention);
6636}
6637
6638void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6639 HUnresolvedInstanceFieldGet* instruction) {
6640 FieldAccessCallingConventionMIPS calling_convention;
6641 codegen_->GenerateUnresolvedFieldAccess(instruction,
6642 instruction->GetFieldType(),
6643 instruction->GetFieldIndex(),
6644 instruction->GetDexPc(),
6645 calling_convention);
6646}
6647
6648void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6649 HUnresolvedInstanceFieldSet* instruction) {
6650 FieldAccessCallingConventionMIPS calling_convention;
6651 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6652 instruction->GetFieldType(),
6653 calling_convention);
6654}
6655
6656void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6657 HUnresolvedInstanceFieldSet* instruction) {
6658 FieldAccessCallingConventionMIPS calling_convention;
6659 codegen_->GenerateUnresolvedFieldAccess(instruction,
6660 instruction->GetFieldType(),
6661 instruction->GetFieldIndex(),
6662 instruction->GetDexPc(),
6663 calling_convention);
6664}
6665
6666void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6667 HUnresolvedStaticFieldGet* instruction) {
6668 FieldAccessCallingConventionMIPS calling_convention;
6669 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6670 instruction->GetFieldType(),
6671 calling_convention);
6672}
6673
6674void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6675 HUnresolvedStaticFieldGet* instruction) {
6676 FieldAccessCallingConventionMIPS calling_convention;
6677 codegen_->GenerateUnresolvedFieldAccess(instruction,
6678 instruction->GetFieldType(),
6679 instruction->GetFieldIndex(),
6680 instruction->GetDexPc(),
6681 calling_convention);
6682}
6683
6684void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6685 HUnresolvedStaticFieldSet* instruction) {
6686 FieldAccessCallingConventionMIPS calling_convention;
6687 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6688 instruction->GetFieldType(),
6689 calling_convention);
6690}
6691
6692void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6693 HUnresolvedStaticFieldSet* instruction) {
6694 FieldAccessCallingConventionMIPS calling_convention;
6695 codegen_->GenerateUnresolvedFieldAccess(instruction,
6696 instruction->GetFieldType(),
6697 instruction->GetFieldIndex(),
6698 instruction->GetDexPc(),
6699 calling_convention);
6700}
6701
6702void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006703 LocationSummary* locations =
6704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006705 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006706}
6707
6708void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6709 HBasicBlock* block = instruction->GetBlock();
6710 if (block->GetLoopInformation() != nullptr) {
6711 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6712 // The back edge will generate the suspend check.
6713 return;
6714 }
6715 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6716 // The goto will generate the suspend check.
6717 return;
6718 }
6719 GenerateSuspendCheck(instruction, nullptr);
6720}
6721
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6723 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006724 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006725 InvokeRuntimeCallingConvention calling_convention;
6726 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6727}
6728
6729void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006730 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6732}
6733
6734void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6735 Primitive::Type input_type = conversion->GetInputType();
6736 Primitive::Type result_type = conversion->GetResultType();
6737 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006738 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739
6740 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6741 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6742 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6743 }
6744
6745 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006746 if (!isR6 &&
6747 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6748 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006749 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006750 }
6751
6752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6753
6754 if (call_kind == LocationSummary::kNoCall) {
6755 if (Primitive::IsFloatingPointType(input_type)) {
6756 locations->SetInAt(0, Location::RequiresFpuRegister());
6757 } else {
6758 locations->SetInAt(0, Location::RequiresRegister());
6759 }
6760
6761 if (Primitive::IsFloatingPointType(result_type)) {
6762 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6763 } else {
6764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6765 }
6766 } else {
6767 InvokeRuntimeCallingConvention calling_convention;
6768
6769 if (Primitive::IsFloatingPointType(input_type)) {
6770 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6771 } else {
6772 DCHECK_EQ(input_type, Primitive::kPrimLong);
6773 locations->SetInAt(0, Location::RegisterPairLocation(
6774 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6775 }
6776
6777 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6778 }
6779}
6780
6781void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6782 LocationSummary* locations = conversion->GetLocations();
6783 Primitive::Type result_type = conversion->GetResultType();
6784 Primitive::Type input_type = conversion->GetInputType();
6785 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006786 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006787
6788 DCHECK_NE(input_type, result_type);
6789
6790 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6791 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6792 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6793 Register src = locations->InAt(0).AsRegister<Register>();
6794
Alexey Frunzea871ef12016-06-27 15:20:11 -07006795 if (dst_low != src) {
6796 __ Move(dst_low, src);
6797 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006798 __ Sra(dst_high, src, 31);
6799 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6800 Register dst = locations->Out().AsRegister<Register>();
6801 Register src = (input_type == Primitive::kPrimLong)
6802 ? locations->InAt(0).AsRegisterPairLow<Register>()
6803 : locations->InAt(0).AsRegister<Register>();
6804
6805 switch (result_type) {
6806 case Primitive::kPrimChar:
6807 __ Andi(dst, src, 0xFFFF);
6808 break;
6809 case Primitive::kPrimByte:
6810 if (has_sign_extension) {
6811 __ Seb(dst, src);
6812 } else {
6813 __ Sll(dst, src, 24);
6814 __ Sra(dst, dst, 24);
6815 }
6816 break;
6817 case Primitive::kPrimShort:
6818 if (has_sign_extension) {
6819 __ Seh(dst, src);
6820 } else {
6821 __ Sll(dst, src, 16);
6822 __ Sra(dst, dst, 16);
6823 }
6824 break;
6825 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006826 if (dst != src) {
6827 __ Move(dst, src);
6828 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006829 break;
6830
6831 default:
6832 LOG(FATAL) << "Unexpected type conversion from " << input_type
6833 << " to " << result_type;
6834 }
6835 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006836 if (input_type == Primitive::kPrimLong) {
6837 if (isR6) {
6838 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6839 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6840 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6841 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6842 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6843 __ Mtc1(src_low, FTMP);
6844 __ Mthc1(src_high, FTMP);
6845 if (result_type == Primitive::kPrimFloat) {
6846 __ Cvtsl(dst, FTMP);
6847 } else {
6848 __ Cvtdl(dst, FTMP);
6849 }
6850 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006851 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6852 : kQuickL2d;
6853 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006854 if (result_type == Primitive::kPrimFloat) {
6855 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6856 } else {
6857 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6858 }
6859 }
6860 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006861 Register src = locations->InAt(0).AsRegister<Register>();
6862 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6863 __ Mtc1(src, FTMP);
6864 if (result_type == Primitive::kPrimFloat) {
6865 __ Cvtsw(dst, FTMP);
6866 } else {
6867 __ Cvtdw(dst, FTMP);
6868 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006869 }
6870 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6871 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006872 if (result_type == Primitive::kPrimLong) {
6873 if (isR6) {
6874 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6875 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6876 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6877 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6878 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6879 MipsLabel truncate;
6880 MipsLabel done;
6881
6882 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6883 // value when the input is either a NaN or is outside of the range of the output type
6884 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6885 // the same result.
6886 //
6887 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6888 // value of the output type if the input is outside of the range after the truncation or
6889 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6890 // results. This matches the desired float/double-to-int/long conversion exactly.
6891 //
6892 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6893 //
6894 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6895 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6896 // even though it must be NAN2008=1 on R6.
6897 //
6898 // The code takes care of the different behaviors by first comparing the input to the
6899 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6900 // If the input is greater than or equal to the minimum, it procedes to the truncate
6901 // instruction, which will handle such an input the same way irrespective of NAN2008.
6902 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6903 // in order to return either zero or the minimum value.
6904 //
6905 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6906 // truncate instruction for MIPS64R6.
6907 if (input_type == Primitive::kPrimFloat) {
6908 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6909 __ LoadConst32(TMP, min_val);
6910 __ Mtc1(TMP, FTMP);
6911 __ CmpLeS(FTMP, FTMP, src);
6912 } else {
6913 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6914 __ LoadConst32(TMP, High32Bits(min_val));
6915 __ Mtc1(ZERO, FTMP);
6916 __ Mthc1(TMP, FTMP);
6917 __ CmpLeD(FTMP, FTMP, src);
6918 }
6919
6920 __ Bc1nez(FTMP, &truncate);
6921
6922 if (input_type == Primitive::kPrimFloat) {
6923 __ CmpEqS(FTMP, src, src);
6924 } else {
6925 __ CmpEqD(FTMP, src, src);
6926 }
6927 __ Move(dst_low, ZERO);
6928 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6929 __ Mfc1(TMP, FTMP);
6930 __ And(dst_high, dst_high, TMP);
6931
6932 __ B(&done);
6933
6934 __ Bind(&truncate);
6935
6936 if (input_type == Primitive::kPrimFloat) {
6937 __ TruncLS(FTMP, src);
6938 } else {
6939 __ TruncLD(FTMP, src);
6940 }
6941 __ Mfc1(dst_low, FTMP);
6942 __ Mfhc1(dst_high, FTMP);
6943
6944 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006945 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006946 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6947 : kQuickD2l;
6948 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006949 if (input_type == Primitive::kPrimFloat) {
6950 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6951 } else {
6952 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6953 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006954 }
6955 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006956 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6957 Register dst = locations->Out().AsRegister<Register>();
6958 MipsLabel truncate;
6959 MipsLabel done;
6960
6961 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6962 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6963 // even though it must be NAN2008=1 on R6.
6964 //
6965 // For details see the large comment above for the truncation of float/double to long on R6.
6966 //
6967 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6968 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006969 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006970 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6971 __ LoadConst32(TMP, min_val);
6972 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006973 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006974 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6975 __ LoadConst32(TMP, High32Bits(min_val));
6976 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006977 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006978 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006979
6980 if (isR6) {
6981 if (input_type == Primitive::kPrimFloat) {
6982 __ CmpLeS(FTMP, FTMP, src);
6983 } else {
6984 __ CmpLeD(FTMP, FTMP, src);
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 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6994 __ Mfc1(TMP, FTMP);
6995 __ And(dst, dst, TMP);
6996 } else {
6997 if (input_type == Primitive::kPrimFloat) {
6998 __ ColeS(0, FTMP, src);
6999 } else {
7000 __ ColeD(0, FTMP, src);
7001 }
7002 __ Bc1t(0, &truncate);
7003
7004 if (input_type == Primitive::kPrimFloat) {
7005 __ CeqS(0, src, src);
7006 } else {
7007 __ CeqD(0, src, src);
7008 }
7009 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
7010 __ Movf(dst, ZERO, 0);
7011 }
7012
7013 __ B(&done);
7014
7015 __ Bind(&truncate);
7016
7017 if (input_type == Primitive::kPrimFloat) {
7018 __ TruncWS(FTMP, src);
7019 } else {
7020 __ TruncWD(FTMP, src);
7021 }
7022 __ Mfc1(dst, FTMP);
7023
7024 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007025 }
7026 } else if (Primitive::IsFloatingPointType(result_type) &&
7027 Primitive::IsFloatingPointType(input_type)) {
7028 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7029 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7030 if (result_type == Primitive::kPrimFloat) {
7031 __ Cvtsd(dst, src);
7032 } else {
7033 __ Cvtds(dst, src);
7034 }
7035 } else {
7036 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7037 << " to " << result_type;
7038 }
7039}
7040
7041void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
7042 HandleShift(ushr);
7043}
7044
7045void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
7046 HandleShift(ushr);
7047}
7048
7049void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
7050 HandleBinaryOp(instruction);
7051}
7052
7053void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
7054 HandleBinaryOp(instruction);
7055}
7056
7057void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7058 // Nothing to do, this should be removed during prepare for register allocator.
7059 LOG(FATAL) << "Unreachable";
7060}
7061
7062void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7063 // Nothing to do, this should be removed during prepare for register allocator.
7064 LOG(FATAL) << "Unreachable";
7065}
7066
7067void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007068 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007069}
7070
7071void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007072 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007073}
7074
7075void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007076 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007077}
7078
7079void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007080 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007081}
7082
7083void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007084 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007085}
7086
7087void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007088 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007089}
7090
7091void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007092 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007093}
7094
7095void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007096 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007097}
7098
7099void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007100 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007101}
7102
7103void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007104 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007105}
7106
7107void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007108 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007109}
7110
7111void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007112 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007113}
7114
7115void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007116 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007117}
7118
7119void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007120 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007121}
7122
7123void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007124 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007125}
7126
7127void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007128 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007129}
7130
7131void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007132 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007133}
7134
7135void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007136 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007137}
7138
7139void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007140 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007141}
7142
7143void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007144 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007145}
7146
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007147void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7148 LocationSummary* locations =
7149 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7150 locations->SetInAt(0, Location::RequiresRegister());
7151}
7152
Alexey Frunze96b66822016-09-10 02:32:44 -07007153void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
7154 int32_t lower_bound,
7155 uint32_t num_entries,
7156 HBasicBlock* switch_block,
7157 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007158 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007159 Register temp_reg = TMP;
7160 __ Addiu32(temp_reg, value_reg, -lower_bound);
7161 // Jump to default if index is negative
7162 // Note: We don't check the case that index is positive while value < lower_bound, because in
7163 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7164 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
7165
Alexey Frunze96b66822016-09-10 02:32:44 -07007166 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007167 // Jump to successors[0] if value == lower_bound.
7168 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
7169 int32_t last_index = 0;
7170 for (; num_entries - last_index > 2; last_index += 2) {
7171 __ Addiu(temp_reg, temp_reg, -2);
7172 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7173 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7174 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7175 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7176 }
7177 if (num_entries - last_index == 2) {
7178 // The last missing case_value.
7179 __ Addiu(temp_reg, temp_reg, -1);
7180 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007181 }
7182
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007183 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07007184 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007185 __ B(codegen_->GetLabelOf(default_block));
7186 }
7187}
7188
Alexey Frunze96b66822016-09-10 02:32:44 -07007189void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
7190 Register constant_area,
7191 int32_t lower_bound,
7192 uint32_t num_entries,
7193 HBasicBlock* switch_block,
7194 HBasicBlock* default_block) {
7195 // Create a jump table.
7196 std::vector<MipsLabel*> labels(num_entries);
7197 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7198 for (uint32_t i = 0; i < num_entries; i++) {
7199 labels[i] = codegen_->GetLabelOf(successors[i]);
7200 }
7201 JumpTable* table = __ CreateJumpTable(std::move(labels));
7202
7203 // Is the value in range?
7204 __ Addiu32(TMP, value_reg, -lower_bound);
7205 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
7206 __ Sltiu(AT, TMP, num_entries);
7207 __ Beqz(AT, codegen_->GetLabelOf(default_block));
7208 } else {
7209 __ LoadConst32(AT, num_entries);
7210 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
7211 }
7212
7213 // We are in the range of the table.
7214 // Load the target address from the jump table, indexing by the value.
7215 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
7216 __ Sll(TMP, TMP, 2);
7217 __ Addu(TMP, TMP, AT);
7218 __ Lw(TMP, TMP, 0);
7219 // Compute the absolute target address by adding the table start address
7220 // (the table contains offsets to targets relative to its start).
7221 __ Addu(TMP, TMP, AT);
7222 // And jump.
7223 __ Jr(TMP);
7224 __ NopIfNoReordering();
7225}
7226
7227void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7228 int32_t lower_bound = switch_instr->GetStartValue();
7229 uint32_t num_entries = switch_instr->GetNumEntries();
7230 LocationSummary* locations = switch_instr->GetLocations();
7231 Register value_reg = locations->InAt(0).AsRegister<Register>();
7232 HBasicBlock* switch_block = switch_instr->GetBlock();
7233 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7234
7235 if (codegen_->GetInstructionSetFeatures().IsR6() &&
7236 num_entries > kPackedSwitchJumpTableThreshold) {
7237 // R6 uses PC-relative addressing to access the jump table.
7238 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
7239 // the jump table and it is implemented by changing HPackedSwitch to
7240 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
7241 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
7242 GenTableBasedPackedSwitch(value_reg,
7243 ZERO,
7244 lower_bound,
7245 num_entries,
7246 switch_block,
7247 default_block);
7248 } else {
7249 GenPackedSwitchWithCompares(value_reg,
7250 lower_bound,
7251 num_entries,
7252 switch_block,
7253 default_block);
7254 }
7255}
7256
7257void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7258 LocationSummary* locations =
7259 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7260 locations->SetInAt(0, Location::RequiresRegister());
7261 // Constant area pointer (HMipsComputeBaseMethodAddress).
7262 locations->SetInAt(1, Location::RequiresRegister());
7263}
7264
7265void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7266 int32_t lower_bound = switch_instr->GetStartValue();
7267 uint32_t num_entries = switch_instr->GetNumEntries();
7268 LocationSummary* locations = switch_instr->GetLocations();
7269 Register value_reg = locations->InAt(0).AsRegister<Register>();
7270 Register constant_area = locations->InAt(1).AsRegister<Register>();
7271 HBasicBlock* switch_block = switch_instr->GetBlock();
7272 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7273
7274 // This is an R2-only path. HPackedSwitch has been changed to
7275 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
7276 // required to address the jump table relative to PC.
7277 GenTableBasedPackedSwitch(value_reg,
7278 constant_area,
7279 lower_bound,
7280 num_entries,
7281 switch_block,
7282 default_block);
7283}
7284
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007285void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
7286 HMipsComputeBaseMethodAddress* insn) {
7287 LocationSummary* locations =
7288 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7289 locations->SetOut(Location::RequiresRegister());
7290}
7291
7292void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
7293 HMipsComputeBaseMethodAddress* insn) {
7294 LocationSummary* locations = insn->GetLocations();
7295 Register reg = locations->Out().AsRegister<Register>();
7296
7297 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7298
7299 // Generate a dummy PC-relative call to obtain PC.
7300 __ Nal();
7301 // Grab the return address off RA.
7302 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007303 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007304
7305 // Remember this offset (the obtained PC value) for later use with constant area.
7306 __ BindPcRelBaseLabel();
7307}
7308
7309void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7311 locations->SetOut(Location::RequiresRegister());
7312}
7313
7314void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7315 Register reg = base->GetLocations()->Out().AsRegister<Register>();
7316 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7317 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007318 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7319 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007320 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007321 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
7322 __ Addiu(reg, reg, /* placeholder */ 0x5678);
7323 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007324}
7325
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007326void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7327 // The trampoline uses the same calling convention as dex calling conventions,
7328 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
7329 // the method_idx.
7330 HandleInvoke(invoke);
7331}
7332
7333void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7334 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
7335}
7336
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007337void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7338 LocationSummary* locations =
7339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7340 locations->SetInAt(0, Location::RequiresRegister());
7341 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007342}
7343
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007344void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7345 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007346 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007347 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007348 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007349 __ LoadFromOffset(kLoadWord,
7350 locations->Out().AsRegister<Register>(),
7351 locations->InAt(0).AsRegister<Register>(),
7352 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007353 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007354 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007355 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007356 __ LoadFromOffset(kLoadWord,
7357 locations->Out().AsRegister<Register>(),
7358 locations->InAt(0).AsRegister<Register>(),
7359 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007360 __ LoadFromOffset(kLoadWord,
7361 locations->Out().AsRegister<Register>(),
7362 locations->Out().AsRegister<Register>(),
7363 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007364 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007365}
7366
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007367#undef __
7368#undef QUICK_ENTRY_POINT
7369
7370} // namespace mips
7371} // namespace art