blob: 791e63265e5a13c857188679391ea1c469326bc2 [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
Alexey Frunze2923db72016-08-20 01:55:47 -07001902auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1903 auto null_checker = [this, instruction]() {
1904 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1905 };
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);
Alexey Frunze2923db72016-08-20 01:55:47 -07001914 auto null_checker = GetImplicitNullChecker(instruction);
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());
Alexey Frunze2923db72016-08-20 01:55:47 -07002151 auto null_checker = GetImplicitNullChecker(instruction);
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();
2571 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002572 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002573
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002574 switch (type) {
2575 default:
2576 // Integer case.
2577 GenerateIntCompare(instruction->GetCondition(), locations);
2578 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002579
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002580 case Primitive::kPrimLong:
2581 // TODO: don't use branches.
2582 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002583 break;
2584
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002585 case Primitive::kPrimFloat:
2586 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002587 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2588 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002590
2591 // Convert the branches into the result.
2592 MipsLabel done;
2593
2594 // False case: result = 0.
2595 __ LoadConst32(dst, 0);
2596 __ B(&done);
2597
2598 // True case: result = 1.
2599 __ Bind(&true_label);
2600 __ LoadConst32(dst, 1);
2601 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002602}
2603
Alexey Frunze7e99e052015-11-24 19:28:01 -08002604void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2605 DCHECK(instruction->IsDiv() || instruction->IsRem());
2606 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2607
2608 LocationSummary* locations = instruction->GetLocations();
2609 Location second = locations->InAt(1);
2610 DCHECK(second.IsConstant());
2611
2612 Register out = locations->Out().AsRegister<Register>();
2613 Register dividend = locations->InAt(0).AsRegister<Register>();
2614 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2615 DCHECK(imm == 1 || imm == -1);
2616
2617 if (instruction->IsRem()) {
2618 __ Move(out, ZERO);
2619 } else {
2620 if (imm == -1) {
2621 __ Subu(out, ZERO, dividend);
2622 } else if (out != dividend) {
2623 __ Move(out, dividend);
2624 }
2625 }
2626}
2627
2628void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2629 DCHECK(instruction->IsDiv() || instruction->IsRem());
2630 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2631
2632 LocationSummary* locations = instruction->GetLocations();
2633 Location second = locations->InAt(1);
2634 DCHECK(second.IsConstant());
2635
2636 Register out = locations->Out().AsRegister<Register>();
2637 Register dividend = locations->InAt(0).AsRegister<Register>();
2638 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002639 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002640 int ctz_imm = CTZ(abs_imm);
2641
2642 if (instruction->IsDiv()) {
2643 if (ctz_imm == 1) {
2644 // Fast path for division by +/-2, which is very common.
2645 __ Srl(TMP, dividend, 31);
2646 } else {
2647 __ Sra(TMP, dividend, 31);
2648 __ Srl(TMP, TMP, 32 - ctz_imm);
2649 }
2650 __ Addu(out, dividend, TMP);
2651 __ Sra(out, out, ctz_imm);
2652 if (imm < 0) {
2653 __ Subu(out, ZERO, out);
2654 }
2655 } else {
2656 if (ctz_imm == 1) {
2657 // Fast path for modulo +/-2, which is very common.
2658 __ Sra(TMP, dividend, 31);
2659 __ Subu(out, dividend, TMP);
2660 __ Andi(out, out, 1);
2661 __ Addu(out, out, TMP);
2662 } else {
2663 __ Sra(TMP, dividend, 31);
2664 __ Srl(TMP, TMP, 32 - ctz_imm);
2665 __ Addu(out, dividend, TMP);
2666 if (IsUint<16>(abs_imm - 1)) {
2667 __ Andi(out, out, abs_imm - 1);
2668 } else {
2669 __ Sll(out, out, 32 - ctz_imm);
2670 __ Srl(out, out, 32 - ctz_imm);
2671 }
2672 __ Subu(out, out, TMP);
2673 }
2674 }
2675}
2676
2677void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2678 DCHECK(instruction->IsDiv() || instruction->IsRem());
2679 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2680
2681 LocationSummary* locations = instruction->GetLocations();
2682 Location second = locations->InAt(1);
2683 DCHECK(second.IsConstant());
2684
2685 Register out = locations->Out().AsRegister<Register>();
2686 Register dividend = locations->InAt(0).AsRegister<Register>();
2687 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2688
2689 int64_t magic;
2690 int shift;
2691 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2692
2693 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2694
2695 __ LoadConst32(TMP, magic);
2696 if (isR6) {
2697 __ MuhR6(TMP, dividend, TMP);
2698 } else {
2699 __ MultR2(dividend, TMP);
2700 __ Mfhi(TMP);
2701 }
2702 if (imm > 0 && magic < 0) {
2703 __ Addu(TMP, TMP, dividend);
2704 } else if (imm < 0 && magic > 0) {
2705 __ Subu(TMP, TMP, dividend);
2706 }
2707
2708 if (shift != 0) {
2709 __ Sra(TMP, TMP, shift);
2710 }
2711
2712 if (instruction->IsDiv()) {
2713 __ Sra(out, TMP, 31);
2714 __ Subu(out, TMP, out);
2715 } else {
2716 __ Sra(AT, TMP, 31);
2717 __ Subu(AT, TMP, AT);
2718 __ LoadConst32(TMP, imm);
2719 if (isR6) {
2720 __ MulR6(TMP, AT, TMP);
2721 } else {
2722 __ MulR2(TMP, AT, TMP);
2723 }
2724 __ Subu(out, dividend, TMP);
2725 }
2726}
2727
2728void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2729 DCHECK(instruction->IsDiv() || instruction->IsRem());
2730 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2731
2732 LocationSummary* locations = instruction->GetLocations();
2733 Register out = locations->Out().AsRegister<Register>();
2734 Location second = locations->InAt(1);
2735
2736 if (second.IsConstant()) {
2737 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2738 if (imm == 0) {
2739 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2740 } else if (imm == 1 || imm == -1) {
2741 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002742 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002743 DivRemByPowerOfTwo(instruction);
2744 } else {
2745 DCHECK(imm <= -2 || imm >= 2);
2746 GenerateDivRemWithAnyConstant(instruction);
2747 }
2748 } else {
2749 Register dividend = locations->InAt(0).AsRegister<Register>();
2750 Register divisor = second.AsRegister<Register>();
2751 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2752 if (instruction->IsDiv()) {
2753 if (isR6) {
2754 __ DivR6(out, dividend, divisor);
2755 } else {
2756 __ DivR2(out, dividend, divisor);
2757 }
2758 } else {
2759 if (isR6) {
2760 __ ModR6(out, dividend, divisor);
2761 } else {
2762 __ ModR2(out, dividend, divisor);
2763 }
2764 }
2765 }
2766}
2767
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002768void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2769 Primitive::Type type = div->GetResultType();
2770 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002771 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002772 : LocationSummary::kNoCall;
2773
2774 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2775
2776 switch (type) {
2777 case Primitive::kPrimInt:
2778 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002779 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2781 break;
2782
2783 case Primitive::kPrimLong: {
2784 InvokeRuntimeCallingConvention calling_convention;
2785 locations->SetInAt(0, Location::RegisterPairLocation(
2786 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2787 locations->SetInAt(1, Location::RegisterPairLocation(
2788 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2789 locations->SetOut(calling_convention.GetReturnLocation(type));
2790 break;
2791 }
2792
2793 case Primitive::kPrimFloat:
2794 case Primitive::kPrimDouble:
2795 locations->SetInAt(0, Location::RequiresFpuRegister());
2796 locations->SetInAt(1, Location::RequiresFpuRegister());
2797 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2798 break;
2799
2800 default:
2801 LOG(FATAL) << "Unexpected div type " << type;
2802 }
2803}
2804
2805void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2806 Primitive::Type type = instruction->GetType();
2807 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002808
2809 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002810 case Primitive::kPrimInt:
2811 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002814 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2816 break;
2817 }
2818 case Primitive::kPrimFloat:
2819 case Primitive::kPrimDouble: {
2820 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2821 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2822 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2823 if (type == Primitive::kPrimFloat) {
2824 __ DivS(dst, lhs, rhs);
2825 } else {
2826 __ DivD(dst, lhs, rhs);
2827 }
2828 break;
2829 }
2830 default:
2831 LOG(FATAL) << "Unexpected div type " << type;
2832 }
2833}
2834
2835void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002836 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002838}
2839
2840void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2841 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2842 codegen_->AddSlowPath(slow_path);
2843 Location value = instruction->GetLocations()->InAt(0);
2844 Primitive::Type type = instruction->GetType();
2845
2846 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002847 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002848 case Primitive::kPrimByte:
2849 case Primitive::kPrimChar:
2850 case Primitive::kPrimShort:
2851 case Primitive::kPrimInt: {
2852 if (value.IsConstant()) {
2853 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2854 __ B(slow_path->GetEntryLabel());
2855 } else {
2856 // A division by a non-null constant is valid. We don't need to perform
2857 // any check, so simply fall through.
2858 }
2859 } else {
2860 DCHECK(value.IsRegister()) << value;
2861 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2862 }
2863 break;
2864 }
2865 case Primitive::kPrimLong: {
2866 if (value.IsConstant()) {
2867 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2868 __ B(slow_path->GetEntryLabel());
2869 } else {
2870 // A division by a non-null constant is valid. We don't need to perform
2871 // any check, so simply fall through.
2872 }
2873 } else {
2874 DCHECK(value.IsRegisterPair()) << value;
2875 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2876 __ Beqz(TMP, slow_path->GetEntryLabel());
2877 }
2878 break;
2879 }
2880 default:
2881 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2882 }
2883}
2884
2885void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2886 LocationSummary* locations =
2887 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2888 locations->SetOut(Location::ConstantLocation(constant));
2889}
2890
2891void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2892 // Will be generated at use site.
2893}
2894
2895void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2896 exit->SetLocations(nullptr);
2897}
2898
2899void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2900}
2901
2902void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2903 LocationSummary* locations =
2904 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2905 locations->SetOut(Location::ConstantLocation(constant));
2906}
2907
2908void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2909 // Will be generated at use site.
2910}
2911
2912void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2913 got->SetLocations(nullptr);
2914}
2915
2916void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2917 DCHECK(!successor->IsExitBlock());
2918 HBasicBlock* block = got->GetBlock();
2919 HInstruction* previous = got->GetPrevious();
2920 HLoopInformation* info = block->GetLoopInformation();
2921
2922 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2923 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2924 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2925 return;
2926 }
2927 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2928 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2929 }
2930 if (!codegen_->GoesToNextBlock(block, successor)) {
2931 __ B(codegen_->GetLabelOf(successor));
2932 }
2933}
2934
2935void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2936 HandleGoto(got, got->GetSuccessor());
2937}
2938
2939void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2940 try_boundary->SetLocations(nullptr);
2941}
2942
2943void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2944 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2945 if (!successor->IsExitBlock()) {
2946 HandleGoto(try_boundary, successor);
2947 }
2948}
2949
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002950void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2951 LocationSummary* locations) {
2952 Register dst = locations->Out().AsRegister<Register>();
2953 Register lhs = locations->InAt(0).AsRegister<Register>();
2954 Location rhs_location = locations->InAt(1);
2955 Register rhs_reg = ZERO;
2956 int64_t rhs_imm = 0;
2957 bool use_imm = rhs_location.IsConstant();
2958 if (use_imm) {
2959 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2960 } else {
2961 rhs_reg = rhs_location.AsRegister<Register>();
2962 }
2963
2964 switch (cond) {
2965 case kCondEQ:
2966 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002967 if (use_imm && IsInt<16>(-rhs_imm)) {
2968 if (rhs_imm == 0) {
2969 if (cond == kCondEQ) {
2970 __ Sltiu(dst, lhs, 1);
2971 } else {
2972 __ Sltu(dst, ZERO, lhs);
2973 }
2974 } else {
2975 __ Addiu(dst, lhs, -rhs_imm);
2976 if (cond == kCondEQ) {
2977 __ Sltiu(dst, dst, 1);
2978 } else {
2979 __ Sltu(dst, ZERO, dst);
2980 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002981 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002982 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002983 if (use_imm && IsUint<16>(rhs_imm)) {
2984 __ Xori(dst, lhs, rhs_imm);
2985 } else {
2986 if (use_imm) {
2987 rhs_reg = TMP;
2988 __ LoadConst32(rhs_reg, rhs_imm);
2989 }
2990 __ Xor(dst, lhs, rhs_reg);
2991 }
2992 if (cond == kCondEQ) {
2993 __ Sltiu(dst, dst, 1);
2994 } else {
2995 __ Sltu(dst, ZERO, dst);
2996 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002997 }
2998 break;
2999
3000 case kCondLT:
3001 case kCondGE:
3002 if (use_imm && IsInt<16>(rhs_imm)) {
3003 __ Slti(dst, lhs, rhs_imm);
3004 } else {
3005 if (use_imm) {
3006 rhs_reg = TMP;
3007 __ LoadConst32(rhs_reg, rhs_imm);
3008 }
3009 __ Slt(dst, lhs, rhs_reg);
3010 }
3011 if (cond == kCondGE) {
3012 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3013 // only the slt instruction but no sge.
3014 __ Xori(dst, dst, 1);
3015 }
3016 break;
3017
3018 case kCondLE:
3019 case kCondGT:
3020 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3021 // Simulate lhs <= rhs via lhs < rhs + 1.
3022 __ Slti(dst, lhs, rhs_imm + 1);
3023 if (cond == kCondGT) {
3024 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3025 // only the slti instruction but no sgti.
3026 __ Xori(dst, dst, 1);
3027 }
3028 } else {
3029 if (use_imm) {
3030 rhs_reg = TMP;
3031 __ LoadConst32(rhs_reg, rhs_imm);
3032 }
3033 __ Slt(dst, rhs_reg, lhs);
3034 if (cond == kCondLE) {
3035 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3036 // only the slt instruction but no sle.
3037 __ Xori(dst, dst, 1);
3038 }
3039 }
3040 break;
3041
3042 case kCondB:
3043 case kCondAE:
3044 if (use_imm && IsInt<16>(rhs_imm)) {
3045 // Sltiu sign-extends its 16-bit immediate operand before
3046 // the comparison and thus lets us compare directly with
3047 // unsigned values in the ranges [0, 0x7fff] and
3048 // [0xffff8000, 0xffffffff].
3049 __ Sltiu(dst, lhs, rhs_imm);
3050 } else {
3051 if (use_imm) {
3052 rhs_reg = TMP;
3053 __ LoadConst32(rhs_reg, rhs_imm);
3054 }
3055 __ Sltu(dst, lhs, rhs_reg);
3056 }
3057 if (cond == kCondAE) {
3058 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3059 // only the sltu instruction but no sgeu.
3060 __ Xori(dst, dst, 1);
3061 }
3062 break;
3063
3064 case kCondBE:
3065 case kCondA:
3066 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3067 // Simulate lhs <= rhs via lhs < rhs + 1.
3068 // Note that this only works if rhs + 1 does not overflow
3069 // to 0, hence the check above.
3070 // Sltiu sign-extends its 16-bit immediate operand before
3071 // the comparison and thus lets us compare directly with
3072 // unsigned values in the ranges [0, 0x7fff] and
3073 // [0xffff8000, 0xffffffff].
3074 __ Sltiu(dst, lhs, rhs_imm + 1);
3075 if (cond == kCondA) {
3076 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3077 // only the sltiu instruction but no sgtiu.
3078 __ Xori(dst, dst, 1);
3079 }
3080 } else {
3081 if (use_imm) {
3082 rhs_reg = TMP;
3083 __ LoadConst32(rhs_reg, rhs_imm);
3084 }
3085 __ Sltu(dst, rhs_reg, lhs);
3086 if (cond == kCondBE) {
3087 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3088 // only the sltu instruction but no sleu.
3089 __ Xori(dst, dst, 1);
3090 }
3091 }
3092 break;
3093 }
3094}
3095
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003096bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3097 LocationSummary* input_locations,
3098 Register dst) {
3099 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3100 Location rhs_location = input_locations->InAt(1);
3101 Register rhs_reg = ZERO;
3102 int64_t rhs_imm = 0;
3103 bool use_imm = rhs_location.IsConstant();
3104 if (use_imm) {
3105 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3106 } else {
3107 rhs_reg = rhs_location.AsRegister<Register>();
3108 }
3109
3110 switch (cond) {
3111 case kCondEQ:
3112 case kCondNE:
3113 if (use_imm && IsInt<16>(-rhs_imm)) {
3114 __ Addiu(dst, lhs, -rhs_imm);
3115 } else if (use_imm && IsUint<16>(rhs_imm)) {
3116 __ Xori(dst, lhs, rhs_imm);
3117 } else {
3118 if (use_imm) {
3119 rhs_reg = TMP;
3120 __ LoadConst32(rhs_reg, rhs_imm);
3121 }
3122 __ Xor(dst, lhs, rhs_reg);
3123 }
3124 return (cond == kCondEQ);
3125
3126 case kCondLT:
3127 case kCondGE:
3128 if (use_imm && IsInt<16>(rhs_imm)) {
3129 __ Slti(dst, lhs, rhs_imm);
3130 } else {
3131 if (use_imm) {
3132 rhs_reg = TMP;
3133 __ LoadConst32(rhs_reg, rhs_imm);
3134 }
3135 __ Slt(dst, lhs, rhs_reg);
3136 }
3137 return (cond == kCondGE);
3138
3139 case kCondLE:
3140 case kCondGT:
3141 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3142 // Simulate lhs <= rhs via lhs < rhs + 1.
3143 __ Slti(dst, lhs, rhs_imm + 1);
3144 return (cond == kCondGT);
3145 } else {
3146 if (use_imm) {
3147 rhs_reg = TMP;
3148 __ LoadConst32(rhs_reg, rhs_imm);
3149 }
3150 __ Slt(dst, rhs_reg, lhs);
3151 return (cond == kCondLE);
3152 }
3153
3154 case kCondB:
3155 case kCondAE:
3156 if (use_imm && IsInt<16>(rhs_imm)) {
3157 // Sltiu sign-extends its 16-bit immediate operand before
3158 // the comparison and thus lets us compare directly with
3159 // unsigned values in the ranges [0, 0x7fff] and
3160 // [0xffff8000, 0xffffffff].
3161 __ Sltiu(dst, lhs, rhs_imm);
3162 } else {
3163 if (use_imm) {
3164 rhs_reg = TMP;
3165 __ LoadConst32(rhs_reg, rhs_imm);
3166 }
3167 __ Sltu(dst, lhs, rhs_reg);
3168 }
3169 return (cond == kCondAE);
3170
3171 case kCondBE:
3172 case kCondA:
3173 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3174 // Simulate lhs <= rhs via lhs < rhs + 1.
3175 // Note that this only works if rhs + 1 does not overflow
3176 // to 0, hence the check above.
3177 // Sltiu sign-extends its 16-bit immediate operand before
3178 // the comparison and thus lets us compare directly with
3179 // unsigned values in the ranges [0, 0x7fff] and
3180 // [0xffff8000, 0xffffffff].
3181 __ Sltiu(dst, lhs, rhs_imm + 1);
3182 return (cond == kCondA);
3183 } else {
3184 if (use_imm) {
3185 rhs_reg = TMP;
3186 __ LoadConst32(rhs_reg, rhs_imm);
3187 }
3188 __ Sltu(dst, rhs_reg, lhs);
3189 return (cond == kCondBE);
3190 }
3191 }
3192}
3193
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003194void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3195 LocationSummary* locations,
3196 MipsLabel* label) {
3197 Register lhs = locations->InAt(0).AsRegister<Register>();
3198 Location rhs_location = locations->InAt(1);
3199 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003200 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003201 bool use_imm = rhs_location.IsConstant();
3202 if (use_imm) {
3203 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3204 } else {
3205 rhs_reg = rhs_location.AsRegister<Register>();
3206 }
3207
3208 if (use_imm && rhs_imm == 0) {
3209 switch (cond) {
3210 case kCondEQ:
3211 case kCondBE: // <= 0 if zero
3212 __ Beqz(lhs, label);
3213 break;
3214 case kCondNE:
3215 case kCondA: // > 0 if non-zero
3216 __ Bnez(lhs, label);
3217 break;
3218 case kCondLT:
3219 __ Bltz(lhs, label);
3220 break;
3221 case kCondGE:
3222 __ Bgez(lhs, label);
3223 break;
3224 case kCondLE:
3225 __ Blez(lhs, label);
3226 break;
3227 case kCondGT:
3228 __ Bgtz(lhs, label);
3229 break;
3230 case kCondB: // always false
3231 break;
3232 case kCondAE: // always true
3233 __ B(label);
3234 break;
3235 }
3236 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003237 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3238 if (isR6 || !use_imm) {
3239 if (use_imm) {
3240 rhs_reg = TMP;
3241 __ LoadConst32(rhs_reg, rhs_imm);
3242 }
3243 switch (cond) {
3244 case kCondEQ:
3245 __ Beq(lhs, rhs_reg, label);
3246 break;
3247 case kCondNE:
3248 __ Bne(lhs, rhs_reg, label);
3249 break;
3250 case kCondLT:
3251 __ Blt(lhs, rhs_reg, label);
3252 break;
3253 case kCondGE:
3254 __ Bge(lhs, rhs_reg, label);
3255 break;
3256 case kCondLE:
3257 __ Bge(rhs_reg, lhs, label);
3258 break;
3259 case kCondGT:
3260 __ Blt(rhs_reg, lhs, label);
3261 break;
3262 case kCondB:
3263 __ Bltu(lhs, rhs_reg, label);
3264 break;
3265 case kCondAE:
3266 __ Bgeu(lhs, rhs_reg, label);
3267 break;
3268 case kCondBE:
3269 __ Bgeu(rhs_reg, lhs, label);
3270 break;
3271 case kCondA:
3272 __ Bltu(rhs_reg, lhs, label);
3273 break;
3274 }
3275 } else {
3276 // Special cases for more efficient comparison with constants on R2.
3277 switch (cond) {
3278 case kCondEQ:
3279 __ LoadConst32(TMP, rhs_imm);
3280 __ Beq(lhs, TMP, label);
3281 break;
3282 case kCondNE:
3283 __ LoadConst32(TMP, rhs_imm);
3284 __ Bne(lhs, TMP, label);
3285 break;
3286 case kCondLT:
3287 if (IsInt<16>(rhs_imm)) {
3288 __ Slti(TMP, lhs, rhs_imm);
3289 __ Bnez(TMP, label);
3290 } else {
3291 __ LoadConst32(TMP, rhs_imm);
3292 __ Blt(lhs, TMP, label);
3293 }
3294 break;
3295 case kCondGE:
3296 if (IsInt<16>(rhs_imm)) {
3297 __ Slti(TMP, lhs, rhs_imm);
3298 __ Beqz(TMP, label);
3299 } else {
3300 __ LoadConst32(TMP, rhs_imm);
3301 __ Bge(lhs, TMP, label);
3302 }
3303 break;
3304 case kCondLE:
3305 if (IsInt<16>(rhs_imm + 1)) {
3306 // Simulate lhs <= rhs via lhs < rhs + 1.
3307 __ Slti(TMP, lhs, rhs_imm + 1);
3308 __ Bnez(TMP, label);
3309 } else {
3310 __ LoadConst32(TMP, rhs_imm);
3311 __ Bge(TMP, lhs, label);
3312 }
3313 break;
3314 case kCondGT:
3315 if (IsInt<16>(rhs_imm + 1)) {
3316 // Simulate lhs > rhs via !(lhs < rhs + 1).
3317 __ Slti(TMP, lhs, rhs_imm + 1);
3318 __ Beqz(TMP, label);
3319 } else {
3320 __ LoadConst32(TMP, rhs_imm);
3321 __ Blt(TMP, lhs, label);
3322 }
3323 break;
3324 case kCondB:
3325 if (IsInt<16>(rhs_imm)) {
3326 __ Sltiu(TMP, lhs, rhs_imm);
3327 __ Bnez(TMP, label);
3328 } else {
3329 __ LoadConst32(TMP, rhs_imm);
3330 __ Bltu(lhs, TMP, label);
3331 }
3332 break;
3333 case kCondAE:
3334 if (IsInt<16>(rhs_imm)) {
3335 __ Sltiu(TMP, lhs, rhs_imm);
3336 __ Beqz(TMP, label);
3337 } else {
3338 __ LoadConst32(TMP, rhs_imm);
3339 __ Bgeu(lhs, TMP, label);
3340 }
3341 break;
3342 case kCondBE:
3343 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3344 // Simulate lhs <= rhs via lhs < rhs + 1.
3345 // Note that this only works if rhs + 1 does not overflow
3346 // to 0, hence the check above.
3347 __ Sltiu(TMP, lhs, rhs_imm + 1);
3348 __ Bnez(TMP, label);
3349 } else {
3350 __ LoadConst32(TMP, rhs_imm);
3351 __ Bgeu(TMP, lhs, label);
3352 }
3353 break;
3354 case kCondA:
3355 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3356 // Simulate lhs > rhs via !(lhs < rhs + 1).
3357 // Note that this only works if rhs + 1 does not overflow
3358 // to 0, hence the check above.
3359 __ Sltiu(TMP, lhs, rhs_imm + 1);
3360 __ Beqz(TMP, label);
3361 } else {
3362 __ LoadConst32(TMP, rhs_imm);
3363 __ Bltu(TMP, lhs, label);
3364 }
3365 break;
3366 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003367 }
3368 }
3369}
3370
3371void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3372 LocationSummary* locations,
3373 MipsLabel* label) {
3374 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3375 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3376 Location rhs_location = locations->InAt(1);
3377 Register rhs_high = ZERO;
3378 Register rhs_low = ZERO;
3379 int64_t imm = 0;
3380 uint32_t imm_high = 0;
3381 uint32_t imm_low = 0;
3382 bool use_imm = rhs_location.IsConstant();
3383 if (use_imm) {
3384 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3385 imm_high = High32Bits(imm);
3386 imm_low = Low32Bits(imm);
3387 } else {
3388 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3389 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3390 }
3391
3392 if (use_imm && imm == 0) {
3393 switch (cond) {
3394 case kCondEQ:
3395 case kCondBE: // <= 0 if zero
3396 __ Or(TMP, lhs_high, lhs_low);
3397 __ Beqz(TMP, label);
3398 break;
3399 case kCondNE:
3400 case kCondA: // > 0 if non-zero
3401 __ Or(TMP, lhs_high, lhs_low);
3402 __ Bnez(TMP, label);
3403 break;
3404 case kCondLT:
3405 __ Bltz(lhs_high, label);
3406 break;
3407 case kCondGE:
3408 __ Bgez(lhs_high, label);
3409 break;
3410 case kCondLE:
3411 __ Or(TMP, lhs_high, lhs_low);
3412 __ Sra(AT, lhs_high, 31);
3413 __ Bgeu(AT, TMP, label);
3414 break;
3415 case kCondGT:
3416 __ Or(TMP, lhs_high, lhs_low);
3417 __ Sra(AT, lhs_high, 31);
3418 __ Bltu(AT, TMP, label);
3419 break;
3420 case kCondB: // always false
3421 break;
3422 case kCondAE: // always true
3423 __ B(label);
3424 break;
3425 }
3426 } else if (use_imm) {
3427 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3428 switch (cond) {
3429 case kCondEQ:
3430 __ LoadConst32(TMP, imm_high);
3431 __ Xor(TMP, TMP, lhs_high);
3432 __ LoadConst32(AT, imm_low);
3433 __ Xor(AT, AT, lhs_low);
3434 __ Or(TMP, TMP, AT);
3435 __ Beqz(TMP, label);
3436 break;
3437 case kCondNE:
3438 __ LoadConst32(TMP, imm_high);
3439 __ Xor(TMP, TMP, lhs_high);
3440 __ LoadConst32(AT, imm_low);
3441 __ Xor(AT, AT, lhs_low);
3442 __ Or(TMP, TMP, AT);
3443 __ Bnez(TMP, label);
3444 break;
3445 case kCondLT:
3446 __ LoadConst32(TMP, imm_high);
3447 __ Blt(lhs_high, TMP, label);
3448 __ Slt(TMP, TMP, lhs_high);
3449 __ LoadConst32(AT, imm_low);
3450 __ Sltu(AT, lhs_low, AT);
3451 __ Blt(TMP, AT, label);
3452 break;
3453 case kCondGE:
3454 __ LoadConst32(TMP, imm_high);
3455 __ Blt(TMP, lhs_high, label);
3456 __ Slt(TMP, lhs_high, TMP);
3457 __ LoadConst32(AT, imm_low);
3458 __ Sltu(AT, lhs_low, AT);
3459 __ Or(TMP, TMP, AT);
3460 __ Beqz(TMP, label);
3461 break;
3462 case kCondLE:
3463 __ LoadConst32(TMP, imm_high);
3464 __ Blt(lhs_high, TMP, label);
3465 __ Slt(TMP, TMP, lhs_high);
3466 __ LoadConst32(AT, imm_low);
3467 __ Sltu(AT, AT, lhs_low);
3468 __ Or(TMP, TMP, AT);
3469 __ Beqz(TMP, label);
3470 break;
3471 case kCondGT:
3472 __ LoadConst32(TMP, imm_high);
3473 __ Blt(TMP, lhs_high, label);
3474 __ Slt(TMP, lhs_high, TMP);
3475 __ LoadConst32(AT, imm_low);
3476 __ Sltu(AT, AT, lhs_low);
3477 __ Blt(TMP, AT, label);
3478 break;
3479 case kCondB:
3480 __ LoadConst32(TMP, imm_high);
3481 __ Bltu(lhs_high, TMP, label);
3482 __ Sltu(TMP, TMP, lhs_high);
3483 __ LoadConst32(AT, imm_low);
3484 __ Sltu(AT, lhs_low, AT);
3485 __ Blt(TMP, AT, label);
3486 break;
3487 case kCondAE:
3488 __ LoadConst32(TMP, imm_high);
3489 __ Bltu(TMP, lhs_high, label);
3490 __ Sltu(TMP, lhs_high, TMP);
3491 __ LoadConst32(AT, imm_low);
3492 __ Sltu(AT, lhs_low, AT);
3493 __ Or(TMP, TMP, AT);
3494 __ Beqz(TMP, label);
3495 break;
3496 case kCondBE:
3497 __ LoadConst32(TMP, imm_high);
3498 __ Bltu(lhs_high, TMP, label);
3499 __ Sltu(TMP, TMP, lhs_high);
3500 __ LoadConst32(AT, imm_low);
3501 __ Sltu(AT, AT, lhs_low);
3502 __ Or(TMP, TMP, AT);
3503 __ Beqz(TMP, label);
3504 break;
3505 case kCondA:
3506 __ LoadConst32(TMP, imm_high);
3507 __ Bltu(TMP, lhs_high, label);
3508 __ Sltu(TMP, lhs_high, TMP);
3509 __ LoadConst32(AT, imm_low);
3510 __ Sltu(AT, AT, lhs_low);
3511 __ Blt(TMP, AT, label);
3512 break;
3513 }
3514 } else {
3515 switch (cond) {
3516 case kCondEQ:
3517 __ Xor(TMP, lhs_high, rhs_high);
3518 __ Xor(AT, lhs_low, rhs_low);
3519 __ Or(TMP, TMP, AT);
3520 __ Beqz(TMP, label);
3521 break;
3522 case kCondNE:
3523 __ Xor(TMP, lhs_high, rhs_high);
3524 __ Xor(AT, lhs_low, rhs_low);
3525 __ Or(TMP, TMP, AT);
3526 __ Bnez(TMP, label);
3527 break;
3528 case kCondLT:
3529 __ Blt(lhs_high, rhs_high, label);
3530 __ Slt(TMP, rhs_high, lhs_high);
3531 __ Sltu(AT, lhs_low, rhs_low);
3532 __ Blt(TMP, AT, label);
3533 break;
3534 case kCondGE:
3535 __ Blt(rhs_high, lhs_high, label);
3536 __ Slt(TMP, lhs_high, rhs_high);
3537 __ Sltu(AT, lhs_low, rhs_low);
3538 __ Or(TMP, TMP, AT);
3539 __ Beqz(TMP, label);
3540 break;
3541 case kCondLE:
3542 __ Blt(lhs_high, rhs_high, label);
3543 __ Slt(TMP, rhs_high, lhs_high);
3544 __ Sltu(AT, rhs_low, lhs_low);
3545 __ Or(TMP, TMP, AT);
3546 __ Beqz(TMP, label);
3547 break;
3548 case kCondGT:
3549 __ Blt(rhs_high, lhs_high, label);
3550 __ Slt(TMP, lhs_high, rhs_high);
3551 __ Sltu(AT, rhs_low, lhs_low);
3552 __ Blt(TMP, AT, label);
3553 break;
3554 case kCondB:
3555 __ Bltu(lhs_high, rhs_high, label);
3556 __ Sltu(TMP, rhs_high, lhs_high);
3557 __ Sltu(AT, lhs_low, rhs_low);
3558 __ Blt(TMP, AT, label);
3559 break;
3560 case kCondAE:
3561 __ Bltu(rhs_high, lhs_high, label);
3562 __ Sltu(TMP, lhs_high, rhs_high);
3563 __ Sltu(AT, lhs_low, rhs_low);
3564 __ Or(TMP, TMP, AT);
3565 __ Beqz(TMP, label);
3566 break;
3567 case kCondBE:
3568 __ Bltu(lhs_high, rhs_high, label);
3569 __ Sltu(TMP, rhs_high, lhs_high);
3570 __ Sltu(AT, rhs_low, lhs_low);
3571 __ Or(TMP, TMP, AT);
3572 __ Beqz(TMP, label);
3573 break;
3574 case kCondA:
3575 __ Bltu(rhs_high, lhs_high, label);
3576 __ Sltu(TMP, lhs_high, rhs_high);
3577 __ Sltu(AT, rhs_low, lhs_low);
3578 __ Blt(TMP, AT, label);
3579 break;
3580 }
3581 }
3582}
3583
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003584void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3585 bool gt_bias,
3586 Primitive::Type type,
3587 LocationSummary* locations) {
3588 Register dst = locations->Out().AsRegister<Register>();
3589 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3590 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3591 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3592 if (type == Primitive::kPrimFloat) {
3593 if (isR6) {
3594 switch (cond) {
3595 case kCondEQ:
3596 __ CmpEqS(FTMP, lhs, rhs);
3597 __ Mfc1(dst, FTMP);
3598 __ Andi(dst, dst, 1);
3599 break;
3600 case kCondNE:
3601 __ CmpEqS(FTMP, lhs, rhs);
3602 __ Mfc1(dst, FTMP);
3603 __ Addiu(dst, dst, 1);
3604 break;
3605 case kCondLT:
3606 if (gt_bias) {
3607 __ CmpLtS(FTMP, lhs, rhs);
3608 } else {
3609 __ CmpUltS(FTMP, lhs, rhs);
3610 }
3611 __ Mfc1(dst, FTMP);
3612 __ Andi(dst, dst, 1);
3613 break;
3614 case kCondLE:
3615 if (gt_bias) {
3616 __ CmpLeS(FTMP, lhs, rhs);
3617 } else {
3618 __ CmpUleS(FTMP, lhs, rhs);
3619 }
3620 __ Mfc1(dst, FTMP);
3621 __ Andi(dst, dst, 1);
3622 break;
3623 case kCondGT:
3624 if (gt_bias) {
3625 __ CmpUltS(FTMP, rhs, lhs);
3626 } else {
3627 __ CmpLtS(FTMP, rhs, lhs);
3628 }
3629 __ Mfc1(dst, FTMP);
3630 __ Andi(dst, dst, 1);
3631 break;
3632 case kCondGE:
3633 if (gt_bias) {
3634 __ CmpUleS(FTMP, rhs, lhs);
3635 } else {
3636 __ CmpLeS(FTMP, rhs, lhs);
3637 }
3638 __ Mfc1(dst, FTMP);
3639 __ Andi(dst, dst, 1);
3640 break;
3641 default:
3642 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3643 UNREACHABLE();
3644 }
3645 } else {
3646 switch (cond) {
3647 case kCondEQ:
3648 __ CeqS(0, lhs, rhs);
3649 __ LoadConst32(dst, 1);
3650 __ Movf(dst, ZERO, 0);
3651 break;
3652 case kCondNE:
3653 __ CeqS(0, lhs, rhs);
3654 __ LoadConst32(dst, 1);
3655 __ Movt(dst, ZERO, 0);
3656 break;
3657 case kCondLT:
3658 if (gt_bias) {
3659 __ ColtS(0, lhs, rhs);
3660 } else {
3661 __ CultS(0, lhs, rhs);
3662 }
3663 __ LoadConst32(dst, 1);
3664 __ Movf(dst, ZERO, 0);
3665 break;
3666 case kCondLE:
3667 if (gt_bias) {
3668 __ ColeS(0, lhs, rhs);
3669 } else {
3670 __ CuleS(0, lhs, rhs);
3671 }
3672 __ LoadConst32(dst, 1);
3673 __ Movf(dst, ZERO, 0);
3674 break;
3675 case kCondGT:
3676 if (gt_bias) {
3677 __ CultS(0, rhs, lhs);
3678 } else {
3679 __ ColtS(0, rhs, lhs);
3680 }
3681 __ LoadConst32(dst, 1);
3682 __ Movf(dst, ZERO, 0);
3683 break;
3684 case kCondGE:
3685 if (gt_bias) {
3686 __ CuleS(0, rhs, lhs);
3687 } else {
3688 __ ColeS(0, rhs, lhs);
3689 }
3690 __ LoadConst32(dst, 1);
3691 __ Movf(dst, ZERO, 0);
3692 break;
3693 default:
3694 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3695 UNREACHABLE();
3696 }
3697 }
3698 } else {
3699 DCHECK_EQ(type, Primitive::kPrimDouble);
3700 if (isR6) {
3701 switch (cond) {
3702 case kCondEQ:
3703 __ CmpEqD(FTMP, lhs, rhs);
3704 __ Mfc1(dst, FTMP);
3705 __ Andi(dst, dst, 1);
3706 break;
3707 case kCondNE:
3708 __ CmpEqD(FTMP, lhs, rhs);
3709 __ Mfc1(dst, FTMP);
3710 __ Addiu(dst, dst, 1);
3711 break;
3712 case kCondLT:
3713 if (gt_bias) {
3714 __ CmpLtD(FTMP, lhs, rhs);
3715 } else {
3716 __ CmpUltD(FTMP, lhs, rhs);
3717 }
3718 __ Mfc1(dst, FTMP);
3719 __ Andi(dst, dst, 1);
3720 break;
3721 case kCondLE:
3722 if (gt_bias) {
3723 __ CmpLeD(FTMP, lhs, rhs);
3724 } else {
3725 __ CmpUleD(FTMP, lhs, rhs);
3726 }
3727 __ Mfc1(dst, FTMP);
3728 __ Andi(dst, dst, 1);
3729 break;
3730 case kCondGT:
3731 if (gt_bias) {
3732 __ CmpUltD(FTMP, rhs, lhs);
3733 } else {
3734 __ CmpLtD(FTMP, rhs, lhs);
3735 }
3736 __ Mfc1(dst, FTMP);
3737 __ Andi(dst, dst, 1);
3738 break;
3739 case kCondGE:
3740 if (gt_bias) {
3741 __ CmpUleD(FTMP, rhs, lhs);
3742 } else {
3743 __ CmpLeD(FTMP, rhs, lhs);
3744 }
3745 __ Mfc1(dst, FTMP);
3746 __ Andi(dst, dst, 1);
3747 break;
3748 default:
3749 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3750 UNREACHABLE();
3751 }
3752 } else {
3753 switch (cond) {
3754 case kCondEQ:
3755 __ CeqD(0, lhs, rhs);
3756 __ LoadConst32(dst, 1);
3757 __ Movf(dst, ZERO, 0);
3758 break;
3759 case kCondNE:
3760 __ CeqD(0, lhs, rhs);
3761 __ LoadConst32(dst, 1);
3762 __ Movt(dst, ZERO, 0);
3763 break;
3764 case kCondLT:
3765 if (gt_bias) {
3766 __ ColtD(0, lhs, rhs);
3767 } else {
3768 __ CultD(0, lhs, rhs);
3769 }
3770 __ LoadConst32(dst, 1);
3771 __ Movf(dst, ZERO, 0);
3772 break;
3773 case kCondLE:
3774 if (gt_bias) {
3775 __ ColeD(0, lhs, rhs);
3776 } else {
3777 __ CuleD(0, lhs, rhs);
3778 }
3779 __ LoadConst32(dst, 1);
3780 __ Movf(dst, ZERO, 0);
3781 break;
3782 case kCondGT:
3783 if (gt_bias) {
3784 __ CultD(0, rhs, lhs);
3785 } else {
3786 __ ColtD(0, rhs, lhs);
3787 }
3788 __ LoadConst32(dst, 1);
3789 __ Movf(dst, ZERO, 0);
3790 break;
3791 case kCondGE:
3792 if (gt_bias) {
3793 __ CuleD(0, rhs, lhs);
3794 } else {
3795 __ ColeD(0, rhs, lhs);
3796 }
3797 __ LoadConst32(dst, 1);
3798 __ Movf(dst, ZERO, 0);
3799 break;
3800 default:
3801 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3802 UNREACHABLE();
3803 }
3804 }
3805 }
3806}
3807
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003808bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3809 bool gt_bias,
3810 Primitive::Type type,
3811 LocationSummary* input_locations,
3812 int cc) {
3813 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3814 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3815 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3816 if (type == Primitive::kPrimFloat) {
3817 switch (cond) {
3818 case kCondEQ:
3819 __ CeqS(cc, lhs, rhs);
3820 return false;
3821 case kCondNE:
3822 __ CeqS(cc, lhs, rhs);
3823 return true;
3824 case kCondLT:
3825 if (gt_bias) {
3826 __ ColtS(cc, lhs, rhs);
3827 } else {
3828 __ CultS(cc, lhs, rhs);
3829 }
3830 return false;
3831 case kCondLE:
3832 if (gt_bias) {
3833 __ ColeS(cc, lhs, rhs);
3834 } else {
3835 __ CuleS(cc, lhs, rhs);
3836 }
3837 return false;
3838 case kCondGT:
3839 if (gt_bias) {
3840 __ CultS(cc, rhs, lhs);
3841 } else {
3842 __ ColtS(cc, rhs, lhs);
3843 }
3844 return false;
3845 case kCondGE:
3846 if (gt_bias) {
3847 __ CuleS(cc, rhs, lhs);
3848 } else {
3849 __ ColeS(cc, rhs, lhs);
3850 }
3851 return false;
3852 default:
3853 LOG(FATAL) << "Unexpected non-floating-point condition";
3854 UNREACHABLE();
3855 }
3856 } else {
3857 DCHECK_EQ(type, Primitive::kPrimDouble);
3858 switch (cond) {
3859 case kCondEQ:
3860 __ CeqD(cc, lhs, rhs);
3861 return false;
3862 case kCondNE:
3863 __ CeqD(cc, lhs, rhs);
3864 return true;
3865 case kCondLT:
3866 if (gt_bias) {
3867 __ ColtD(cc, lhs, rhs);
3868 } else {
3869 __ CultD(cc, lhs, rhs);
3870 }
3871 return false;
3872 case kCondLE:
3873 if (gt_bias) {
3874 __ ColeD(cc, lhs, rhs);
3875 } else {
3876 __ CuleD(cc, lhs, rhs);
3877 }
3878 return false;
3879 case kCondGT:
3880 if (gt_bias) {
3881 __ CultD(cc, rhs, lhs);
3882 } else {
3883 __ ColtD(cc, rhs, lhs);
3884 }
3885 return false;
3886 case kCondGE:
3887 if (gt_bias) {
3888 __ CuleD(cc, rhs, lhs);
3889 } else {
3890 __ ColeD(cc, rhs, lhs);
3891 }
3892 return false;
3893 default:
3894 LOG(FATAL) << "Unexpected non-floating-point condition";
3895 UNREACHABLE();
3896 }
3897 }
3898}
3899
3900bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3901 bool gt_bias,
3902 Primitive::Type type,
3903 LocationSummary* input_locations,
3904 FRegister dst) {
3905 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3906 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3907 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3908 if (type == Primitive::kPrimFloat) {
3909 switch (cond) {
3910 case kCondEQ:
3911 __ CmpEqS(dst, lhs, rhs);
3912 return false;
3913 case kCondNE:
3914 __ CmpEqS(dst, lhs, rhs);
3915 return true;
3916 case kCondLT:
3917 if (gt_bias) {
3918 __ CmpLtS(dst, lhs, rhs);
3919 } else {
3920 __ CmpUltS(dst, lhs, rhs);
3921 }
3922 return false;
3923 case kCondLE:
3924 if (gt_bias) {
3925 __ CmpLeS(dst, lhs, rhs);
3926 } else {
3927 __ CmpUleS(dst, lhs, rhs);
3928 }
3929 return false;
3930 case kCondGT:
3931 if (gt_bias) {
3932 __ CmpUltS(dst, rhs, lhs);
3933 } else {
3934 __ CmpLtS(dst, rhs, lhs);
3935 }
3936 return false;
3937 case kCondGE:
3938 if (gt_bias) {
3939 __ CmpUleS(dst, rhs, lhs);
3940 } else {
3941 __ CmpLeS(dst, rhs, lhs);
3942 }
3943 return false;
3944 default:
3945 LOG(FATAL) << "Unexpected non-floating-point condition";
3946 UNREACHABLE();
3947 }
3948 } else {
3949 DCHECK_EQ(type, Primitive::kPrimDouble);
3950 switch (cond) {
3951 case kCondEQ:
3952 __ CmpEqD(dst, lhs, rhs);
3953 return false;
3954 case kCondNE:
3955 __ CmpEqD(dst, lhs, rhs);
3956 return true;
3957 case kCondLT:
3958 if (gt_bias) {
3959 __ CmpLtD(dst, lhs, rhs);
3960 } else {
3961 __ CmpUltD(dst, lhs, rhs);
3962 }
3963 return false;
3964 case kCondLE:
3965 if (gt_bias) {
3966 __ CmpLeD(dst, lhs, rhs);
3967 } else {
3968 __ CmpUleD(dst, lhs, rhs);
3969 }
3970 return false;
3971 case kCondGT:
3972 if (gt_bias) {
3973 __ CmpUltD(dst, rhs, lhs);
3974 } else {
3975 __ CmpLtD(dst, rhs, lhs);
3976 }
3977 return false;
3978 case kCondGE:
3979 if (gt_bias) {
3980 __ CmpUleD(dst, rhs, lhs);
3981 } else {
3982 __ CmpLeD(dst, rhs, lhs);
3983 }
3984 return false;
3985 default:
3986 LOG(FATAL) << "Unexpected non-floating-point condition";
3987 UNREACHABLE();
3988 }
3989 }
3990}
3991
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003992void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3993 bool gt_bias,
3994 Primitive::Type type,
3995 LocationSummary* locations,
3996 MipsLabel* label) {
3997 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3998 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3999 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4000 if (type == Primitive::kPrimFloat) {
4001 if (isR6) {
4002 switch (cond) {
4003 case kCondEQ:
4004 __ CmpEqS(FTMP, lhs, rhs);
4005 __ Bc1nez(FTMP, label);
4006 break;
4007 case kCondNE:
4008 __ CmpEqS(FTMP, lhs, rhs);
4009 __ Bc1eqz(FTMP, label);
4010 break;
4011 case kCondLT:
4012 if (gt_bias) {
4013 __ CmpLtS(FTMP, lhs, rhs);
4014 } else {
4015 __ CmpUltS(FTMP, lhs, rhs);
4016 }
4017 __ Bc1nez(FTMP, label);
4018 break;
4019 case kCondLE:
4020 if (gt_bias) {
4021 __ CmpLeS(FTMP, lhs, rhs);
4022 } else {
4023 __ CmpUleS(FTMP, lhs, rhs);
4024 }
4025 __ Bc1nez(FTMP, label);
4026 break;
4027 case kCondGT:
4028 if (gt_bias) {
4029 __ CmpUltS(FTMP, rhs, lhs);
4030 } else {
4031 __ CmpLtS(FTMP, rhs, lhs);
4032 }
4033 __ Bc1nez(FTMP, label);
4034 break;
4035 case kCondGE:
4036 if (gt_bias) {
4037 __ CmpUleS(FTMP, rhs, lhs);
4038 } else {
4039 __ CmpLeS(FTMP, rhs, lhs);
4040 }
4041 __ Bc1nez(FTMP, label);
4042 break;
4043 default:
4044 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004045 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004046 }
4047 } else {
4048 switch (cond) {
4049 case kCondEQ:
4050 __ CeqS(0, lhs, rhs);
4051 __ Bc1t(0, label);
4052 break;
4053 case kCondNE:
4054 __ CeqS(0, lhs, rhs);
4055 __ Bc1f(0, label);
4056 break;
4057 case kCondLT:
4058 if (gt_bias) {
4059 __ ColtS(0, lhs, rhs);
4060 } else {
4061 __ CultS(0, lhs, rhs);
4062 }
4063 __ Bc1t(0, label);
4064 break;
4065 case kCondLE:
4066 if (gt_bias) {
4067 __ ColeS(0, lhs, rhs);
4068 } else {
4069 __ CuleS(0, lhs, rhs);
4070 }
4071 __ Bc1t(0, label);
4072 break;
4073 case kCondGT:
4074 if (gt_bias) {
4075 __ CultS(0, rhs, lhs);
4076 } else {
4077 __ ColtS(0, rhs, lhs);
4078 }
4079 __ Bc1t(0, label);
4080 break;
4081 case kCondGE:
4082 if (gt_bias) {
4083 __ CuleS(0, rhs, lhs);
4084 } else {
4085 __ ColeS(0, rhs, lhs);
4086 }
4087 __ Bc1t(0, label);
4088 break;
4089 default:
4090 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004091 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004092 }
4093 }
4094 } else {
4095 DCHECK_EQ(type, Primitive::kPrimDouble);
4096 if (isR6) {
4097 switch (cond) {
4098 case kCondEQ:
4099 __ CmpEqD(FTMP, lhs, rhs);
4100 __ Bc1nez(FTMP, label);
4101 break;
4102 case kCondNE:
4103 __ CmpEqD(FTMP, lhs, rhs);
4104 __ Bc1eqz(FTMP, label);
4105 break;
4106 case kCondLT:
4107 if (gt_bias) {
4108 __ CmpLtD(FTMP, lhs, rhs);
4109 } else {
4110 __ CmpUltD(FTMP, lhs, rhs);
4111 }
4112 __ Bc1nez(FTMP, label);
4113 break;
4114 case kCondLE:
4115 if (gt_bias) {
4116 __ CmpLeD(FTMP, lhs, rhs);
4117 } else {
4118 __ CmpUleD(FTMP, lhs, rhs);
4119 }
4120 __ Bc1nez(FTMP, label);
4121 break;
4122 case kCondGT:
4123 if (gt_bias) {
4124 __ CmpUltD(FTMP, rhs, lhs);
4125 } else {
4126 __ CmpLtD(FTMP, rhs, lhs);
4127 }
4128 __ Bc1nez(FTMP, label);
4129 break;
4130 case kCondGE:
4131 if (gt_bias) {
4132 __ CmpUleD(FTMP, rhs, lhs);
4133 } else {
4134 __ CmpLeD(FTMP, rhs, lhs);
4135 }
4136 __ Bc1nez(FTMP, label);
4137 break;
4138 default:
4139 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004140 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004141 }
4142 } else {
4143 switch (cond) {
4144 case kCondEQ:
4145 __ CeqD(0, lhs, rhs);
4146 __ Bc1t(0, label);
4147 break;
4148 case kCondNE:
4149 __ CeqD(0, lhs, rhs);
4150 __ Bc1f(0, label);
4151 break;
4152 case kCondLT:
4153 if (gt_bias) {
4154 __ ColtD(0, lhs, rhs);
4155 } else {
4156 __ CultD(0, lhs, rhs);
4157 }
4158 __ Bc1t(0, label);
4159 break;
4160 case kCondLE:
4161 if (gt_bias) {
4162 __ ColeD(0, lhs, rhs);
4163 } else {
4164 __ CuleD(0, lhs, rhs);
4165 }
4166 __ Bc1t(0, label);
4167 break;
4168 case kCondGT:
4169 if (gt_bias) {
4170 __ CultD(0, rhs, lhs);
4171 } else {
4172 __ ColtD(0, rhs, lhs);
4173 }
4174 __ Bc1t(0, label);
4175 break;
4176 case kCondGE:
4177 if (gt_bias) {
4178 __ CuleD(0, rhs, lhs);
4179 } else {
4180 __ ColeD(0, rhs, lhs);
4181 }
4182 __ Bc1t(0, label);
4183 break;
4184 default:
4185 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004186 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004187 }
4188 }
4189 }
4190}
4191
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004192void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004193 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004194 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004195 MipsLabel* false_target) {
4196 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004197
David Brazdil0debae72015-11-12 18:37:00 +00004198 if (true_target == nullptr && false_target == nullptr) {
4199 // Nothing to do. The code always falls through.
4200 return;
4201 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004202 // Constant condition, statically compared against "true" (integer value 1).
4203 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004204 if (true_target != nullptr) {
4205 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004207 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004208 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004209 if (false_target != nullptr) {
4210 __ B(false_target);
4211 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004212 }
David Brazdil0debae72015-11-12 18:37:00 +00004213 return;
4214 }
4215
4216 // The following code generates these patterns:
4217 // (1) true_target == nullptr && false_target != nullptr
4218 // - opposite condition true => branch to false_target
4219 // (2) true_target != nullptr && false_target == nullptr
4220 // - condition true => branch to true_target
4221 // (3) true_target != nullptr && false_target != nullptr
4222 // - condition true => branch to true_target
4223 // - branch to false_target
4224 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004225 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004226 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004227 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004228 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004229 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4230 } else {
4231 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4232 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004233 } else {
4234 // The condition instruction has not been materialized, use its inputs as
4235 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004236 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004237 Primitive::Type type = condition->InputAt(0)->GetType();
4238 LocationSummary* locations = cond->GetLocations();
4239 IfCondition if_cond = condition->GetCondition();
4240 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004241
David Brazdil0debae72015-11-12 18:37:00 +00004242 if (true_target == nullptr) {
4243 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004244 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004245 }
4246
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004247 switch (type) {
4248 default:
4249 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4250 break;
4251 case Primitive::kPrimLong:
4252 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4253 break;
4254 case Primitive::kPrimFloat:
4255 case Primitive::kPrimDouble:
4256 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4257 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004258 }
4259 }
David Brazdil0debae72015-11-12 18:37:00 +00004260
4261 // If neither branch falls through (case 3), the conditional branch to `true_target`
4262 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4263 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004264 __ B(false_target);
4265 }
4266}
4267
4268void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004270 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004271 locations->SetInAt(0, Location::RequiresRegister());
4272 }
4273}
4274
4275void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004276 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4277 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4278 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4279 nullptr : codegen_->GetLabelOf(true_successor);
4280 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4281 nullptr : codegen_->GetLabelOf(false_successor);
4282 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004283}
4284
4285void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4286 LocationSummary* locations = new (GetGraph()->GetArena())
4287 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004288 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004289 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004290 locations->SetInAt(0, Location::RequiresRegister());
4291 }
4292}
4293
4294void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004295 SlowPathCodeMIPS* slow_path =
4296 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004297 GenerateTestAndBranch(deoptimize,
4298 /* condition_input_index */ 0,
4299 slow_path->GetEntryLabel(),
4300 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004301}
4302
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004303// This function returns true if a conditional move can be generated for HSelect.
4304// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4305// branches and regular moves.
4306//
4307// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4308//
4309// While determining feasibility of a conditional move and setting inputs/outputs
4310// are two distinct tasks, this function does both because they share quite a bit
4311// of common logic.
4312static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4313 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4314 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4315 HCondition* condition = cond->AsCondition();
4316
4317 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4318 Primitive::Type dst_type = select->GetType();
4319
4320 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4321 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4322 bool is_true_value_zero_constant =
4323 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4324 bool is_false_value_zero_constant =
4325 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4326
4327 bool can_move_conditionally = false;
4328 bool use_const_for_false_in = false;
4329 bool use_const_for_true_in = false;
4330
4331 if (!cond->IsConstant()) {
4332 switch (cond_type) {
4333 default:
4334 switch (dst_type) {
4335 default:
4336 // Moving int on int condition.
4337 if (is_r6) {
4338 if (is_true_value_zero_constant) {
4339 // seleqz out_reg, false_reg, cond_reg
4340 can_move_conditionally = true;
4341 use_const_for_true_in = true;
4342 } else if (is_false_value_zero_constant) {
4343 // selnez out_reg, true_reg, cond_reg
4344 can_move_conditionally = true;
4345 use_const_for_false_in = true;
4346 } else if (materialized) {
4347 // Not materializing unmaterialized int conditions
4348 // to keep the instruction count low.
4349 // selnez AT, true_reg, cond_reg
4350 // seleqz TMP, false_reg, cond_reg
4351 // or out_reg, AT, TMP
4352 can_move_conditionally = true;
4353 }
4354 } else {
4355 // movn out_reg, true_reg/ZERO, cond_reg
4356 can_move_conditionally = true;
4357 use_const_for_true_in = is_true_value_zero_constant;
4358 }
4359 break;
4360 case Primitive::kPrimLong:
4361 // Moving long on int condition.
4362 if (is_r6) {
4363 if (is_true_value_zero_constant) {
4364 // seleqz out_reg_lo, false_reg_lo, cond_reg
4365 // seleqz out_reg_hi, false_reg_hi, cond_reg
4366 can_move_conditionally = true;
4367 use_const_for_true_in = true;
4368 } else if (is_false_value_zero_constant) {
4369 // selnez out_reg_lo, true_reg_lo, cond_reg
4370 // selnez out_reg_hi, true_reg_hi, cond_reg
4371 can_move_conditionally = true;
4372 use_const_for_false_in = true;
4373 }
4374 // Other long conditional moves would generate 6+ instructions,
4375 // which is too many.
4376 } else {
4377 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4378 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4379 can_move_conditionally = true;
4380 use_const_for_true_in = is_true_value_zero_constant;
4381 }
4382 break;
4383 case Primitive::kPrimFloat:
4384 case Primitive::kPrimDouble:
4385 // Moving float/double on int condition.
4386 if (is_r6) {
4387 if (materialized) {
4388 // Not materializing unmaterialized int conditions
4389 // to keep the instruction count low.
4390 can_move_conditionally = true;
4391 if (is_true_value_zero_constant) {
4392 // sltu TMP, ZERO, cond_reg
4393 // mtc1 TMP, temp_cond_reg
4394 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4395 use_const_for_true_in = true;
4396 } else if (is_false_value_zero_constant) {
4397 // sltu TMP, ZERO, cond_reg
4398 // mtc1 TMP, temp_cond_reg
4399 // selnez.fmt out_reg, true_reg, temp_cond_reg
4400 use_const_for_false_in = true;
4401 } else {
4402 // sltu TMP, ZERO, cond_reg
4403 // mtc1 TMP, temp_cond_reg
4404 // sel.fmt temp_cond_reg, false_reg, true_reg
4405 // mov.fmt out_reg, temp_cond_reg
4406 }
4407 }
4408 } else {
4409 // movn.fmt out_reg, true_reg, cond_reg
4410 can_move_conditionally = true;
4411 }
4412 break;
4413 }
4414 break;
4415 case Primitive::kPrimLong:
4416 // We don't materialize long comparison now
4417 // and use conditional branches instead.
4418 break;
4419 case Primitive::kPrimFloat:
4420 case Primitive::kPrimDouble:
4421 switch (dst_type) {
4422 default:
4423 // Moving int on float/double condition.
4424 if (is_r6) {
4425 if (is_true_value_zero_constant) {
4426 // mfc1 TMP, temp_cond_reg
4427 // seleqz out_reg, false_reg, TMP
4428 can_move_conditionally = true;
4429 use_const_for_true_in = true;
4430 } else if (is_false_value_zero_constant) {
4431 // mfc1 TMP, temp_cond_reg
4432 // selnez out_reg, true_reg, TMP
4433 can_move_conditionally = true;
4434 use_const_for_false_in = true;
4435 } else {
4436 // mfc1 TMP, temp_cond_reg
4437 // selnez AT, true_reg, TMP
4438 // seleqz TMP, false_reg, TMP
4439 // or out_reg, AT, TMP
4440 can_move_conditionally = true;
4441 }
4442 } else {
4443 // movt out_reg, true_reg/ZERO, cc
4444 can_move_conditionally = true;
4445 use_const_for_true_in = is_true_value_zero_constant;
4446 }
4447 break;
4448 case Primitive::kPrimLong:
4449 // Moving long on float/double condition.
4450 if (is_r6) {
4451 if (is_true_value_zero_constant) {
4452 // mfc1 TMP, temp_cond_reg
4453 // seleqz out_reg_lo, false_reg_lo, TMP
4454 // seleqz out_reg_hi, false_reg_hi, TMP
4455 can_move_conditionally = true;
4456 use_const_for_true_in = true;
4457 } else if (is_false_value_zero_constant) {
4458 // mfc1 TMP, temp_cond_reg
4459 // selnez out_reg_lo, true_reg_lo, TMP
4460 // selnez out_reg_hi, true_reg_hi, TMP
4461 can_move_conditionally = true;
4462 use_const_for_false_in = true;
4463 }
4464 // Other long conditional moves would generate 6+ instructions,
4465 // which is too many.
4466 } else {
4467 // movt out_reg_lo, true_reg_lo/ZERO, cc
4468 // movt out_reg_hi, true_reg_hi/ZERO, cc
4469 can_move_conditionally = true;
4470 use_const_for_true_in = is_true_value_zero_constant;
4471 }
4472 break;
4473 case Primitive::kPrimFloat:
4474 case Primitive::kPrimDouble:
4475 // Moving float/double on float/double condition.
4476 if (is_r6) {
4477 can_move_conditionally = true;
4478 if (is_true_value_zero_constant) {
4479 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4480 use_const_for_true_in = true;
4481 } else if (is_false_value_zero_constant) {
4482 // selnez.fmt out_reg, true_reg, temp_cond_reg
4483 use_const_for_false_in = true;
4484 } else {
4485 // sel.fmt temp_cond_reg, false_reg, true_reg
4486 // mov.fmt out_reg, temp_cond_reg
4487 }
4488 } else {
4489 // movt.fmt out_reg, true_reg, cc
4490 can_move_conditionally = true;
4491 }
4492 break;
4493 }
4494 break;
4495 }
4496 }
4497
4498 if (can_move_conditionally) {
4499 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4500 } else {
4501 DCHECK(!use_const_for_false_in);
4502 DCHECK(!use_const_for_true_in);
4503 }
4504
4505 if (locations_to_set != nullptr) {
4506 if (use_const_for_false_in) {
4507 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4508 } else {
4509 locations_to_set->SetInAt(0,
4510 Primitive::IsFloatingPointType(dst_type)
4511 ? Location::RequiresFpuRegister()
4512 : Location::RequiresRegister());
4513 }
4514 if (use_const_for_true_in) {
4515 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4516 } else {
4517 locations_to_set->SetInAt(1,
4518 Primitive::IsFloatingPointType(dst_type)
4519 ? Location::RequiresFpuRegister()
4520 : Location::RequiresRegister());
4521 }
4522 if (materialized) {
4523 locations_to_set->SetInAt(2, Location::RequiresRegister());
4524 }
4525 // On R6 we don't require the output to be the same as the
4526 // first input for conditional moves unlike on R2.
4527 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4528 if (is_out_same_as_first_in) {
4529 locations_to_set->SetOut(Location::SameAsFirstInput());
4530 } else {
4531 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4532 ? Location::RequiresFpuRegister()
4533 : Location::RequiresRegister());
4534 }
4535 }
4536
4537 return can_move_conditionally;
4538}
4539
4540void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4541 LocationSummary* locations = select->GetLocations();
4542 Location dst = locations->Out();
4543 Location src = locations->InAt(1);
4544 Register src_reg = ZERO;
4545 Register src_reg_high = ZERO;
4546 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4547 Register cond_reg = TMP;
4548 int cond_cc = 0;
4549 Primitive::Type cond_type = Primitive::kPrimInt;
4550 bool cond_inverted = false;
4551 Primitive::Type dst_type = select->GetType();
4552
4553 if (IsBooleanValueOrMaterializedCondition(cond)) {
4554 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4555 } else {
4556 HCondition* condition = cond->AsCondition();
4557 LocationSummary* cond_locations = cond->GetLocations();
4558 IfCondition if_cond = condition->GetCondition();
4559 cond_type = condition->InputAt(0)->GetType();
4560 switch (cond_type) {
4561 default:
4562 DCHECK_NE(cond_type, Primitive::kPrimLong);
4563 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4564 break;
4565 case Primitive::kPrimFloat:
4566 case Primitive::kPrimDouble:
4567 cond_inverted = MaterializeFpCompareR2(if_cond,
4568 condition->IsGtBias(),
4569 cond_type,
4570 cond_locations,
4571 cond_cc);
4572 break;
4573 }
4574 }
4575
4576 DCHECK(dst.Equals(locations->InAt(0)));
4577 if (src.IsRegister()) {
4578 src_reg = src.AsRegister<Register>();
4579 } else if (src.IsRegisterPair()) {
4580 src_reg = src.AsRegisterPairLow<Register>();
4581 src_reg_high = src.AsRegisterPairHigh<Register>();
4582 } else if (src.IsConstant()) {
4583 DCHECK(src.GetConstant()->IsZeroBitPattern());
4584 }
4585
4586 switch (cond_type) {
4587 default:
4588 switch (dst_type) {
4589 default:
4590 if (cond_inverted) {
4591 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4592 } else {
4593 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4594 }
4595 break;
4596 case Primitive::kPrimLong:
4597 if (cond_inverted) {
4598 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4599 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4600 } else {
4601 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4602 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4603 }
4604 break;
4605 case Primitive::kPrimFloat:
4606 if (cond_inverted) {
4607 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4608 } else {
4609 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4610 }
4611 break;
4612 case Primitive::kPrimDouble:
4613 if (cond_inverted) {
4614 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4615 } else {
4616 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4617 }
4618 break;
4619 }
4620 break;
4621 case Primitive::kPrimLong:
4622 LOG(FATAL) << "Unreachable";
4623 UNREACHABLE();
4624 case Primitive::kPrimFloat:
4625 case Primitive::kPrimDouble:
4626 switch (dst_type) {
4627 default:
4628 if (cond_inverted) {
4629 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4630 } else {
4631 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4632 }
4633 break;
4634 case Primitive::kPrimLong:
4635 if (cond_inverted) {
4636 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4637 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4638 } else {
4639 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4640 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4641 }
4642 break;
4643 case Primitive::kPrimFloat:
4644 if (cond_inverted) {
4645 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4646 } else {
4647 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4648 }
4649 break;
4650 case Primitive::kPrimDouble:
4651 if (cond_inverted) {
4652 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4653 } else {
4654 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4655 }
4656 break;
4657 }
4658 break;
4659 }
4660}
4661
4662void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4663 LocationSummary* locations = select->GetLocations();
4664 Location dst = locations->Out();
4665 Location false_src = locations->InAt(0);
4666 Location true_src = locations->InAt(1);
4667 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4668 Register cond_reg = TMP;
4669 FRegister fcond_reg = FTMP;
4670 Primitive::Type cond_type = Primitive::kPrimInt;
4671 bool cond_inverted = false;
4672 Primitive::Type dst_type = select->GetType();
4673
4674 if (IsBooleanValueOrMaterializedCondition(cond)) {
4675 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4676 } else {
4677 HCondition* condition = cond->AsCondition();
4678 LocationSummary* cond_locations = cond->GetLocations();
4679 IfCondition if_cond = condition->GetCondition();
4680 cond_type = condition->InputAt(0)->GetType();
4681 switch (cond_type) {
4682 default:
4683 DCHECK_NE(cond_type, Primitive::kPrimLong);
4684 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4685 break;
4686 case Primitive::kPrimFloat:
4687 case Primitive::kPrimDouble:
4688 cond_inverted = MaterializeFpCompareR6(if_cond,
4689 condition->IsGtBias(),
4690 cond_type,
4691 cond_locations,
4692 fcond_reg);
4693 break;
4694 }
4695 }
4696
4697 if (true_src.IsConstant()) {
4698 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4699 }
4700 if (false_src.IsConstant()) {
4701 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4702 }
4703
4704 switch (dst_type) {
4705 default:
4706 if (Primitive::IsFloatingPointType(cond_type)) {
4707 __ Mfc1(cond_reg, fcond_reg);
4708 }
4709 if (true_src.IsConstant()) {
4710 if (cond_inverted) {
4711 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4712 } else {
4713 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4714 }
4715 } else if (false_src.IsConstant()) {
4716 if (cond_inverted) {
4717 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4718 } else {
4719 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4720 }
4721 } else {
4722 DCHECK_NE(cond_reg, AT);
4723 if (cond_inverted) {
4724 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4725 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4726 } else {
4727 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4728 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4729 }
4730 __ Or(dst.AsRegister<Register>(), AT, TMP);
4731 }
4732 break;
4733 case Primitive::kPrimLong: {
4734 if (Primitive::IsFloatingPointType(cond_type)) {
4735 __ Mfc1(cond_reg, fcond_reg);
4736 }
4737 Register dst_lo = dst.AsRegisterPairLow<Register>();
4738 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4739 if (true_src.IsConstant()) {
4740 Register src_lo = false_src.AsRegisterPairLow<Register>();
4741 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4742 if (cond_inverted) {
4743 __ Selnez(dst_lo, src_lo, cond_reg);
4744 __ Selnez(dst_hi, src_hi, cond_reg);
4745 } else {
4746 __ Seleqz(dst_lo, src_lo, cond_reg);
4747 __ Seleqz(dst_hi, src_hi, cond_reg);
4748 }
4749 } else {
4750 DCHECK(false_src.IsConstant());
4751 Register src_lo = true_src.AsRegisterPairLow<Register>();
4752 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4753 if (cond_inverted) {
4754 __ Seleqz(dst_lo, src_lo, cond_reg);
4755 __ Seleqz(dst_hi, src_hi, cond_reg);
4756 } else {
4757 __ Selnez(dst_lo, src_lo, cond_reg);
4758 __ Selnez(dst_hi, src_hi, cond_reg);
4759 }
4760 }
4761 break;
4762 }
4763 case Primitive::kPrimFloat: {
4764 if (!Primitive::IsFloatingPointType(cond_type)) {
4765 // sel*.fmt tests bit 0 of the condition register, account for that.
4766 __ Sltu(TMP, ZERO, cond_reg);
4767 __ Mtc1(TMP, fcond_reg);
4768 }
4769 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4770 if (true_src.IsConstant()) {
4771 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4772 if (cond_inverted) {
4773 __ SelnezS(dst_reg, src_reg, fcond_reg);
4774 } else {
4775 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4776 }
4777 } else if (false_src.IsConstant()) {
4778 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4779 if (cond_inverted) {
4780 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4781 } else {
4782 __ SelnezS(dst_reg, src_reg, fcond_reg);
4783 }
4784 } else {
4785 if (cond_inverted) {
4786 __ SelS(fcond_reg,
4787 true_src.AsFpuRegister<FRegister>(),
4788 false_src.AsFpuRegister<FRegister>());
4789 } else {
4790 __ SelS(fcond_reg,
4791 false_src.AsFpuRegister<FRegister>(),
4792 true_src.AsFpuRegister<FRegister>());
4793 }
4794 __ MovS(dst_reg, fcond_reg);
4795 }
4796 break;
4797 }
4798 case Primitive::kPrimDouble: {
4799 if (!Primitive::IsFloatingPointType(cond_type)) {
4800 // sel*.fmt tests bit 0 of the condition register, account for that.
4801 __ Sltu(TMP, ZERO, cond_reg);
4802 __ Mtc1(TMP, fcond_reg);
4803 }
4804 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4805 if (true_src.IsConstant()) {
4806 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4807 if (cond_inverted) {
4808 __ SelnezD(dst_reg, src_reg, fcond_reg);
4809 } else {
4810 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4811 }
4812 } else if (false_src.IsConstant()) {
4813 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4814 if (cond_inverted) {
4815 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4816 } else {
4817 __ SelnezD(dst_reg, src_reg, fcond_reg);
4818 }
4819 } else {
4820 if (cond_inverted) {
4821 __ SelD(fcond_reg,
4822 true_src.AsFpuRegister<FRegister>(),
4823 false_src.AsFpuRegister<FRegister>());
4824 } else {
4825 __ SelD(fcond_reg,
4826 false_src.AsFpuRegister<FRegister>(),
4827 true_src.AsFpuRegister<FRegister>());
4828 }
4829 __ MovD(dst_reg, fcond_reg);
4830 }
4831 break;
4832 }
4833 }
4834}
4835
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004836void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4837 LocationSummary* locations = new (GetGraph()->GetArena())
4838 LocationSummary(flag, LocationSummary::kNoCall);
4839 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004840}
4841
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004842void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4843 __ LoadFromOffset(kLoadWord,
4844 flag->GetLocations()->Out().AsRegister<Register>(),
4845 SP,
4846 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004847}
4848
David Brazdil74eb1b22015-12-14 11:44:01 +00004849void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004851 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004852}
4853
4854void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004855 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4856 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4857 if (is_r6) {
4858 GenConditionalMoveR6(select);
4859 } else {
4860 GenConditionalMoveR2(select);
4861 }
4862 } else {
4863 LocationSummary* locations = select->GetLocations();
4864 MipsLabel false_target;
4865 GenerateTestAndBranch(select,
4866 /* condition_input_index */ 2,
4867 /* true_target */ nullptr,
4868 &false_target);
4869 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4870 __ Bind(&false_target);
4871 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004872}
4873
David Srbecky0cf44932015-12-09 14:09:59 +00004874void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4875 new (GetGraph()->GetArena()) LocationSummary(info);
4876}
4877
David Srbeckyd28f4a02016-03-14 17:14:24 +00004878void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4879 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004880}
4881
4882void CodeGeneratorMIPS::GenerateNop() {
4883 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004884}
4885
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004886void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4887 Primitive::Type field_type = field_info.GetFieldType();
4888 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4889 bool generate_volatile = field_info.IsVolatile() && is_wide;
4890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004891 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004892
4893 locations->SetInAt(0, Location::RequiresRegister());
4894 if (generate_volatile) {
4895 InvokeRuntimeCallingConvention calling_convention;
4896 // need A0 to hold base + offset
4897 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4898 if (field_type == Primitive::kPrimLong) {
4899 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4900 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004901 // Use Location::Any() to prevent situations when running out of available fp registers.
4902 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004903 // Need some temp core regs since FP results are returned in core registers
4904 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4905 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4906 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4907 }
4908 } else {
4909 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4910 locations->SetOut(Location::RequiresFpuRegister());
4911 } else {
4912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4913 }
4914 }
4915}
4916
4917void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4918 const FieldInfo& field_info,
4919 uint32_t dex_pc) {
4920 Primitive::Type type = field_info.GetFieldType();
4921 LocationSummary* locations = instruction->GetLocations();
4922 Register obj = locations->InAt(0).AsRegister<Register>();
4923 LoadOperandType load_type = kLoadUnsignedByte;
4924 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004925 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004926 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004927
4928 switch (type) {
4929 case Primitive::kPrimBoolean:
4930 load_type = kLoadUnsignedByte;
4931 break;
4932 case Primitive::kPrimByte:
4933 load_type = kLoadSignedByte;
4934 break;
4935 case Primitive::kPrimShort:
4936 load_type = kLoadSignedHalfword;
4937 break;
4938 case Primitive::kPrimChar:
4939 load_type = kLoadUnsignedHalfword;
4940 break;
4941 case Primitive::kPrimInt:
4942 case Primitive::kPrimFloat:
4943 case Primitive::kPrimNot:
4944 load_type = kLoadWord;
4945 break;
4946 case Primitive::kPrimLong:
4947 case Primitive::kPrimDouble:
4948 load_type = kLoadDoubleword;
4949 break;
4950 case Primitive::kPrimVoid:
4951 LOG(FATAL) << "Unreachable type " << type;
4952 UNREACHABLE();
4953 }
4954
4955 if (is_volatile && load_type == kLoadDoubleword) {
4956 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004957 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004958 // Do implicit Null check
4959 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4960 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004961 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004962 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4963 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004964 // FP results are returned in core registers. Need to move them.
4965 Location out = locations->Out();
4966 if (out.IsFpuRegister()) {
4967 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4968 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4969 out.AsFpuRegister<FRegister>());
4970 } else {
4971 DCHECK(out.IsDoubleStackSlot());
4972 __ StoreToOffset(kStoreWord,
4973 locations->GetTemp(1).AsRegister<Register>(),
4974 SP,
4975 out.GetStackIndex());
4976 __ StoreToOffset(kStoreWord,
4977 locations->GetTemp(2).AsRegister<Register>(),
4978 SP,
4979 out.GetStackIndex() + 4);
4980 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004981 }
4982 } else {
4983 if (!Primitive::IsFloatingPointType(type)) {
4984 Register dst;
4985 if (type == Primitive::kPrimLong) {
4986 DCHECK(locations->Out().IsRegisterPair());
4987 dst = locations->Out().AsRegisterPairLow<Register>();
4988 } else {
4989 DCHECK(locations->Out().IsRegister());
4990 dst = locations->Out().AsRegister<Register>();
4991 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004992 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08004993 if (type == Primitive::kPrimNot) {
4994 __ MaybeUnpoisonHeapReference(dst);
4995 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004996 } else {
4997 DCHECK(locations->Out().IsFpuRegister());
4998 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4999 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005000 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005001 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005002 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005003 }
5004 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005005 }
5006
5007 if (is_volatile) {
5008 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5009 }
5010}
5011
5012void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5013 Primitive::Type field_type = field_info.GetFieldType();
5014 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5015 bool generate_volatile = field_info.IsVolatile() && is_wide;
5016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005017 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005018
5019 locations->SetInAt(0, Location::RequiresRegister());
5020 if (generate_volatile) {
5021 InvokeRuntimeCallingConvention calling_convention;
5022 // need A0 to hold base + offset
5023 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5024 if (field_type == Primitive::kPrimLong) {
5025 locations->SetInAt(1, Location::RegisterPairLocation(
5026 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5027 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005028 // Use Location::Any() to prevent situations when running out of available fp registers.
5029 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005030 // Pass FP parameters in core registers.
5031 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5032 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
5033 }
5034 } else {
5035 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005036 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005037 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005038 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005039 }
5040 }
5041}
5042
5043void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
5044 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01005045 uint32_t dex_pc,
5046 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005047 Primitive::Type type = field_info.GetFieldType();
5048 LocationSummary* locations = instruction->GetLocations();
5049 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07005050 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005051 StoreOperandType store_type = kStoreByte;
5052 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005053 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08005054 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Alexey Frunze2923db72016-08-20 01:55:47 -07005055 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005056
5057 switch (type) {
5058 case Primitive::kPrimBoolean:
5059 case Primitive::kPrimByte:
5060 store_type = kStoreByte;
5061 break;
5062 case Primitive::kPrimShort:
5063 case Primitive::kPrimChar:
5064 store_type = kStoreHalfword;
5065 break;
5066 case Primitive::kPrimInt:
5067 case Primitive::kPrimFloat:
5068 case Primitive::kPrimNot:
5069 store_type = kStoreWord;
5070 break;
5071 case Primitive::kPrimLong:
5072 case Primitive::kPrimDouble:
5073 store_type = kStoreDoubleword;
5074 break;
5075 case Primitive::kPrimVoid:
5076 LOG(FATAL) << "Unreachable type " << type;
5077 UNREACHABLE();
5078 }
5079
5080 if (is_volatile) {
5081 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5082 }
5083
5084 if (is_volatile && store_type == kStoreDoubleword) {
5085 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005086 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005087 // Do implicit Null check.
5088 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5089 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5090 if (type == Primitive::kPrimDouble) {
5091 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07005092 if (value_location.IsFpuRegister()) {
5093 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
5094 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005095 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07005096 value_location.AsFpuRegister<FRegister>());
5097 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005098 __ LoadFromOffset(kLoadWord,
5099 locations->GetTemp(1).AsRegister<Register>(),
5100 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005101 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005102 __ LoadFromOffset(kLoadWord,
5103 locations->GetTemp(2).AsRegister<Register>(),
5104 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005105 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005106 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005107 DCHECK(value_location.IsConstant());
5108 DCHECK(value_location.GetConstant()->IsDoubleConstant());
5109 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005110 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
5111 locations->GetTemp(1).AsRegister<Register>(),
5112 value);
5113 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005114 }
Serban Constantinescufca16662016-07-14 09:21:59 +01005115 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
5117 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005118 if (value_location.IsConstant()) {
5119 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
5120 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
5121 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005122 Register src;
5123 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005124 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005125 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005126 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005127 }
Alexey Frunzec061de12017-02-14 13:27:23 -08005128 if (kPoisonHeapReferences && needs_write_barrier) {
5129 // Note that in the case where `value` is a null reference,
5130 // we do not enter this block, as a null reference does not
5131 // need poisoning.
5132 DCHECK_EQ(type, Primitive::kPrimNot);
5133 __ PoisonHeapReference(TMP, src);
5134 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
5135 } else {
5136 __ StoreToOffset(store_type, src, obj, offset, null_checker);
5137 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005138 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005139 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005140 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005141 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005142 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005143 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 }
5145 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146 }
5147
5148 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08005149 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005150 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01005151 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005152 }
5153
5154 if (is_volatile) {
5155 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5156 }
5157}
5158
5159void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5160 HandleFieldGet(instruction, instruction->GetFieldInfo());
5161}
5162
5163void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5164 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5165}
5166
5167void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5168 HandleFieldSet(instruction, instruction->GetFieldInfo());
5169}
5170
5171void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005172 HandleFieldSet(instruction,
5173 instruction->GetFieldInfo(),
5174 instruction->GetDexPc(),
5175 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005176}
5177
Alexey Frunze06a46c42016-07-19 15:00:40 -07005178void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5179 HInstruction* instruction ATTRIBUTE_UNUSED,
5180 Location root,
5181 Register obj,
5182 uint32_t offset) {
5183 Register root_reg = root.AsRegister<Register>();
5184 if (kEmitCompilerReadBarrier) {
5185 UNIMPLEMENTED(FATAL) << "for read barrier";
5186 } else {
5187 // Plain GC root load with no read barrier.
5188 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5189 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5190 // Note that GC roots are not affected by heap poisoning, thus we
5191 // do not have to unpoison `root_reg` here.
5192 }
5193}
5194
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005195void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5196 LocationSummary::CallKind call_kind =
5197 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5199 locations->SetInAt(0, Location::RequiresRegister());
5200 locations->SetInAt(1, Location::RequiresRegister());
5201 // The output does overlap inputs.
5202 // Note that TypeCheckSlowPathMIPS uses this register too.
5203 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5204}
5205
5206void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5207 LocationSummary* locations = instruction->GetLocations();
5208 Register obj = locations->InAt(0).AsRegister<Register>();
5209 Register cls = locations->InAt(1).AsRegister<Register>();
5210 Register out = locations->Out().AsRegister<Register>();
5211
5212 MipsLabel done;
5213
5214 // Return 0 if `obj` is null.
5215 // TODO: Avoid this check if we know `obj` is not null.
5216 __ Move(out, ZERO);
5217 __ Beqz(obj, &done);
5218
5219 // Compare the class of `obj` with `cls`.
5220 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08005221 __ MaybeUnpoisonHeapReference(out);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005222 if (instruction->IsExactCheck()) {
5223 // Classes must be equal for the instanceof to succeed.
5224 __ Xor(out, out, cls);
5225 __ Sltiu(out, out, 1);
5226 } else {
5227 // If the classes are not equal, we go into a slow path.
5228 DCHECK(locations->OnlyCallsOnSlowPath());
5229 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5230 codegen_->AddSlowPath(slow_path);
5231 __ Bne(out, cls, slow_path->GetEntryLabel());
5232 __ LoadConst32(out, 1);
5233 __ Bind(slow_path->GetExitLabel());
5234 }
5235
5236 __ Bind(&done);
5237}
5238
5239void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5241 locations->SetOut(Location::ConstantLocation(constant));
5242}
5243
5244void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5245 // Will be generated at use site.
5246}
5247
5248void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5249 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5250 locations->SetOut(Location::ConstantLocation(constant));
5251}
5252
5253void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5254 // Will be generated at use site.
5255}
5256
5257void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5258 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5259 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5260}
5261
5262void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5263 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005264 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005265 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005266 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005267}
5268
5269void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5270 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5271 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005272 Location receiver = invoke->GetLocations()->InAt(0);
5273 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005274 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005275
5276 // Set the hidden argument.
5277 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5278 invoke->GetDexMethodIndex());
5279
5280 // temp = object->GetClass();
5281 if (receiver.IsStackSlot()) {
5282 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5283 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5284 } else {
5285 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5286 }
5287 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005288 // Instead of simply (possibly) unpoisoning `temp` here, we should
5289 // emit a read barrier for the previous class reference load.
5290 // However this is not required in practice, as this is an
5291 // intermediate/temporary reference and because the current
5292 // concurrent copying collector keeps the from-space memory
5293 // intact/accessible until the end of the marking phase (the
5294 // concurrent copying collector may not in the future).
5295 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005296 __ LoadFromOffset(kLoadWord, temp, temp,
5297 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5298 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005299 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005300 // temp = temp->GetImtEntryAt(method_offset);
5301 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5302 // T9 = temp->GetEntryPoint();
5303 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5304 // T9();
5305 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005306 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005307 DCHECK(!codegen_->IsLeafMethod());
5308 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5309}
5310
5311void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005312 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5313 if (intrinsic.TryDispatch(invoke)) {
5314 return;
5315 }
5316
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005317 HandleInvoke(invoke);
5318}
5319
5320void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005321 // Explicit clinit checks triggered by static invokes must have been pruned by
5322 // art::PrepareForRegisterAllocation.
5323 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005324
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005325 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5326 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005327
Chris Larsen701566a2015-10-27 15:29:13 -07005328 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5329 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005330 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5331 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5332 }
Chris Larsen701566a2015-10-27 15:29:13 -07005333 return;
5334 }
5335
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005336 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005337
5338 // Add the extra input register if either the dex cache array base register
5339 // or the PC-relative base register for accessing literals is needed.
5340 if (has_extra_input) {
5341 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005343}
5344
Orion Hodsonac141392017-01-13 11:53:47 +00005345void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5346 HandleInvoke(invoke);
5347}
5348
5349void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5350 codegen_->GenerateInvokePolymorphicCall(invoke);
5351}
5352
Chris Larsen701566a2015-10-27 15:29:13 -07005353static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005354 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005355 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5356 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005357 return true;
5358 }
5359 return false;
5360}
5361
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005362HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005363 HLoadString::LoadKind desired_string_load_kind) {
5364 if (kEmitCompilerReadBarrier) {
5365 UNIMPLEMENTED(FATAL) << "for read barrier";
5366 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005367 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005368 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005369 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5370 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005371 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005372 bool is_r6 = GetInstructionSetFeatures().IsR6();
5373 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005374 switch (desired_string_load_kind) {
5375 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5376 DCHECK(!GetCompilerOptions().GetCompilePic());
5377 break;
5378 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5379 DCHECK(GetCompilerOptions().GetCompilePic());
5380 break;
5381 case HLoadString::LoadKind::kBootImageAddress:
5382 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005383 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005384 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005385 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005386 case HLoadString::LoadKind::kJitTableAddress:
5387 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005388 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005389 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005390 case HLoadString::LoadKind::kDexCacheViaMethod:
5391 fallback_load = false;
5392 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005393 }
5394 if (fallback_load) {
5395 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5396 }
5397 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005398}
5399
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005400HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5401 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005402 if (kEmitCompilerReadBarrier) {
5403 UNIMPLEMENTED(FATAL) << "for read barrier";
5404 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005405 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005406 // is incompatible with it.
5407 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005408 bool is_r6 = GetInstructionSetFeatures().IsR6();
5409 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005410 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005411 case HLoadClass::LoadKind::kInvalid:
5412 LOG(FATAL) << "UNREACHABLE";
5413 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005414 case HLoadClass::LoadKind::kReferrersClass:
5415 fallback_load = false;
5416 break;
5417 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5418 DCHECK(!GetCompilerOptions().GetCompilePic());
5419 break;
5420 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5421 DCHECK(GetCompilerOptions().GetCompilePic());
5422 break;
5423 case HLoadClass::LoadKind::kBootImageAddress:
5424 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005425 case HLoadClass::LoadKind::kBssEntry:
5426 DCHECK(!Runtime::Current()->UseJitCompilation());
5427 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005428 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005429 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005430 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005431 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005432 case HLoadClass::LoadKind::kDexCacheViaMethod:
5433 fallback_load = false;
5434 break;
5435 }
5436 if (fallback_load) {
5437 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5438 }
5439 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440}
5441
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005442Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5443 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005444 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005445 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5446 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5447 if (!invoke->GetLocations()->Intrinsified()) {
5448 return location.AsRegister<Register>();
5449 }
5450 // For intrinsics we allow any location, so it may be on the stack.
5451 if (!location.IsRegister()) {
5452 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5453 return temp;
5454 }
5455 // For register locations, check if the register was saved. If so, get it from the stack.
5456 // Note: There is a chance that the register was saved but not overwritten, so we could
5457 // save one load. However, since this is just an intrinsic slow path we prefer this
5458 // simple and more robust approach rather that trying to determine if that's the case.
5459 SlowPathCode* slow_path = GetCurrentSlowPath();
5460 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5461 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5462 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5463 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5464 return temp;
5465 }
5466 return location.AsRegister<Register>();
5467}
5468
Vladimir Markodc151b22015-10-15 18:02:30 +01005469HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5470 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005471 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005472 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005473 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005474 // is incompatible with it.
5475 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005476 bool is_r6 = GetInstructionSetFeatures().IsR6();
5477 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005478 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005479 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005480 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005481 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005482 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005483 break;
5484 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005485 if (fallback_load) {
5486 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5487 dispatch_info.method_load_data = 0;
5488 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005489 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005490}
5491
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005492void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5493 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005494 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005495 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5496 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005497 bool is_r6 = GetInstructionSetFeatures().IsR6();
5498 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005499 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5500 : ZERO;
5501
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005502 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005503 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005504 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005505 uint32_t offset =
5506 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005507 __ LoadFromOffset(kLoadWord,
5508 temp.AsRegister<Register>(),
5509 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005510 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005511 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005513 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005514 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005515 break;
5516 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5517 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5518 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005519 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5520 if (is_r6) {
5521 uint32_t offset = invoke->GetDexCacheArrayOffset();
5522 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5523 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
5524 bool reordering = __ SetReorder(false);
5525 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
5526 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
5527 __ SetReorder(reordering);
5528 } else {
5529 HMipsDexCacheArraysBase* base =
5530 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5531 int32_t offset =
5532 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5533 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5534 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005535 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005537 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005538 Register reg = temp.AsRegister<Register>();
5539 Register method_reg;
5540 if (current_method.IsRegister()) {
5541 method_reg = current_method.AsRegister<Register>();
5542 } else {
5543 // TODO: use the appropriate DCHECK() here if possible.
5544 // DCHECK(invoke->GetLocations()->Intrinsified());
5545 DCHECK(!current_method.IsValid());
5546 method_reg = reg;
5547 __ Lw(reg, SP, kCurrentMethodStackOffset);
5548 }
5549
5550 // temp = temp->dex_cache_resolved_methods_;
5551 __ LoadFromOffset(kLoadWord,
5552 reg,
5553 method_reg,
5554 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005555 // temp = temp[index_in_cache];
5556 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5557 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005558 __ LoadFromOffset(kLoadWord,
5559 reg,
5560 reg,
5561 CodeGenerator::GetCachePointerOffset(index_in_cache));
5562 break;
5563 }
5564 }
5565
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005566 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005567 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005568 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005569 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005570 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5571 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005572 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005573 T9,
5574 callee_method.AsRegister<Register>(),
5575 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005576 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005577 // T9()
5578 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005579 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580 break;
5581 }
5582 DCHECK(!IsLeafMethod());
5583}
5584
5585void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005586 // Explicit clinit checks triggered by static invokes must have been pruned by
5587 // art::PrepareForRegisterAllocation.
5588 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005589
5590 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5591 return;
5592 }
5593
5594 LocationSummary* locations = invoke->GetLocations();
5595 codegen_->GenerateStaticOrDirectCall(invoke,
5596 locations->HasTemps()
5597 ? locations->GetTemp(0)
5598 : Location::NoLocation());
5599 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5600}
5601
Chris Larsen3acee732015-11-18 13:31:08 -08005602void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005603 // Use the calling convention instead of the location of the receiver, as
5604 // intrinsics may have put the receiver in a different register. In the intrinsics
5605 // slow path, the arguments have been moved to the right place, so here we are
5606 // guaranteed that the receiver is the first register of the calling convention.
5607 InvokeDexCallingConvention calling_convention;
5608 Register receiver = calling_convention.GetRegisterAt(0);
5609
Chris Larsen3acee732015-11-18 13:31:08 -08005610 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005611 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5612 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5613 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005614 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005615
5616 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005617 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005618 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005619 // Instead of simply (possibly) unpoisoning `temp` here, we should
5620 // emit a read barrier for the previous class reference load.
5621 // However this is not required in practice, as this is an
5622 // intermediate/temporary reference and because the current
5623 // concurrent copying collector keeps the from-space memory
5624 // intact/accessible until the end of the marking phase (the
5625 // concurrent copying collector may not in the future).
5626 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005627 // temp = temp->GetMethodAt(method_offset);
5628 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5629 // T9 = temp->GetEntryPoint();
5630 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5631 // T9();
5632 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005633 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005634}
5635
5636void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5637 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5638 return;
5639 }
5640
5641 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005642 DCHECK(!codegen_->IsLeafMethod());
5643 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5644}
5645
5646void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005647 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5648 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005649 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005650 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005651 cls,
5652 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005653 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005654 return;
5655 }
Vladimir Marko41559982017-01-06 14:04:23 +00005656 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005657
5658 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5659 ? LocationSummary::kCallOnSlowPath
5660 : LocationSummary::kNoCall;
5661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005662 switch (load_kind) {
5663 // We need an extra register for PC-relative literals on R2.
5664 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005665 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005666 case HLoadClass::LoadKind::kBootImageAddress:
5667 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005668 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5669 break;
5670 }
5671 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005672 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005673 locations->SetInAt(0, Location::RequiresRegister());
5674 break;
5675 default:
5676 break;
5677 }
5678 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005679}
5680
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005681// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5682// move.
5683void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005684 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5685 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5686 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005687 return;
5688 }
Vladimir Marko41559982017-01-06 14:04:23 +00005689 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005690
Vladimir Marko41559982017-01-06 14:04:23 +00005691 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005692 Location out_loc = locations->Out();
5693 Register out = out_loc.AsRegister<Register>();
5694 Register base_or_current_method_reg;
5695 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5696 switch (load_kind) {
5697 // We need an extra register for PC-relative literals on R2.
5698 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005699 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005700 case HLoadClass::LoadKind::kBootImageAddress:
5701 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005702 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5703 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005704 case HLoadClass::LoadKind::kReferrersClass:
5705 case HLoadClass::LoadKind::kDexCacheViaMethod:
5706 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5707 break;
5708 default:
5709 base_or_current_method_reg = ZERO;
5710 break;
5711 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005712
Alexey Frunze06a46c42016-07-19 15:00:40 -07005713 bool generate_null_check = false;
5714 switch (load_kind) {
5715 case HLoadClass::LoadKind::kReferrersClass: {
5716 DCHECK(!cls->CanCallRuntime());
5717 DCHECK(!cls->MustGenerateClinitCheck());
5718 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5719 GenerateGcRootFieldLoad(cls,
5720 out_loc,
5721 base_or_current_method_reg,
5722 ArtMethod::DeclaringClassOffset().Int32Value());
5723 break;
5724 }
5725 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005726 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005727 __ LoadLiteral(out,
5728 base_or_current_method_reg,
5729 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5730 cls->GetTypeIndex()));
5731 break;
5732 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005733 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005734 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5735 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005736 bool reordering = __ SetReorder(false);
5737 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5738 __ Addiu(out, out, /* placeholder */ 0x5678);
5739 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005740 break;
5741 }
5742 case HLoadClass::LoadKind::kBootImageAddress: {
5743 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005744 uint32_t address = dchecked_integral_cast<uint32_t>(
5745 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5746 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005747 __ LoadLiteral(out,
5748 base_or_current_method_reg,
5749 codegen_->DeduplicateBootImageAddressLiteral(address));
5750 break;
5751 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005752 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005753 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005754 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005755 bool reordering = __ SetReorder(false);
5756 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08005757 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005758 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005759 generate_null_check = true;
5760 break;
5761 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005762 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08005763 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
5764 cls->GetTypeIndex(),
5765 cls->GetClass());
5766 bool reordering = __ SetReorder(false);
5767 __ Bind(&info->high_label);
5768 __ Lui(out, /* placeholder */ 0x1234);
5769 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
5770 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005771 break;
5772 }
Vladimir Marko41559982017-01-06 14:04:23 +00005773 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005774 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005775 LOG(FATAL) << "UNREACHABLE";
5776 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005777 }
5778
5779 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5780 DCHECK(cls->CanCallRuntime());
5781 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5782 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5783 codegen_->AddSlowPath(slow_path);
5784 if (generate_null_check) {
5785 __ Beqz(out, slow_path->GetEntryLabel());
5786 }
5787 if (cls->MustGenerateClinitCheck()) {
5788 GenerateClassInitializationCheck(slow_path, out);
5789 } else {
5790 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005791 }
5792 }
5793}
5794
5795static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005796 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005797}
5798
5799void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5800 LocationSummary* locations =
5801 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5802 locations->SetOut(Location::RequiresRegister());
5803}
5804
5805void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5806 Register out = load->GetLocations()->Out().AsRegister<Register>();
5807 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5808}
5809
5810void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5811 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5812}
5813
5814void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5815 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5816}
5817
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005818void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005819 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005821 HLoadString::LoadKind load_kind = load->GetLoadKind();
5822 switch (load_kind) {
5823 // We need an extra register for PC-relative literals on R2.
5824 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5825 case HLoadString::LoadKind::kBootImageAddress:
5826 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005827 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005828 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5829 break;
5830 }
5831 FALLTHROUGH_INTENDED;
5832 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005833 case HLoadString::LoadKind::kDexCacheViaMethod:
5834 locations->SetInAt(0, Location::RequiresRegister());
5835 break;
5836 default:
5837 break;
5838 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005839 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5840 InvokeRuntimeCallingConvention calling_convention;
5841 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5842 } else {
5843 locations->SetOut(Location::RequiresRegister());
5844 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005845}
5846
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005847// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5848// move.
5849void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005850 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005851 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005852 Location out_loc = locations->Out();
5853 Register out = out_loc.AsRegister<Register>();
5854 Register base_or_current_method_reg;
5855 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5856 switch (load_kind) {
5857 // We need an extra register for PC-relative literals on R2.
5858 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5859 case HLoadString::LoadKind::kBootImageAddress:
5860 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005861 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005862 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5863 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005864 default:
5865 base_or_current_method_reg = ZERO;
5866 break;
5867 }
5868
5869 switch (load_kind) {
5870 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005871 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005872 __ LoadLiteral(out,
5873 base_or_current_method_reg,
5874 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5875 load->GetStringIndex()));
5876 return; // No dex cache slow path.
5877 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005878 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005879 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005880 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005881 bool reordering = __ SetReorder(false);
5882 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5883 __ Addiu(out, out, /* placeholder */ 0x5678);
5884 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005885 return; // No dex cache slow path.
5886 }
5887 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005888 uint32_t address = dchecked_integral_cast<uint32_t>(
5889 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5890 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005891 __ LoadLiteral(out,
5892 base_or_current_method_reg,
5893 codegen_->DeduplicateBootImageAddressLiteral(address));
5894 return; // No dex cache slow path.
5895 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005896 case HLoadString::LoadKind::kBssEntry: {
5897 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5898 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005899 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005900 bool reordering = __ SetReorder(false);
5901 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08005902 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005903 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005904 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5905 codegen_->AddSlowPath(slow_path);
5906 __ Beqz(out, slow_path->GetEntryLabel());
5907 __ Bind(slow_path->GetExitLabel());
5908 return;
5909 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005910 case HLoadString::LoadKind::kJitTableAddress: {
5911 CodeGeneratorMIPS::JitPatchInfo* info =
5912 codegen_->NewJitRootStringPatch(load->GetDexFile(),
5913 load->GetStringIndex(),
5914 load->GetString());
5915 bool reordering = __ SetReorder(false);
5916 __ Bind(&info->high_label);
5917 __ Lui(out, /* placeholder */ 0x1234);
5918 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
5919 __ SetReorder(reordering);
5920 return;
5921 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005922 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005923 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005924 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005925
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005926 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005927 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5928 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005929 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005930 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5931 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005932}
5933
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005934void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5936 locations->SetOut(Location::ConstantLocation(constant));
5937}
5938
5939void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5940 // Will be generated at use site.
5941}
5942
5943void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5944 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005946 InvokeRuntimeCallingConvention calling_convention;
5947 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5948}
5949
5950void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5951 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005952 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005953 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5954 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005955 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005956 }
5957 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5958}
5959
5960void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5961 LocationSummary* locations =
5962 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5963 switch (mul->GetResultType()) {
5964 case Primitive::kPrimInt:
5965 case Primitive::kPrimLong:
5966 locations->SetInAt(0, Location::RequiresRegister());
5967 locations->SetInAt(1, Location::RequiresRegister());
5968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5969 break;
5970
5971 case Primitive::kPrimFloat:
5972 case Primitive::kPrimDouble:
5973 locations->SetInAt(0, Location::RequiresFpuRegister());
5974 locations->SetInAt(1, Location::RequiresFpuRegister());
5975 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5976 break;
5977
5978 default:
5979 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5980 }
5981}
5982
5983void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5984 Primitive::Type type = instruction->GetType();
5985 LocationSummary* locations = instruction->GetLocations();
5986 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5987
5988 switch (type) {
5989 case Primitive::kPrimInt: {
5990 Register dst = locations->Out().AsRegister<Register>();
5991 Register lhs = locations->InAt(0).AsRegister<Register>();
5992 Register rhs = locations->InAt(1).AsRegister<Register>();
5993
5994 if (isR6) {
5995 __ MulR6(dst, lhs, rhs);
5996 } else {
5997 __ MulR2(dst, lhs, rhs);
5998 }
5999 break;
6000 }
6001 case Primitive::kPrimLong: {
6002 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6003 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6004 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6005 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
6006 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
6007 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
6008
6009 // Extra checks to protect caused by the existance of A1_A2.
6010 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
6011 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
6012 DCHECK_NE(dst_high, lhs_low);
6013 DCHECK_NE(dst_high, rhs_low);
6014
6015 // A_B * C_D
6016 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
6017 // dst_lo: [ low(B*D) ]
6018 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
6019
6020 if (isR6) {
6021 __ MulR6(TMP, lhs_high, rhs_low);
6022 __ MulR6(dst_high, lhs_low, rhs_high);
6023 __ Addu(dst_high, dst_high, TMP);
6024 __ MuhuR6(TMP, lhs_low, rhs_low);
6025 __ Addu(dst_high, dst_high, TMP);
6026 __ MulR6(dst_low, lhs_low, rhs_low);
6027 } else {
6028 __ MulR2(TMP, lhs_high, rhs_low);
6029 __ MulR2(dst_high, lhs_low, rhs_high);
6030 __ Addu(dst_high, dst_high, TMP);
6031 __ MultuR2(lhs_low, rhs_low);
6032 __ Mfhi(TMP);
6033 __ Addu(dst_high, dst_high, TMP);
6034 __ Mflo(dst_low);
6035 }
6036 break;
6037 }
6038 case Primitive::kPrimFloat:
6039 case Primitive::kPrimDouble: {
6040 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6041 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
6042 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
6043 if (type == Primitive::kPrimFloat) {
6044 __ MulS(dst, lhs, rhs);
6045 } else {
6046 __ MulD(dst, lhs, rhs);
6047 }
6048 break;
6049 }
6050 default:
6051 LOG(FATAL) << "Unexpected mul type " << type;
6052 }
6053}
6054
6055void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
6056 LocationSummary* locations =
6057 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6058 switch (neg->GetResultType()) {
6059 case Primitive::kPrimInt:
6060 case Primitive::kPrimLong:
6061 locations->SetInAt(0, Location::RequiresRegister());
6062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6063 break;
6064
6065 case Primitive::kPrimFloat:
6066 case Primitive::kPrimDouble:
6067 locations->SetInAt(0, Location::RequiresFpuRegister());
6068 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6069 break;
6070
6071 default:
6072 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6073 }
6074}
6075
6076void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
6077 Primitive::Type type = instruction->GetType();
6078 LocationSummary* locations = instruction->GetLocations();
6079
6080 switch (type) {
6081 case Primitive::kPrimInt: {
6082 Register dst = locations->Out().AsRegister<Register>();
6083 Register src = locations->InAt(0).AsRegister<Register>();
6084 __ Subu(dst, ZERO, src);
6085 break;
6086 }
6087 case Primitive::kPrimLong: {
6088 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6089 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6090 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6091 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6092 __ Subu(dst_low, ZERO, src_low);
6093 __ Sltu(TMP, ZERO, dst_low);
6094 __ Subu(dst_high, ZERO, src_high);
6095 __ Subu(dst_high, dst_high, TMP);
6096 break;
6097 }
6098 case Primitive::kPrimFloat:
6099 case Primitive::kPrimDouble: {
6100 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6101 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6102 if (type == Primitive::kPrimFloat) {
6103 __ NegS(dst, src);
6104 } else {
6105 __ NegD(dst, src);
6106 }
6107 break;
6108 }
6109 default:
6110 LOG(FATAL) << "Unexpected neg type " << type;
6111 }
6112}
6113
6114void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
6115 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006117 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006118 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006119 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6120 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006121}
6122
6123void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006124 // Note: if heap poisoning is enabled, the entry point takes care
6125 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006126 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
6127 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006128}
6129
6130void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
6131 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006133 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006134 if (instruction->IsStringAlloc()) {
6135 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6136 } else {
6137 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006138 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006139 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6140}
6141
6142void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006143 // Note: if heap poisoning is enabled, the entry point takes care
6144 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006145 if (instruction->IsStringAlloc()) {
6146 // String is allocated through StringFactory. Call NewEmptyString entry point.
6147 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006148 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006149 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6150 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6151 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006152 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006153 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6154 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006155 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006156 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006157 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006158}
6159
6160void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6161 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6162 locations->SetInAt(0, Location::RequiresRegister());
6163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6164}
6165
6166void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6167 Primitive::Type type = instruction->GetType();
6168 LocationSummary* locations = instruction->GetLocations();
6169
6170 switch (type) {
6171 case Primitive::kPrimInt: {
6172 Register dst = locations->Out().AsRegister<Register>();
6173 Register src = locations->InAt(0).AsRegister<Register>();
6174 __ Nor(dst, src, ZERO);
6175 break;
6176 }
6177
6178 case Primitive::kPrimLong: {
6179 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6180 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6181 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6182 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6183 __ Nor(dst_high, src_high, ZERO);
6184 __ Nor(dst_low, src_low, ZERO);
6185 break;
6186 }
6187
6188 default:
6189 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6190 }
6191}
6192
6193void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6194 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6195 locations->SetInAt(0, Location::RequiresRegister());
6196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6197}
6198
6199void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6200 LocationSummary* locations = instruction->GetLocations();
6201 __ Xori(locations->Out().AsRegister<Register>(),
6202 locations->InAt(0).AsRegister<Register>(),
6203 1);
6204}
6205
6206void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006207 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6208 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006209}
6210
Calin Juravle2ae48182016-03-16 14:05:09 +00006211void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6212 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006213 return;
6214 }
6215 Location obj = instruction->GetLocations()->InAt(0);
6216
6217 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006218 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006219}
6220
Calin Juravle2ae48182016-03-16 14:05:09 +00006221void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006222 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006223 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006224
6225 Location obj = instruction->GetLocations()->InAt(0);
6226
6227 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6228}
6229
6230void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006231 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006232}
6233
6234void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6235 HandleBinaryOp(instruction);
6236}
6237
6238void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6239 HandleBinaryOp(instruction);
6240}
6241
6242void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6243 LOG(FATAL) << "Unreachable";
6244}
6245
6246void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6247 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6248}
6249
6250void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6252 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6253 if (location.IsStackSlot()) {
6254 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6255 } else if (location.IsDoubleStackSlot()) {
6256 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6257 }
6258 locations->SetOut(location);
6259}
6260
6261void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6262 ATTRIBUTE_UNUSED) {
6263 // Nothing to do, the parameter is already at its location.
6264}
6265
6266void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6267 LocationSummary* locations =
6268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6269 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6270}
6271
6272void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6273 ATTRIBUTE_UNUSED) {
6274 // Nothing to do, the method is already at its location.
6275}
6276
6277void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006279 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006280 locations->SetInAt(i, Location::Any());
6281 }
6282 locations->SetOut(Location::Any());
6283}
6284
6285void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6286 LOG(FATAL) << "Unreachable";
6287}
6288
6289void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6290 Primitive::Type type = rem->GetResultType();
6291 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006292 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6294
6295 switch (type) {
6296 case Primitive::kPrimInt:
6297 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006298 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6300 break;
6301
6302 case Primitive::kPrimLong: {
6303 InvokeRuntimeCallingConvention calling_convention;
6304 locations->SetInAt(0, Location::RegisterPairLocation(
6305 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6306 locations->SetInAt(1, Location::RegisterPairLocation(
6307 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6308 locations->SetOut(calling_convention.GetReturnLocation(type));
6309 break;
6310 }
6311
6312 case Primitive::kPrimFloat:
6313 case Primitive::kPrimDouble: {
6314 InvokeRuntimeCallingConvention calling_convention;
6315 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6316 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6317 locations->SetOut(calling_convention.GetReturnLocation(type));
6318 break;
6319 }
6320
6321 default:
6322 LOG(FATAL) << "Unexpected rem type " << type;
6323 }
6324}
6325
6326void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6327 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006328
6329 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006330 case Primitive::kPrimInt:
6331 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006332 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006333 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006334 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006335 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6336 break;
6337 }
6338 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006339 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006340 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006341 break;
6342 }
6343 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006344 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006345 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006346 break;
6347 }
6348 default:
6349 LOG(FATAL) << "Unexpected rem type " << type;
6350 }
6351}
6352
6353void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6354 memory_barrier->SetLocations(nullptr);
6355}
6356
6357void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6358 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6359}
6360
6361void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6363 Primitive::Type return_type = ret->InputAt(0)->GetType();
6364 locations->SetInAt(0, MipsReturnLocation(return_type));
6365}
6366
6367void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6368 codegen_->GenerateFrameExit();
6369}
6370
6371void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6372 ret->SetLocations(nullptr);
6373}
6374
6375void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6376 codegen_->GenerateFrameExit();
6377}
6378
Alexey Frunze92d90602015-12-18 18:16:36 -08006379void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6380 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006381}
6382
Alexey Frunze92d90602015-12-18 18:16:36 -08006383void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6384 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006385}
6386
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6388 HandleShift(shl);
6389}
6390
6391void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6392 HandleShift(shl);
6393}
6394
6395void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6396 HandleShift(shr);
6397}
6398
6399void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6400 HandleShift(shr);
6401}
6402
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6404 HandleBinaryOp(instruction);
6405}
6406
6407void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6408 HandleBinaryOp(instruction);
6409}
6410
6411void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6412 HandleFieldGet(instruction, instruction->GetFieldInfo());
6413}
6414
6415void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6416 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6417}
6418
6419void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6420 HandleFieldSet(instruction, instruction->GetFieldInfo());
6421}
6422
6423void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006424 HandleFieldSet(instruction,
6425 instruction->GetFieldInfo(),
6426 instruction->GetDexPc(),
6427 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006428}
6429
6430void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6431 HUnresolvedInstanceFieldGet* instruction) {
6432 FieldAccessCallingConventionMIPS calling_convention;
6433 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6434 instruction->GetFieldType(),
6435 calling_convention);
6436}
6437
6438void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6439 HUnresolvedInstanceFieldGet* instruction) {
6440 FieldAccessCallingConventionMIPS calling_convention;
6441 codegen_->GenerateUnresolvedFieldAccess(instruction,
6442 instruction->GetFieldType(),
6443 instruction->GetFieldIndex(),
6444 instruction->GetDexPc(),
6445 calling_convention);
6446}
6447
6448void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6449 HUnresolvedInstanceFieldSet* instruction) {
6450 FieldAccessCallingConventionMIPS calling_convention;
6451 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6452 instruction->GetFieldType(),
6453 calling_convention);
6454}
6455
6456void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6457 HUnresolvedInstanceFieldSet* instruction) {
6458 FieldAccessCallingConventionMIPS calling_convention;
6459 codegen_->GenerateUnresolvedFieldAccess(instruction,
6460 instruction->GetFieldType(),
6461 instruction->GetFieldIndex(),
6462 instruction->GetDexPc(),
6463 calling_convention);
6464}
6465
6466void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6467 HUnresolvedStaticFieldGet* instruction) {
6468 FieldAccessCallingConventionMIPS calling_convention;
6469 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6470 instruction->GetFieldType(),
6471 calling_convention);
6472}
6473
6474void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6475 HUnresolvedStaticFieldGet* instruction) {
6476 FieldAccessCallingConventionMIPS calling_convention;
6477 codegen_->GenerateUnresolvedFieldAccess(instruction,
6478 instruction->GetFieldType(),
6479 instruction->GetFieldIndex(),
6480 instruction->GetDexPc(),
6481 calling_convention);
6482}
6483
6484void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6485 HUnresolvedStaticFieldSet* instruction) {
6486 FieldAccessCallingConventionMIPS calling_convention;
6487 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6488 instruction->GetFieldType(),
6489 calling_convention);
6490}
6491
6492void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6493 HUnresolvedStaticFieldSet* instruction) {
6494 FieldAccessCallingConventionMIPS calling_convention;
6495 codegen_->GenerateUnresolvedFieldAccess(instruction,
6496 instruction->GetFieldType(),
6497 instruction->GetFieldIndex(),
6498 instruction->GetDexPc(),
6499 calling_convention);
6500}
6501
6502void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006503 LocationSummary* locations =
6504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006505 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506}
6507
6508void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6509 HBasicBlock* block = instruction->GetBlock();
6510 if (block->GetLoopInformation() != nullptr) {
6511 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6512 // The back edge will generate the suspend check.
6513 return;
6514 }
6515 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6516 // The goto will generate the suspend check.
6517 return;
6518 }
6519 GenerateSuspendCheck(instruction, nullptr);
6520}
6521
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6523 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006525 InvokeRuntimeCallingConvention calling_convention;
6526 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6527}
6528
6529void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006530 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006531 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6532}
6533
6534void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6535 Primitive::Type input_type = conversion->GetInputType();
6536 Primitive::Type result_type = conversion->GetResultType();
6537 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006538 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006539
6540 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6541 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6542 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6543 }
6544
6545 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006546 if (!isR6 &&
6547 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6548 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006549 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006550 }
6551
6552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6553
6554 if (call_kind == LocationSummary::kNoCall) {
6555 if (Primitive::IsFloatingPointType(input_type)) {
6556 locations->SetInAt(0, Location::RequiresFpuRegister());
6557 } else {
6558 locations->SetInAt(0, Location::RequiresRegister());
6559 }
6560
6561 if (Primitive::IsFloatingPointType(result_type)) {
6562 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6563 } else {
6564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6565 }
6566 } else {
6567 InvokeRuntimeCallingConvention calling_convention;
6568
6569 if (Primitive::IsFloatingPointType(input_type)) {
6570 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6571 } else {
6572 DCHECK_EQ(input_type, Primitive::kPrimLong);
6573 locations->SetInAt(0, Location::RegisterPairLocation(
6574 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6575 }
6576
6577 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6578 }
6579}
6580
6581void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6582 LocationSummary* locations = conversion->GetLocations();
6583 Primitive::Type result_type = conversion->GetResultType();
6584 Primitive::Type input_type = conversion->GetInputType();
6585 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006586 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006587
6588 DCHECK_NE(input_type, result_type);
6589
6590 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6591 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6592 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6593 Register src = locations->InAt(0).AsRegister<Register>();
6594
Alexey Frunzea871ef12016-06-27 15:20:11 -07006595 if (dst_low != src) {
6596 __ Move(dst_low, src);
6597 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006598 __ Sra(dst_high, src, 31);
6599 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6600 Register dst = locations->Out().AsRegister<Register>();
6601 Register src = (input_type == Primitive::kPrimLong)
6602 ? locations->InAt(0).AsRegisterPairLow<Register>()
6603 : locations->InAt(0).AsRegister<Register>();
6604
6605 switch (result_type) {
6606 case Primitive::kPrimChar:
6607 __ Andi(dst, src, 0xFFFF);
6608 break;
6609 case Primitive::kPrimByte:
6610 if (has_sign_extension) {
6611 __ Seb(dst, src);
6612 } else {
6613 __ Sll(dst, src, 24);
6614 __ Sra(dst, dst, 24);
6615 }
6616 break;
6617 case Primitive::kPrimShort:
6618 if (has_sign_extension) {
6619 __ Seh(dst, src);
6620 } else {
6621 __ Sll(dst, src, 16);
6622 __ Sra(dst, dst, 16);
6623 }
6624 break;
6625 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006626 if (dst != src) {
6627 __ Move(dst, src);
6628 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006629 break;
6630
6631 default:
6632 LOG(FATAL) << "Unexpected type conversion from " << input_type
6633 << " to " << result_type;
6634 }
6635 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006636 if (input_type == Primitive::kPrimLong) {
6637 if (isR6) {
6638 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6639 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6640 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6641 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6642 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6643 __ Mtc1(src_low, FTMP);
6644 __ Mthc1(src_high, FTMP);
6645 if (result_type == Primitive::kPrimFloat) {
6646 __ Cvtsl(dst, FTMP);
6647 } else {
6648 __ Cvtdl(dst, FTMP);
6649 }
6650 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006651 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6652 : kQuickL2d;
6653 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006654 if (result_type == Primitive::kPrimFloat) {
6655 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6656 } else {
6657 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6658 }
6659 }
6660 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006661 Register src = locations->InAt(0).AsRegister<Register>();
6662 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6663 __ Mtc1(src, FTMP);
6664 if (result_type == Primitive::kPrimFloat) {
6665 __ Cvtsw(dst, FTMP);
6666 } else {
6667 __ Cvtdw(dst, FTMP);
6668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006669 }
6670 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6671 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006672 if (result_type == Primitive::kPrimLong) {
6673 if (isR6) {
6674 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6675 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6676 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6677 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6678 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6679 MipsLabel truncate;
6680 MipsLabel done;
6681
6682 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6683 // value when the input is either a NaN or is outside of the range of the output type
6684 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6685 // the same result.
6686 //
6687 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6688 // value of the output type if the input is outside of the range after the truncation or
6689 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6690 // results. This matches the desired float/double-to-int/long conversion exactly.
6691 //
6692 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6693 //
6694 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6695 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6696 // even though it must be NAN2008=1 on R6.
6697 //
6698 // The code takes care of the different behaviors by first comparing the input to the
6699 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6700 // If the input is greater than or equal to the minimum, it procedes to the truncate
6701 // instruction, which will handle such an input the same way irrespective of NAN2008.
6702 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6703 // in order to return either zero or the minimum value.
6704 //
6705 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6706 // truncate instruction for MIPS64R6.
6707 if (input_type == Primitive::kPrimFloat) {
6708 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6709 __ LoadConst32(TMP, min_val);
6710 __ Mtc1(TMP, FTMP);
6711 __ CmpLeS(FTMP, FTMP, src);
6712 } else {
6713 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6714 __ LoadConst32(TMP, High32Bits(min_val));
6715 __ Mtc1(ZERO, FTMP);
6716 __ Mthc1(TMP, FTMP);
6717 __ CmpLeD(FTMP, FTMP, src);
6718 }
6719
6720 __ Bc1nez(FTMP, &truncate);
6721
6722 if (input_type == Primitive::kPrimFloat) {
6723 __ CmpEqS(FTMP, src, src);
6724 } else {
6725 __ CmpEqD(FTMP, src, src);
6726 }
6727 __ Move(dst_low, ZERO);
6728 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6729 __ Mfc1(TMP, FTMP);
6730 __ And(dst_high, dst_high, TMP);
6731
6732 __ B(&done);
6733
6734 __ Bind(&truncate);
6735
6736 if (input_type == Primitive::kPrimFloat) {
6737 __ TruncLS(FTMP, src);
6738 } else {
6739 __ TruncLD(FTMP, src);
6740 }
6741 __ Mfc1(dst_low, FTMP);
6742 __ Mfhc1(dst_high, FTMP);
6743
6744 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006745 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006746 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6747 : kQuickD2l;
6748 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006749 if (input_type == Primitive::kPrimFloat) {
6750 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6751 } else {
6752 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6753 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006754 }
6755 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006756 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6757 Register dst = locations->Out().AsRegister<Register>();
6758 MipsLabel truncate;
6759 MipsLabel done;
6760
6761 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6762 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6763 // even though it must be NAN2008=1 on R6.
6764 //
6765 // For details see the large comment above for the truncation of float/double to long on R6.
6766 //
6767 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6768 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006769 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006770 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6771 __ LoadConst32(TMP, min_val);
6772 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006773 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006774 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6775 __ LoadConst32(TMP, High32Bits(min_val));
6776 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006777 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006778 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006779
6780 if (isR6) {
6781 if (input_type == Primitive::kPrimFloat) {
6782 __ CmpLeS(FTMP, FTMP, src);
6783 } else {
6784 __ CmpLeD(FTMP, FTMP, src);
6785 }
6786 __ Bc1nez(FTMP, &truncate);
6787
6788 if (input_type == Primitive::kPrimFloat) {
6789 __ CmpEqS(FTMP, src, src);
6790 } else {
6791 __ CmpEqD(FTMP, src, src);
6792 }
6793 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6794 __ Mfc1(TMP, FTMP);
6795 __ And(dst, dst, TMP);
6796 } else {
6797 if (input_type == Primitive::kPrimFloat) {
6798 __ ColeS(0, FTMP, src);
6799 } else {
6800 __ ColeD(0, FTMP, src);
6801 }
6802 __ Bc1t(0, &truncate);
6803
6804 if (input_type == Primitive::kPrimFloat) {
6805 __ CeqS(0, src, src);
6806 } else {
6807 __ CeqD(0, src, src);
6808 }
6809 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6810 __ Movf(dst, ZERO, 0);
6811 }
6812
6813 __ B(&done);
6814
6815 __ Bind(&truncate);
6816
6817 if (input_type == Primitive::kPrimFloat) {
6818 __ TruncWS(FTMP, src);
6819 } else {
6820 __ TruncWD(FTMP, src);
6821 }
6822 __ Mfc1(dst, FTMP);
6823
6824 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006825 }
6826 } else if (Primitive::IsFloatingPointType(result_type) &&
6827 Primitive::IsFloatingPointType(input_type)) {
6828 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6829 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6830 if (result_type == Primitive::kPrimFloat) {
6831 __ Cvtsd(dst, src);
6832 } else {
6833 __ Cvtds(dst, src);
6834 }
6835 } else {
6836 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6837 << " to " << result_type;
6838 }
6839}
6840
6841void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6842 HandleShift(ushr);
6843}
6844
6845void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6846 HandleShift(ushr);
6847}
6848
6849void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6850 HandleBinaryOp(instruction);
6851}
6852
6853void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6854 HandleBinaryOp(instruction);
6855}
6856
6857void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6858 // Nothing to do, this should be removed during prepare for register allocator.
6859 LOG(FATAL) << "Unreachable";
6860}
6861
6862void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6863 // Nothing to do, this should be removed during prepare for register allocator.
6864 LOG(FATAL) << "Unreachable";
6865}
6866
6867void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006868 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006869}
6870
6871void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006872 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006873}
6874
6875void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006876 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006877}
6878
6879void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006880 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006881}
6882
6883void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006884 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006885}
6886
6887void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006888 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006889}
6890
6891void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006892 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006893}
6894
6895void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006896 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006897}
6898
6899void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006900 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006901}
6902
6903void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006904 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006905}
6906
6907void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006908 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006909}
6910
6911void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006912 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006913}
6914
6915void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006916 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006917}
6918
6919void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006920 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006921}
6922
6923void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006924 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006925}
6926
6927void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006928 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006929}
6930
6931void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006932 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006933}
6934
6935void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006937}
6938
6939void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006941}
6942
6943void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006945}
6946
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006947void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6948 LocationSummary* locations =
6949 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6950 locations->SetInAt(0, Location::RequiresRegister());
6951}
6952
Alexey Frunze96b66822016-09-10 02:32:44 -07006953void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6954 int32_t lower_bound,
6955 uint32_t num_entries,
6956 HBasicBlock* switch_block,
6957 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006958 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006959 Register temp_reg = TMP;
6960 __ Addiu32(temp_reg, value_reg, -lower_bound);
6961 // Jump to default if index is negative
6962 // Note: We don't check the case that index is positive while value < lower_bound, because in
6963 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6964 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6965
Alexey Frunze96b66822016-09-10 02:32:44 -07006966 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006967 // Jump to successors[0] if value == lower_bound.
6968 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6969 int32_t last_index = 0;
6970 for (; num_entries - last_index > 2; last_index += 2) {
6971 __ Addiu(temp_reg, temp_reg, -2);
6972 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6973 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6974 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6975 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6976 }
6977 if (num_entries - last_index == 2) {
6978 // The last missing case_value.
6979 __ Addiu(temp_reg, temp_reg, -1);
6980 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006981 }
6982
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006983 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006984 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006985 __ B(codegen_->GetLabelOf(default_block));
6986 }
6987}
6988
Alexey Frunze96b66822016-09-10 02:32:44 -07006989void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6990 Register constant_area,
6991 int32_t lower_bound,
6992 uint32_t num_entries,
6993 HBasicBlock* switch_block,
6994 HBasicBlock* default_block) {
6995 // Create a jump table.
6996 std::vector<MipsLabel*> labels(num_entries);
6997 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6998 for (uint32_t i = 0; i < num_entries; i++) {
6999 labels[i] = codegen_->GetLabelOf(successors[i]);
7000 }
7001 JumpTable* table = __ CreateJumpTable(std::move(labels));
7002
7003 // Is the value in range?
7004 __ Addiu32(TMP, value_reg, -lower_bound);
7005 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
7006 __ Sltiu(AT, TMP, num_entries);
7007 __ Beqz(AT, codegen_->GetLabelOf(default_block));
7008 } else {
7009 __ LoadConst32(AT, num_entries);
7010 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
7011 }
7012
7013 // We are in the range of the table.
7014 // Load the target address from the jump table, indexing by the value.
7015 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
7016 __ Sll(TMP, TMP, 2);
7017 __ Addu(TMP, TMP, AT);
7018 __ Lw(TMP, TMP, 0);
7019 // Compute the absolute target address by adding the table start address
7020 // (the table contains offsets to targets relative to its start).
7021 __ Addu(TMP, TMP, AT);
7022 // And jump.
7023 __ Jr(TMP);
7024 __ NopIfNoReordering();
7025}
7026
7027void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7028 int32_t lower_bound = switch_instr->GetStartValue();
7029 uint32_t num_entries = switch_instr->GetNumEntries();
7030 LocationSummary* locations = switch_instr->GetLocations();
7031 Register value_reg = locations->InAt(0).AsRegister<Register>();
7032 HBasicBlock* switch_block = switch_instr->GetBlock();
7033 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7034
7035 if (codegen_->GetInstructionSetFeatures().IsR6() &&
7036 num_entries > kPackedSwitchJumpTableThreshold) {
7037 // R6 uses PC-relative addressing to access the jump table.
7038 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
7039 // the jump table and it is implemented by changing HPackedSwitch to
7040 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
7041 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
7042 GenTableBasedPackedSwitch(value_reg,
7043 ZERO,
7044 lower_bound,
7045 num_entries,
7046 switch_block,
7047 default_block);
7048 } else {
7049 GenPackedSwitchWithCompares(value_reg,
7050 lower_bound,
7051 num_entries,
7052 switch_block,
7053 default_block);
7054 }
7055}
7056
7057void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7058 LocationSummary* locations =
7059 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7060 locations->SetInAt(0, Location::RequiresRegister());
7061 // Constant area pointer (HMipsComputeBaseMethodAddress).
7062 locations->SetInAt(1, Location::RequiresRegister());
7063}
7064
7065void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7066 int32_t lower_bound = switch_instr->GetStartValue();
7067 uint32_t num_entries = switch_instr->GetNumEntries();
7068 LocationSummary* locations = switch_instr->GetLocations();
7069 Register value_reg = locations->InAt(0).AsRegister<Register>();
7070 Register constant_area = locations->InAt(1).AsRegister<Register>();
7071 HBasicBlock* switch_block = switch_instr->GetBlock();
7072 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7073
7074 // This is an R2-only path. HPackedSwitch has been changed to
7075 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
7076 // required to address the jump table relative to PC.
7077 GenTableBasedPackedSwitch(value_reg,
7078 constant_area,
7079 lower_bound,
7080 num_entries,
7081 switch_block,
7082 default_block);
7083}
7084
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007085void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
7086 HMipsComputeBaseMethodAddress* insn) {
7087 LocationSummary* locations =
7088 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7089 locations->SetOut(Location::RequiresRegister());
7090}
7091
7092void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
7093 HMipsComputeBaseMethodAddress* insn) {
7094 LocationSummary* locations = insn->GetLocations();
7095 Register reg = locations->Out().AsRegister<Register>();
7096
7097 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7098
7099 // Generate a dummy PC-relative call to obtain PC.
7100 __ Nal();
7101 // Grab the return address off RA.
7102 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007103 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007104
7105 // Remember this offset (the obtained PC value) for later use with constant area.
7106 __ BindPcRelBaseLabel();
7107}
7108
7109void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7110 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7111 locations->SetOut(Location::RequiresRegister());
7112}
7113
7114void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7115 Register reg = base->GetLocations()->Out().AsRegister<Register>();
7116 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7117 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007118 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7119 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007120 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007121 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
7122 __ Addiu(reg, reg, /* placeholder */ 0x5678);
7123 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007124}
7125
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007126void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7127 // The trampoline uses the same calling convention as dex calling conventions,
7128 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
7129 // the method_idx.
7130 HandleInvoke(invoke);
7131}
7132
7133void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7134 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
7135}
7136
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007137void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7138 LocationSummary* locations =
7139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7140 locations->SetInAt(0, Location::RequiresRegister());
7141 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007142}
7143
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007144void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7145 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007146 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007147 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007148 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007149 __ LoadFromOffset(kLoadWord,
7150 locations->Out().AsRegister<Register>(),
7151 locations->InAt(0).AsRegister<Register>(),
7152 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007153 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007154 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007155 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007156 __ LoadFromOffset(kLoadWord,
7157 locations->Out().AsRegister<Register>(),
7158 locations->InAt(0).AsRegister<Register>(),
7159 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007160 __ LoadFromOffset(kLoadWord,
7161 locations->Out().AsRegister<Register>(),
7162 locations->Out().AsRegister<Register>(),
7163 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007164 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007165}
7166
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007167#undef __
7168#undef QUICK_ENTRY_POINT
7169
7170} // namespace mips
7171} // namespace art