blob: 6828fed94481a54172f7d7adf68a155d1f728024 [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
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020043Location MipsReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 return Location::RegisterLocation(V0);
52
53 case Primitive::kPrimLong:
54 return Location::RegisterPairLocation(V0, V1);
55
56 case Primitive::kPrimFloat:
57 case Primitive::kPrimDouble:
58 return Location::FpuRegisterLocation(F0);
59
60 case Primitive::kPrimVoid:
61 return Location();
62 }
63 UNREACHABLE();
64}
65
66Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
67 return MipsReturnLocation(type);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
71 return Location::RegisterLocation(kMethodRegisterArgument);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
75 Location next_location;
76
77 switch (type) {
78 case Primitive::kPrimBoolean:
79 case Primitive::kPrimByte:
80 case Primitive::kPrimChar:
81 case Primitive::kPrimShort:
82 case Primitive::kPrimInt:
83 case Primitive::kPrimNot: {
84 uint32_t gp_index = gp_index_++;
85 if (gp_index < calling_convention.GetNumberOfRegisters()) {
86 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Location::StackSlot(stack_offset);
90 }
91 break;
92 }
93
94 case Primitive::kPrimLong: {
95 uint32_t gp_index = gp_index_;
96 gp_index_ += 2;
97 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -080098 Register reg = calling_convention.GetRegisterAt(gp_index);
99 if (reg == A1 || reg == A3) {
100 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200101 gp_index++;
102 }
103 Register low_even = calling_convention.GetRegisterAt(gp_index);
104 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
105 DCHECK_EQ(low_even + 1, high_odd);
106 next_location = Location::RegisterPairLocation(low_even, high_odd);
107 } else {
108 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
109 next_location = Location::DoubleStackSlot(stack_offset);
110 }
111 break;
112 }
113
114 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
115 // will take up the even/odd pair, while floats are stored in even regs only.
116 // On 64 bit FPU, both double and float are stored in even registers only.
117 case Primitive::kPrimFloat:
118 case Primitive::kPrimDouble: {
119 uint32_t float_index = float_index_++;
120 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
121 next_location = Location::FpuRegisterLocation(
122 calling_convention.GetFpuRegisterAt(float_index));
123 } else {
124 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
125 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
126 : Location::StackSlot(stack_offset);
127 }
128 break;
129 }
130
131 case Primitive::kPrimVoid:
132 LOG(FATAL) << "Unexpected parameter type " << type;
133 break;
134 }
135
136 // Space on the stack is reserved for all arguments.
137 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
138
139 return next_location;
140}
141
142Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
143 return MipsReturnLocation(type);
144}
145
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100146// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
147#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700148#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149
150class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
151 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000152 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200153
154 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
155 LocationSummary* locations = instruction_->GetLocations();
156 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
157 __ Bind(GetEntryLabel());
158 if (instruction_->CanThrowIntoCatchBlock()) {
159 // Live registers will be restored in the catch block if caught.
160 SaveLiveRegisters(codegen, instruction_->GetLocations());
161 }
162 // We're moving two locations to locations that could overlap, so we need a parallel
163 // move resolver.
164 InvokeRuntimeCallingConvention calling_convention;
165 codegen->EmitParallelMoves(locations->InAt(0),
166 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
167 Primitive::kPrimInt,
168 locations->InAt(1),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
170 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100171 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
172 ? kQuickThrowStringBounds
173 : kQuickThrowArrayBounds;
174 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
177 }
178
179 bool IsFatal() const OVERRIDE { return true; }
180
181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
182
183 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
185};
186
187class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
188 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000189 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190
191 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
192 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
193 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100194 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200195 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
196 }
197
198 bool IsFatal() const OVERRIDE { return true; }
199
200 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
201
202 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200203 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
204};
205
206class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
207 public:
208 LoadClassSlowPathMIPS(HLoadClass* cls,
209 HInstruction* at,
210 uint32_t dex_pc,
211 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000212 : SlowPathCodeMIPS(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200213 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
214 }
215
216 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000217 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700218 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700220 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
221 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
222 InvokeRuntimeCallingConvention calling_convention;
223 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
224 const bool is_load_class_bss_entry =
225 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200226 __ Bind(GetEntryLabel());
227 SaveLiveRegisters(codegen, locations);
228
Alexey Frunzec61c0762017-04-10 13:54:23 -0700229 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
230 Register entry_address = kNoRegister;
231 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
232 Register temp = locations->GetTemp(0).AsRegister<Register>();
233 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
234 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
235 // kSaveEverything call.
236 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
237 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
238 if (temp_is_a0) {
239 __ Move(entry_address, temp);
240 }
241 }
242
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000243 dex::TypeIndex type_index = cls_->GetTypeIndex();
244 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100245 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
246 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000247 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200248 if (do_clinit_) {
249 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
250 } else {
251 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
252 }
253
Alexey Frunzec61c0762017-04-10 13:54:23 -0700254 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
255 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
256 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
257 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
258 }
259
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200260 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200261 if (out.IsValid()) {
262 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264 mips_codegen->MoveLocation(out,
265 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
266 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200267 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200268 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700269
270 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
271 if (is_load_class_bss_entry && !r2_baker_or_no_read_barriers) {
272 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
273 // the class entry.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000274 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000275 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000276 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800277 bool reordering = __ SetReorder(false);
278 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
279 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
280 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 __ B(GetExitLabel());
283 }
284
285 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
286
287 private:
288 // The class this slow path will load.
289 HLoadClass* const cls_;
290
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200291 // The dex PC of `at_`.
292 const uint32_t dex_pc_;
293
294 // Whether to initialize the class.
295 const bool do_clinit_;
296
297 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
298};
299
300class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
301 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700305 DCHECK(instruction_->IsLoadString());
306 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 LocationSummary* locations = instruction_->GetLocations();
308 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700309 HLoadString* load = instruction_->AsLoadString();
310 const dex::StringIndex string_index = load->GetStringIndex();
311 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700313 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
314 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
315 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 __ Bind(GetEntryLabel());
317 SaveLiveRegisters(codegen, locations);
318
Alexey Frunzec61c0762017-04-10 13:54:23 -0700319 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
320 Register entry_address = kNoRegister;
321 if (r2_baker_or_no_read_barriers) {
322 Register temp = locations->GetTemp(0).AsRegister<Register>();
323 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
324 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
325 // kSaveEverything call.
326 entry_address = temp_is_a0 ? out : temp;
327 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
328 if (temp_is_a0) {
329 __ Move(entry_address, temp);
330 }
331 }
332
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000333 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100334 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700336
337 // Store the resolved string to the BSS entry.
338 if (r2_baker_or_no_read_barriers) {
339 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
340 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
341 }
342
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 Primitive::Type type = instruction_->GetType();
344 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700345 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200346 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348
Alexey Frunzec61c0762017-04-10 13:54:23 -0700349 // Store the resolved string to the BSS entry.
350 if (!r2_baker_or_no_read_barriers) {
351 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
352 // the string entry.
353 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
354 CodeGeneratorMIPS::PcRelativePatchInfo* info =
355 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
356 bool reordering = __ SetReorder(false);
357 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
358 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
359 __ SetReorder(reordering);
360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200361 __ B(GetExitLabel());
362 }
363
364 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
365
366 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
368};
369
370class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
371 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000372 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200373
374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
376 __ Bind(GetEntryLabel());
377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100381 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382 instruction_,
383 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100384 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
386 }
387
388 bool IsFatal() const OVERRIDE { return true; }
389
390 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
391
392 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200393 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
394};
395
396class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
397 public:
398 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000399 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
403 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100404 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 if (successor_ == nullptr) {
407 __ B(GetReturnLabel());
408 } else {
409 __ B(mips_codegen->GetLabelOf(successor_));
410 }
411 }
412
413 MipsLabel* GetReturnLabel() {
414 DCHECK(successor_ == nullptr);
415 return &return_label_;
416 }
417
418 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
419
420 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 // If not null, the block to branch to after the suspend check.
422 HBasicBlock* const successor_;
423
424 // If `successor_` is null, the label to branch to after the suspend check.
425 MipsLabel return_label_;
426
427 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
428};
429
430class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
431 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800432 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
433 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434
435 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
436 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437 uint32_t dex_pc = instruction_->GetDexPc();
438 DCHECK(instruction_->IsCheckCast()
439 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
440 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
441
442 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800443 if (!is_fatal_) {
444 SaveLiveRegisters(codegen, locations);
445 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446
447 // We're moving two locations to locations that could overlap, so we need a parallel
448 // move resolver.
449 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800450 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200451 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
452 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800453 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
455 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200456 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100457 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800458 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200459 Primitive::Type ret_type = instruction_->GetType();
460 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
461 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 } else {
463 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800464 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
465 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466 }
467
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800468 if (!is_fatal_) {
469 RestoreLiveRegisters(codegen, locations);
470 __ B(GetExitLabel());
471 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200472 }
473
474 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
475
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800476 bool IsFatal() const OVERRIDE { return is_fatal_; }
477
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200478 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800479 const bool is_fatal_;
480
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200481 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
482};
483
484class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
485 public:
Aart Bik42249c32016-01-07 15:33:50 -0800486 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000487 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200488
489 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800490 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200491 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100492 LocationSummary* locations = instruction_->GetLocations();
493 SaveLiveRegisters(codegen, locations);
494 InvokeRuntimeCallingConvention calling_convention;
495 __ LoadConst32(calling_convention.GetRegisterAt(0),
496 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100497 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100498 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 }
500
501 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
502
503 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200504 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
505};
506
Alexey Frunze15958152017-02-09 19:08:30 -0800507class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
508 public:
509 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
510
511 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
512 LocationSummary* locations = instruction_->GetLocations();
513 __ Bind(GetEntryLabel());
514 SaveLiveRegisters(codegen, locations);
515
516 InvokeRuntimeCallingConvention calling_convention;
517 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
518 parallel_move.AddMove(
519 locations->InAt(0),
520 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(1),
525 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimInt,
527 nullptr);
528 parallel_move.AddMove(
529 locations->InAt(2),
530 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
531 Primitive::kPrimNot,
532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534
535 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
536 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
537 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
538 RestoreLiveRegisters(codegen, locations);
539 __ B(GetExitLabel());
540 }
541
542 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
543
544 private:
545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
546};
547
548// Slow path marking an object reference `ref` during a read
549// barrier. The field `obj.field` in the object `obj` holding this
550// reference does not get updated by this slow path after marking (see
551// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
552//
553// This means that after the execution of this slow path, `ref` will
554// always be up-to-date, but `obj.field` may not; i.e., after the
555// flip, `ref` will be a to-space reference, but `obj.field` will
556// probably still be a from-space reference (unless it gets updated by
557// another thread, or if another thread installed another object
558// reference (different from `ref`) in `obj.field`).
559//
560// If `entrypoint` is a valid location it is assumed to already be
561// holding the entrypoint. The case where the entrypoint is passed in
562// is for the GcRoot read barrier.
563class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
564 public:
565 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
566 Location ref,
567 Location entrypoint = Location::NoLocation())
568 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
569 DCHECK(kEmitCompilerReadBarrier);
570 }
571
572 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
573
574 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
575 LocationSummary* locations = instruction_->GetLocations();
576 Register ref_reg = ref_.AsRegister<Register>();
577 DCHECK(locations->CanCall());
578 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
579 DCHECK(instruction_->IsInstanceFieldGet() ||
580 instruction_->IsStaticFieldGet() ||
581 instruction_->IsArrayGet() ||
582 instruction_->IsArraySet() ||
583 instruction_->IsLoadClass() ||
584 instruction_->IsLoadString() ||
585 instruction_->IsInstanceOf() ||
586 instruction_->IsCheckCast() ||
587 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
588 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
589 << "Unexpected instruction in read barrier marking slow path: "
590 << instruction_->DebugName();
591
592 __ Bind(GetEntryLabel());
593 // No need to save live registers; it's taken care of by the
594 // entrypoint. Also, there is no need to update the stack mask,
595 // as this runtime call will not trigger a garbage collection.
596 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
597 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
598 (S2 <= ref_reg && ref_reg <= S7) ||
599 (ref_reg == FP)) << ref_reg;
600 // "Compact" slow path, saving two moves.
601 //
602 // Instead of using the standard runtime calling convention (input
603 // and output in A0 and V0 respectively):
604 //
605 // A0 <- ref
606 // V0 <- ReadBarrierMark(A0)
607 // ref <- V0
608 //
609 // we just use rX (the register containing `ref`) as input and output
610 // of a dedicated entrypoint:
611 //
612 // rX <- ReadBarrierMarkRegX(rX)
613 //
614 if (entrypoint_.IsValid()) {
615 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
616 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
617 __ Jalr(entrypoint_.AsRegister<Register>());
618 __ NopIfNoReordering();
619 } else {
620 int32_t entry_point_offset =
621 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
622 // This runtime call does not require a stack map.
623 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
624 instruction_,
625 this,
626 /* direct */ false);
627 }
628 __ B(GetExitLabel());
629 }
630
631 private:
632 // The location (register) of the marked object reference.
633 const Location ref_;
634
635 // The location of the entrypoint if already loaded.
636 const Location entrypoint_;
637
638 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
639};
640
641// Slow path marking an object reference `ref` during a read barrier,
642// and if needed, atomically updating the field `obj.field` in the
643// object `obj` holding this reference after marking (contrary to
644// ReadBarrierMarkSlowPathMIPS above, which never tries to update
645// `obj.field`).
646//
647// This means that after the execution of this slow path, both `ref`
648// and `obj.field` will be up-to-date; i.e., after the flip, both will
649// hold the same to-space reference (unless another thread installed
650// another object reference (different from `ref`) in `obj.field`).
651class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
652 public:
653 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
654 Location ref,
655 Register obj,
656 Location field_offset,
657 Register temp1)
658 : SlowPathCodeMIPS(instruction),
659 ref_(ref),
660 obj_(obj),
661 field_offset_(field_offset),
662 temp1_(temp1) {
663 DCHECK(kEmitCompilerReadBarrier);
664 }
665
666 const char* GetDescription() const OVERRIDE {
667 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
668 }
669
670 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
671 LocationSummary* locations = instruction_->GetLocations();
672 Register ref_reg = ref_.AsRegister<Register>();
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
675 // This slow path is only used by the UnsafeCASObject intrinsic.
676 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
677 << "Unexpected instruction in read barrier marking and field updating slow path: "
678 << instruction_->DebugName();
679 DCHECK(instruction_->GetLocations()->Intrinsified());
680 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
681 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
682
683 __ Bind(GetEntryLabel());
684
685 // Save the old reference.
686 // Note that we cannot use AT or TMP to save the old reference, as those
687 // are used by the code that follows, but we need the old reference after
688 // the call to the ReadBarrierMarkRegX entry point.
689 DCHECK_NE(temp1_, AT);
690 DCHECK_NE(temp1_, TMP);
691 __ Move(temp1_, ref_reg);
692
693 // No need to save live registers; it's taken care of by the
694 // entrypoint. Also, there is no need to update the stack mask,
695 // as this runtime call will not trigger a garbage collection.
696 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
697 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
698 (S2 <= ref_reg && ref_reg <= S7) ||
699 (ref_reg == FP)) << ref_reg;
700 // "Compact" slow path, saving two moves.
701 //
702 // Instead of using the standard runtime calling convention (input
703 // and output in A0 and V0 respectively):
704 //
705 // A0 <- ref
706 // V0 <- ReadBarrierMark(A0)
707 // ref <- V0
708 //
709 // we just use rX (the register containing `ref`) as input and output
710 // of a dedicated entrypoint:
711 //
712 // rX <- ReadBarrierMarkRegX(rX)
713 //
714 int32_t entry_point_offset =
715 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
716 // This runtime call does not require a stack map.
717 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
718 instruction_,
719 this,
720 /* direct */ false);
721
722 // If the new reference is different from the old reference,
723 // update the field in the holder (`*(obj_ + field_offset_)`).
724 //
725 // Note that this field could also hold a different object, if
726 // another thread had concurrently changed it. In that case, the
727 // the compare-and-set (CAS) loop below would abort, leaving the
728 // field as-is.
729 MipsLabel done;
730 __ Beq(temp1_, ref_reg, &done);
731
732 // Update the the holder's field atomically. This may fail if
733 // mutator updates before us, but it's OK. This is achieved
734 // using a strong compare-and-set (CAS) operation with relaxed
735 // memory synchronization ordering, where the expected value is
736 // the old reference and the desired value is the new reference.
737
738 // Convenience aliases.
739 Register base = obj_;
740 // The UnsafeCASObject intrinsic uses a register pair as field
741 // offset ("long offset"), of which only the low part contains
742 // data.
743 Register offset = field_offset_.AsRegisterPairLow<Register>();
744 Register expected = temp1_;
745 Register value = ref_reg;
746 Register tmp_ptr = TMP; // Pointer to actual memory.
747 Register tmp = AT; // Value in memory.
748
749 __ Addu(tmp_ptr, base, offset);
750
751 if (kPoisonHeapReferences) {
752 __ PoisonHeapReference(expected);
753 // Do not poison `value` if it is the same register as
754 // `expected`, which has just been poisoned.
755 if (value != expected) {
756 __ PoisonHeapReference(value);
757 }
758 }
759
760 // do {
761 // tmp = [r_ptr] - expected;
762 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
763
764 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
765 MipsLabel loop_head, exit_loop;
766 __ Bind(&loop_head);
767 if (is_r6) {
768 __ LlR6(tmp, tmp_ptr);
769 } else {
770 __ LlR2(tmp, tmp_ptr);
771 }
772 __ Bne(tmp, expected, &exit_loop);
773 __ Move(tmp, value);
774 if (is_r6) {
775 __ ScR6(tmp, tmp_ptr);
776 } else {
777 __ ScR2(tmp, tmp_ptr);
778 }
779 __ Beqz(tmp, &loop_head);
780 __ Bind(&exit_loop);
781
782 if (kPoisonHeapReferences) {
783 __ UnpoisonHeapReference(expected);
784 // Do not unpoison `value` if it is the same register as
785 // `expected`, which has just been unpoisoned.
786 if (value != expected) {
787 __ UnpoisonHeapReference(value);
788 }
789 }
790
791 __ Bind(&done);
792 __ B(GetExitLabel());
793 }
794
795 private:
796 // The location (register) of the marked object reference.
797 const Location ref_;
798 // The register containing the object holding the marked object reference field.
799 const Register obj_;
800 // The location of the offset of the marked reference field within `obj_`.
801 Location field_offset_;
802
803 const Register temp1_;
804
805 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
806};
807
808// Slow path generating a read barrier for a heap reference.
809class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
810 public:
811 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
812 Location out,
813 Location ref,
814 Location obj,
815 uint32_t offset,
816 Location index)
817 : SlowPathCodeMIPS(instruction),
818 out_(out),
819 ref_(ref),
820 obj_(obj),
821 offset_(offset),
822 index_(index) {
823 DCHECK(kEmitCompilerReadBarrier);
824 // If `obj` is equal to `out` or `ref`, it means the initial object
825 // has been overwritten by (or after) the heap object reference load
826 // to be instrumented, e.g.:
827 //
828 // __ LoadFromOffset(kLoadWord, out, out, offset);
829 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
830 //
831 // In that case, we have lost the information about the original
832 // object, and the emitted read barrier cannot work properly.
833 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
834 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
835 }
836
837 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
838 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
839 LocationSummary* locations = instruction_->GetLocations();
840 Register reg_out = out_.AsRegister<Register>();
841 DCHECK(locations->CanCall());
842 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
843 DCHECK(instruction_->IsInstanceFieldGet() ||
844 instruction_->IsStaticFieldGet() ||
845 instruction_->IsArrayGet() ||
846 instruction_->IsInstanceOf() ||
847 instruction_->IsCheckCast() ||
848 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
849 << "Unexpected instruction in read barrier for heap reference slow path: "
850 << instruction_->DebugName();
851
852 __ Bind(GetEntryLabel());
853 SaveLiveRegisters(codegen, locations);
854
855 // We may have to change the index's value, but as `index_` is a
856 // constant member (like other "inputs" of this slow path),
857 // introduce a copy of it, `index`.
858 Location index = index_;
859 if (index_.IsValid()) {
860 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
861 if (instruction_->IsArrayGet()) {
862 // Compute the actual memory offset and store it in `index`.
863 Register index_reg = index_.AsRegister<Register>();
864 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
865 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
866 // We are about to change the value of `index_reg` (see the
867 // calls to art::mips::MipsAssembler::Sll and
868 // art::mips::MipsAssembler::Addiu32 below), but it has
869 // not been saved by the previous call to
870 // art::SlowPathCode::SaveLiveRegisters, as it is a
871 // callee-save register --
872 // art::SlowPathCode::SaveLiveRegisters does not consider
873 // callee-save registers, as it has been designed with the
874 // assumption that callee-save registers are supposed to be
875 // handled by the called function. So, as a callee-save
876 // register, `index_reg` _would_ eventually be saved onto
877 // the stack, but it would be too late: we would have
878 // changed its value earlier. Therefore, we manually save
879 // it here into another freely available register,
880 // `free_reg`, chosen of course among the caller-save
881 // registers (as a callee-save `free_reg` register would
882 // exhibit the same problem).
883 //
884 // Note we could have requested a temporary register from
885 // the register allocator instead; but we prefer not to, as
886 // this is a slow path, and we know we can find a
887 // caller-save register that is available.
888 Register free_reg = FindAvailableCallerSaveRegister(codegen);
889 __ Move(free_reg, index_reg);
890 index_reg = free_reg;
891 index = Location::RegisterLocation(index_reg);
892 } else {
893 // The initial register stored in `index_` has already been
894 // saved in the call to art::SlowPathCode::SaveLiveRegisters
895 // (as it is not a callee-save register), so we can freely
896 // use it.
897 }
898 // Shifting the index value contained in `index_reg` by the scale
899 // factor (2) cannot overflow in practice, as the runtime is
900 // unable to allocate object arrays with a size larger than
901 // 2^26 - 1 (that is, 2^28 - 4 bytes).
902 __ Sll(index_reg, index_reg, TIMES_4);
903 static_assert(
904 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
905 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
906 __ Addiu32(index_reg, index_reg, offset_);
907 } else {
908 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
909 // intrinsics, `index_` is not shifted by a scale factor of 2
910 // (as in the case of ArrayGet), as it is actually an offset
911 // to an object field within an object.
912 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
913 DCHECK(instruction_->GetLocations()->Intrinsified());
914 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
915 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
916 << instruction_->AsInvoke()->GetIntrinsic();
917 DCHECK_EQ(offset_, 0U);
918 DCHECK(index_.IsRegisterPair());
919 // UnsafeGet's offset location is a register pair, the low
920 // part contains the correct offset.
921 index = index_.ToLow();
922 }
923 }
924
925 // We're moving two or three locations to locations that could
926 // overlap, so we need a parallel move resolver.
927 InvokeRuntimeCallingConvention calling_convention;
928 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
929 parallel_move.AddMove(ref_,
930 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
931 Primitive::kPrimNot,
932 nullptr);
933 parallel_move.AddMove(obj_,
934 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
935 Primitive::kPrimNot,
936 nullptr);
937 if (index.IsValid()) {
938 parallel_move.AddMove(index,
939 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
940 Primitive::kPrimInt,
941 nullptr);
942 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
943 } else {
944 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
945 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
946 }
947 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
948 instruction_,
949 instruction_->GetDexPc(),
950 this);
951 CheckEntrypointTypes<
952 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200953 mips_codegen->MoveLocation(out_,
954 calling_convention.GetReturnLocation(Primitive::kPrimNot),
955 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -0800956
957 RestoreLiveRegisters(codegen, locations);
958 __ B(GetExitLabel());
959 }
960
961 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
962
963 private:
964 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
965 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
966 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
967 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
968 if (i != ref &&
969 i != obj &&
970 !codegen->IsCoreCalleeSaveRegister(i) &&
971 !codegen->IsBlockedCoreRegister(i)) {
972 return static_cast<Register>(i);
973 }
974 }
975 // We shall never fail to find a free caller-save register, as
976 // there are more than two core caller-save registers on MIPS
977 // (meaning it is possible to find one which is different from
978 // `ref` and `obj`).
979 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
980 LOG(FATAL) << "Could not find a free caller-save register";
981 UNREACHABLE();
982 }
983
984 const Location out_;
985 const Location ref_;
986 const Location obj_;
987 const uint32_t offset_;
988 // An additional location containing an index to an array.
989 // Only used for HArrayGet and the UnsafeGetObject &
990 // UnsafeGetObjectVolatile intrinsics.
991 const Location index_;
992
993 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
994};
995
996// Slow path generating a read barrier for a GC root.
997class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
998 public:
999 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1000 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1001 DCHECK(kEmitCompilerReadBarrier);
1002 }
1003
1004 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1005 LocationSummary* locations = instruction_->GetLocations();
1006 Register reg_out = out_.AsRegister<Register>();
1007 DCHECK(locations->CanCall());
1008 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1009 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1010 << "Unexpected instruction in read barrier for GC root slow path: "
1011 << instruction_->DebugName();
1012
1013 __ Bind(GetEntryLabel());
1014 SaveLiveRegisters(codegen, locations);
1015
1016 InvokeRuntimeCallingConvention calling_convention;
1017 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001018 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1019 root_,
1020 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001021 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1022 instruction_,
1023 instruction_->GetDexPc(),
1024 this);
1025 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001026 mips_codegen->MoveLocation(out_,
1027 calling_convention.GetReturnLocation(Primitive::kPrimNot),
1028 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001029
1030 RestoreLiveRegisters(codegen, locations);
1031 __ B(GetExitLabel());
1032 }
1033
1034 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1035
1036 private:
1037 const Location out_;
1038 const Location root_;
1039
1040 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1041};
1042
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001043CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1044 const MipsInstructionSetFeatures& isa_features,
1045 const CompilerOptions& compiler_options,
1046 OptimizingCompilerStats* stats)
1047 : CodeGenerator(graph,
1048 kNumberOfCoreRegisters,
1049 kNumberOfFRegisters,
1050 kNumberOfRegisterPairs,
1051 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1052 arraysize(kCoreCalleeSaves)),
1053 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1054 arraysize(kFpuCalleeSaves)),
1055 compiler_options,
1056 stats),
1057 block_labels_(nullptr),
1058 location_builder_(graph, this),
1059 instruction_visitor_(graph, this),
1060 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001061 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001062 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001063 uint32_literals_(std::less<uint32_t>(),
1064 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001065 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001066 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001067 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001068 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001069 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001070 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1071 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001072 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001073 // Save RA (containing the return address) to mimic Quick.
1074 AddAllocatedRegister(Location::RegisterLocation(RA));
1075}
1076
1077#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001078// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1079#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001080#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001081
1082void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1083 // Ensure that we fix up branches.
1084 __ FinalizeCode();
1085
1086 // Adjust native pc offsets in stack maps.
1087 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001088 uint32_t old_position =
1089 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001090 uint32_t new_position = __ GetAdjustedPosition(old_position);
1091 DCHECK_GE(new_position, old_position);
1092 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1093 }
1094
1095 // Adjust pc offsets for the disassembly information.
1096 if (disasm_info_ != nullptr) {
1097 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1098 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1099 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1100 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1101 it.second.start = __ GetAdjustedPosition(it.second.start);
1102 it.second.end = __ GetAdjustedPosition(it.second.end);
1103 }
1104 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1105 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1106 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1107 }
1108 }
1109
1110 CodeGenerator::Finalize(allocator);
1111}
1112
1113MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1114 return codegen_->GetAssembler();
1115}
1116
1117void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1118 DCHECK_LT(index, moves_.size());
1119 MoveOperands* move = moves_[index];
1120 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1121}
1122
1123void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1124 DCHECK_LT(index, moves_.size());
1125 MoveOperands* move = moves_[index];
1126 Primitive::Type type = move->GetType();
1127 Location loc1 = move->GetDestination();
1128 Location loc2 = move->GetSource();
1129
1130 DCHECK(!loc1.IsConstant());
1131 DCHECK(!loc2.IsConstant());
1132
1133 if (loc1.Equals(loc2)) {
1134 return;
1135 }
1136
1137 if (loc1.IsRegister() && loc2.IsRegister()) {
1138 // Swap 2 GPRs.
1139 Register r1 = loc1.AsRegister<Register>();
1140 Register r2 = loc2.AsRegister<Register>();
1141 __ Move(TMP, r2);
1142 __ Move(r2, r1);
1143 __ Move(r1, TMP);
1144 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1145 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1146 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1147 if (type == Primitive::kPrimFloat) {
1148 __ MovS(FTMP, f2);
1149 __ MovS(f2, f1);
1150 __ MovS(f1, FTMP);
1151 } else {
1152 DCHECK_EQ(type, Primitive::kPrimDouble);
1153 __ MovD(FTMP, f2);
1154 __ MovD(f2, f1);
1155 __ MovD(f1, FTMP);
1156 }
1157 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1158 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1159 // Swap FPR and GPR.
1160 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1161 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1162 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001163 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001164 __ Move(TMP, r2);
1165 __ Mfc1(r2, f1);
1166 __ Mtc1(TMP, f1);
1167 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1168 // Swap 2 GPR register pairs.
1169 Register r1 = loc1.AsRegisterPairLow<Register>();
1170 Register r2 = loc2.AsRegisterPairLow<Register>();
1171 __ Move(TMP, r2);
1172 __ Move(r2, r1);
1173 __ Move(r1, TMP);
1174 r1 = loc1.AsRegisterPairHigh<Register>();
1175 r2 = loc2.AsRegisterPairHigh<Register>();
1176 __ Move(TMP, r2);
1177 __ Move(r2, r1);
1178 __ Move(r1, TMP);
1179 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1180 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1181 // Swap FPR and GPR register pair.
1182 DCHECK_EQ(type, Primitive::kPrimDouble);
1183 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1184 : loc2.AsFpuRegister<FRegister>();
1185 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1186 : loc2.AsRegisterPairLow<Register>();
1187 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1188 : loc2.AsRegisterPairHigh<Register>();
1189 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1190 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1191 // unpredictable and the following mfch1 will fail.
1192 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001193 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001195 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001196 __ Move(r2_l, TMP);
1197 __ Move(r2_h, AT);
1198 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1199 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1200 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1201 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001202 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1203 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001204 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1205 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001206 __ Move(TMP, reg);
1207 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1208 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1209 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1210 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1211 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1212 : loc2.AsRegisterPairLow<Register>();
1213 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1214 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001215 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001216 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1217 : loc2.GetHighStackIndex(kMipsWordSize);
1218 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001219 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001220 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001221 __ Move(TMP, reg_h);
1222 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1223 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001224 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1225 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1226 : loc2.AsFpuRegister<FRegister>();
1227 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1228 if (type == Primitive::kPrimFloat) {
1229 __ MovS(FTMP, reg);
1230 __ LoadSFromOffset(reg, SP, offset);
1231 __ StoreSToOffset(FTMP, SP, offset);
1232 } else {
1233 DCHECK_EQ(type, Primitive::kPrimDouble);
1234 __ MovD(FTMP, reg);
1235 __ LoadDFromOffset(reg, SP, offset);
1236 __ StoreDToOffset(FTMP, SP, offset);
1237 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238 } else {
1239 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1240 }
1241}
1242
1243void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1244 __ Pop(static_cast<Register>(reg));
1245}
1246
1247void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1248 __ Push(static_cast<Register>(reg));
1249}
1250
1251void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1252 // Allocate a scratch register other than TMP, if available.
1253 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1254 // automatically unspilled when the scratch scope object is destroyed).
1255 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1256 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1257 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1258 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1259 __ LoadFromOffset(kLoadWord,
1260 Register(ensure_scratch.GetRegister()),
1261 SP,
1262 index1 + stack_offset);
1263 __ LoadFromOffset(kLoadWord,
1264 TMP,
1265 SP,
1266 index2 + stack_offset);
1267 __ StoreToOffset(kStoreWord,
1268 Register(ensure_scratch.GetRegister()),
1269 SP,
1270 index2 + stack_offset);
1271 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1272 }
1273}
1274
Alexey Frunze73296a72016-06-03 22:51:46 -07001275void CodeGeneratorMIPS::ComputeSpillMask() {
1276 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1277 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1278 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1279 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1280 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1281 // within the stack frame.
1282 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1283 core_spill_mask_ |= (1 << ZERO);
1284 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001285}
1286
1287bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001288 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001289 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1290 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1291 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001292 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001293}
1294
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001295static dwarf::Reg DWARFReg(Register reg) {
1296 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1297}
1298
1299// TODO: mapping of floating-point registers to DWARF.
1300
1301void CodeGeneratorMIPS::GenerateFrameEntry() {
1302 __ Bind(&frame_entry_label_);
1303
1304 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1305
1306 if (do_overflow_check) {
1307 __ LoadFromOffset(kLoadWord,
1308 ZERO,
1309 SP,
1310 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1311 RecordPcInfo(nullptr, 0);
1312 }
1313
1314 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001315 CHECK_EQ(fpu_spill_mask_, 0u);
1316 CHECK_EQ(core_spill_mask_, 1u << RA);
1317 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001318 return;
1319 }
1320
1321 // Make sure the frame size isn't unreasonably large.
1322 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1323 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1324 }
1325
1326 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001327
Alexey Frunze73296a72016-06-03 22:51:46 -07001328 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329 __ IncreaseFrameSize(ofs);
1330
Alexey Frunze73296a72016-06-03 22:51:46 -07001331 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1332 Register reg = static_cast<Register>(MostSignificantBit(mask));
1333 mask ^= 1u << reg;
1334 ofs -= kMipsWordSize;
1335 // The ZERO register is only included for alignment.
1336 if (reg != ZERO) {
1337 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001338 __ cfi().RelOffset(DWARFReg(reg), ofs);
1339 }
1340 }
1341
Alexey Frunze73296a72016-06-03 22:51:46 -07001342 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1343 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1344 mask ^= 1u << reg;
1345 ofs -= kMipsDoublewordSize;
1346 __ StoreDToOffset(reg, SP, ofs);
1347 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348 }
1349
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001350 // Save the current method if we need it. Note that we do not
1351 // do this in HCurrentMethod, as the instruction might have been removed
1352 // in the SSA graph.
1353 if (RequiresCurrentMethod()) {
1354 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1355 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001356
1357 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1358 // Initialize should deoptimize flag to 0.
1359 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001361}
1362
1363void CodeGeneratorMIPS::GenerateFrameExit() {
1364 __ cfi().RememberState();
1365
1366 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001367 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 // For better instruction scheduling restore RA before other registers.
1370 uint32_t ofs = GetFrameSize();
1371 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1372 Register reg = static_cast<Register>(MostSignificantBit(mask));
1373 mask ^= 1u << reg;
1374 ofs -= kMipsWordSize;
1375 // The ZERO register is only included for alignment.
1376 if (reg != ZERO) {
1377 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001378 __ cfi().Restore(DWARFReg(reg));
1379 }
1380 }
1381
Alexey Frunze73296a72016-06-03 22:51:46 -07001382 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1383 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1384 mask ^= 1u << reg;
1385 ofs -= kMipsDoublewordSize;
1386 __ LoadDFromOffset(reg, SP, ofs);
1387 // TODO: __ cfi().Restore(DWARFReg(reg));
1388 }
1389
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001390 size_t frame_size = GetFrameSize();
1391 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1392 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1393 bool reordering = __ SetReorder(false);
1394 if (exchange) {
1395 __ Jr(RA);
1396 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1397 } else {
1398 __ DecreaseFrameSize(frame_size);
1399 __ Jr(RA);
1400 __ Nop(); // In delay slot.
1401 }
1402 __ SetReorder(reordering);
1403 } else {
1404 __ Jr(RA);
1405 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 }
1407
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001408 __ cfi().RestoreState();
1409 __ cfi().DefCFAOffset(GetFrameSize());
1410}
1411
1412void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1413 __ Bind(GetLabelOf(block));
1414}
1415
Lena Djokic8098da92017-06-28 12:07:50 +02001416void CodeGeneratorMIPS::MoveLocation(Location destination,
1417 Location source,
1418 Primitive::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 if (source.Equals(destination)) {
1420 return;
1421 }
1422
Lena Djokic8098da92017-06-28 12:07:50 +02001423 if (source.IsConstant()) {
1424 MoveConstant(destination, source.GetConstant());
1425 } else {
1426 if (destination.IsRegister()) {
1427 if (source.IsRegister()) {
1428 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1429 } else if (source.IsFpuRegister()) {
1430 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1431 } else {
1432 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001433 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001434 }
1435 } else if (destination.IsRegisterPair()) {
1436 if (source.IsRegisterPair()) {
1437 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1438 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1439 } else if (source.IsFpuRegister()) {
1440 Register dst_high = destination.AsRegisterPairHigh<Register>();
1441 Register dst_low = destination.AsRegisterPairLow<Register>();
1442 FRegister src = source.AsFpuRegister<FRegister>();
1443 __ Mfc1(dst_low, src);
1444 __ MoveFromFpuHigh(dst_high, src);
1445 } else {
1446 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1447 int32_t off = source.GetStackIndex();
1448 Register r = destination.AsRegisterPairLow<Register>();
1449 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1450 }
1451 } else if (destination.IsFpuRegister()) {
1452 if (source.IsRegister()) {
1453 DCHECK(!Primitive::Is64BitType(dst_type));
1454 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1455 } else if (source.IsRegisterPair()) {
1456 DCHECK(Primitive::Is64BitType(dst_type));
1457 FRegister dst = destination.AsFpuRegister<FRegister>();
1458 Register src_high = source.AsRegisterPairHigh<Register>();
1459 Register src_low = source.AsRegisterPairLow<Register>();
1460 __ Mtc1(src_low, dst);
1461 __ MoveToFpuHigh(src_high, dst);
1462 } else if (source.IsFpuRegister()) {
1463 if (Primitive::Is64BitType(dst_type)) {
1464 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1465 } else {
1466 DCHECK_EQ(dst_type, Primitive::kPrimFloat);
1467 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1468 }
1469 } else if (source.IsDoubleStackSlot()) {
1470 DCHECK(Primitive::Is64BitType(dst_type));
1471 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1472 } else {
1473 DCHECK(!Primitive::Is64BitType(dst_type));
1474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1475 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1476 }
1477 } else if (destination.IsDoubleStackSlot()) {
1478 int32_t dst_offset = destination.GetStackIndex();
1479 if (source.IsRegisterPair()) {
1480 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1481 } else if (source.IsFpuRegister()) {
1482 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1483 } else {
1484 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1485 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1486 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1487 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1488 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001490 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001491 DCHECK(destination.IsStackSlot()) << destination;
1492 int32_t dst_offset = destination.GetStackIndex();
1493 if (source.IsRegister()) {
1494 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1495 } else if (source.IsFpuRegister()) {
1496 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1497 } else {
1498 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1499 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1500 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1501 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001502 }
1503 }
1504}
1505
1506void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1507 if (c->IsIntConstant() || c->IsNullConstant()) {
1508 // Move 32 bit constant.
1509 int32_t value = GetInt32ValueOf(c);
1510 if (destination.IsRegister()) {
1511 Register dst = destination.AsRegister<Register>();
1512 __ LoadConst32(dst, value);
1513 } else {
1514 DCHECK(destination.IsStackSlot())
1515 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001516 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001517 }
1518 } else if (c->IsLongConstant()) {
1519 // Move 64 bit constant.
1520 int64_t value = GetInt64ValueOf(c);
1521 if (destination.IsRegisterPair()) {
1522 Register r_h = destination.AsRegisterPairHigh<Register>();
1523 Register r_l = destination.AsRegisterPairLow<Register>();
1524 __ LoadConst64(r_h, r_l, value);
1525 } else {
1526 DCHECK(destination.IsDoubleStackSlot())
1527 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001528 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001529 }
1530 } else if (c->IsFloatConstant()) {
1531 // Move 32 bit float constant.
1532 int32_t value = GetInt32ValueOf(c);
1533 if (destination.IsFpuRegister()) {
1534 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1535 } else {
1536 DCHECK(destination.IsStackSlot())
1537 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001538 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001539 }
1540 } else {
1541 // Move 64 bit double constant.
1542 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1543 int64_t value = GetInt64ValueOf(c);
1544 if (destination.IsFpuRegister()) {
1545 FRegister fd = destination.AsFpuRegister<FRegister>();
1546 __ LoadDConst64(fd, value, TMP);
1547 } else {
1548 DCHECK(destination.IsDoubleStackSlot())
1549 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001550 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001551 }
1552 }
1553}
1554
1555void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1556 DCHECK(destination.IsRegister());
1557 Register dst = destination.AsRegister<Register>();
1558 __ LoadConst32(dst, value);
1559}
1560
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001561void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1562 if (location.IsRegister()) {
1563 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001564 } else if (location.IsRegisterPair()) {
1565 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1566 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001567 } else {
1568 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1569 }
1570}
1571
Vladimir Markoaad75c62016-10-03 08:46:48 +00001572template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1573inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1574 const ArenaDeque<PcRelativePatchInfo>& infos,
1575 ArenaVector<LinkerPatch>* linker_patches) {
1576 for (const PcRelativePatchInfo& info : infos) {
1577 const DexFile& dex_file = info.target_dex_file;
1578 size_t offset_or_index = info.offset_or_index;
1579 DCHECK(info.high_label.IsBound());
1580 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1581 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1582 // the assembler's base label used for PC-relative addressing.
1583 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1584 ? __ GetLabelLocation(&info.pc_rel_label)
1585 : __ GetPcRelBaseLabelLocation();
1586 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1587 }
1588}
1589
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001590void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1591 DCHECK(linker_patches->empty());
1592 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001593 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001594 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001595 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001596 type_bss_entry_patches_.size() +
1597 pc_relative_string_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001598 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001599 if (GetCompilerOptions().IsBootImage()) {
1600 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00001601 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001602 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1603 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001604 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1605 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001606 } else {
1607 DCHECK(pc_relative_method_patches_.empty());
1608 DCHECK(pc_relative_type_patches_.empty());
1609 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1610 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001611 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001612 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1613 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001614 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1615 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001616 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001617}
1618
Vladimir Marko65979462017-05-19 17:25:12 +01001619CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
1620 MethodReference target_method) {
1621 return NewPcRelativePatch(*target_method.dex_file,
1622 target_method.dex_method_index,
1623 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001624}
1625
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001626CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
1627 MethodReference target_method) {
1628 return NewPcRelativePatch(*target_method.dex_file,
1629 target_method.dex_method_index,
1630 &method_bss_entry_patches_);
1631}
1632
Alexey Frunze06a46c42016-07-19 15:00:40 -07001633CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001634 const DexFile& dex_file, dex::TypeIndex type_index) {
1635 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001636}
1637
Vladimir Marko1998cd02017-01-13 13:02:58 +00001638CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1639 const DexFile& dex_file, dex::TypeIndex type_index) {
1640 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1641}
1642
Vladimir Marko65979462017-05-19 17:25:12 +01001643CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1644 const DexFile& dex_file, dex::StringIndex string_index) {
1645 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
1646}
1647
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001648CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1649 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1650 patches->emplace_back(dex_file, offset_or_index);
1651 return &patches->back();
1652}
1653
Alexey Frunze06a46c42016-07-19 15:00:40 -07001654Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1655 return map->GetOrCreate(
1656 value,
1657 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1658}
1659
Alexey Frunze06a46c42016-07-19 15:00:40 -07001660Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001661 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001662}
1663
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001664void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1665 Register out,
1666 Register base) {
Alexey Frunze6079dca2017-05-28 19:10:28 -07001667 DCHECK_NE(out, base);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001668 if (GetInstructionSetFeatures().IsR6()) {
1669 DCHECK_EQ(base, ZERO);
1670 __ Bind(&info->high_label);
1671 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001672 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001673 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001674 } else {
1675 // If base is ZERO, emit NAL to obtain the actual base.
1676 if (base == ZERO) {
1677 // Generate a dummy PC-relative call to obtain PC.
1678 __ Nal();
1679 }
1680 __ Bind(&info->high_label);
1681 __ Lui(out, /* placeholder */ 0x1234);
1682 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1683 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1684 if (base == ZERO) {
1685 __ Bind(&info->pc_rel_label);
1686 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001687 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001688 __ Addu(out, out, (base == ZERO) ? RA : base);
1689 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001690 // The immediately following instruction will add the sign-extended low half of the 32-bit
1691 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001692}
1693
Alexey Frunze627c1a02017-01-30 19:28:14 -08001694CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1695 const DexFile& dex_file,
1696 dex::StringIndex dex_index,
1697 Handle<mirror::String> handle) {
1698 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1699 reinterpret_cast64<uint64_t>(handle.GetReference()));
1700 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1701 return &jit_string_patches_.back();
1702}
1703
1704CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1705 const DexFile& dex_file,
1706 dex::TypeIndex dex_index,
1707 Handle<mirror::Class> handle) {
1708 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1709 reinterpret_cast64<uint64_t>(handle.GetReference()));
1710 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1711 return &jit_class_patches_.back();
1712}
1713
1714void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1715 const uint8_t* roots_data,
1716 const CodeGeneratorMIPS::JitPatchInfo& info,
1717 uint64_t index_in_table) const {
1718 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1719 uintptr_t address =
1720 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1721 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1722 // lui reg, addr32_high
1723 DCHECK_EQ(code[literal_offset + 0], 0x34);
1724 DCHECK_EQ(code[literal_offset + 1], 0x12);
1725 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1726 DCHECK_EQ(code[literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001727 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001728 DCHECK_EQ(code[literal_offset + 4], 0x78);
1729 DCHECK_EQ(code[literal_offset + 5], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001730 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001731 // lui reg, addr32_high
1732 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1733 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001734 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001735 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1736 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1737}
1738
1739void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1740 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001741 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1742 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001743 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001744 uint64_t index_in_table = it->second;
1745 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001746 }
1747 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001748 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1749 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001750 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001751 uint64_t index_in_table = it->second;
1752 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001753 }
1754}
1755
Goran Jakovljevice114da22016-12-26 14:21:43 +01001756void CodeGeneratorMIPS::MarkGCCard(Register object,
1757 Register value,
1758 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001759 MipsLabel done;
1760 Register card = AT;
1761 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001762 if (value_can_be_null) {
1763 __ Beqz(value, &done);
1764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001765 __ LoadFromOffset(kLoadWord,
1766 card,
1767 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001768 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001769 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1770 __ Addu(temp, card, temp);
1771 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001772 if (value_can_be_null) {
1773 __ Bind(&done);
1774 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001775}
1776
David Brazdil58282f42016-01-14 12:45:10 +00001777void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001778 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1779 blocked_core_registers_[ZERO] = true;
1780 blocked_core_registers_[K0] = true;
1781 blocked_core_registers_[K1] = true;
1782 blocked_core_registers_[GP] = true;
1783 blocked_core_registers_[SP] = true;
1784 blocked_core_registers_[RA] = true;
1785
1786 // AT and TMP(T8) are used as temporary/scratch registers
1787 // (similar to how AT is used by MIPS assemblers).
1788 blocked_core_registers_[AT] = true;
1789 blocked_core_registers_[TMP] = true;
1790 blocked_fpu_registers_[FTMP] = true;
1791
1792 // Reserve suspend and thread registers.
1793 blocked_core_registers_[S0] = true;
1794 blocked_core_registers_[TR] = true;
1795
1796 // Reserve T9 for function calls
1797 blocked_core_registers_[T9] = true;
1798
1799 // Reserve odd-numbered FPU registers.
1800 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1801 blocked_fpu_registers_[i] = true;
1802 }
1803
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001804 if (GetGraph()->IsDebuggable()) {
1805 // Stubs do not save callee-save floating point registers. If the graph
1806 // is debuggable, we need to deal with these registers differently. For
1807 // now, just block them.
1808 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1809 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1810 }
1811 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001812}
1813
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001814size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1815 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1816 return kMipsWordSize;
1817}
1818
1819size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1820 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1821 return kMipsWordSize;
1822}
1823
1824size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1825 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1826 return kMipsDoublewordSize;
1827}
1828
1829size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1830 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1831 return kMipsDoublewordSize;
1832}
1833
1834void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001835 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001836}
1837
1838void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001839 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840}
1841
Serban Constantinescufca16662016-07-14 09:21:59 +01001842constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1843
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1845 HInstruction* instruction,
1846 uint32_t dex_pc,
1847 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001848 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001849 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1850 IsDirectEntrypoint(entrypoint));
1851 if (EntrypointRequiresStackMap(entrypoint)) {
1852 RecordPcInfo(instruction, dex_pc, slow_path);
1853 }
1854}
1855
1856void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1857 HInstruction* instruction,
1858 SlowPathCode* slow_path,
1859 bool direct) {
1860 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1861 GenerateInvokeRuntime(entry_point_offset, direct);
1862}
1863
1864void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001865 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001866 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001867 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001868 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869 // Reserve argument space on stack (for $a0-$a3) for
1870 // entrypoints that directly reference native implementations.
1871 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001872 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001874 } else {
1875 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001877 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878}
1879
1880void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1881 Register class_reg) {
1882 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1883 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1884 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1885 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1886 __ Sync(0);
1887 __ Bind(slow_path->GetExitLabel());
1888}
1889
1890void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1891 __ Sync(0); // Only stype 0 is supported.
1892}
1893
1894void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1895 HBasicBlock* successor) {
1896 SuspendCheckSlowPathMIPS* slow_path =
1897 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1898 codegen_->AddSlowPath(slow_path);
1899
1900 __ LoadFromOffset(kLoadUnsignedHalfword,
1901 TMP,
1902 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001903 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001904 if (successor == nullptr) {
1905 __ Bnez(TMP, slow_path->GetEntryLabel());
1906 __ Bind(slow_path->GetReturnLabel());
1907 } else {
1908 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1909 __ B(slow_path->GetEntryLabel());
1910 // slow_path will return to GetLabelOf(successor).
1911 }
1912}
1913
1914InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1915 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001916 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917 assembler_(codegen->GetAssembler()),
1918 codegen_(codegen) {}
1919
1920void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1921 DCHECK_EQ(instruction->InputCount(), 2U);
1922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1923 Primitive::Type type = instruction->GetResultType();
1924 switch (type) {
1925 case Primitive::kPrimInt: {
1926 locations->SetInAt(0, Location::RequiresRegister());
1927 HInstruction* right = instruction->InputAt(1);
1928 bool can_use_imm = false;
1929 if (right->IsConstant()) {
1930 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1931 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1932 can_use_imm = IsUint<16>(imm);
1933 } else if (instruction->IsAdd()) {
1934 can_use_imm = IsInt<16>(imm);
1935 } else {
1936 DCHECK(instruction->IsSub());
1937 can_use_imm = IsInt<16>(-imm);
1938 }
1939 }
1940 if (can_use_imm)
1941 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1942 else
1943 locations->SetInAt(1, Location::RequiresRegister());
1944 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1945 break;
1946 }
1947
1948 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001949 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001950 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 break;
1953 }
1954
1955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble:
1957 DCHECK(instruction->IsAdd() || instruction->IsSub());
1958 locations->SetInAt(0, Location::RequiresFpuRegister());
1959 locations->SetInAt(1, Location::RequiresFpuRegister());
1960 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1961 break;
1962
1963 default:
1964 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1965 }
1966}
1967
1968void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1969 Primitive::Type type = instruction->GetType();
1970 LocationSummary* locations = instruction->GetLocations();
1971
1972 switch (type) {
1973 case Primitive::kPrimInt: {
1974 Register dst = locations->Out().AsRegister<Register>();
1975 Register lhs = locations->InAt(0).AsRegister<Register>();
1976 Location rhs_location = locations->InAt(1);
1977
1978 Register rhs_reg = ZERO;
1979 int32_t rhs_imm = 0;
1980 bool use_imm = rhs_location.IsConstant();
1981 if (use_imm) {
1982 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1983 } else {
1984 rhs_reg = rhs_location.AsRegister<Register>();
1985 }
1986
1987 if (instruction->IsAnd()) {
1988 if (use_imm)
1989 __ Andi(dst, lhs, rhs_imm);
1990 else
1991 __ And(dst, lhs, rhs_reg);
1992 } else if (instruction->IsOr()) {
1993 if (use_imm)
1994 __ Ori(dst, lhs, rhs_imm);
1995 else
1996 __ Or(dst, lhs, rhs_reg);
1997 } else if (instruction->IsXor()) {
1998 if (use_imm)
1999 __ Xori(dst, lhs, rhs_imm);
2000 else
2001 __ Xor(dst, lhs, rhs_reg);
2002 } else if (instruction->IsAdd()) {
2003 if (use_imm)
2004 __ Addiu(dst, lhs, rhs_imm);
2005 else
2006 __ Addu(dst, lhs, rhs_reg);
2007 } else {
2008 DCHECK(instruction->IsSub());
2009 if (use_imm)
2010 __ Addiu(dst, lhs, -rhs_imm);
2011 else
2012 __ Subu(dst, lhs, rhs_reg);
2013 }
2014 break;
2015 }
2016
2017 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2019 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2020 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2021 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002022 Location rhs_location = locations->InAt(1);
2023 bool use_imm = rhs_location.IsConstant();
2024 if (!use_imm) {
2025 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2026 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2027 if (instruction->IsAnd()) {
2028 __ And(dst_low, lhs_low, rhs_low);
2029 __ And(dst_high, lhs_high, rhs_high);
2030 } else if (instruction->IsOr()) {
2031 __ Or(dst_low, lhs_low, rhs_low);
2032 __ Or(dst_high, lhs_high, rhs_high);
2033 } else if (instruction->IsXor()) {
2034 __ Xor(dst_low, lhs_low, rhs_low);
2035 __ Xor(dst_high, lhs_high, rhs_high);
2036 } else if (instruction->IsAdd()) {
2037 if (lhs_low == rhs_low) {
2038 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2039 __ Slt(TMP, lhs_low, ZERO);
2040 __ Addu(dst_low, lhs_low, rhs_low);
2041 } else {
2042 __ Addu(dst_low, lhs_low, rhs_low);
2043 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2044 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2045 }
2046 __ Addu(dst_high, lhs_high, rhs_high);
2047 __ Addu(dst_high, dst_high, TMP);
2048 } else {
2049 DCHECK(instruction->IsSub());
2050 __ Sltu(TMP, lhs_low, rhs_low);
2051 __ Subu(dst_low, lhs_low, rhs_low);
2052 __ Subu(dst_high, lhs_high, rhs_high);
2053 __ Subu(dst_high, dst_high, TMP);
2054 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002055 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002056 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2057 if (instruction->IsOr()) {
2058 uint32_t low = Low32Bits(value);
2059 uint32_t high = High32Bits(value);
2060 if (IsUint<16>(low)) {
2061 if (dst_low != lhs_low || low != 0) {
2062 __ Ori(dst_low, lhs_low, low);
2063 }
2064 } else {
2065 __ LoadConst32(TMP, low);
2066 __ Or(dst_low, lhs_low, TMP);
2067 }
2068 if (IsUint<16>(high)) {
2069 if (dst_high != lhs_high || high != 0) {
2070 __ Ori(dst_high, lhs_high, high);
2071 }
2072 } else {
2073 if (high != low) {
2074 __ LoadConst32(TMP, high);
2075 }
2076 __ Or(dst_high, lhs_high, TMP);
2077 }
2078 } else if (instruction->IsXor()) {
2079 uint32_t low = Low32Bits(value);
2080 uint32_t high = High32Bits(value);
2081 if (IsUint<16>(low)) {
2082 if (dst_low != lhs_low || low != 0) {
2083 __ Xori(dst_low, lhs_low, low);
2084 }
2085 } else {
2086 __ LoadConst32(TMP, low);
2087 __ Xor(dst_low, lhs_low, TMP);
2088 }
2089 if (IsUint<16>(high)) {
2090 if (dst_high != lhs_high || high != 0) {
2091 __ Xori(dst_high, lhs_high, high);
2092 }
2093 } else {
2094 if (high != low) {
2095 __ LoadConst32(TMP, high);
2096 }
2097 __ Xor(dst_high, lhs_high, TMP);
2098 }
2099 } else if (instruction->IsAnd()) {
2100 uint32_t low = Low32Bits(value);
2101 uint32_t high = High32Bits(value);
2102 if (IsUint<16>(low)) {
2103 __ Andi(dst_low, lhs_low, low);
2104 } else if (low != 0xFFFFFFFF) {
2105 __ LoadConst32(TMP, low);
2106 __ And(dst_low, lhs_low, TMP);
2107 } else if (dst_low != lhs_low) {
2108 __ Move(dst_low, lhs_low);
2109 }
2110 if (IsUint<16>(high)) {
2111 __ Andi(dst_high, lhs_high, high);
2112 } else if (high != 0xFFFFFFFF) {
2113 if (high != low) {
2114 __ LoadConst32(TMP, high);
2115 }
2116 __ And(dst_high, lhs_high, TMP);
2117 } else if (dst_high != lhs_high) {
2118 __ Move(dst_high, lhs_high);
2119 }
2120 } else {
2121 if (instruction->IsSub()) {
2122 value = -value;
2123 } else {
2124 DCHECK(instruction->IsAdd());
2125 }
2126 int32_t low = Low32Bits(value);
2127 int32_t high = High32Bits(value);
2128 if (IsInt<16>(low)) {
2129 if (dst_low != lhs_low || low != 0) {
2130 __ Addiu(dst_low, lhs_low, low);
2131 }
2132 if (low != 0) {
2133 __ Sltiu(AT, dst_low, low);
2134 }
2135 } else {
2136 __ LoadConst32(TMP, low);
2137 __ Addu(dst_low, lhs_low, TMP);
2138 __ Sltu(AT, dst_low, TMP);
2139 }
2140 if (IsInt<16>(high)) {
2141 if (dst_high != lhs_high || high != 0) {
2142 __ Addiu(dst_high, lhs_high, high);
2143 }
2144 } else {
2145 if (high != low) {
2146 __ LoadConst32(TMP, high);
2147 }
2148 __ Addu(dst_high, lhs_high, TMP);
2149 }
2150 if (low != 0) {
2151 __ Addu(dst_high, dst_high, AT);
2152 }
2153 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002154 }
2155 break;
2156 }
2157
2158 case Primitive::kPrimFloat:
2159 case Primitive::kPrimDouble: {
2160 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2161 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2162 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2163 if (instruction->IsAdd()) {
2164 if (type == Primitive::kPrimFloat) {
2165 __ AddS(dst, lhs, rhs);
2166 } else {
2167 __ AddD(dst, lhs, rhs);
2168 }
2169 } else {
2170 DCHECK(instruction->IsSub());
2171 if (type == Primitive::kPrimFloat) {
2172 __ SubS(dst, lhs, rhs);
2173 } else {
2174 __ SubD(dst, lhs, rhs);
2175 }
2176 }
2177 break;
2178 }
2179
2180 default:
2181 LOG(FATAL) << "Unexpected binary operation type " << type;
2182 }
2183}
2184
2185void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002186 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002187
2188 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2189 Primitive::Type type = instr->GetResultType();
2190 switch (type) {
2191 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002192 locations->SetInAt(0, Location::RequiresRegister());
2193 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2194 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2195 break;
2196 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002197 locations->SetInAt(0, Location::RequiresRegister());
2198 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2199 locations->SetOut(Location::RequiresRegister());
2200 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 default:
2202 LOG(FATAL) << "Unexpected shift type " << type;
2203 }
2204}
2205
2206static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2207
2208void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002209 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002210 LocationSummary* locations = instr->GetLocations();
2211 Primitive::Type type = instr->GetType();
2212
2213 Location rhs_location = locations->InAt(1);
2214 bool use_imm = rhs_location.IsConstant();
2215 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2216 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002217 const uint32_t shift_mask =
2218 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002219 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002220 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2221 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002222
2223 switch (type) {
2224 case Primitive::kPrimInt: {
2225 Register dst = locations->Out().AsRegister<Register>();
2226 Register lhs = locations->InAt(0).AsRegister<Register>();
2227 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002228 if (shift_value == 0) {
2229 if (dst != lhs) {
2230 __ Move(dst, lhs);
2231 }
2232 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002233 __ Sll(dst, lhs, shift_value);
2234 } else if (instr->IsShr()) {
2235 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002236 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002238 } else {
2239 if (has_ins_rotr) {
2240 __ Rotr(dst, lhs, shift_value);
2241 } else {
2242 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2243 __ Srl(dst, lhs, shift_value);
2244 __ Or(dst, dst, TMP);
2245 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002246 }
2247 } else {
2248 if (instr->IsShl()) {
2249 __ Sllv(dst, lhs, rhs_reg);
2250 } else if (instr->IsShr()) {
2251 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002252 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002253 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002254 } else {
2255 if (has_ins_rotr) {
2256 __ Rotrv(dst, lhs, rhs_reg);
2257 } else {
2258 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002259 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2260 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2261 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2262 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2263 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002264 __ Sllv(TMP, lhs, TMP);
2265 __ Srlv(dst, lhs, rhs_reg);
2266 __ Or(dst, dst, TMP);
2267 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268 }
2269 }
2270 break;
2271 }
2272
2273 case Primitive::kPrimLong: {
2274 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2275 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2276 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2277 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2278 if (use_imm) {
2279 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002280 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002282 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002283 if (instr->IsShl()) {
2284 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2285 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2286 __ Sll(dst_low, lhs_low, shift_value);
2287 } else if (instr->IsShr()) {
2288 __ Srl(dst_low, lhs_low, shift_value);
2289 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2290 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002291 } else if (instr->IsUShr()) {
2292 __ Srl(dst_low, lhs_low, shift_value);
2293 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2294 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002295 } else {
2296 __ Srl(dst_low, lhs_low, shift_value);
2297 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2298 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002299 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002300 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002302 if (instr->IsShl()) {
2303 __ Sll(dst_low, lhs_low, shift_value);
2304 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2305 __ Sll(dst_high, lhs_high, shift_value);
2306 __ Or(dst_high, dst_high, TMP);
2307 } else if (instr->IsShr()) {
2308 __ Sra(dst_high, lhs_high, shift_value);
2309 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2310 __ Srl(dst_low, lhs_low, shift_value);
2311 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002312 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002313 __ Srl(dst_high, lhs_high, shift_value);
2314 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2315 __ Srl(dst_low, lhs_low, shift_value);
2316 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002317 } else {
2318 __ Srl(TMP, lhs_low, shift_value);
2319 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2320 __ Or(dst_low, dst_low, TMP);
2321 __ Srl(TMP, lhs_high, shift_value);
2322 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2323 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002324 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 }
2326 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002327 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002328 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002329 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002330 __ Move(dst_low, ZERO);
2331 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002332 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002333 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002334 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002335 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002336 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002338 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 // 64-bit rotation by 32 is just a swap.
2340 __ Move(dst_low, lhs_high);
2341 __ Move(dst_high, lhs_low);
2342 } else {
2343 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002344 __ Srl(dst_low, lhs_high, shift_value_high);
2345 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2346 __ Srl(dst_high, lhs_low, shift_value_high);
2347 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002348 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002349 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2350 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002351 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002352 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2353 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 __ Or(dst_high, dst_high, TMP);
2355 }
2356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 }
2358 }
2359 } else {
2360 MipsLabel done;
2361 if (instr->IsShl()) {
2362 __ Sllv(dst_low, lhs_low, rhs_reg);
2363 __ Nor(AT, ZERO, rhs_reg);
2364 __ Srl(TMP, lhs_low, 1);
2365 __ Srlv(TMP, TMP, AT);
2366 __ Sllv(dst_high, lhs_high, rhs_reg);
2367 __ Or(dst_high, dst_high, TMP);
2368 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2369 __ Beqz(TMP, &done);
2370 __ Move(dst_high, dst_low);
2371 __ Move(dst_low, ZERO);
2372 } else if (instr->IsShr()) {
2373 __ Srav(dst_high, lhs_high, rhs_reg);
2374 __ Nor(AT, ZERO, rhs_reg);
2375 __ Sll(TMP, lhs_high, 1);
2376 __ Sllv(TMP, TMP, AT);
2377 __ Srlv(dst_low, lhs_low, rhs_reg);
2378 __ Or(dst_low, dst_low, TMP);
2379 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2380 __ Beqz(TMP, &done);
2381 __ Move(dst_low, dst_high);
2382 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002383 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384 __ Srlv(dst_high, lhs_high, rhs_reg);
2385 __ Nor(AT, ZERO, rhs_reg);
2386 __ Sll(TMP, lhs_high, 1);
2387 __ Sllv(TMP, TMP, AT);
2388 __ Srlv(dst_low, lhs_low, rhs_reg);
2389 __ Or(dst_low, dst_low, TMP);
2390 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2391 __ Beqz(TMP, &done);
2392 __ Move(dst_low, dst_high);
2393 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002394 } else {
2395 __ Nor(AT, ZERO, rhs_reg);
2396 __ Srlv(TMP, lhs_low, rhs_reg);
2397 __ Sll(dst_low, lhs_high, 1);
2398 __ Sllv(dst_low, dst_low, AT);
2399 __ Or(dst_low, dst_low, TMP);
2400 __ Srlv(TMP, lhs_high, rhs_reg);
2401 __ Sll(dst_high, lhs_low, 1);
2402 __ Sllv(dst_high, dst_high, AT);
2403 __ Or(dst_high, dst_high, TMP);
2404 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2405 __ Beqz(TMP, &done);
2406 __ Move(TMP, dst_high);
2407 __ Move(dst_high, dst_low);
2408 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002409 }
2410 __ Bind(&done);
2411 }
2412 break;
2413 }
2414
2415 default:
2416 LOG(FATAL) << "Unexpected shift operation type " << type;
2417 }
2418}
2419
2420void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2421 HandleBinaryOp(instruction);
2422}
2423
2424void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2425 HandleBinaryOp(instruction);
2426}
2427
2428void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2429 HandleBinaryOp(instruction);
2430}
2431
2432void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2433 HandleBinaryOp(instruction);
2434}
2435
2436void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002437 Primitive::Type type = instruction->GetType();
2438 bool object_array_get_with_read_barrier =
2439 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002441 new (GetGraph()->GetArena()) LocationSummary(instruction,
2442 object_array_get_with_read_barrier
2443 ? LocationSummary::kCallOnSlowPath
2444 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002445 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2446 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002448 locations->SetInAt(0, Location::RequiresRegister());
2449 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002450 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002451 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2452 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002453 // The output overlaps in the case of an object array get with
2454 // read barriers enabled: we do not want the move to overwrite the
2455 // array's location, as we need it to emit the read barrier.
2456 locations->SetOut(Location::RequiresRegister(),
2457 object_array_get_with_read_barrier
2458 ? Location::kOutputOverlap
2459 : Location::kNoOutputOverlap);
2460 }
2461 // We need a temporary register for the read barrier marking slow
2462 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2463 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2464 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002465 }
2466}
2467
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002468static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2469 auto null_checker = [codegen, instruction]() {
2470 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002471 };
2472 return null_checker;
2473}
2474
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002475void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2476 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002477 Location obj_loc = locations->InAt(0);
2478 Register obj = obj_loc.AsRegister<Register>();
2479 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002480 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002481 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002482 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002483
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002484 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002485 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2486 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002487 switch (type) {
2488 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002489 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002490 if (index.IsConstant()) {
2491 size_t offset =
2492 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002493 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002494 } else {
2495 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002496 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002497 }
2498 break;
2499 }
2500
2501 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002502 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002503 if (index.IsConstant()) {
2504 size_t offset =
2505 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002506 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 } else {
2508 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002509 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002510 }
2511 break;
2512 }
2513
2514 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002515 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002516 if (index.IsConstant()) {
2517 size_t offset =
2518 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002519 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002520 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002521 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002522 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002523 }
2524 break;
2525 }
2526
2527 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002528 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002529 if (maybe_compressed_char_at) {
2530 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2531 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2532 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2533 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2534 "Expecting 0=compressed, 1=uncompressed");
2535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002536 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002537 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2538 if (maybe_compressed_char_at) {
2539 MipsLabel uncompressed_load, done;
2540 __ Bnez(TMP, &uncompressed_load);
2541 __ LoadFromOffset(kLoadUnsignedByte,
2542 out,
2543 obj,
2544 data_offset + (const_index << TIMES_1));
2545 __ B(&done);
2546 __ Bind(&uncompressed_load);
2547 __ LoadFromOffset(kLoadUnsignedHalfword,
2548 out,
2549 obj,
2550 data_offset + (const_index << TIMES_2));
2551 __ Bind(&done);
2552 } else {
2553 __ LoadFromOffset(kLoadUnsignedHalfword,
2554 out,
2555 obj,
2556 data_offset + (const_index << TIMES_2),
2557 null_checker);
2558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002559 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002560 Register index_reg = index.AsRegister<Register>();
2561 if (maybe_compressed_char_at) {
2562 MipsLabel uncompressed_load, done;
2563 __ Bnez(TMP, &uncompressed_load);
2564 __ Addu(TMP, obj, index_reg);
2565 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2566 __ B(&done);
2567 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002568 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002569 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2570 __ Bind(&done);
2571 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002572 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002573 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2574 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002575 }
2576 break;
2577 }
2578
Alexey Frunze15958152017-02-09 19:08:30 -08002579 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002581 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002582 if (index.IsConstant()) {
2583 size_t offset =
2584 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002585 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002586 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002587 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002588 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 }
2590 break;
2591 }
2592
Alexey Frunze15958152017-02-09 19:08:30 -08002593 case Primitive::kPrimNot: {
2594 static_assert(
2595 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2596 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2597 // /* HeapReference<Object> */ out =
2598 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2599 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2600 Location temp = locations->GetTemp(0);
2601 // Note that a potential implicit null check is handled in this
2602 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2603 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2604 out_loc,
2605 obj,
2606 data_offset,
2607 index,
2608 temp,
2609 /* needs_null_check */ true);
2610 } else {
2611 Register out = out_loc.AsRegister<Register>();
2612 if (index.IsConstant()) {
2613 size_t offset =
2614 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2615 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2616 // If read barriers are enabled, emit read barriers other than
2617 // Baker's using a slow path (and also unpoison the loaded
2618 // reference, if heap poisoning is enabled).
2619 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2620 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002621 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002622 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2623 // If read barriers are enabled, emit read barriers other than
2624 // Baker's using a slow path (and also unpoison the loaded
2625 // reference, if heap poisoning is enabled).
2626 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2627 out_loc,
2628 out_loc,
2629 obj_loc,
2630 data_offset,
2631 index);
2632 }
2633 }
2634 break;
2635 }
2636
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002637 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002638 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002639 if (index.IsConstant()) {
2640 size_t offset =
2641 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002642 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002644 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002645 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002646 }
2647 break;
2648 }
2649
2650 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002651 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002652 if (index.IsConstant()) {
2653 size_t offset =
2654 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002655 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002657 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002658 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002659 }
2660 break;
2661 }
2662
2663 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002664 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002665 if (index.IsConstant()) {
2666 size_t offset =
2667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002668 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002670 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002671 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 }
2673 break;
2674 }
2675
2676 case Primitive::kPrimVoid:
2677 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2678 UNREACHABLE();
2679 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002680}
2681
2682void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2683 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2684 locations->SetInAt(0, Location::RequiresRegister());
2685 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2686}
2687
2688void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2689 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002690 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691 Register obj = locations->InAt(0).AsRegister<Register>();
2692 Register out = locations->Out().AsRegister<Register>();
2693 __ LoadFromOffset(kLoadWord, out, obj, offset);
2694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002695 // Mask out compression flag from String's array length.
2696 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2697 __ Srl(out, out, 1u);
2698 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699}
2700
Alexey Frunzef58b2482016-09-02 22:14:06 -07002701Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2702 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2703 ? Location::ConstantLocation(instruction->AsConstant())
2704 : Location::RequiresRegister();
2705}
2706
2707Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2708 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2709 // We can store a non-zero float or double constant without first loading it into the FPU,
2710 // but we should only prefer this if the constant has a single use.
2711 if (instruction->IsConstant() &&
2712 (instruction->AsConstant()->IsZeroBitPattern() ||
2713 instruction->GetUses().HasExactlyOneElement())) {
2714 return Location::ConstantLocation(instruction->AsConstant());
2715 // Otherwise fall through and require an FPU register for the constant.
2716 }
2717 return Location::RequiresFpuRegister();
2718}
2719
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002720void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002721 Primitive::Type value_type = instruction->GetComponentType();
2722
2723 bool needs_write_barrier =
2724 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2725 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2726
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002727 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2728 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002729 may_need_runtime_call_for_type_check ?
2730 LocationSummary::kCallOnSlowPath :
2731 LocationSummary::kNoCall);
2732
2733 locations->SetInAt(0, Location::RequiresRegister());
2734 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2735 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2736 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002737 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002738 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2739 }
2740 if (needs_write_barrier) {
2741 // Temporary register for the write barrier.
2742 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002743 }
2744}
2745
2746void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2747 LocationSummary* locations = instruction->GetLocations();
2748 Register obj = locations->InAt(0).AsRegister<Register>();
2749 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002750 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002751 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002752 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002753 bool needs_write_barrier =
2754 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002755 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002756 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002757
2758 switch (value_type) {
2759 case Primitive::kPrimBoolean:
2760 case Primitive::kPrimByte: {
2761 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002762 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002763 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002764 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002765 __ Addu(base_reg, obj, index.AsRegister<Register>());
2766 }
2767 if (value_location.IsConstant()) {
2768 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2769 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2770 } else {
2771 Register value = value_location.AsRegister<Register>();
2772 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002773 }
2774 break;
2775 }
2776
2777 case Primitive::kPrimShort:
2778 case Primitive::kPrimChar: {
2779 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002780 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002781 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002783 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002784 }
2785 if (value_location.IsConstant()) {
2786 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2787 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2788 } else {
2789 Register value = value_location.AsRegister<Register>();
2790 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 }
2792 break;
2793 }
2794
Alexey Frunze15958152017-02-09 19:08:30 -08002795 case Primitive::kPrimInt: {
2796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2797 if (index.IsConstant()) {
2798 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2799 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002800 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002801 }
2802 if (value_location.IsConstant()) {
2803 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2804 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2805 } else {
2806 Register value = value_location.AsRegister<Register>();
2807 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2808 }
2809 break;
2810 }
2811
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002813 if (value_location.IsConstant()) {
2814 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002816 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002817 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002818 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002819 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 }
Alexey Frunze15958152017-02-09 19:08:30 -08002821 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2822 DCHECK_EQ(value, 0);
2823 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2824 DCHECK(!needs_write_barrier);
2825 DCHECK(!may_need_runtime_call_for_type_check);
2826 break;
2827 }
2828
2829 DCHECK(needs_write_barrier);
2830 Register value = value_location.AsRegister<Register>();
2831 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2832 Register temp2 = TMP; // Doesn't need to survive slow path.
2833 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2834 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2835 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2836 MipsLabel done;
2837 SlowPathCodeMIPS* slow_path = nullptr;
2838
2839 if (may_need_runtime_call_for_type_check) {
2840 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2841 codegen_->AddSlowPath(slow_path);
2842 if (instruction->GetValueCanBeNull()) {
2843 MipsLabel non_zero;
2844 __ Bnez(value, &non_zero);
2845 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2846 if (index.IsConstant()) {
2847 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002848 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002849 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002850 }
Alexey Frunze15958152017-02-09 19:08:30 -08002851 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2852 __ B(&done);
2853 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002854 }
Alexey Frunze15958152017-02-09 19:08:30 -08002855
2856 // Note that when read barriers are enabled, the type checks
2857 // are performed without read barriers. This is fine, even in
2858 // the case where a class object is in the from-space after
2859 // the flip, as a comparison involving such a type would not
2860 // produce a false positive; it may of course produce a false
2861 // negative, in which case we would take the ArraySet slow
2862 // path.
2863
2864 // /* HeapReference<Class> */ temp1 = obj->klass_
2865 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2866 __ MaybeUnpoisonHeapReference(temp1);
2867
2868 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2869 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2870 // /* HeapReference<Class> */ temp2 = value->klass_
2871 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2872 // If heap poisoning is enabled, no need to unpoison `temp1`
2873 // nor `temp2`, as we are comparing two poisoned references.
2874
2875 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2876 MipsLabel do_put;
2877 __ Beq(temp1, temp2, &do_put);
2878 // If heap poisoning is enabled, the `temp1` reference has
2879 // not been unpoisoned yet; unpoison it now.
2880 __ MaybeUnpoisonHeapReference(temp1);
2881
2882 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2883 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2884 // If heap poisoning is enabled, no need to unpoison
2885 // `temp1`, as we are comparing against null below.
2886 __ Bnez(temp1, slow_path->GetEntryLabel());
2887 __ Bind(&do_put);
2888 } else {
2889 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2890 }
2891 }
2892
2893 Register source = value;
2894 if (kPoisonHeapReferences) {
2895 // Note that in the case where `value` is a null reference,
2896 // we do not enter this block, as a null reference does not
2897 // need poisoning.
2898 __ Move(temp1, value);
2899 __ PoisonHeapReference(temp1);
2900 source = temp1;
2901 }
2902
2903 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2904 if (index.IsConstant()) {
2905 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002907 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002908 }
2909 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2910
2911 if (!may_need_runtime_call_for_type_check) {
2912 codegen_->MaybeRecordImplicitNullCheck(instruction);
2913 }
2914
2915 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2916
2917 if (done.IsLinked()) {
2918 __ Bind(&done);
2919 }
2920
2921 if (slow_path != nullptr) {
2922 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 }
2924 break;
2925 }
2926
2927 case Primitive::kPrimLong: {
2928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002930 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002932 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002933 }
2934 if (value_location.IsConstant()) {
2935 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2936 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2937 } else {
2938 Register value = value_location.AsRegisterPairLow<Register>();
2939 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002940 }
2941 break;
2942 }
2943
2944 case Primitive::kPrimFloat: {
2945 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002946 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002947 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002948 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002949 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002950 }
2951 if (value_location.IsConstant()) {
2952 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2953 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2954 } else {
2955 FRegister value = value_location.AsFpuRegister<FRegister>();
2956 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002957 }
2958 break;
2959 }
2960
2961 case Primitive::kPrimDouble: {
2962 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002964 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002966 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002967 }
2968 if (value_location.IsConstant()) {
2969 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2970 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2971 } else {
2972 FRegister value = value_location.AsFpuRegister<FRegister>();
2973 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002974 }
2975 break;
2976 }
2977
2978 case Primitive::kPrimVoid:
2979 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2980 UNREACHABLE();
2981 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002982}
2983
2984void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002985 RegisterSet caller_saves = RegisterSet::Empty();
2986 InvokeRuntimeCallingConvention calling_convention;
2987 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2988 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2989 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990 locations->SetInAt(0, Location::RequiresRegister());
2991 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002992}
2993
2994void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2995 LocationSummary* locations = instruction->GetLocations();
2996 BoundsCheckSlowPathMIPS* slow_path =
2997 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2998 codegen_->AddSlowPath(slow_path);
2999
3000 Register index = locations->InAt(0).AsRegister<Register>();
3001 Register length = locations->InAt(1).AsRegister<Register>();
3002
3003 // length is limited by the maximum positive signed 32-bit integer.
3004 // Unsigned comparison of length and index checks for index < 0
3005 // and for length <= index simultaneously.
3006 __ Bgeu(index, length, slow_path->GetEntryLabel());
3007}
3008
Alexey Frunze15958152017-02-09 19:08:30 -08003009// Temp is used for read barrier.
3010static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3011 if (kEmitCompilerReadBarrier &&
3012 (kUseBakerReadBarrier ||
3013 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3014 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3015 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3016 return 1;
3017 }
3018 return 0;
3019}
3020
3021// Extra temp is used for read barrier.
3022static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3023 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3024}
3025
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003026void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003027 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3028 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3029
3030 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3031 switch (type_check_kind) {
3032 case TypeCheckKind::kExactCheck:
3033 case TypeCheckKind::kAbstractClassCheck:
3034 case TypeCheckKind::kClassHierarchyCheck:
3035 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003036 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003037 ? LocationSummary::kCallOnSlowPath
3038 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3039 break;
3040 case TypeCheckKind::kArrayCheck:
3041 case TypeCheckKind::kUnresolvedCheck:
3042 case TypeCheckKind::kInterfaceCheck:
3043 call_kind = LocationSummary::kCallOnSlowPath;
3044 break;
3045 }
3046
3047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003048 locations->SetInAt(0, Location::RequiresRegister());
3049 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003050 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003051}
3052
3053void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003054 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003055 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003056 Location obj_loc = locations->InAt(0);
3057 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003058 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003059 Location temp_loc = locations->GetTemp(0);
3060 Register temp = temp_loc.AsRegister<Register>();
3061 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3062 DCHECK_LE(num_temps, 2u);
3063 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003064 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3065 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3066 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3067 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3068 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3069 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3070 const uint32_t object_array_data_offset =
3071 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3072 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003074 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3075 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3076 // read barriers is done for performance and code size reasons.
3077 bool is_type_check_slow_path_fatal = false;
3078 if (!kEmitCompilerReadBarrier) {
3079 is_type_check_slow_path_fatal =
3080 (type_check_kind == TypeCheckKind::kExactCheck ||
3081 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3082 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3083 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3084 !instruction->CanThrowIntoCatchBlock();
3085 }
3086 SlowPathCodeMIPS* slow_path =
3087 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3088 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 codegen_->AddSlowPath(slow_path);
3090
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003091 // Avoid this check if we know `obj` is not null.
3092 if (instruction->MustDoNullCheck()) {
3093 __ Beqz(obj, &done);
3094 }
3095
3096 switch (type_check_kind) {
3097 case TypeCheckKind::kExactCheck:
3098 case TypeCheckKind::kArrayCheck: {
3099 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003100 GenerateReferenceLoadTwoRegisters(instruction,
3101 temp_loc,
3102 obj_loc,
3103 class_offset,
3104 maybe_temp2_loc,
3105 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003106 // Jump to slow path for throwing the exception or doing a
3107 // more involved array check.
3108 __ Bne(temp, cls, slow_path->GetEntryLabel());
3109 break;
3110 }
3111
3112 case TypeCheckKind::kAbstractClassCheck: {
3113 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003114 GenerateReferenceLoadTwoRegisters(instruction,
3115 temp_loc,
3116 obj_loc,
3117 class_offset,
3118 maybe_temp2_loc,
3119 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003120 // If the class is abstract, we eagerly fetch the super class of the
3121 // object to avoid doing a comparison we know will fail.
3122 MipsLabel loop;
3123 __ Bind(&loop);
3124 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003125 GenerateReferenceLoadOneRegister(instruction,
3126 temp_loc,
3127 super_offset,
3128 maybe_temp2_loc,
3129 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003130 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3131 // exception.
3132 __ Beqz(temp, slow_path->GetEntryLabel());
3133 // Otherwise, compare the classes.
3134 __ Bne(temp, cls, &loop);
3135 break;
3136 }
3137
3138 case TypeCheckKind::kClassHierarchyCheck: {
3139 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003140 GenerateReferenceLoadTwoRegisters(instruction,
3141 temp_loc,
3142 obj_loc,
3143 class_offset,
3144 maybe_temp2_loc,
3145 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003146 // Walk over the class hierarchy to find a match.
3147 MipsLabel loop;
3148 __ Bind(&loop);
3149 __ Beq(temp, cls, &done);
3150 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003151 GenerateReferenceLoadOneRegister(instruction,
3152 temp_loc,
3153 super_offset,
3154 maybe_temp2_loc,
3155 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003156 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3157 // exception. Otherwise, jump to the beginning of the loop.
3158 __ Bnez(temp, &loop);
3159 __ B(slow_path->GetEntryLabel());
3160 break;
3161 }
3162
3163 case TypeCheckKind::kArrayObjectCheck: {
3164 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003165 GenerateReferenceLoadTwoRegisters(instruction,
3166 temp_loc,
3167 obj_loc,
3168 class_offset,
3169 maybe_temp2_loc,
3170 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003171 // Do an exact check.
3172 __ Beq(temp, cls, &done);
3173 // Otherwise, we need to check that the object's class is a non-primitive array.
3174 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003175 GenerateReferenceLoadOneRegister(instruction,
3176 temp_loc,
3177 component_offset,
3178 maybe_temp2_loc,
3179 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003180 // If the component type is null, jump to the slow path to throw the exception.
3181 __ Beqz(temp, slow_path->GetEntryLabel());
3182 // Otherwise, the object is indeed an array, further check that this component
3183 // type is not a primitive type.
3184 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3185 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3186 __ Bnez(temp, slow_path->GetEntryLabel());
3187 break;
3188 }
3189
3190 case TypeCheckKind::kUnresolvedCheck:
3191 // We always go into the type check slow path for the unresolved check case.
3192 // We cannot directly call the CheckCast runtime entry point
3193 // without resorting to a type checking slow path here (i.e. by
3194 // calling InvokeRuntime directly), as it would require to
3195 // assign fixed registers for the inputs of this HInstanceOf
3196 // instruction (following the runtime calling convention), which
3197 // might be cluttered by the potential first read barrier
3198 // emission at the beginning of this method.
3199 __ B(slow_path->GetEntryLabel());
3200 break;
3201
3202 case TypeCheckKind::kInterfaceCheck: {
3203 // Avoid read barriers to improve performance of the fast path. We can not get false
3204 // positives by doing this.
3205 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003206 GenerateReferenceLoadTwoRegisters(instruction,
3207 temp_loc,
3208 obj_loc,
3209 class_offset,
3210 maybe_temp2_loc,
3211 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003212 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003213 GenerateReferenceLoadTwoRegisters(instruction,
3214 temp_loc,
3215 temp_loc,
3216 iftable_offset,
3217 maybe_temp2_loc,
3218 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003219 // Iftable is never null.
3220 __ Lw(TMP, temp, array_length_offset);
3221 // Loop through the iftable and check if any class matches.
3222 MipsLabel loop;
3223 __ Bind(&loop);
3224 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3225 __ Beqz(TMP, slow_path->GetEntryLabel());
3226 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3227 __ MaybeUnpoisonHeapReference(AT);
3228 // Go to next interface.
3229 __ Addiu(TMP, TMP, -2);
3230 // Compare the classes and continue the loop if they do not match.
3231 __ Bne(AT, cls, &loop);
3232 break;
3233 }
3234 }
3235
3236 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003237 __ Bind(slow_path->GetExitLabel());
3238}
3239
3240void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3241 LocationSummary* locations =
3242 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3243 locations->SetInAt(0, Location::RequiresRegister());
3244 if (check->HasUses()) {
3245 locations->SetOut(Location::SameAsFirstInput());
3246 }
3247}
3248
3249void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3250 // We assume the class is not null.
3251 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3252 check->GetLoadClass(),
3253 check,
3254 check->GetDexPc(),
3255 true);
3256 codegen_->AddSlowPath(slow_path);
3257 GenerateClassInitializationCheck(slow_path,
3258 check->GetLocations()->InAt(0).AsRegister<Register>());
3259}
3260
3261void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3262 Primitive::Type in_type = compare->InputAt(0)->GetType();
3263
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003264 LocationSummary* locations =
3265 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003266
3267 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003268 case Primitive::kPrimBoolean:
3269 case Primitive::kPrimByte:
3270 case Primitive::kPrimShort:
3271 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003272 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003273 locations->SetInAt(0, Location::RequiresRegister());
3274 locations->SetInAt(1, Location::RequiresRegister());
3275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3276 break;
3277
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003278 case Primitive::kPrimLong:
3279 locations->SetInAt(0, Location::RequiresRegister());
3280 locations->SetInAt(1, Location::RequiresRegister());
3281 // Output overlaps because it is written before doing the low comparison.
3282 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3283 break;
3284
3285 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003286 case Primitive::kPrimDouble:
3287 locations->SetInAt(0, Location::RequiresFpuRegister());
3288 locations->SetInAt(1, Location::RequiresFpuRegister());
3289 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003290 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003291
3292 default:
3293 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3294 }
3295}
3296
3297void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3298 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003299 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003301 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003302
3303 // 0 if: left == right
3304 // 1 if: left > right
3305 // -1 if: left < right
3306 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003307 case Primitive::kPrimBoolean:
3308 case Primitive::kPrimByte:
3309 case Primitive::kPrimShort:
3310 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003311 case Primitive::kPrimInt: {
3312 Register lhs = locations->InAt(0).AsRegister<Register>();
3313 Register rhs = locations->InAt(1).AsRegister<Register>();
3314 __ Slt(TMP, lhs, rhs);
3315 __ Slt(res, rhs, lhs);
3316 __ Subu(res, res, TMP);
3317 break;
3318 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003319 case Primitive::kPrimLong: {
3320 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003321 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3322 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3323 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3324 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3325 // TODO: more efficient (direct) comparison with a constant.
3326 __ Slt(TMP, lhs_high, rhs_high);
3327 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3328 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3329 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3330 __ Sltu(TMP, lhs_low, rhs_low);
3331 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3332 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3333 __ Bind(&done);
3334 break;
3335 }
3336
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003337 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003338 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003339 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3340 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3341 MipsLabel done;
3342 if (isR6) {
3343 __ CmpEqS(FTMP, lhs, rhs);
3344 __ LoadConst32(res, 0);
3345 __ Bc1nez(FTMP, &done);
3346 if (gt_bias) {
3347 __ CmpLtS(FTMP, lhs, rhs);
3348 __ LoadConst32(res, -1);
3349 __ Bc1nez(FTMP, &done);
3350 __ LoadConst32(res, 1);
3351 } else {
3352 __ CmpLtS(FTMP, rhs, lhs);
3353 __ LoadConst32(res, 1);
3354 __ Bc1nez(FTMP, &done);
3355 __ LoadConst32(res, -1);
3356 }
3357 } else {
3358 if (gt_bias) {
3359 __ ColtS(0, lhs, rhs);
3360 __ LoadConst32(res, -1);
3361 __ Bc1t(0, &done);
3362 __ CeqS(0, lhs, rhs);
3363 __ LoadConst32(res, 1);
3364 __ Movt(res, ZERO, 0);
3365 } else {
3366 __ ColtS(0, rhs, lhs);
3367 __ LoadConst32(res, 1);
3368 __ Bc1t(0, &done);
3369 __ CeqS(0, lhs, rhs);
3370 __ LoadConst32(res, -1);
3371 __ Movt(res, ZERO, 0);
3372 }
3373 }
3374 __ Bind(&done);
3375 break;
3376 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003377 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003378 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003379 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3380 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3381 MipsLabel done;
3382 if (isR6) {
3383 __ CmpEqD(FTMP, lhs, rhs);
3384 __ LoadConst32(res, 0);
3385 __ Bc1nez(FTMP, &done);
3386 if (gt_bias) {
3387 __ CmpLtD(FTMP, lhs, rhs);
3388 __ LoadConst32(res, -1);
3389 __ Bc1nez(FTMP, &done);
3390 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003391 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003392 __ CmpLtD(FTMP, rhs, lhs);
3393 __ LoadConst32(res, 1);
3394 __ Bc1nez(FTMP, &done);
3395 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003396 }
3397 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003398 if (gt_bias) {
3399 __ ColtD(0, lhs, rhs);
3400 __ LoadConst32(res, -1);
3401 __ Bc1t(0, &done);
3402 __ CeqD(0, lhs, rhs);
3403 __ LoadConst32(res, 1);
3404 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003405 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003406 __ ColtD(0, rhs, lhs);
3407 __ LoadConst32(res, 1);
3408 __ Bc1t(0, &done);
3409 __ CeqD(0, lhs, rhs);
3410 __ LoadConst32(res, -1);
3411 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 }
3413 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003414 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003415 break;
3416 }
3417
3418 default:
3419 LOG(FATAL) << "Unimplemented compare type " << in_type;
3420 }
3421}
3422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003423void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003425 switch (instruction->InputAt(0)->GetType()) {
3426 default:
3427 case Primitive::kPrimLong:
3428 locations->SetInAt(0, Location::RequiresRegister());
3429 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3430 break;
3431
3432 case Primitive::kPrimFloat:
3433 case Primitive::kPrimDouble:
3434 locations->SetInAt(0, Location::RequiresFpuRegister());
3435 locations->SetInAt(1, Location::RequiresFpuRegister());
3436 break;
3437 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003438 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3440 }
3441}
3442
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003443void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003444 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445 return;
3446 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003447
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003448 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003449 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003450
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003451 switch (type) {
3452 default:
3453 // Integer case.
3454 GenerateIntCompare(instruction->GetCondition(), locations);
3455 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003456
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003457 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003458 GenerateLongCompare(instruction->GetCondition(), locations);
3459 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003460
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003461 case Primitive::kPrimFloat:
3462 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003463 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3464 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003465 }
3466}
3467
Alexey Frunze7e99e052015-11-24 19:28:01 -08003468void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3469 DCHECK(instruction->IsDiv() || instruction->IsRem());
3470 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3471
3472 LocationSummary* locations = instruction->GetLocations();
3473 Location second = locations->InAt(1);
3474 DCHECK(second.IsConstant());
3475
3476 Register out = locations->Out().AsRegister<Register>();
3477 Register dividend = locations->InAt(0).AsRegister<Register>();
3478 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3479 DCHECK(imm == 1 || imm == -1);
3480
3481 if (instruction->IsRem()) {
3482 __ Move(out, ZERO);
3483 } else {
3484 if (imm == -1) {
3485 __ Subu(out, ZERO, dividend);
3486 } else if (out != dividend) {
3487 __ Move(out, dividend);
3488 }
3489 }
3490}
3491
3492void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3493 DCHECK(instruction->IsDiv() || instruction->IsRem());
3494 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3495
3496 LocationSummary* locations = instruction->GetLocations();
3497 Location second = locations->InAt(1);
3498 DCHECK(second.IsConstant());
3499
3500 Register out = locations->Out().AsRegister<Register>();
3501 Register dividend = locations->InAt(0).AsRegister<Register>();
3502 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003503 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003504 int ctz_imm = CTZ(abs_imm);
3505
3506 if (instruction->IsDiv()) {
3507 if (ctz_imm == 1) {
3508 // Fast path for division by +/-2, which is very common.
3509 __ Srl(TMP, dividend, 31);
3510 } else {
3511 __ Sra(TMP, dividend, 31);
3512 __ Srl(TMP, TMP, 32 - ctz_imm);
3513 }
3514 __ Addu(out, dividend, TMP);
3515 __ Sra(out, out, ctz_imm);
3516 if (imm < 0) {
3517 __ Subu(out, ZERO, out);
3518 }
3519 } else {
3520 if (ctz_imm == 1) {
3521 // Fast path for modulo +/-2, which is very common.
3522 __ Sra(TMP, dividend, 31);
3523 __ Subu(out, dividend, TMP);
3524 __ Andi(out, out, 1);
3525 __ Addu(out, out, TMP);
3526 } else {
3527 __ Sra(TMP, dividend, 31);
3528 __ Srl(TMP, TMP, 32 - ctz_imm);
3529 __ Addu(out, dividend, TMP);
3530 if (IsUint<16>(abs_imm - 1)) {
3531 __ Andi(out, out, abs_imm - 1);
3532 } else {
3533 __ Sll(out, out, 32 - ctz_imm);
3534 __ Srl(out, out, 32 - ctz_imm);
3535 }
3536 __ Subu(out, out, TMP);
3537 }
3538 }
3539}
3540
3541void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3542 DCHECK(instruction->IsDiv() || instruction->IsRem());
3543 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3544
3545 LocationSummary* locations = instruction->GetLocations();
3546 Location second = locations->InAt(1);
3547 DCHECK(second.IsConstant());
3548
3549 Register out = locations->Out().AsRegister<Register>();
3550 Register dividend = locations->InAt(0).AsRegister<Register>();
3551 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3552
3553 int64_t magic;
3554 int shift;
3555 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3556
3557 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3558
3559 __ LoadConst32(TMP, magic);
3560 if (isR6) {
3561 __ MuhR6(TMP, dividend, TMP);
3562 } else {
3563 __ MultR2(dividend, TMP);
3564 __ Mfhi(TMP);
3565 }
3566 if (imm > 0 && magic < 0) {
3567 __ Addu(TMP, TMP, dividend);
3568 } else if (imm < 0 && magic > 0) {
3569 __ Subu(TMP, TMP, dividend);
3570 }
3571
3572 if (shift != 0) {
3573 __ Sra(TMP, TMP, shift);
3574 }
3575
3576 if (instruction->IsDiv()) {
3577 __ Sra(out, TMP, 31);
3578 __ Subu(out, TMP, out);
3579 } else {
3580 __ Sra(AT, TMP, 31);
3581 __ Subu(AT, TMP, AT);
3582 __ LoadConst32(TMP, imm);
3583 if (isR6) {
3584 __ MulR6(TMP, AT, TMP);
3585 } else {
3586 __ MulR2(TMP, AT, TMP);
3587 }
3588 __ Subu(out, dividend, TMP);
3589 }
3590}
3591
3592void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3593 DCHECK(instruction->IsDiv() || instruction->IsRem());
3594 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3595
3596 LocationSummary* locations = instruction->GetLocations();
3597 Register out = locations->Out().AsRegister<Register>();
3598 Location second = locations->InAt(1);
3599
3600 if (second.IsConstant()) {
3601 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3602 if (imm == 0) {
3603 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3604 } else if (imm == 1 || imm == -1) {
3605 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003606 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003607 DivRemByPowerOfTwo(instruction);
3608 } else {
3609 DCHECK(imm <= -2 || imm >= 2);
3610 GenerateDivRemWithAnyConstant(instruction);
3611 }
3612 } else {
3613 Register dividend = locations->InAt(0).AsRegister<Register>();
3614 Register divisor = second.AsRegister<Register>();
3615 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3616 if (instruction->IsDiv()) {
3617 if (isR6) {
3618 __ DivR6(out, dividend, divisor);
3619 } else {
3620 __ DivR2(out, dividend, divisor);
3621 }
3622 } else {
3623 if (isR6) {
3624 __ ModR6(out, dividend, divisor);
3625 } else {
3626 __ ModR2(out, dividend, divisor);
3627 }
3628 }
3629 }
3630}
3631
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003632void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3633 Primitive::Type type = div->GetResultType();
3634 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003635 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003636 : LocationSummary::kNoCall;
3637
3638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3639
3640 switch (type) {
3641 case Primitive::kPrimInt:
3642 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003643 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3645 break;
3646
3647 case Primitive::kPrimLong: {
3648 InvokeRuntimeCallingConvention calling_convention;
3649 locations->SetInAt(0, Location::RegisterPairLocation(
3650 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3651 locations->SetInAt(1, Location::RegisterPairLocation(
3652 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3653 locations->SetOut(calling_convention.GetReturnLocation(type));
3654 break;
3655 }
3656
3657 case Primitive::kPrimFloat:
3658 case Primitive::kPrimDouble:
3659 locations->SetInAt(0, Location::RequiresFpuRegister());
3660 locations->SetInAt(1, Location::RequiresFpuRegister());
3661 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3662 break;
3663
3664 default:
3665 LOG(FATAL) << "Unexpected div type " << type;
3666 }
3667}
3668
3669void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3670 Primitive::Type type = instruction->GetType();
3671 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672
3673 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003674 case Primitive::kPrimInt:
3675 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003676 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003678 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3680 break;
3681 }
3682 case Primitive::kPrimFloat:
3683 case Primitive::kPrimDouble: {
3684 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3685 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3686 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3687 if (type == Primitive::kPrimFloat) {
3688 __ DivS(dst, lhs, rhs);
3689 } else {
3690 __ DivD(dst, lhs, rhs);
3691 }
3692 break;
3693 }
3694 default:
3695 LOG(FATAL) << "Unexpected div type " << type;
3696 }
3697}
3698
3699void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003700 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003701 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003702}
3703
3704void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3705 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3706 codegen_->AddSlowPath(slow_path);
3707 Location value = instruction->GetLocations()->InAt(0);
3708 Primitive::Type type = instruction->GetType();
3709
3710 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003711 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003712 case Primitive::kPrimByte:
3713 case Primitive::kPrimChar:
3714 case Primitive::kPrimShort:
3715 case Primitive::kPrimInt: {
3716 if (value.IsConstant()) {
3717 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3718 __ B(slow_path->GetEntryLabel());
3719 } else {
3720 // A division by a non-null constant is valid. We don't need to perform
3721 // any check, so simply fall through.
3722 }
3723 } else {
3724 DCHECK(value.IsRegister()) << value;
3725 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3726 }
3727 break;
3728 }
3729 case Primitive::kPrimLong: {
3730 if (value.IsConstant()) {
3731 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3732 __ B(slow_path->GetEntryLabel());
3733 } else {
3734 // A division by a non-null constant is valid. We don't need to perform
3735 // any check, so simply fall through.
3736 }
3737 } else {
3738 DCHECK(value.IsRegisterPair()) << value;
3739 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3740 __ Beqz(TMP, slow_path->GetEntryLabel());
3741 }
3742 break;
3743 }
3744 default:
3745 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3746 }
3747}
3748
3749void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3750 LocationSummary* locations =
3751 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3752 locations->SetOut(Location::ConstantLocation(constant));
3753}
3754
3755void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3756 // Will be generated at use site.
3757}
3758
3759void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3760 exit->SetLocations(nullptr);
3761}
3762
3763void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3764}
3765
3766void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3767 LocationSummary* locations =
3768 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3769 locations->SetOut(Location::ConstantLocation(constant));
3770}
3771
3772void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3773 // Will be generated at use site.
3774}
3775
3776void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3777 got->SetLocations(nullptr);
3778}
3779
3780void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3781 DCHECK(!successor->IsExitBlock());
3782 HBasicBlock* block = got->GetBlock();
3783 HInstruction* previous = got->GetPrevious();
3784 HLoopInformation* info = block->GetLoopInformation();
3785
3786 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3787 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3788 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3789 return;
3790 }
3791 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3792 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3793 }
3794 if (!codegen_->GoesToNextBlock(block, successor)) {
3795 __ B(codegen_->GetLabelOf(successor));
3796 }
3797}
3798
3799void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3800 HandleGoto(got, got->GetSuccessor());
3801}
3802
3803void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3804 try_boundary->SetLocations(nullptr);
3805}
3806
3807void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3808 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3809 if (!successor->IsExitBlock()) {
3810 HandleGoto(try_boundary, successor);
3811 }
3812}
3813
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003814void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3815 LocationSummary* locations) {
3816 Register dst = locations->Out().AsRegister<Register>();
3817 Register lhs = locations->InAt(0).AsRegister<Register>();
3818 Location rhs_location = locations->InAt(1);
3819 Register rhs_reg = ZERO;
3820 int64_t rhs_imm = 0;
3821 bool use_imm = rhs_location.IsConstant();
3822 if (use_imm) {
3823 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3824 } else {
3825 rhs_reg = rhs_location.AsRegister<Register>();
3826 }
3827
3828 switch (cond) {
3829 case kCondEQ:
3830 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003831 if (use_imm && IsInt<16>(-rhs_imm)) {
3832 if (rhs_imm == 0) {
3833 if (cond == kCondEQ) {
3834 __ Sltiu(dst, lhs, 1);
3835 } else {
3836 __ Sltu(dst, ZERO, lhs);
3837 }
3838 } else {
3839 __ Addiu(dst, lhs, -rhs_imm);
3840 if (cond == kCondEQ) {
3841 __ Sltiu(dst, dst, 1);
3842 } else {
3843 __ Sltu(dst, ZERO, dst);
3844 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003845 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003846 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003847 if (use_imm && IsUint<16>(rhs_imm)) {
3848 __ Xori(dst, lhs, rhs_imm);
3849 } else {
3850 if (use_imm) {
3851 rhs_reg = TMP;
3852 __ LoadConst32(rhs_reg, rhs_imm);
3853 }
3854 __ Xor(dst, lhs, rhs_reg);
3855 }
3856 if (cond == kCondEQ) {
3857 __ Sltiu(dst, dst, 1);
3858 } else {
3859 __ Sltu(dst, ZERO, dst);
3860 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003861 }
3862 break;
3863
3864 case kCondLT:
3865 case kCondGE:
3866 if (use_imm && IsInt<16>(rhs_imm)) {
3867 __ Slti(dst, lhs, rhs_imm);
3868 } else {
3869 if (use_imm) {
3870 rhs_reg = TMP;
3871 __ LoadConst32(rhs_reg, rhs_imm);
3872 }
3873 __ Slt(dst, lhs, rhs_reg);
3874 }
3875 if (cond == kCondGE) {
3876 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3877 // only the slt instruction but no sge.
3878 __ Xori(dst, dst, 1);
3879 }
3880 break;
3881
3882 case kCondLE:
3883 case kCondGT:
3884 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3885 // Simulate lhs <= rhs via lhs < rhs + 1.
3886 __ Slti(dst, lhs, rhs_imm + 1);
3887 if (cond == kCondGT) {
3888 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3889 // only the slti instruction but no sgti.
3890 __ Xori(dst, dst, 1);
3891 }
3892 } else {
3893 if (use_imm) {
3894 rhs_reg = TMP;
3895 __ LoadConst32(rhs_reg, rhs_imm);
3896 }
3897 __ Slt(dst, rhs_reg, lhs);
3898 if (cond == kCondLE) {
3899 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3900 // only the slt instruction but no sle.
3901 __ Xori(dst, dst, 1);
3902 }
3903 }
3904 break;
3905
3906 case kCondB:
3907 case kCondAE:
3908 if (use_imm && IsInt<16>(rhs_imm)) {
3909 // Sltiu sign-extends its 16-bit immediate operand before
3910 // the comparison and thus lets us compare directly with
3911 // unsigned values in the ranges [0, 0x7fff] and
3912 // [0xffff8000, 0xffffffff].
3913 __ Sltiu(dst, lhs, rhs_imm);
3914 } else {
3915 if (use_imm) {
3916 rhs_reg = TMP;
3917 __ LoadConst32(rhs_reg, rhs_imm);
3918 }
3919 __ Sltu(dst, lhs, rhs_reg);
3920 }
3921 if (cond == kCondAE) {
3922 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3923 // only the sltu instruction but no sgeu.
3924 __ Xori(dst, dst, 1);
3925 }
3926 break;
3927
3928 case kCondBE:
3929 case kCondA:
3930 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3931 // Simulate lhs <= rhs via lhs < rhs + 1.
3932 // Note that this only works if rhs + 1 does not overflow
3933 // to 0, hence the check above.
3934 // Sltiu sign-extends its 16-bit immediate operand before
3935 // the comparison and thus lets us compare directly with
3936 // unsigned values in the ranges [0, 0x7fff] and
3937 // [0xffff8000, 0xffffffff].
3938 __ Sltiu(dst, lhs, rhs_imm + 1);
3939 if (cond == kCondA) {
3940 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3941 // only the sltiu instruction but no sgtiu.
3942 __ Xori(dst, dst, 1);
3943 }
3944 } else {
3945 if (use_imm) {
3946 rhs_reg = TMP;
3947 __ LoadConst32(rhs_reg, rhs_imm);
3948 }
3949 __ Sltu(dst, rhs_reg, lhs);
3950 if (cond == kCondBE) {
3951 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3952 // only the sltu instruction but no sleu.
3953 __ Xori(dst, dst, 1);
3954 }
3955 }
3956 break;
3957 }
3958}
3959
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003960bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3961 LocationSummary* input_locations,
3962 Register dst) {
3963 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3964 Location rhs_location = input_locations->InAt(1);
3965 Register rhs_reg = ZERO;
3966 int64_t rhs_imm = 0;
3967 bool use_imm = rhs_location.IsConstant();
3968 if (use_imm) {
3969 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3970 } else {
3971 rhs_reg = rhs_location.AsRegister<Register>();
3972 }
3973
3974 switch (cond) {
3975 case kCondEQ:
3976 case kCondNE:
3977 if (use_imm && IsInt<16>(-rhs_imm)) {
3978 __ Addiu(dst, lhs, -rhs_imm);
3979 } else if (use_imm && IsUint<16>(rhs_imm)) {
3980 __ Xori(dst, lhs, rhs_imm);
3981 } else {
3982 if (use_imm) {
3983 rhs_reg = TMP;
3984 __ LoadConst32(rhs_reg, rhs_imm);
3985 }
3986 __ Xor(dst, lhs, rhs_reg);
3987 }
3988 return (cond == kCondEQ);
3989
3990 case kCondLT:
3991 case kCondGE:
3992 if (use_imm && IsInt<16>(rhs_imm)) {
3993 __ Slti(dst, lhs, rhs_imm);
3994 } else {
3995 if (use_imm) {
3996 rhs_reg = TMP;
3997 __ LoadConst32(rhs_reg, rhs_imm);
3998 }
3999 __ Slt(dst, lhs, rhs_reg);
4000 }
4001 return (cond == kCondGE);
4002
4003 case kCondLE:
4004 case kCondGT:
4005 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4006 // Simulate lhs <= rhs via lhs < rhs + 1.
4007 __ Slti(dst, lhs, rhs_imm + 1);
4008 return (cond == kCondGT);
4009 } else {
4010 if (use_imm) {
4011 rhs_reg = TMP;
4012 __ LoadConst32(rhs_reg, rhs_imm);
4013 }
4014 __ Slt(dst, rhs_reg, lhs);
4015 return (cond == kCondLE);
4016 }
4017
4018 case kCondB:
4019 case kCondAE:
4020 if (use_imm && IsInt<16>(rhs_imm)) {
4021 // Sltiu sign-extends its 16-bit immediate operand before
4022 // the comparison and thus lets us compare directly with
4023 // unsigned values in the ranges [0, 0x7fff] and
4024 // [0xffff8000, 0xffffffff].
4025 __ Sltiu(dst, lhs, rhs_imm);
4026 } else {
4027 if (use_imm) {
4028 rhs_reg = TMP;
4029 __ LoadConst32(rhs_reg, rhs_imm);
4030 }
4031 __ Sltu(dst, lhs, rhs_reg);
4032 }
4033 return (cond == kCondAE);
4034
4035 case kCondBE:
4036 case kCondA:
4037 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4038 // Simulate lhs <= rhs via lhs < rhs + 1.
4039 // Note that this only works if rhs + 1 does not overflow
4040 // to 0, hence the check above.
4041 // Sltiu sign-extends its 16-bit immediate operand before
4042 // the comparison and thus lets us compare directly with
4043 // unsigned values in the ranges [0, 0x7fff] and
4044 // [0xffff8000, 0xffffffff].
4045 __ Sltiu(dst, lhs, rhs_imm + 1);
4046 return (cond == kCondA);
4047 } else {
4048 if (use_imm) {
4049 rhs_reg = TMP;
4050 __ LoadConst32(rhs_reg, rhs_imm);
4051 }
4052 __ Sltu(dst, rhs_reg, lhs);
4053 return (cond == kCondBE);
4054 }
4055 }
4056}
4057
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004058void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4059 LocationSummary* locations,
4060 MipsLabel* label) {
4061 Register lhs = locations->InAt(0).AsRegister<Register>();
4062 Location rhs_location = locations->InAt(1);
4063 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004064 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004065 bool use_imm = rhs_location.IsConstant();
4066 if (use_imm) {
4067 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4068 } else {
4069 rhs_reg = rhs_location.AsRegister<Register>();
4070 }
4071
4072 if (use_imm && rhs_imm == 0) {
4073 switch (cond) {
4074 case kCondEQ:
4075 case kCondBE: // <= 0 if zero
4076 __ Beqz(lhs, label);
4077 break;
4078 case kCondNE:
4079 case kCondA: // > 0 if non-zero
4080 __ Bnez(lhs, label);
4081 break;
4082 case kCondLT:
4083 __ Bltz(lhs, label);
4084 break;
4085 case kCondGE:
4086 __ Bgez(lhs, label);
4087 break;
4088 case kCondLE:
4089 __ Blez(lhs, label);
4090 break;
4091 case kCondGT:
4092 __ Bgtz(lhs, label);
4093 break;
4094 case kCondB: // always false
4095 break;
4096 case kCondAE: // always true
4097 __ B(label);
4098 break;
4099 }
4100 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004101 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4102 if (isR6 || !use_imm) {
4103 if (use_imm) {
4104 rhs_reg = TMP;
4105 __ LoadConst32(rhs_reg, rhs_imm);
4106 }
4107 switch (cond) {
4108 case kCondEQ:
4109 __ Beq(lhs, rhs_reg, label);
4110 break;
4111 case kCondNE:
4112 __ Bne(lhs, rhs_reg, label);
4113 break;
4114 case kCondLT:
4115 __ Blt(lhs, rhs_reg, label);
4116 break;
4117 case kCondGE:
4118 __ Bge(lhs, rhs_reg, label);
4119 break;
4120 case kCondLE:
4121 __ Bge(rhs_reg, lhs, label);
4122 break;
4123 case kCondGT:
4124 __ Blt(rhs_reg, lhs, label);
4125 break;
4126 case kCondB:
4127 __ Bltu(lhs, rhs_reg, label);
4128 break;
4129 case kCondAE:
4130 __ Bgeu(lhs, rhs_reg, label);
4131 break;
4132 case kCondBE:
4133 __ Bgeu(rhs_reg, lhs, label);
4134 break;
4135 case kCondA:
4136 __ Bltu(rhs_reg, lhs, label);
4137 break;
4138 }
4139 } else {
4140 // Special cases for more efficient comparison with constants on R2.
4141 switch (cond) {
4142 case kCondEQ:
4143 __ LoadConst32(TMP, rhs_imm);
4144 __ Beq(lhs, TMP, label);
4145 break;
4146 case kCondNE:
4147 __ LoadConst32(TMP, rhs_imm);
4148 __ Bne(lhs, TMP, label);
4149 break;
4150 case kCondLT:
4151 if (IsInt<16>(rhs_imm)) {
4152 __ Slti(TMP, lhs, rhs_imm);
4153 __ Bnez(TMP, label);
4154 } else {
4155 __ LoadConst32(TMP, rhs_imm);
4156 __ Blt(lhs, TMP, label);
4157 }
4158 break;
4159 case kCondGE:
4160 if (IsInt<16>(rhs_imm)) {
4161 __ Slti(TMP, lhs, rhs_imm);
4162 __ Beqz(TMP, label);
4163 } else {
4164 __ LoadConst32(TMP, rhs_imm);
4165 __ Bge(lhs, TMP, label);
4166 }
4167 break;
4168 case kCondLE:
4169 if (IsInt<16>(rhs_imm + 1)) {
4170 // Simulate lhs <= rhs via lhs < rhs + 1.
4171 __ Slti(TMP, lhs, rhs_imm + 1);
4172 __ Bnez(TMP, label);
4173 } else {
4174 __ LoadConst32(TMP, rhs_imm);
4175 __ Bge(TMP, lhs, label);
4176 }
4177 break;
4178 case kCondGT:
4179 if (IsInt<16>(rhs_imm + 1)) {
4180 // Simulate lhs > rhs via !(lhs < rhs + 1).
4181 __ Slti(TMP, lhs, rhs_imm + 1);
4182 __ Beqz(TMP, label);
4183 } else {
4184 __ LoadConst32(TMP, rhs_imm);
4185 __ Blt(TMP, lhs, label);
4186 }
4187 break;
4188 case kCondB:
4189 if (IsInt<16>(rhs_imm)) {
4190 __ Sltiu(TMP, lhs, rhs_imm);
4191 __ Bnez(TMP, label);
4192 } else {
4193 __ LoadConst32(TMP, rhs_imm);
4194 __ Bltu(lhs, TMP, label);
4195 }
4196 break;
4197 case kCondAE:
4198 if (IsInt<16>(rhs_imm)) {
4199 __ Sltiu(TMP, lhs, rhs_imm);
4200 __ Beqz(TMP, label);
4201 } else {
4202 __ LoadConst32(TMP, rhs_imm);
4203 __ Bgeu(lhs, TMP, label);
4204 }
4205 break;
4206 case kCondBE:
4207 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4208 // Simulate lhs <= rhs via lhs < rhs + 1.
4209 // Note that this only works if rhs + 1 does not overflow
4210 // to 0, hence the check above.
4211 __ Sltiu(TMP, lhs, rhs_imm + 1);
4212 __ Bnez(TMP, label);
4213 } else {
4214 __ LoadConst32(TMP, rhs_imm);
4215 __ Bgeu(TMP, lhs, label);
4216 }
4217 break;
4218 case kCondA:
4219 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4220 // Simulate lhs > rhs via !(lhs < rhs + 1).
4221 // Note that this only works if rhs + 1 does not overflow
4222 // to 0, hence the check above.
4223 __ Sltiu(TMP, lhs, rhs_imm + 1);
4224 __ Beqz(TMP, label);
4225 } else {
4226 __ LoadConst32(TMP, rhs_imm);
4227 __ Bltu(TMP, lhs, label);
4228 }
4229 break;
4230 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004231 }
4232 }
4233}
4234
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004235void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4236 LocationSummary* locations) {
4237 Register dst = locations->Out().AsRegister<Register>();
4238 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4239 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4240 Location rhs_location = locations->InAt(1);
4241 Register rhs_high = ZERO;
4242 Register rhs_low = ZERO;
4243 int64_t imm = 0;
4244 uint32_t imm_high = 0;
4245 uint32_t imm_low = 0;
4246 bool use_imm = rhs_location.IsConstant();
4247 if (use_imm) {
4248 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4249 imm_high = High32Bits(imm);
4250 imm_low = Low32Bits(imm);
4251 } else {
4252 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4253 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4254 }
4255 if (use_imm && imm == 0) {
4256 switch (cond) {
4257 case kCondEQ:
4258 case kCondBE: // <= 0 if zero
4259 __ Or(dst, lhs_high, lhs_low);
4260 __ Sltiu(dst, dst, 1);
4261 break;
4262 case kCondNE:
4263 case kCondA: // > 0 if non-zero
4264 __ Or(dst, lhs_high, lhs_low);
4265 __ Sltu(dst, ZERO, dst);
4266 break;
4267 case kCondLT:
4268 __ Slt(dst, lhs_high, ZERO);
4269 break;
4270 case kCondGE:
4271 __ Slt(dst, lhs_high, ZERO);
4272 __ Xori(dst, dst, 1);
4273 break;
4274 case kCondLE:
4275 __ Or(TMP, lhs_high, lhs_low);
4276 __ Sra(AT, lhs_high, 31);
4277 __ Sltu(dst, AT, TMP);
4278 __ Xori(dst, dst, 1);
4279 break;
4280 case kCondGT:
4281 __ Or(TMP, lhs_high, lhs_low);
4282 __ Sra(AT, lhs_high, 31);
4283 __ Sltu(dst, AT, TMP);
4284 break;
4285 case kCondB: // always false
4286 __ Andi(dst, dst, 0);
4287 break;
4288 case kCondAE: // always true
4289 __ Ori(dst, ZERO, 1);
4290 break;
4291 }
4292 } else if (use_imm) {
4293 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4294 switch (cond) {
4295 case kCondEQ:
4296 __ LoadConst32(TMP, imm_high);
4297 __ Xor(TMP, TMP, lhs_high);
4298 __ LoadConst32(AT, imm_low);
4299 __ Xor(AT, AT, lhs_low);
4300 __ Or(dst, TMP, AT);
4301 __ Sltiu(dst, dst, 1);
4302 break;
4303 case kCondNE:
4304 __ LoadConst32(TMP, imm_high);
4305 __ Xor(TMP, TMP, lhs_high);
4306 __ LoadConst32(AT, imm_low);
4307 __ Xor(AT, AT, lhs_low);
4308 __ Or(dst, TMP, AT);
4309 __ Sltu(dst, ZERO, dst);
4310 break;
4311 case kCondLT:
4312 case kCondGE:
4313 if (dst == lhs_low) {
4314 __ LoadConst32(TMP, imm_low);
4315 __ Sltu(dst, lhs_low, TMP);
4316 }
4317 __ LoadConst32(TMP, imm_high);
4318 __ Slt(AT, lhs_high, TMP);
4319 __ Slt(TMP, TMP, lhs_high);
4320 if (dst != lhs_low) {
4321 __ LoadConst32(dst, imm_low);
4322 __ Sltu(dst, lhs_low, dst);
4323 }
4324 __ Slt(dst, TMP, dst);
4325 __ Or(dst, dst, AT);
4326 if (cond == kCondGE) {
4327 __ Xori(dst, dst, 1);
4328 }
4329 break;
4330 case kCondGT:
4331 case kCondLE:
4332 if (dst == lhs_low) {
4333 __ LoadConst32(TMP, imm_low);
4334 __ Sltu(dst, TMP, lhs_low);
4335 }
4336 __ LoadConst32(TMP, imm_high);
4337 __ Slt(AT, TMP, lhs_high);
4338 __ Slt(TMP, lhs_high, TMP);
4339 if (dst != lhs_low) {
4340 __ LoadConst32(dst, imm_low);
4341 __ Sltu(dst, dst, lhs_low);
4342 }
4343 __ Slt(dst, TMP, dst);
4344 __ Or(dst, dst, AT);
4345 if (cond == kCondLE) {
4346 __ Xori(dst, dst, 1);
4347 }
4348 break;
4349 case kCondB:
4350 case kCondAE:
4351 if (dst == lhs_low) {
4352 __ LoadConst32(TMP, imm_low);
4353 __ Sltu(dst, lhs_low, TMP);
4354 }
4355 __ LoadConst32(TMP, imm_high);
4356 __ Sltu(AT, lhs_high, TMP);
4357 __ Sltu(TMP, TMP, lhs_high);
4358 if (dst != lhs_low) {
4359 __ LoadConst32(dst, imm_low);
4360 __ Sltu(dst, lhs_low, dst);
4361 }
4362 __ Slt(dst, TMP, dst);
4363 __ Or(dst, dst, AT);
4364 if (cond == kCondAE) {
4365 __ Xori(dst, dst, 1);
4366 }
4367 break;
4368 case kCondA:
4369 case kCondBE:
4370 if (dst == lhs_low) {
4371 __ LoadConst32(TMP, imm_low);
4372 __ Sltu(dst, TMP, lhs_low);
4373 }
4374 __ LoadConst32(TMP, imm_high);
4375 __ Sltu(AT, TMP, lhs_high);
4376 __ Sltu(TMP, lhs_high, TMP);
4377 if (dst != lhs_low) {
4378 __ LoadConst32(dst, imm_low);
4379 __ Sltu(dst, dst, lhs_low);
4380 }
4381 __ Slt(dst, TMP, dst);
4382 __ Or(dst, dst, AT);
4383 if (cond == kCondBE) {
4384 __ Xori(dst, dst, 1);
4385 }
4386 break;
4387 }
4388 } else {
4389 switch (cond) {
4390 case kCondEQ:
4391 __ Xor(TMP, lhs_high, rhs_high);
4392 __ Xor(AT, lhs_low, rhs_low);
4393 __ Or(dst, TMP, AT);
4394 __ Sltiu(dst, dst, 1);
4395 break;
4396 case kCondNE:
4397 __ Xor(TMP, lhs_high, rhs_high);
4398 __ Xor(AT, lhs_low, rhs_low);
4399 __ Or(dst, TMP, AT);
4400 __ Sltu(dst, ZERO, dst);
4401 break;
4402 case kCondLT:
4403 case kCondGE:
4404 __ Slt(TMP, rhs_high, lhs_high);
4405 __ Sltu(AT, lhs_low, rhs_low);
4406 __ Slt(TMP, TMP, AT);
4407 __ Slt(AT, lhs_high, rhs_high);
4408 __ Or(dst, AT, TMP);
4409 if (cond == kCondGE) {
4410 __ Xori(dst, dst, 1);
4411 }
4412 break;
4413 case kCondGT:
4414 case kCondLE:
4415 __ Slt(TMP, lhs_high, rhs_high);
4416 __ Sltu(AT, rhs_low, lhs_low);
4417 __ Slt(TMP, TMP, AT);
4418 __ Slt(AT, rhs_high, lhs_high);
4419 __ Or(dst, AT, TMP);
4420 if (cond == kCondLE) {
4421 __ Xori(dst, dst, 1);
4422 }
4423 break;
4424 case kCondB:
4425 case kCondAE:
4426 __ Sltu(TMP, rhs_high, lhs_high);
4427 __ Sltu(AT, lhs_low, rhs_low);
4428 __ Slt(TMP, TMP, AT);
4429 __ Sltu(AT, lhs_high, rhs_high);
4430 __ Or(dst, AT, TMP);
4431 if (cond == kCondAE) {
4432 __ Xori(dst, dst, 1);
4433 }
4434 break;
4435 case kCondA:
4436 case kCondBE:
4437 __ Sltu(TMP, lhs_high, rhs_high);
4438 __ Sltu(AT, rhs_low, lhs_low);
4439 __ Slt(TMP, TMP, AT);
4440 __ Sltu(AT, rhs_high, lhs_high);
4441 __ Or(dst, AT, TMP);
4442 if (cond == kCondBE) {
4443 __ Xori(dst, dst, 1);
4444 }
4445 break;
4446 }
4447 }
4448}
4449
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004450void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4451 LocationSummary* locations,
4452 MipsLabel* label) {
4453 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4454 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4455 Location rhs_location = locations->InAt(1);
4456 Register rhs_high = ZERO;
4457 Register rhs_low = ZERO;
4458 int64_t imm = 0;
4459 uint32_t imm_high = 0;
4460 uint32_t imm_low = 0;
4461 bool use_imm = rhs_location.IsConstant();
4462 if (use_imm) {
4463 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4464 imm_high = High32Bits(imm);
4465 imm_low = Low32Bits(imm);
4466 } else {
4467 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4468 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4469 }
4470
4471 if (use_imm && imm == 0) {
4472 switch (cond) {
4473 case kCondEQ:
4474 case kCondBE: // <= 0 if zero
4475 __ Or(TMP, lhs_high, lhs_low);
4476 __ Beqz(TMP, label);
4477 break;
4478 case kCondNE:
4479 case kCondA: // > 0 if non-zero
4480 __ Or(TMP, lhs_high, lhs_low);
4481 __ Bnez(TMP, label);
4482 break;
4483 case kCondLT:
4484 __ Bltz(lhs_high, label);
4485 break;
4486 case kCondGE:
4487 __ Bgez(lhs_high, label);
4488 break;
4489 case kCondLE:
4490 __ Or(TMP, lhs_high, lhs_low);
4491 __ Sra(AT, lhs_high, 31);
4492 __ Bgeu(AT, TMP, label);
4493 break;
4494 case kCondGT:
4495 __ Or(TMP, lhs_high, lhs_low);
4496 __ Sra(AT, lhs_high, 31);
4497 __ Bltu(AT, TMP, label);
4498 break;
4499 case kCondB: // always false
4500 break;
4501 case kCondAE: // always true
4502 __ B(label);
4503 break;
4504 }
4505 } else if (use_imm) {
4506 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4507 switch (cond) {
4508 case kCondEQ:
4509 __ LoadConst32(TMP, imm_high);
4510 __ Xor(TMP, TMP, lhs_high);
4511 __ LoadConst32(AT, imm_low);
4512 __ Xor(AT, AT, lhs_low);
4513 __ Or(TMP, TMP, AT);
4514 __ Beqz(TMP, label);
4515 break;
4516 case kCondNE:
4517 __ LoadConst32(TMP, imm_high);
4518 __ Xor(TMP, TMP, lhs_high);
4519 __ LoadConst32(AT, imm_low);
4520 __ Xor(AT, AT, lhs_low);
4521 __ Or(TMP, TMP, AT);
4522 __ Bnez(TMP, label);
4523 break;
4524 case kCondLT:
4525 __ LoadConst32(TMP, imm_high);
4526 __ Blt(lhs_high, TMP, label);
4527 __ Slt(TMP, TMP, lhs_high);
4528 __ LoadConst32(AT, imm_low);
4529 __ Sltu(AT, lhs_low, AT);
4530 __ Blt(TMP, AT, label);
4531 break;
4532 case kCondGE:
4533 __ LoadConst32(TMP, imm_high);
4534 __ Blt(TMP, lhs_high, label);
4535 __ Slt(TMP, lhs_high, TMP);
4536 __ LoadConst32(AT, imm_low);
4537 __ Sltu(AT, lhs_low, AT);
4538 __ Or(TMP, TMP, AT);
4539 __ Beqz(TMP, label);
4540 break;
4541 case kCondLE:
4542 __ LoadConst32(TMP, imm_high);
4543 __ Blt(lhs_high, TMP, label);
4544 __ Slt(TMP, TMP, lhs_high);
4545 __ LoadConst32(AT, imm_low);
4546 __ Sltu(AT, AT, lhs_low);
4547 __ Or(TMP, TMP, AT);
4548 __ Beqz(TMP, label);
4549 break;
4550 case kCondGT:
4551 __ LoadConst32(TMP, imm_high);
4552 __ Blt(TMP, lhs_high, label);
4553 __ Slt(TMP, lhs_high, TMP);
4554 __ LoadConst32(AT, imm_low);
4555 __ Sltu(AT, AT, lhs_low);
4556 __ Blt(TMP, AT, label);
4557 break;
4558 case kCondB:
4559 __ LoadConst32(TMP, imm_high);
4560 __ Bltu(lhs_high, TMP, label);
4561 __ Sltu(TMP, TMP, lhs_high);
4562 __ LoadConst32(AT, imm_low);
4563 __ Sltu(AT, lhs_low, AT);
4564 __ Blt(TMP, AT, label);
4565 break;
4566 case kCondAE:
4567 __ LoadConst32(TMP, imm_high);
4568 __ Bltu(TMP, lhs_high, label);
4569 __ Sltu(TMP, lhs_high, TMP);
4570 __ LoadConst32(AT, imm_low);
4571 __ Sltu(AT, lhs_low, AT);
4572 __ Or(TMP, TMP, AT);
4573 __ Beqz(TMP, label);
4574 break;
4575 case kCondBE:
4576 __ LoadConst32(TMP, imm_high);
4577 __ Bltu(lhs_high, TMP, label);
4578 __ Sltu(TMP, TMP, lhs_high);
4579 __ LoadConst32(AT, imm_low);
4580 __ Sltu(AT, AT, lhs_low);
4581 __ Or(TMP, TMP, AT);
4582 __ Beqz(TMP, label);
4583 break;
4584 case kCondA:
4585 __ LoadConst32(TMP, imm_high);
4586 __ Bltu(TMP, lhs_high, label);
4587 __ Sltu(TMP, lhs_high, TMP);
4588 __ LoadConst32(AT, imm_low);
4589 __ Sltu(AT, AT, lhs_low);
4590 __ Blt(TMP, AT, label);
4591 break;
4592 }
4593 } else {
4594 switch (cond) {
4595 case kCondEQ:
4596 __ Xor(TMP, lhs_high, rhs_high);
4597 __ Xor(AT, lhs_low, rhs_low);
4598 __ Or(TMP, TMP, AT);
4599 __ Beqz(TMP, label);
4600 break;
4601 case kCondNE:
4602 __ Xor(TMP, lhs_high, rhs_high);
4603 __ Xor(AT, lhs_low, rhs_low);
4604 __ Or(TMP, TMP, AT);
4605 __ Bnez(TMP, label);
4606 break;
4607 case kCondLT:
4608 __ Blt(lhs_high, rhs_high, label);
4609 __ Slt(TMP, rhs_high, lhs_high);
4610 __ Sltu(AT, lhs_low, rhs_low);
4611 __ Blt(TMP, AT, label);
4612 break;
4613 case kCondGE:
4614 __ Blt(rhs_high, lhs_high, label);
4615 __ Slt(TMP, lhs_high, rhs_high);
4616 __ Sltu(AT, lhs_low, rhs_low);
4617 __ Or(TMP, TMP, AT);
4618 __ Beqz(TMP, label);
4619 break;
4620 case kCondLE:
4621 __ Blt(lhs_high, rhs_high, label);
4622 __ Slt(TMP, rhs_high, lhs_high);
4623 __ Sltu(AT, rhs_low, lhs_low);
4624 __ Or(TMP, TMP, AT);
4625 __ Beqz(TMP, label);
4626 break;
4627 case kCondGT:
4628 __ Blt(rhs_high, lhs_high, label);
4629 __ Slt(TMP, lhs_high, rhs_high);
4630 __ Sltu(AT, rhs_low, lhs_low);
4631 __ Blt(TMP, AT, label);
4632 break;
4633 case kCondB:
4634 __ Bltu(lhs_high, rhs_high, label);
4635 __ Sltu(TMP, rhs_high, lhs_high);
4636 __ Sltu(AT, lhs_low, rhs_low);
4637 __ Blt(TMP, AT, label);
4638 break;
4639 case kCondAE:
4640 __ Bltu(rhs_high, lhs_high, label);
4641 __ Sltu(TMP, lhs_high, rhs_high);
4642 __ Sltu(AT, lhs_low, rhs_low);
4643 __ Or(TMP, TMP, AT);
4644 __ Beqz(TMP, label);
4645 break;
4646 case kCondBE:
4647 __ Bltu(lhs_high, rhs_high, label);
4648 __ Sltu(TMP, rhs_high, lhs_high);
4649 __ Sltu(AT, rhs_low, lhs_low);
4650 __ Or(TMP, TMP, AT);
4651 __ Beqz(TMP, label);
4652 break;
4653 case kCondA:
4654 __ Bltu(rhs_high, lhs_high, label);
4655 __ Sltu(TMP, lhs_high, rhs_high);
4656 __ Sltu(AT, rhs_low, lhs_low);
4657 __ Blt(TMP, AT, label);
4658 break;
4659 }
4660 }
4661}
4662
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004663void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4664 bool gt_bias,
4665 Primitive::Type type,
4666 LocationSummary* locations) {
4667 Register dst = locations->Out().AsRegister<Register>();
4668 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4669 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4670 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4671 if (type == Primitive::kPrimFloat) {
4672 if (isR6) {
4673 switch (cond) {
4674 case kCondEQ:
4675 __ CmpEqS(FTMP, lhs, rhs);
4676 __ Mfc1(dst, FTMP);
4677 __ Andi(dst, dst, 1);
4678 break;
4679 case kCondNE:
4680 __ CmpEqS(FTMP, lhs, rhs);
4681 __ Mfc1(dst, FTMP);
4682 __ Addiu(dst, dst, 1);
4683 break;
4684 case kCondLT:
4685 if (gt_bias) {
4686 __ CmpLtS(FTMP, lhs, rhs);
4687 } else {
4688 __ CmpUltS(FTMP, lhs, rhs);
4689 }
4690 __ Mfc1(dst, FTMP);
4691 __ Andi(dst, dst, 1);
4692 break;
4693 case kCondLE:
4694 if (gt_bias) {
4695 __ CmpLeS(FTMP, lhs, rhs);
4696 } else {
4697 __ CmpUleS(FTMP, lhs, rhs);
4698 }
4699 __ Mfc1(dst, FTMP);
4700 __ Andi(dst, dst, 1);
4701 break;
4702 case kCondGT:
4703 if (gt_bias) {
4704 __ CmpUltS(FTMP, rhs, lhs);
4705 } else {
4706 __ CmpLtS(FTMP, rhs, lhs);
4707 }
4708 __ Mfc1(dst, FTMP);
4709 __ Andi(dst, dst, 1);
4710 break;
4711 case kCondGE:
4712 if (gt_bias) {
4713 __ CmpUleS(FTMP, rhs, lhs);
4714 } else {
4715 __ CmpLeS(FTMP, rhs, lhs);
4716 }
4717 __ Mfc1(dst, FTMP);
4718 __ Andi(dst, dst, 1);
4719 break;
4720 default:
4721 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4722 UNREACHABLE();
4723 }
4724 } else {
4725 switch (cond) {
4726 case kCondEQ:
4727 __ CeqS(0, lhs, rhs);
4728 __ LoadConst32(dst, 1);
4729 __ Movf(dst, ZERO, 0);
4730 break;
4731 case kCondNE:
4732 __ CeqS(0, lhs, rhs);
4733 __ LoadConst32(dst, 1);
4734 __ Movt(dst, ZERO, 0);
4735 break;
4736 case kCondLT:
4737 if (gt_bias) {
4738 __ ColtS(0, lhs, rhs);
4739 } else {
4740 __ CultS(0, lhs, rhs);
4741 }
4742 __ LoadConst32(dst, 1);
4743 __ Movf(dst, ZERO, 0);
4744 break;
4745 case kCondLE:
4746 if (gt_bias) {
4747 __ ColeS(0, lhs, rhs);
4748 } else {
4749 __ CuleS(0, lhs, rhs);
4750 }
4751 __ LoadConst32(dst, 1);
4752 __ Movf(dst, ZERO, 0);
4753 break;
4754 case kCondGT:
4755 if (gt_bias) {
4756 __ CultS(0, rhs, lhs);
4757 } else {
4758 __ ColtS(0, rhs, lhs);
4759 }
4760 __ LoadConst32(dst, 1);
4761 __ Movf(dst, ZERO, 0);
4762 break;
4763 case kCondGE:
4764 if (gt_bias) {
4765 __ CuleS(0, rhs, lhs);
4766 } else {
4767 __ ColeS(0, rhs, lhs);
4768 }
4769 __ LoadConst32(dst, 1);
4770 __ Movf(dst, ZERO, 0);
4771 break;
4772 default:
4773 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4774 UNREACHABLE();
4775 }
4776 }
4777 } else {
4778 DCHECK_EQ(type, Primitive::kPrimDouble);
4779 if (isR6) {
4780 switch (cond) {
4781 case kCondEQ:
4782 __ CmpEqD(FTMP, lhs, rhs);
4783 __ Mfc1(dst, FTMP);
4784 __ Andi(dst, dst, 1);
4785 break;
4786 case kCondNE:
4787 __ CmpEqD(FTMP, lhs, rhs);
4788 __ Mfc1(dst, FTMP);
4789 __ Addiu(dst, dst, 1);
4790 break;
4791 case kCondLT:
4792 if (gt_bias) {
4793 __ CmpLtD(FTMP, lhs, rhs);
4794 } else {
4795 __ CmpUltD(FTMP, lhs, rhs);
4796 }
4797 __ Mfc1(dst, FTMP);
4798 __ Andi(dst, dst, 1);
4799 break;
4800 case kCondLE:
4801 if (gt_bias) {
4802 __ CmpLeD(FTMP, lhs, rhs);
4803 } else {
4804 __ CmpUleD(FTMP, lhs, rhs);
4805 }
4806 __ Mfc1(dst, FTMP);
4807 __ Andi(dst, dst, 1);
4808 break;
4809 case kCondGT:
4810 if (gt_bias) {
4811 __ CmpUltD(FTMP, rhs, lhs);
4812 } else {
4813 __ CmpLtD(FTMP, rhs, lhs);
4814 }
4815 __ Mfc1(dst, FTMP);
4816 __ Andi(dst, dst, 1);
4817 break;
4818 case kCondGE:
4819 if (gt_bias) {
4820 __ CmpUleD(FTMP, rhs, lhs);
4821 } else {
4822 __ CmpLeD(FTMP, rhs, lhs);
4823 }
4824 __ Mfc1(dst, FTMP);
4825 __ Andi(dst, dst, 1);
4826 break;
4827 default:
4828 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4829 UNREACHABLE();
4830 }
4831 } else {
4832 switch (cond) {
4833 case kCondEQ:
4834 __ CeqD(0, lhs, rhs);
4835 __ LoadConst32(dst, 1);
4836 __ Movf(dst, ZERO, 0);
4837 break;
4838 case kCondNE:
4839 __ CeqD(0, lhs, rhs);
4840 __ LoadConst32(dst, 1);
4841 __ Movt(dst, ZERO, 0);
4842 break;
4843 case kCondLT:
4844 if (gt_bias) {
4845 __ ColtD(0, lhs, rhs);
4846 } else {
4847 __ CultD(0, lhs, rhs);
4848 }
4849 __ LoadConst32(dst, 1);
4850 __ Movf(dst, ZERO, 0);
4851 break;
4852 case kCondLE:
4853 if (gt_bias) {
4854 __ ColeD(0, lhs, rhs);
4855 } else {
4856 __ CuleD(0, lhs, rhs);
4857 }
4858 __ LoadConst32(dst, 1);
4859 __ Movf(dst, ZERO, 0);
4860 break;
4861 case kCondGT:
4862 if (gt_bias) {
4863 __ CultD(0, rhs, lhs);
4864 } else {
4865 __ ColtD(0, rhs, lhs);
4866 }
4867 __ LoadConst32(dst, 1);
4868 __ Movf(dst, ZERO, 0);
4869 break;
4870 case kCondGE:
4871 if (gt_bias) {
4872 __ CuleD(0, rhs, lhs);
4873 } else {
4874 __ ColeD(0, rhs, lhs);
4875 }
4876 __ LoadConst32(dst, 1);
4877 __ Movf(dst, ZERO, 0);
4878 break;
4879 default:
4880 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4881 UNREACHABLE();
4882 }
4883 }
4884 }
4885}
4886
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004887bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4888 bool gt_bias,
4889 Primitive::Type type,
4890 LocationSummary* input_locations,
4891 int cc) {
4892 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4893 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4894 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4895 if (type == Primitive::kPrimFloat) {
4896 switch (cond) {
4897 case kCondEQ:
4898 __ CeqS(cc, lhs, rhs);
4899 return false;
4900 case kCondNE:
4901 __ CeqS(cc, lhs, rhs);
4902 return true;
4903 case kCondLT:
4904 if (gt_bias) {
4905 __ ColtS(cc, lhs, rhs);
4906 } else {
4907 __ CultS(cc, lhs, rhs);
4908 }
4909 return false;
4910 case kCondLE:
4911 if (gt_bias) {
4912 __ ColeS(cc, lhs, rhs);
4913 } else {
4914 __ CuleS(cc, lhs, rhs);
4915 }
4916 return false;
4917 case kCondGT:
4918 if (gt_bias) {
4919 __ CultS(cc, rhs, lhs);
4920 } else {
4921 __ ColtS(cc, rhs, lhs);
4922 }
4923 return false;
4924 case kCondGE:
4925 if (gt_bias) {
4926 __ CuleS(cc, rhs, lhs);
4927 } else {
4928 __ ColeS(cc, rhs, lhs);
4929 }
4930 return false;
4931 default:
4932 LOG(FATAL) << "Unexpected non-floating-point condition";
4933 UNREACHABLE();
4934 }
4935 } else {
4936 DCHECK_EQ(type, Primitive::kPrimDouble);
4937 switch (cond) {
4938 case kCondEQ:
4939 __ CeqD(cc, lhs, rhs);
4940 return false;
4941 case kCondNE:
4942 __ CeqD(cc, lhs, rhs);
4943 return true;
4944 case kCondLT:
4945 if (gt_bias) {
4946 __ ColtD(cc, lhs, rhs);
4947 } else {
4948 __ CultD(cc, lhs, rhs);
4949 }
4950 return false;
4951 case kCondLE:
4952 if (gt_bias) {
4953 __ ColeD(cc, lhs, rhs);
4954 } else {
4955 __ CuleD(cc, lhs, rhs);
4956 }
4957 return false;
4958 case kCondGT:
4959 if (gt_bias) {
4960 __ CultD(cc, rhs, lhs);
4961 } else {
4962 __ ColtD(cc, rhs, lhs);
4963 }
4964 return false;
4965 case kCondGE:
4966 if (gt_bias) {
4967 __ CuleD(cc, rhs, lhs);
4968 } else {
4969 __ ColeD(cc, rhs, lhs);
4970 }
4971 return false;
4972 default:
4973 LOG(FATAL) << "Unexpected non-floating-point condition";
4974 UNREACHABLE();
4975 }
4976 }
4977}
4978
4979bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4980 bool gt_bias,
4981 Primitive::Type type,
4982 LocationSummary* input_locations,
4983 FRegister dst) {
4984 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4985 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4986 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4987 if (type == Primitive::kPrimFloat) {
4988 switch (cond) {
4989 case kCondEQ:
4990 __ CmpEqS(dst, lhs, rhs);
4991 return false;
4992 case kCondNE:
4993 __ CmpEqS(dst, lhs, rhs);
4994 return true;
4995 case kCondLT:
4996 if (gt_bias) {
4997 __ CmpLtS(dst, lhs, rhs);
4998 } else {
4999 __ CmpUltS(dst, lhs, rhs);
5000 }
5001 return false;
5002 case kCondLE:
5003 if (gt_bias) {
5004 __ CmpLeS(dst, lhs, rhs);
5005 } else {
5006 __ CmpUleS(dst, lhs, rhs);
5007 }
5008 return false;
5009 case kCondGT:
5010 if (gt_bias) {
5011 __ CmpUltS(dst, rhs, lhs);
5012 } else {
5013 __ CmpLtS(dst, rhs, lhs);
5014 }
5015 return false;
5016 case kCondGE:
5017 if (gt_bias) {
5018 __ CmpUleS(dst, rhs, lhs);
5019 } else {
5020 __ CmpLeS(dst, rhs, lhs);
5021 }
5022 return false;
5023 default:
5024 LOG(FATAL) << "Unexpected non-floating-point condition";
5025 UNREACHABLE();
5026 }
5027 } else {
5028 DCHECK_EQ(type, Primitive::kPrimDouble);
5029 switch (cond) {
5030 case kCondEQ:
5031 __ CmpEqD(dst, lhs, rhs);
5032 return false;
5033 case kCondNE:
5034 __ CmpEqD(dst, lhs, rhs);
5035 return true;
5036 case kCondLT:
5037 if (gt_bias) {
5038 __ CmpLtD(dst, lhs, rhs);
5039 } else {
5040 __ CmpUltD(dst, lhs, rhs);
5041 }
5042 return false;
5043 case kCondLE:
5044 if (gt_bias) {
5045 __ CmpLeD(dst, lhs, rhs);
5046 } else {
5047 __ CmpUleD(dst, lhs, rhs);
5048 }
5049 return false;
5050 case kCondGT:
5051 if (gt_bias) {
5052 __ CmpUltD(dst, rhs, lhs);
5053 } else {
5054 __ CmpLtD(dst, rhs, lhs);
5055 }
5056 return false;
5057 case kCondGE:
5058 if (gt_bias) {
5059 __ CmpUleD(dst, rhs, lhs);
5060 } else {
5061 __ CmpLeD(dst, rhs, lhs);
5062 }
5063 return false;
5064 default:
5065 LOG(FATAL) << "Unexpected non-floating-point condition";
5066 UNREACHABLE();
5067 }
5068 }
5069}
5070
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005071void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5072 bool gt_bias,
5073 Primitive::Type type,
5074 LocationSummary* locations,
5075 MipsLabel* label) {
5076 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5077 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5078 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5079 if (type == Primitive::kPrimFloat) {
5080 if (isR6) {
5081 switch (cond) {
5082 case kCondEQ:
5083 __ CmpEqS(FTMP, lhs, rhs);
5084 __ Bc1nez(FTMP, label);
5085 break;
5086 case kCondNE:
5087 __ CmpEqS(FTMP, lhs, rhs);
5088 __ Bc1eqz(FTMP, label);
5089 break;
5090 case kCondLT:
5091 if (gt_bias) {
5092 __ CmpLtS(FTMP, lhs, rhs);
5093 } else {
5094 __ CmpUltS(FTMP, lhs, rhs);
5095 }
5096 __ Bc1nez(FTMP, label);
5097 break;
5098 case kCondLE:
5099 if (gt_bias) {
5100 __ CmpLeS(FTMP, lhs, rhs);
5101 } else {
5102 __ CmpUleS(FTMP, lhs, rhs);
5103 }
5104 __ Bc1nez(FTMP, label);
5105 break;
5106 case kCondGT:
5107 if (gt_bias) {
5108 __ CmpUltS(FTMP, rhs, lhs);
5109 } else {
5110 __ CmpLtS(FTMP, rhs, lhs);
5111 }
5112 __ Bc1nez(FTMP, label);
5113 break;
5114 case kCondGE:
5115 if (gt_bias) {
5116 __ CmpUleS(FTMP, rhs, lhs);
5117 } else {
5118 __ CmpLeS(FTMP, rhs, lhs);
5119 }
5120 __ Bc1nez(FTMP, label);
5121 break;
5122 default:
5123 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005124 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005125 }
5126 } else {
5127 switch (cond) {
5128 case kCondEQ:
5129 __ CeqS(0, lhs, rhs);
5130 __ Bc1t(0, label);
5131 break;
5132 case kCondNE:
5133 __ CeqS(0, lhs, rhs);
5134 __ Bc1f(0, label);
5135 break;
5136 case kCondLT:
5137 if (gt_bias) {
5138 __ ColtS(0, lhs, rhs);
5139 } else {
5140 __ CultS(0, lhs, rhs);
5141 }
5142 __ Bc1t(0, label);
5143 break;
5144 case kCondLE:
5145 if (gt_bias) {
5146 __ ColeS(0, lhs, rhs);
5147 } else {
5148 __ CuleS(0, lhs, rhs);
5149 }
5150 __ Bc1t(0, label);
5151 break;
5152 case kCondGT:
5153 if (gt_bias) {
5154 __ CultS(0, rhs, lhs);
5155 } else {
5156 __ ColtS(0, rhs, lhs);
5157 }
5158 __ Bc1t(0, label);
5159 break;
5160 case kCondGE:
5161 if (gt_bias) {
5162 __ CuleS(0, rhs, lhs);
5163 } else {
5164 __ ColeS(0, rhs, lhs);
5165 }
5166 __ Bc1t(0, label);
5167 break;
5168 default:
5169 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005170 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005171 }
5172 }
5173 } else {
5174 DCHECK_EQ(type, Primitive::kPrimDouble);
5175 if (isR6) {
5176 switch (cond) {
5177 case kCondEQ:
5178 __ CmpEqD(FTMP, lhs, rhs);
5179 __ Bc1nez(FTMP, label);
5180 break;
5181 case kCondNE:
5182 __ CmpEqD(FTMP, lhs, rhs);
5183 __ Bc1eqz(FTMP, label);
5184 break;
5185 case kCondLT:
5186 if (gt_bias) {
5187 __ CmpLtD(FTMP, lhs, rhs);
5188 } else {
5189 __ CmpUltD(FTMP, lhs, rhs);
5190 }
5191 __ Bc1nez(FTMP, label);
5192 break;
5193 case kCondLE:
5194 if (gt_bias) {
5195 __ CmpLeD(FTMP, lhs, rhs);
5196 } else {
5197 __ CmpUleD(FTMP, lhs, rhs);
5198 }
5199 __ Bc1nez(FTMP, label);
5200 break;
5201 case kCondGT:
5202 if (gt_bias) {
5203 __ CmpUltD(FTMP, rhs, lhs);
5204 } else {
5205 __ CmpLtD(FTMP, rhs, lhs);
5206 }
5207 __ Bc1nez(FTMP, label);
5208 break;
5209 case kCondGE:
5210 if (gt_bias) {
5211 __ CmpUleD(FTMP, rhs, lhs);
5212 } else {
5213 __ CmpLeD(FTMP, rhs, lhs);
5214 }
5215 __ Bc1nez(FTMP, label);
5216 break;
5217 default:
5218 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005219 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005220 }
5221 } else {
5222 switch (cond) {
5223 case kCondEQ:
5224 __ CeqD(0, lhs, rhs);
5225 __ Bc1t(0, label);
5226 break;
5227 case kCondNE:
5228 __ CeqD(0, lhs, rhs);
5229 __ Bc1f(0, label);
5230 break;
5231 case kCondLT:
5232 if (gt_bias) {
5233 __ ColtD(0, lhs, rhs);
5234 } else {
5235 __ CultD(0, lhs, rhs);
5236 }
5237 __ Bc1t(0, label);
5238 break;
5239 case kCondLE:
5240 if (gt_bias) {
5241 __ ColeD(0, lhs, rhs);
5242 } else {
5243 __ CuleD(0, lhs, rhs);
5244 }
5245 __ Bc1t(0, label);
5246 break;
5247 case kCondGT:
5248 if (gt_bias) {
5249 __ CultD(0, rhs, lhs);
5250 } else {
5251 __ ColtD(0, rhs, lhs);
5252 }
5253 __ Bc1t(0, label);
5254 break;
5255 case kCondGE:
5256 if (gt_bias) {
5257 __ CuleD(0, rhs, lhs);
5258 } else {
5259 __ ColeD(0, rhs, lhs);
5260 }
5261 __ Bc1t(0, label);
5262 break;
5263 default:
5264 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005265 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005266 }
5267 }
5268 }
5269}
5270
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005271void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005272 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005273 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005274 MipsLabel* false_target) {
5275 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005276
David Brazdil0debae72015-11-12 18:37:00 +00005277 if (true_target == nullptr && false_target == nullptr) {
5278 // Nothing to do. The code always falls through.
5279 return;
5280 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005281 // Constant condition, statically compared against "true" (integer value 1).
5282 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005283 if (true_target != nullptr) {
5284 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005285 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005286 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005287 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005288 if (false_target != nullptr) {
5289 __ B(false_target);
5290 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005291 }
David Brazdil0debae72015-11-12 18:37:00 +00005292 return;
5293 }
5294
5295 // The following code generates these patterns:
5296 // (1) true_target == nullptr && false_target != nullptr
5297 // - opposite condition true => branch to false_target
5298 // (2) true_target != nullptr && false_target == nullptr
5299 // - condition true => branch to true_target
5300 // (3) true_target != nullptr && false_target != nullptr
5301 // - condition true => branch to true_target
5302 // - branch to false_target
5303 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005304 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005305 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005306 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005307 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005308 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5309 } else {
5310 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5311 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005312 } else {
5313 // The condition instruction has not been materialized, use its inputs as
5314 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005315 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005316 Primitive::Type type = condition->InputAt(0)->GetType();
5317 LocationSummary* locations = cond->GetLocations();
5318 IfCondition if_cond = condition->GetCondition();
5319 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005320
David Brazdil0debae72015-11-12 18:37:00 +00005321 if (true_target == nullptr) {
5322 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005323 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005324 }
5325
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005326 switch (type) {
5327 default:
5328 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5329 break;
5330 case Primitive::kPrimLong:
5331 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5332 break;
5333 case Primitive::kPrimFloat:
5334 case Primitive::kPrimDouble:
5335 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5336 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005337 }
5338 }
David Brazdil0debae72015-11-12 18:37:00 +00005339
5340 // If neither branch falls through (case 3), the conditional branch to `true_target`
5341 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5342 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005343 __ B(false_target);
5344 }
5345}
5346
5347void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005349 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005350 locations->SetInAt(0, Location::RequiresRegister());
5351 }
5352}
5353
5354void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005355 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5356 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5357 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5358 nullptr : codegen_->GetLabelOf(true_successor);
5359 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5360 nullptr : codegen_->GetLabelOf(false_successor);
5361 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005362}
5363
5364void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5365 LocationSummary* locations = new (GetGraph()->GetArena())
5366 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005367 InvokeRuntimeCallingConvention calling_convention;
5368 RegisterSet caller_saves = RegisterSet::Empty();
5369 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5370 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005371 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005372 locations->SetInAt(0, Location::RequiresRegister());
5373 }
5374}
5375
5376void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005377 SlowPathCodeMIPS* slow_path =
5378 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005379 GenerateTestAndBranch(deoptimize,
5380 /* condition_input_index */ 0,
5381 slow_path->GetEntryLabel(),
5382 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005383}
5384
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005385// This function returns true if a conditional move can be generated for HSelect.
5386// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5387// branches and regular moves.
5388//
5389// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5390//
5391// While determining feasibility of a conditional move and setting inputs/outputs
5392// are two distinct tasks, this function does both because they share quite a bit
5393// of common logic.
5394static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5395 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5396 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5397 HCondition* condition = cond->AsCondition();
5398
5399 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5400 Primitive::Type dst_type = select->GetType();
5401
5402 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5403 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5404 bool is_true_value_zero_constant =
5405 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5406 bool is_false_value_zero_constant =
5407 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5408
5409 bool can_move_conditionally = false;
5410 bool use_const_for_false_in = false;
5411 bool use_const_for_true_in = false;
5412
5413 if (!cond->IsConstant()) {
5414 switch (cond_type) {
5415 default:
5416 switch (dst_type) {
5417 default:
5418 // Moving int on int condition.
5419 if (is_r6) {
5420 if (is_true_value_zero_constant) {
5421 // seleqz out_reg, false_reg, cond_reg
5422 can_move_conditionally = true;
5423 use_const_for_true_in = true;
5424 } else if (is_false_value_zero_constant) {
5425 // selnez out_reg, true_reg, cond_reg
5426 can_move_conditionally = true;
5427 use_const_for_false_in = true;
5428 } else if (materialized) {
5429 // Not materializing unmaterialized int conditions
5430 // to keep the instruction count low.
5431 // selnez AT, true_reg, cond_reg
5432 // seleqz TMP, false_reg, cond_reg
5433 // or out_reg, AT, TMP
5434 can_move_conditionally = true;
5435 }
5436 } else {
5437 // movn out_reg, true_reg/ZERO, cond_reg
5438 can_move_conditionally = true;
5439 use_const_for_true_in = is_true_value_zero_constant;
5440 }
5441 break;
5442 case Primitive::kPrimLong:
5443 // Moving long on int condition.
5444 if (is_r6) {
5445 if (is_true_value_zero_constant) {
5446 // seleqz out_reg_lo, false_reg_lo, cond_reg
5447 // seleqz out_reg_hi, false_reg_hi, cond_reg
5448 can_move_conditionally = true;
5449 use_const_for_true_in = true;
5450 } else if (is_false_value_zero_constant) {
5451 // selnez out_reg_lo, true_reg_lo, cond_reg
5452 // selnez out_reg_hi, true_reg_hi, cond_reg
5453 can_move_conditionally = true;
5454 use_const_for_false_in = true;
5455 }
5456 // Other long conditional moves would generate 6+ instructions,
5457 // which is too many.
5458 } else {
5459 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5460 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5461 can_move_conditionally = true;
5462 use_const_for_true_in = is_true_value_zero_constant;
5463 }
5464 break;
5465 case Primitive::kPrimFloat:
5466 case Primitive::kPrimDouble:
5467 // Moving float/double on int condition.
5468 if (is_r6) {
5469 if (materialized) {
5470 // Not materializing unmaterialized int conditions
5471 // to keep the instruction count low.
5472 can_move_conditionally = true;
5473 if (is_true_value_zero_constant) {
5474 // sltu TMP, ZERO, cond_reg
5475 // mtc1 TMP, temp_cond_reg
5476 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5477 use_const_for_true_in = true;
5478 } else if (is_false_value_zero_constant) {
5479 // sltu TMP, ZERO, cond_reg
5480 // mtc1 TMP, temp_cond_reg
5481 // selnez.fmt out_reg, true_reg, temp_cond_reg
5482 use_const_for_false_in = true;
5483 } else {
5484 // sltu TMP, ZERO, cond_reg
5485 // mtc1 TMP, temp_cond_reg
5486 // sel.fmt temp_cond_reg, false_reg, true_reg
5487 // mov.fmt out_reg, temp_cond_reg
5488 }
5489 }
5490 } else {
5491 // movn.fmt out_reg, true_reg, cond_reg
5492 can_move_conditionally = true;
5493 }
5494 break;
5495 }
5496 break;
5497 case Primitive::kPrimLong:
5498 // We don't materialize long comparison now
5499 // and use conditional branches instead.
5500 break;
5501 case Primitive::kPrimFloat:
5502 case Primitive::kPrimDouble:
5503 switch (dst_type) {
5504 default:
5505 // Moving int on float/double condition.
5506 if (is_r6) {
5507 if (is_true_value_zero_constant) {
5508 // mfc1 TMP, temp_cond_reg
5509 // seleqz out_reg, false_reg, TMP
5510 can_move_conditionally = true;
5511 use_const_for_true_in = true;
5512 } else if (is_false_value_zero_constant) {
5513 // mfc1 TMP, temp_cond_reg
5514 // selnez out_reg, true_reg, TMP
5515 can_move_conditionally = true;
5516 use_const_for_false_in = true;
5517 } else {
5518 // mfc1 TMP, temp_cond_reg
5519 // selnez AT, true_reg, TMP
5520 // seleqz TMP, false_reg, TMP
5521 // or out_reg, AT, TMP
5522 can_move_conditionally = true;
5523 }
5524 } else {
5525 // movt out_reg, true_reg/ZERO, cc
5526 can_move_conditionally = true;
5527 use_const_for_true_in = is_true_value_zero_constant;
5528 }
5529 break;
5530 case Primitive::kPrimLong:
5531 // Moving long on float/double condition.
5532 if (is_r6) {
5533 if (is_true_value_zero_constant) {
5534 // mfc1 TMP, temp_cond_reg
5535 // seleqz out_reg_lo, false_reg_lo, TMP
5536 // seleqz out_reg_hi, false_reg_hi, TMP
5537 can_move_conditionally = true;
5538 use_const_for_true_in = true;
5539 } else if (is_false_value_zero_constant) {
5540 // mfc1 TMP, temp_cond_reg
5541 // selnez out_reg_lo, true_reg_lo, TMP
5542 // selnez out_reg_hi, true_reg_hi, TMP
5543 can_move_conditionally = true;
5544 use_const_for_false_in = true;
5545 }
5546 // Other long conditional moves would generate 6+ instructions,
5547 // which is too many.
5548 } else {
5549 // movt out_reg_lo, true_reg_lo/ZERO, cc
5550 // movt out_reg_hi, true_reg_hi/ZERO, cc
5551 can_move_conditionally = true;
5552 use_const_for_true_in = is_true_value_zero_constant;
5553 }
5554 break;
5555 case Primitive::kPrimFloat:
5556 case Primitive::kPrimDouble:
5557 // Moving float/double on float/double condition.
5558 if (is_r6) {
5559 can_move_conditionally = true;
5560 if (is_true_value_zero_constant) {
5561 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5562 use_const_for_true_in = true;
5563 } else if (is_false_value_zero_constant) {
5564 // selnez.fmt out_reg, true_reg, temp_cond_reg
5565 use_const_for_false_in = true;
5566 } else {
5567 // sel.fmt temp_cond_reg, false_reg, true_reg
5568 // mov.fmt out_reg, temp_cond_reg
5569 }
5570 } else {
5571 // movt.fmt out_reg, true_reg, cc
5572 can_move_conditionally = true;
5573 }
5574 break;
5575 }
5576 break;
5577 }
5578 }
5579
5580 if (can_move_conditionally) {
5581 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5582 } else {
5583 DCHECK(!use_const_for_false_in);
5584 DCHECK(!use_const_for_true_in);
5585 }
5586
5587 if (locations_to_set != nullptr) {
5588 if (use_const_for_false_in) {
5589 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5590 } else {
5591 locations_to_set->SetInAt(0,
5592 Primitive::IsFloatingPointType(dst_type)
5593 ? Location::RequiresFpuRegister()
5594 : Location::RequiresRegister());
5595 }
5596 if (use_const_for_true_in) {
5597 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5598 } else {
5599 locations_to_set->SetInAt(1,
5600 Primitive::IsFloatingPointType(dst_type)
5601 ? Location::RequiresFpuRegister()
5602 : Location::RequiresRegister());
5603 }
5604 if (materialized) {
5605 locations_to_set->SetInAt(2, Location::RequiresRegister());
5606 }
5607 // On R6 we don't require the output to be the same as the
5608 // first input for conditional moves unlike on R2.
5609 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5610 if (is_out_same_as_first_in) {
5611 locations_to_set->SetOut(Location::SameAsFirstInput());
5612 } else {
5613 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5614 ? Location::RequiresFpuRegister()
5615 : Location::RequiresRegister());
5616 }
5617 }
5618
5619 return can_move_conditionally;
5620}
5621
5622void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5623 LocationSummary* locations = select->GetLocations();
5624 Location dst = locations->Out();
5625 Location src = locations->InAt(1);
5626 Register src_reg = ZERO;
5627 Register src_reg_high = ZERO;
5628 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5629 Register cond_reg = TMP;
5630 int cond_cc = 0;
5631 Primitive::Type cond_type = Primitive::kPrimInt;
5632 bool cond_inverted = false;
5633 Primitive::Type dst_type = select->GetType();
5634
5635 if (IsBooleanValueOrMaterializedCondition(cond)) {
5636 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5637 } else {
5638 HCondition* condition = cond->AsCondition();
5639 LocationSummary* cond_locations = cond->GetLocations();
5640 IfCondition if_cond = condition->GetCondition();
5641 cond_type = condition->InputAt(0)->GetType();
5642 switch (cond_type) {
5643 default:
5644 DCHECK_NE(cond_type, Primitive::kPrimLong);
5645 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5646 break;
5647 case Primitive::kPrimFloat:
5648 case Primitive::kPrimDouble:
5649 cond_inverted = MaterializeFpCompareR2(if_cond,
5650 condition->IsGtBias(),
5651 cond_type,
5652 cond_locations,
5653 cond_cc);
5654 break;
5655 }
5656 }
5657
5658 DCHECK(dst.Equals(locations->InAt(0)));
5659 if (src.IsRegister()) {
5660 src_reg = src.AsRegister<Register>();
5661 } else if (src.IsRegisterPair()) {
5662 src_reg = src.AsRegisterPairLow<Register>();
5663 src_reg_high = src.AsRegisterPairHigh<Register>();
5664 } else if (src.IsConstant()) {
5665 DCHECK(src.GetConstant()->IsZeroBitPattern());
5666 }
5667
5668 switch (cond_type) {
5669 default:
5670 switch (dst_type) {
5671 default:
5672 if (cond_inverted) {
5673 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5674 } else {
5675 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5676 }
5677 break;
5678 case Primitive::kPrimLong:
5679 if (cond_inverted) {
5680 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5681 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5682 } else {
5683 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5684 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5685 }
5686 break;
5687 case Primitive::kPrimFloat:
5688 if (cond_inverted) {
5689 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5690 } else {
5691 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5692 }
5693 break;
5694 case Primitive::kPrimDouble:
5695 if (cond_inverted) {
5696 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5697 } else {
5698 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5699 }
5700 break;
5701 }
5702 break;
5703 case Primitive::kPrimLong:
5704 LOG(FATAL) << "Unreachable";
5705 UNREACHABLE();
5706 case Primitive::kPrimFloat:
5707 case Primitive::kPrimDouble:
5708 switch (dst_type) {
5709 default:
5710 if (cond_inverted) {
5711 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5712 } else {
5713 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5714 }
5715 break;
5716 case Primitive::kPrimLong:
5717 if (cond_inverted) {
5718 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5719 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5720 } else {
5721 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5722 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5723 }
5724 break;
5725 case Primitive::kPrimFloat:
5726 if (cond_inverted) {
5727 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5728 } else {
5729 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5730 }
5731 break;
5732 case Primitive::kPrimDouble:
5733 if (cond_inverted) {
5734 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5735 } else {
5736 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5737 }
5738 break;
5739 }
5740 break;
5741 }
5742}
5743
5744void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5745 LocationSummary* locations = select->GetLocations();
5746 Location dst = locations->Out();
5747 Location false_src = locations->InAt(0);
5748 Location true_src = locations->InAt(1);
5749 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5750 Register cond_reg = TMP;
5751 FRegister fcond_reg = FTMP;
5752 Primitive::Type cond_type = Primitive::kPrimInt;
5753 bool cond_inverted = false;
5754 Primitive::Type dst_type = select->GetType();
5755
5756 if (IsBooleanValueOrMaterializedCondition(cond)) {
5757 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5758 } else {
5759 HCondition* condition = cond->AsCondition();
5760 LocationSummary* cond_locations = cond->GetLocations();
5761 IfCondition if_cond = condition->GetCondition();
5762 cond_type = condition->InputAt(0)->GetType();
5763 switch (cond_type) {
5764 default:
5765 DCHECK_NE(cond_type, Primitive::kPrimLong);
5766 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5767 break;
5768 case Primitive::kPrimFloat:
5769 case Primitive::kPrimDouble:
5770 cond_inverted = MaterializeFpCompareR6(if_cond,
5771 condition->IsGtBias(),
5772 cond_type,
5773 cond_locations,
5774 fcond_reg);
5775 break;
5776 }
5777 }
5778
5779 if (true_src.IsConstant()) {
5780 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5781 }
5782 if (false_src.IsConstant()) {
5783 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5784 }
5785
5786 switch (dst_type) {
5787 default:
5788 if (Primitive::IsFloatingPointType(cond_type)) {
5789 __ Mfc1(cond_reg, fcond_reg);
5790 }
5791 if (true_src.IsConstant()) {
5792 if (cond_inverted) {
5793 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5794 } else {
5795 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5796 }
5797 } else if (false_src.IsConstant()) {
5798 if (cond_inverted) {
5799 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5800 } else {
5801 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5802 }
5803 } else {
5804 DCHECK_NE(cond_reg, AT);
5805 if (cond_inverted) {
5806 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5807 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5808 } else {
5809 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5810 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5811 }
5812 __ Or(dst.AsRegister<Register>(), AT, TMP);
5813 }
5814 break;
5815 case Primitive::kPrimLong: {
5816 if (Primitive::IsFloatingPointType(cond_type)) {
5817 __ Mfc1(cond_reg, fcond_reg);
5818 }
5819 Register dst_lo = dst.AsRegisterPairLow<Register>();
5820 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5821 if (true_src.IsConstant()) {
5822 Register src_lo = false_src.AsRegisterPairLow<Register>();
5823 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5824 if (cond_inverted) {
5825 __ Selnez(dst_lo, src_lo, cond_reg);
5826 __ Selnez(dst_hi, src_hi, cond_reg);
5827 } else {
5828 __ Seleqz(dst_lo, src_lo, cond_reg);
5829 __ Seleqz(dst_hi, src_hi, cond_reg);
5830 }
5831 } else {
5832 DCHECK(false_src.IsConstant());
5833 Register src_lo = true_src.AsRegisterPairLow<Register>();
5834 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5835 if (cond_inverted) {
5836 __ Seleqz(dst_lo, src_lo, cond_reg);
5837 __ Seleqz(dst_hi, src_hi, cond_reg);
5838 } else {
5839 __ Selnez(dst_lo, src_lo, cond_reg);
5840 __ Selnez(dst_hi, src_hi, cond_reg);
5841 }
5842 }
5843 break;
5844 }
5845 case Primitive::kPrimFloat: {
5846 if (!Primitive::IsFloatingPointType(cond_type)) {
5847 // sel*.fmt tests bit 0 of the condition register, account for that.
5848 __ Sltu(TMP, ZERO, cond_reg);
5849 __ Mtc1(TMP, fcond_reg);
5850 }
5851 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5852 if (true_src.IsConstant()) {
5853 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5854 if (cond_inverted) {
5855 __ SelnezS(dst_reg, src_reg, fcond_reg);
5856 } else {
5857 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5858 }
5859 } else if (false_src.IsConstant()) {
5860 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5861 if (cond_inverted) {
5862 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5863 } else {
5864 __ SelnezS(dst_reg, src_reg, fcond_reg);
5865 }
5866 } else {
5867 if (cond_inverted) {
5868 __ SelS(fcond_reg,
5869 true_src.AsFpuRegister<FRegister>(),
5870 false_src.AsFpuRegister<FRegister>());
5871 } else {
5872 __ SelS(fcond_reg,
5873 false_src.AsFpuRegister<FRegister>(),
5874 true_src.AsFpuRegister<FRegister>());
5875 }
5876 __ MovS(dst_reg, fcond_reg);
5877 }
5878 break;
5879 }
5880 case Primitive::kPrimDouble: {
5881 if (!Primitive::IsFloatingPointType(cond_type)) {
5882 // sel*.fmt tests bit 0 of the condition register, account for that.
5883 __ Sltu(TMP, ZERO, cond_reg);
5884 __ Mtc1(TMP, fcond_reg);
5885 }
5886 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5887 if (true_src.IsConstant()) {
5888 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5889 if (cond_inverted) {
5890 __ SelnezD(dst_reg, src_reg, fcond_reg);
5891 } else {
5892 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5893 }
5894 } else if (false_src.IsConstant()) {
5895 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5896 if (cond_inverted) {
5897 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5898 } else {
5899 __ SelnezD(dst_reg, src_reg, fcond_reg);
5900 }
5901 } else {
5902 if (cond_inverted) {
5903 __ SelD(fcond_reg,
5904 true_src.AsFpuRegister<FRegister>(),
5905 false_src.AsFpuRegister<FRegister>());
5906 } else {
5907 __ SelD(fcond_reg,
5908 false_src.AsFpuRegister<FRegister>(),
5909 true_src.AsFpuRegister<FRegister>());
5910 }
5911 __ MovD(dst_reg, fcond_reg);
5912 }
5913 break;
5914 }
5915 }
5916}
5917
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005918void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5919 LocationSummary* locations = new (GetGraph()->GetArena())
5920 LocationSummary(flag, LocationSummary::kNoCall);
5921 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005922}
5923
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005924void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5925 __ LoadFromOffset(kLoadWord,
5926 flag->GetLocations()->Out().AsRegister<Register>(),
5927 SP,
5928 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005929}
5930
David Brazdil74eb1b22015-12-14 11:44:01 +00005931void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005933 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005934}
5935
5936void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005937 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5938 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5939 if (is_r6) {
5940 GenConditionalMoveR6(select);
5941 } else {
5942 GenConditionalMoveR2(select);
5943 }
5944 } else {
5945 LocationSummary* locations = select->GetLocations();
5946 MipsLabel false_target;
5947 GenerateTestAndBranch(select,
5948 /* condition_input_index */ 2,
5949 /* true_target */ nullptr,
5950 &false_target);
5951 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5952 __ Bind(&false_target);
5953 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005954}
5955
David Srbecky0cf44932015-12-09 14:09:59 +00005956void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5957 new (GetGraph()->GetArena()) LocationSummary(info);
5958}
5959
David Srbeckyd28f4a02016-03-14 17:14:24 +00005960void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5961 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005962}
5963
5964void CodeGeneratorMIPS::GenerateNop() {
5965 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005966}
5967
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005968void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5969 Primitive::Type field_type = field_info.GetFieldType();
5970 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5971 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005972 bool object_field_get_with_read_barrier =
5973 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005975 instruction,
5976 generate_volatile
5977 ? LocationSummary::kCallOnMainOnly
5978 : (object_field_get_with_read_barrier
5979 ? LocationSummary::kCallOnSlowPath
5980 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005981
Alexey Frunzec61c0762017-04-10 13:54:23 -07005982 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5983 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5984 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005985 locations->SetInAt(0, Location::RequiresRegister());
5986 if (generate_volatile) {
5987 InvokeRuntimeCallingConvention calling_convention;
5988 // need A0 to hold base + offset
5989 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5990 if (field_type == Primitive::kPrimLong) {
5991 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5992 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005993 // Use Location::Any() to prevent situations when running out of available fp registers.
5994 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005995 // Need some temp core regs since FP results are returned in core registers
5996 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5997 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5998 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5999 }
6000 } else {
6001 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6002 locations->SetOut(Location::RequiresFpuRegister());
6003 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006004 // The output overlaps in the case of an object field get with
6005 // read barriers enabled: we do not want the move to overwrite the
6006 // object's location, as we need it to emit the read barrier.
6007 locations->SetOut(Location::RequiresRegister(),
6008 object_field_get_with_read_barrier
6009 ? Location::kOutputOverlap
6010 : Location::kNoOutputOverlap);
6011 }
6012 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6013 // We need a temporary register for the read barrier marking slow
6014 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
6015 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006016 }
6017 }
6018}
6019
6020void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6021 const FieldInfo& field_info,
6022 uint32_t dex_pc) {
6023 Primitive::Type type = field_info.GetFieldType();
6024 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006025 Location obj_loc = locations->InAt(0);
6026 Register obj = obj_loc.AsRegister<Register>();
6027 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006028 LoadOperandType load_type = kLoadUnsignedByte;
6029 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006030 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006031 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006032
6033 switch (type) {
6034 case Primitive::kPrimBoolean:
6035 load_type = kLoadUnsignedByte;
6036 break;
6037 case Primitive::kPrimByte:
6038 load_type = kLoadSignedByte;
6039 break;
6040 case Primitive::kPrimShort:
6041 load_type = kLoadSignedHalfword;
6042 break;
6043 case Primitive::kPrimChar:
6044 load_type = kLoadUnsignedHalfword;
6045 break;
6046 case Primitive::kPrimInt:
6047 case Primitive::kPrimFloat:
6048 case Primitive::kPrimNot:
6049 load_type = kLoadWord;
6050 break;
6051 case Primitive::kPrimLong:
6052 case Primitive::kPrimDouble:
6053 load_type = kLoadDoubleword;
6054 break;
6055 case Primitive::kPrimVoid:
6056 LOG(FATAL) << "Unreachable type " << type;
6057 UNREACHABLE();
6058 }
6059
6060 if (is_volatile && load_type == kLoadDoubleword) {
6061 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006062 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006063 // Do implicit Null check
6064 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6065 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006066 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006067 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6068 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006069 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006070 if (dst_loc.IsFpuRegister()) {
6071 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006072 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006073 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006074 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006075 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006076 __ StoreToOffset(kStoreWord,
6077 locations->GetTemp(1).AsRegister<Register>(),
6078 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006079 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006080 __ StoreToOffset(kStoreWord,
6081 locations->GetTemp(2).AsRegister<Register>(),
6082 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006083 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006084 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006085 }
6086 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006087 if (type == Primitive::kPrimNot) {
6088 // /* HeapReference<Object> */ dst = *(obj + offset)
6089 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6090 Location temp_loc = locations->GetTemp(0);
6091 // Note that a potential implicit null check is handled in this
6092 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6093 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6094 dst_loc,
6095 obj,
6096 offset,
6097 temp_loc,
6098 /* needs_null_check */ true);
6099 if (is_volatile) {
6100 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6101 }
6102 } else {
6103 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6104 if (is_volatile) {
6105 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6106 }
6107 // If read barriers are enabled, emit read barriers other than
6108 // Baker's using a slow path (and also unpoison the loaded
6109 // reference, if heap poisoning is enabled).
6110 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6111 }
6112 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006113 Register dst;
6114 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006115 DCHECK(dst_loc.IsRegisterPair());
6116 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006117 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006118 DCHECK(dst_loc.IsRegister());
6119 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006120 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006121 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006122 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006123 DCHECK(dst_loc.IsFpuRegister());
6124 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006125 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006126 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006127 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006128 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006129 }
6130 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006131 }
6132
Alexey Frunze15958152017-02-09 19:08:30 -08006133 // Memory barriers, in the case of references, are handled in the
6134 // previous switch statement.
6135 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006136 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6137 }
6138}
6139
6140void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6141 Primitive::Type field_type = field_info.GetFieldType();
6142 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6143 bool generate_volatile = field_info.IsVolatile() && is_wide;
6144 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006145 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006146
6147 locations->SetInAt(0, Location::RequiresRegister());
6148 if (generate_volatile) {
6149 InvokeRuntimeCallingConvention calling_convention;
6150 // need A0 to hold base + offset
6151 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6152 if (field_type == Primitive::kPrimLong) {
6153 locations->SetInAt(1, Location::RegisterPairLocation(
6154 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6155 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006156 // Use Location::Any() to prevent situations when running out of available fp registers.
6157 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006158 // Pass FP parameters in core registers.
6159 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6160 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6161 }
6162 } else {
6163 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006164 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006165 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006166 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167 }
6168 }
6169}
6170
6171void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6172 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006173 uint32_t dex_pc,
6174 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006175 Primitive::Type type = field_info.GetFieldType();
6176 LocationSummary* locations = instruction->GetLocations();
6177 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006178 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006179 StoreOperandType store_type = kStoreByte;
6180 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006181 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006182 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006183 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006184
6185 switch (type) {
6186 case Primitive::kPrimBoolean:
6187 case Primitive::kPrimByte:
6188 store_type = kStoreByte;
6189 break;
6190 case Primitive::kPrimShort:
6191 case Primitive::kPrimChar:
6192 store_type = kStoreHalfword;
6193 break;
6194 case Primitive::kPrimInt:
6195 case Primitive::kPrimFloat:
6196 case Primitive::kPrimNot:
6197 store_type = kStoreWord;
6198 break;
6199 case Primitive::kPrimLong:
6200 case Primitive::kPrimDouble:
6201 store_type = kStoreDoubleword;
6202 break;
6203 case Primitive::kPrimVoid:
6204 LOG(FATAL) << "Unreachable type " << type;
6205 UNREACHABLE();
6206 }
6207
6208 if (is_volatile) {
6209 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6210 }
6211
6212 if (is_volatile && store_type == kStoreDoubleword) {
6213 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006214 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006215 // Do implicit Null check.
6216 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6217 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6218 if (type == Primitive::kPrimDouble) {
6219 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006220 if (value_location.IsFpuRegister()) {
6221 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6222 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006223 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006224 value_location.AsFpuRegister<FRegister>());
6225 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006226 __ LoadFromOffset(kLoadWord,
6227 locations->GetTemp(1).AsRegister<Register>(),
6228 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006229 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006230 __ LoadFromOffset(kLoadWord,
6231 locations->GetTemp(2).AsRegister<Register>(),
6232 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006233 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006234 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006235 DCHECK(value_location.IsConstant());
6236 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6237 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006238 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6239 locations->GetTemp(1).AsRegister<Register>(),
6240 value);
6241 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006242 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006243 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006244 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6245 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006246 if (value_location.IsConstant()) {
6247 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6248 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6249 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250 Register src;
6251 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006252 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006254 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006255 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006256 if (kPoisonHeapReferences && needs_write_barrier) {
6257 // Note that in the case where `value` is a null reference,
6258 // we do not enter this block, as a null reference does not
6259 // need poisoning.
6260 DCHECK_EQ(type, Primitive::kPrimNot);
6261 __ PoisonHeapReference(TMP, src);
6262 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6263 } else {
6264 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6265 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006267 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006268 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006269 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006270 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006271 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006272 }
6273 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006274 }
6275
Alexey Frunzec061de12017-02-14 13:27:23 -08006276 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006277 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006278 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006279 }
6280
6281 if (is_volatile) {
6282 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6283 }
6284}
6285
6286void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6287 HandleFieldGet(instruction, instruction->GetFieldInfo());
6288}
6289
6290void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6291 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6292}
6293
6294void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6295 HandleFieldSet(instruction, instruction->GetFieldInfo());
6296}
6297
6298void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006299 HandleFieldSet(instruction,
6300 instruction->GetFieldInfo(),
6301 instruction->GetDexPc(),
6302 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303}
6304
Alexey Frunze15958152017-02-09 19:08:30 -08006305void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6306 HInstruction* instruction,
6307 Location out,
6308 uint32_t offset,
6309 Location maybe_temp,
6310 ReadBarrierOption read_barrier_option) {
6311 Register out_reg = out.AsRegister<Register>();
6312 if (read_barrier_option == kWithReadBarrier) {
6313 CHECK(kEmitCompilerReadBarrier);
6314 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6315 if (kUseBakerReadBarrier) {
6316 // Load with fast path based Baker's read barrier.
6317 // /* HeapReference<Object> */ out = *(out + offset)
6318 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6319 out,
6320 out_reg,
6321 offset,
6322 maybe_temp,
6323 /* needs_null_check */ false);
6324 } else {
6325 // Load with slow path based read barrier.
6326 // Save the value of `out` into `maybe_temp` before overwriting it
6327 // in the following move operation, as we will need it for the
6328 // read barrier below.
6329 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6330 // /* HeapReference<Object> */ out = *(out + offset)
6331 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6332 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6333 }
6334 } else {
6335 // Plain load with no read barrier.
6336 // /* HeapReference<Object> */ out = *(out + offset)
6337 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6338 __ MaybeUnpoisonHeapReference(out_reg);
6339 }
6340}
6341
6342void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6343 HInstruction* instruction,
6344 Location out,
6345 Location obj,
6346 uint32_t offset,
6347 Location maybe_temp,
6348 ReadBarrierOption read_barrier_option) {
6349 Register out_reg = out.AsRegister<Register>();
6350 Register obj_reg = obj.AsRegister<Register>();
6351 if (read_barrier_option == kWithReadBarrier) {
6352 CHECK(kEmitCompilerReadBarrier);
6353 if (kUseBakerReadBarrier) {
6354 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6355 // Load with fast path based Baker's read barrier.
6356 // /* HeapReference<Object> */ out = *(obj + offset)
6357 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6358 out,
6359 obj_reg,
6360 offset,
6361 maybe_temp,
6362 /* needs_null_check */ false);
6363 } else {
6364 // Load with slow path based read barrier.
6365 // /* HeapReference<Object> */ out = *(obj + offset)
6366 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6367 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6368 }
6369 } else {
6370 // Plain load with no read barrier.
6371 // /* HeapReference<Object> */ out = *(obj + offset)
6372 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6373 __ MaybeUnpoisonHeapReference(out_reg);
6374 }
6375}
6376
6377void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6378 Location root,
6379 Register obj,
6380 uint32_t offset,
6381 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006382 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006383 if (read_barrier_option == kWithReadBarrier) {
6384 DCHECK(kEmitCompilerReadBarrier);
6385 if (kUseBakerReadBarrier) {
6386 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6387 // Baker's read barrier are used:
6388 //
6389 // root = obj.field;
6390 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6391 // if (temp != null) {
6392 // root = temp(root)
6393 // }
6394
6395 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6396 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6397 static_assert(
6398 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6399 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6400 "have different sizes.");
6401 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6402 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6403 "have different sizes.");
6404
6405 // Slow path marking the GC root `root`.
6406 Location temp = Location::RegisterLocation(T9);
6407 SlowPathCodeMIPS* slow_path =
6408 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6409 instruction,
6410 root,
6411 /*entrypoint*/ temp);
6412 codegen_->AddSlowPath(slow_path);
6413
6414 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6415 const int32_t entry_point_offset =
6416 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6417 // Loading the entrypoint does not require a load acquire since it is only changed when
6418 // threads are suspended or running a checkpoint.
6419 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6420 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6421 // checking GetIsGcMarking.
6422 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6423 __ Bind(slow_path->GetExitLabel());
6424 } else {
6425 // GC root loaded through a slow path for read barriers other
6426 // than Baker's.
6427 // /* GcRoot<mirror::Object>* */ root = obj + offset
6428 __ Addiu32(root_reg, obj, offset);
6429 // /* mirror::Object* */ root = root->Read()
6430 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6431 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006432 } else {
6433 // Plain GC root load with no read barrier.
6434 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6435 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6436 // Note that GC roots are not affected by heap poisoning, thus we
6437 // do not have to unpoison `root_reg` here.
6438 }
6439}
6440
Alexey Frunze15958152017-02-09 19:08:30 -08006441void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6442 Location ref,
6443 Register obj,
6444 uint32_t offset,
6445 Location temp,
6446 bool needs_null_check) {
6447 DCHECK(kEmitCompilerReadBarrier);
6448 DCHECK(kUseBakerReadBarrier);
6449
6450 // /* HeapReference<Object> */ ref = *(obj + offset)
6451 Location no_index = Location::NoLocation();
6452 ScaleFactor no_scale_factor = TIMES_1;
6453 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6454 ref,
6455 obj,
6456 offset,
6457 no_index,
6458 no_scale_factor,
6459 temp,
6460 needs_null_check);
6461}
6462
6463void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6464 Location ref,
6465 Register obj,
6466 uint32_t data_offset,
6467 Location index,
6468 Location temp,
6469 bool needs_null_check) {
6470 DCHECK(kEmitCompilerReadBarrier);
6471 DCHECK(kUseBakerReadBarrier);
6472
6473 static_assert(
6474 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6475 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6476 // /* HeapReference<Object> */ ref =
6477 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6478 ScaleFactor scale_factor = TIMES_4;
6479 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6480 ref,
6481 obj,
6482 data_offset,
6483 index,
6484 scale_factor,
6485 temp,
6486 needs_null_check);
6487}
6488
6489void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6490 Location ref,
6491 Register obj,
6492 uint32_t offset,
6493 Location index,
6494 ScaleFactor scale_factor,
6495 Location temp,
6496 bool needs_null_check,
6497 bool always_update_field) {
6498 DCHECK(kEmitCompilerReadBarrier);
6499 DCHECK(kUseBakerReadBarrier);
6500
6501 // In slow path based read barriers, the read barrier call is
6502 // inserted after the original load. However, in fast path based
6503 // Baker's read barriers, we need to perform the load of
6504 // mirror::Object::monitor_ *before* the original reference load.
6505 // This load-load ordering is required by the read barrier.
6506 // The fast path/slow path (for Baker's algorithm) should look like:
6507 //
6508 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6509 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6510 // HeapReference<Object> ref = *src; // Original reference load.
6511 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6512 // if (is_gray) {
6513 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6514 // }
6515 //
6516 // Note: the original implementation in ReadBarrier::Barrier is
6517 // slightly more complex as it performs additional checks that we do
6518 // not do here for performance reasons.
6519
6520 Register ref_reg = ref.AsRegister<Register>();
6521 Register temp_reg = temp.AsRegister<Register>();
6522 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6523
6524 // /* int32_t */ monitor = obj->monitor_
6525 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6526 if (needs_null_check) {
6527 MaybeRecordImplicitNullCheck(instruction);
6528 }
6529 // /* LockWord */ lock_word = LockWord(monitor)
6530 static_assert(sizeof(LockWord) == sizeof(int32_t),
6531 "art::LockWord and int32_t have different sizes.");
6532
6533 __ Sync(0); // Barrier to prevent load-load reordering.
6534
6535 // The actual reference load.
6536 if (index.IsValid()) {
6537 // Load types involving an "index": ArrayGet,
6538 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6539 // intrinsics.
6540 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6541 if (index.IsConstant()) {
6542 size_t computed_offset =
6543 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6544 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6545 } else {
6546 // Handle the special case of the
6547 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6548 // intrinsics, which use a register pair as index ("long
6549 // offset"), of which only the low part contains data.
6550 Register index_reg = index.IsRegisterPair()
6551 ? index.AsRegisterPairLow<Register>()
6552 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006553 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006554 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6555 }
6556 } else {
6557 // /* HeapReference<Object> */ ref = *(obj + offset)
6558 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6559 }
6560
6561 // Object* ref = ref_addr->AsMirrorPtr()
6562 __ MaybeUnpoisonHeapReference(ref_reg);
6563
6564 // Slow path marking the object `ref` when it is gray.
6565 SlowPathCodeMIPS* slow_path;
6566 if (always_update_field) {
6567 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6568 // of the form `obj + field_offset`, where `obj` is a register and
6569 // `field_offset` is a register pair (of which only the lower half
6570 // is used). Thus `offset` and `scale_factor` above are expected
6571 // to be null in this code path.
6572 DCHECK_EQ(offset, 0u);
6573 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6574 slow_path = new (GetGraph()->GetArena())
6575 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6576 ref,
6577 obj,
6578 /* field_offset */ index,
6579 temp_reg);
6580 } else {
6581 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6582 }
6583 AddSlowPath(slow_path);
6584
6585 // if (rb_state == ReadBarrier::GrayState())
6586 // ref = ReadBarrier::Mark(ref);
6587 // Given the numeric representation, it's enough to check the low bit of the
6588 // rb_state. We do that by shifting the bit into the sign bit (31) and
6589 // performing a branch on less than zero.
6590 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6591 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6592 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6593 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6594 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6595 __ Bind(slow_path->GetExitLabel());
6596}
6597
6598void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6599 Location out,
6600 Location ref,
6601 Location obj,
6602 uint32_t offset,
6603 Location index) {
6604 DCHECK(kEmitCompilerReadBarrier);
6605
6606 // Insert a slow path based read barrier *after* the reference load.
6607 //
6608 // If heap poisoning is enabled, the unpoisoning of the loaded
6609 // reference will be carried out by the runtime within the slow
6610 // path.
6611 //
6612 // Note that `ref` currently does not get unpoisoned (when heap
6613 // poisoning is enabled), which is alright as the `ref` argument is
6614 // not used by the artReadBarrierSlow entry point.
6615 //
6616 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6617 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6618 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6619 AddSlowPath(slow_path);
6620
6621 __ B(slow_path->GetEntryLabel());
6622 __ Bind(slow_path->GetExitLabel());
6623}
6624
6625void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6626 Location out,
6627 Location ref,
6628 Location obj,
6629 uint32_t offset,
6630 Location index) {
6631 if (kEmitCompilerReadBarrier) {
6632 // Baker's read barriers shall be handled by the fast path
6633 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6634 DCHECK(!kUseBakerReadBarrier);
6635 // If heap poisoning is enabled, unpoisoning will be taken care of
6636 // by the runtime within the slow path.
6637 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6638 } else if (kPoisonHeapReferences) {
6639 __ UnpoisonHeapReference(out.AsRegister<Register>());
6640 }
6641}
6642
6643void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6644 Location out,
6645 Location root) {
6646 DCHECK(kEmitCompilerReadBarrier);
6647
6648 // Insert a slow path based read barrier *after* the GC root load.
6649 //
6650 // Note that GC roots are not affected by heap poisoning, so we do
6651 // not need to do anything special for this here.
6652 SlowPathCodeMIPS* slow_path =
6653 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6654 AddSlowPath(slow_path);
6655
6656 __ B(slow_path->GetEntryLabel());
6657 __ Bind(slow_path->GetExitLabel());
6658}
6659
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006660void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006661 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6662 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006663 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006664 switch (type_check_kind) {
6665 case TypeCheckKind::kExactCheck:
6666 case TypeCheckKind::kAbstractClassCheck:
6667 case TypeCheckKind::kClassHierarchyCheck:
6668 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006669 call_kind =
6670 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006671 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006672 break;
6673 case TypeCheckKind::kArrayCheck:
6674 case TypeCheckKind::kUnresolvedCheck:
6675 case TypeCheckKind::kInterfaceCheck:
6676 call_kind = LocationSummary::kCallOnSlowPath;
6677 break;
6678 }
6679
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006681 if (baker_read_barrier_slow_path) {
6682 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6683 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684 locations->SetInAt(0, Location::RequiresRegister());
6685 locations->SetInAt(1, Location::RequiresRegister());
6686 // The output does overlap inputs.
6687 // Note that TypeCheckSlowPathMIPS uses this register too.
6688 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006689 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006690}
6691
6692void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006693 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006694 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006695 Location obj_loc = locations->InAt(0);
6696 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006697 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006698 Location out_loc = locations->Out();
6699 Register out = out_loc.AsRegister<Register>();
6700 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6701 DCHECK_LE(num_temps, 1u);
6702 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006703 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6704 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6705 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6706 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006708 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006709
6710 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006711 // Avoid this check if we know `obj` is not null.
6712 if (instruction->MustDoNullCheck()) {
6713 __ Move(out, ZERO);
6714 __ Beqz(obj, &done);
6715 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006716
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006717 switch (type_check_kind) {
6718 case TypeCheckKind::kExactCheck: {
6719 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006720 GenerateReferenceLoadTwoRegisters(instruction,
6721 out_loc,
6722 obj_loc,
6723 class_offset,
6724 maybe_temp_loc,
6725 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006726 // Classes must be equal for the instanceof to succeed.
6727 __ Xor(out, out, cls);
6728 __ Sltiu(out, out, 1);
6729 break;
6730 }
6731
6732 case TypeCheckKind::kAbstractClassCheck: {
6733 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006734 GenerateReferenceLoadTwoRegisters(instruction,
6735 out_loc,
6736 obj_loc,
6737 class_offset,
6738 maybe_temp_loc,
6739 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006740 // If the class is abstract, we eagerly fetch the super class of the
6741 // object to avoid doing a comparison we know will fail.
6742 MipsLabel loop;
6743 __ Bind(&loop);
6744 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006745 GenerateReferenceLoadOneRegister(instruction,
6746 out_loc,
6747 super_offset,
6748 maybe_temp_loc,
6749 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006750 // If `out` is null, we use it for the result, and jump to `done`.
6751 __ Beqz(out, &done);
6752 __ Bne(out, cls, &loop);
6753 __ LoadConst32(out, 1);
6754 break;
6755 }
6756
6757 case TypeCheckKind::kClassHierarchyCheck: {
6758 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006759 GenerateReferenceLoadTwoRegisters(instruction,
6760 out_loc,
6761 obj_loc,
6762 class_offset,
6763 maybe_temp_loc,
6764 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006765 // Walk over the class hierarchy to find a match.
6766 MipsLabel loop, success;
6767 __ Bind(&loop);
6768 __ Beq(out, cls, &success);
6769 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006770 GenerateReferenceLoadOneRegister(instruction,
6771 out_loc,
6772 super_offset,
6773 maybe_temp_loc,
6774 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006775 __ Bnez(out, &loop);
6776 // If `out` is null, we use it for the result, and jump to `done`.
6777 __ B(&done);
6778 __ Bind(&success);
6779 __ LoadConst32(out, 1);
6780 break;
6781 }
6782
6783 case TypeCheckKind::kArrayObjectCheck: {
6784 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006785 GenerateReferenceLoadTwoRegisters(instruction,
6786 out_loc,
6787 obj_loc,
6788 class_offset,
6789 maybe_temp_loc,
6790 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006791 // Do an exact check.
6792 MipsLabel success;
6793 __ Beq(out, cls, &success);
6794 // Otherwise, we need to check that the object's class is a non-primitive array.
6795 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006796 GenerateReferenceLoadOneRegister(instruction,
6797 out_loc,
6798 component_offset,
6799 maybe_temp_loc,
6800 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006801 // If `out` is null, we use it for the result, and jump to `done`.
6802 __ Beqz(out, &done);
6803 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6804 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6805 __ Sltiu(out, out, 1);
6806 __ B(&done);
6807 __ Bind(&success);
6808 __ LoadConst32(out, 1);
6809 break;
6810 }
6811
6812 case TypeCheckKind::kArrayCheck: {
6813 // No read barrier since the slow path will retry upon failure.
6814 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006815 GenerateReferenceLoadTwoRegisters(instruction,
6816 out_loc,
6817 obj_loc,
6818 class_offset,
6819 maybe_temp_loc,
6820 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006821 DCHECK(locations->OnlyCallsOnSlowPath());
6822 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6823 /* is_fatal */ false);
6824 codegen_->AddSlowPath(slow_path);
6825 __ Bne(out, cls, slow_path->GetEntryLabel());
6826 __ LoadConst32(out, 1);
6827 break;
6828 }
6829
6830 case TypeCheckKind::kUnresolvedCheck:
6831 case TypeCheckKind::kInterfaceCheck: {
6832 // Note that we indeed only call on slow path, but we always go
6833 // into the slow path for the unresolved and interface check
6834 // cases.
6835 //
6836 // We cannot directly call the InstanceofNonTrivial runtime
6837 // entry point without resorting to a type checking slow path
6838 // here (i.e. by calling InvokeRuntime directly), as it would
6839 // require to assign fixed registers for the inputs of this
6840 // HInstanceOf instruction (following the runtime calling
6841 // convention), which might be cluttered by the potential first
6842 // read barrier emission at the beginning of this method.
6843 //
6844 // TODO: Introduce a new runtime entry point taking the object
6845 // to test (instead of its class) as argument, and let it deal
6846 // with the read barrier issues. This will let us refactor this
6847 // case of the `switch` code as it was previously (with a direct
6848 // call to the runtime not using a type checking slow path).
6849 // This should also be beneficial for the other cases above.
6850 DCHECK(locations->OnlyCallsOnSlowPath());
6851 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6852 /* is_fatal */ false);
6853 codegen_->AddSlowPath(slow_path);
6854 __ B(slow_path->GetEntryLabel());
6855 break;
6856 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006857 }
6858
6859 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006860
6861 if (slow_path != nullptr) {
6862 __ Bind(slow_path->GetExitLabel());
6863 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006864}
6865
6866void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6867 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6868 locations->SetOut(Location::ConstantLocation(constant));
6869}
6870
6871void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6872 // Will be generated at use site.
6873}
6874
6875void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6877 locations->SetOut(Location::ConstantLocation(constant));
6878}
6879
6880void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6881 // Will be generated at use site.
6882}
6883
6884void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6885 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6886 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6887}
6888
6889void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6890 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006891 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006892 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006893 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006894}
6895
6896void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6897 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6898 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006899 Location receiver = invoke->GetLocations()->InAt(0);
6900 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006901 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006902
6903 // Set the hidden argument.
6904 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6905 invoke->GetDexMethodIndex());
6906
6907 // temp = object->GetClass();
6908 if (receiver.IsStackSlot()) {
6909 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6910 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6911 } else {
6912 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6913 }
6914 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006915 // Instead of simply (possibly) unpoisoning `temp` here, we should
6916 // emit a read barrier for the previous class reference load.
6917 // However this is not required in practice, as this is an
6918 // intermediate/temporary reference and because the current
6919 // concurrent copying collector keeps the from-space memory
6920 // intact/accessible until the end of the marking phase (the
6921 // concurrent copying collector may not in the future).
6922 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006923 __ LoadFromOffset(kLoadWord, temp, temp,
6924 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6925 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006926 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006927 // temp = temp->GetImtEntryAt(method_offset);
6928 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6929 // T9 = temp->GetEntryPoint();
6930 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6931 // T9();
6932 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006933 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006934 DCHECK(!codegen_->IsLeafMethod());
6935 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6936}
6937
6938void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006939 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6940 if (intrinsic.TryDispatch(invoke)) {
6941 return;
6942 }
6943
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006944 HandleInvoke(invoke);
6945}
6946
6947void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006948 // Explicit clinit checks triggered by static invokes must have been pruned by
6949 // art::PrepareForRegisterAllocation.
6950 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006951
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006952 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01006953 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006954
Chris Larsen701566a2015-10-27 15:29:13 -07006955 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6956 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006957 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6958 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6959 }
Chris Larsen701566a2015-10-27 15:29:13 -07006960 return;
6961 }
6962
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006963 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006964
6965 // Add the extra input register if either the dex cache array base register
6966 // or the PC-relative base register for accessing literals is needed.
6967 if (has_extra_input) {
6968 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6969 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006970}
6971
Orion Hodsonac141392017-01-13 11:53:47 +00006972void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6973 HandleInvoke(invoke);
6974}
6975
6976void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6977 codegen_->GenerateInvokePolymorphicCall(invoke);
6978}
6979
Chris Larsen701566a2015-10-27 15:29:13 -07006980static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006981 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006982 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6983 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006984 return true;
6985 }
6986 return false;
6987}
6988
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006989HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07006990 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006991 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006992 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006993 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
Vladimir Markoaad75c62016-10-03 08:46:48 +00006994 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006995 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006996 bool is_r6 = GetInstructionSetFeatures().IsR6();
6997 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006998 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006999 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007000 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007001 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007002 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007003 case HLoadString::LoadKind::kBootImageAddress:
7004 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007005 case HLoadString::LoadKind::kJitTableAddress:
7006 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007007 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007008 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007009 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007010 fallback_load = false;
7011 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007012 }
7013 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007014 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007015 }
7016 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007017}
7018
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007019HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7020 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007021 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007022 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007023 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7024 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007025 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007026 bool is_r6 = GetInstructionSetFeatures().IsR6();
7027 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007028 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007029 case HLoadClass::LoadKind::kInvalid:
7030 LOG(FATAL) << "UNREACHABLE";
7031 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007032 case HLoadClass::LoadKind::kReferrersClass:
7033 fallback_load = false;
7034 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007035 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007036 case HLoadClass::LoadKind::kBssEntry:
7037 DCHECK(!Runtime::Current()->UseJitCompilation());
7038 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007039 case HLoadClass::LoadKind::kBootImageAddress:
7040 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007041 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007042 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007043 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007044 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007045 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007046 fallback_load = false;
7047 break;
7048 }
7049 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007050 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007051 }
7052 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007053}
7054
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007055Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7056 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007057 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007058 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7059 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7060 if (!invoke->GetLocations()->Intrinsified()) {
7061 return location.AsRegister<Register>();
7062 }
7063 // For intrinsics we allow any location, so it may be on the stack.
7064 if (!location.IsRegister()) {
7065 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7066 return temp;
7067 }
7068 // For register locations, check if the register was saved. If so, get it from the stack.
7069 // Note: There is a chance that the register was saved but not overwritten, so we could
7070 // save one load. However, since this is just an intrinsic slow path we prefer this
7071 // simple and more robust approach rather that trying to determine if that's the case.
7072 SlowPathCode* slow_path = GetCurrentSlowPath();
7073 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7074 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7075 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7076 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7077 return temp;
7078 }
7079 return location.AsRegister<Register>();
7080}
7081
Vladimir Markodc151b22015-10-15 18:02:30 +01007082HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7083 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007084 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007085 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007086 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007087 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007088 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7089 // with irreducible loops.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007090 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007091 bool is_r6 = GetInstructionSetFeatures().IsR6();
7092 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007093 switch (dispatch_info.method_load_kind) {
Vladimir Marko65979462017-05-19 17:25:12 +01007094 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007095 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007096 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007097 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007098 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007099 break;
7100 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007101 if (fallback_load) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007102 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007103 dispatch_info.method_load_data = 0;
7104 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007105 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007106}
7107
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007108void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7109 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007110 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007111 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007112 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7113 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007114 bool is_r6 = GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007115 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007116 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7117 : ZERO;
7118
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007119 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007120 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007121 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007122 uint32_t offset =
7123 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007124 __ LoadFromOffset(kLoadWord,
7125 temp.AsRegister<Register>(),
7126 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007127 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007128 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007129 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007130 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007131 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007132 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007133 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7134 DCHECK(GetCompilerOptions().IsBootImage());
7135 PcRelativePatchInfo* info = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7136 bool reordering = __ SetReorder(false);
7137 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunze6079dca2017-05-28 19:10:28 -07007138 EmitPcRelativeAddressPlaceholderHigh(info, TMP, base_reg);
7139 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678);
Vladimir Marko65979462017-05-19 17:25:12 +01007140 __ SetReorder(reordering);
7141 break;
7142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007143 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7144 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7145 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007146 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
7147 PcRelativePatchInfo* info = NewMethodBssEntryPatch(
7148 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
7149 Register temp_reg = temp.AsRegister<Register>();
7150 bool reordering = __ SetReorder(false);
7151 EmitPcRelativeAddressPlaceholderHigh(info, TMP, base_reg);
7152 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678);
7153 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007154 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007155 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007156 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7157 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7158 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007159 }
7160 }
7161
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007162 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007163 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007164 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007165 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007166 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7167 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007168 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007169 T9,
7170 callee_method.AsRegister<Register>(),
7171 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007172 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007173 // T9()
7174 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007175 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176 break;
7177 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007178 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7179
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007180 DCHECK(!IsLeafMethod());
7181}
7182
7183void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007184 // Explicit clinit checks triggered by static invokes must have been pruned by
7185 // art::PrepareForRegisterAllocation.
7186 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007187
7188 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7189 return;
7190 }
7191
7192 LocationSummary* locations = invoke->GetLocations();
7193 codegen_->GenerateStaticOrDirectCall(invoke,
7194 locations->HasTemps()
7195 ? locations->GetTemp(0)
7196 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007197}
7198
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007199void CodeGeneratorMIPS::GenerateVirtualCall(
7200 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007201 // Use the calling convention instead of the location of the receiver, as
7202 // intrinsics may have put the receiver in a different register. In the intrinsics
7203 // slow path, the arguments have been moved to the right place, so here we are
7204 // guaranteed that the receiver is the first register of the calling convention.
7205 InvokeDexCallingConvention calling_convention;
7206 Register receiver = calling_convention.GetRegisterAt(0);
7207
Chris Larsen3acee732015-11-18 13:31:08 -08007208 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007209 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7210 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7211 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007212 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007213
7214 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007215 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007216 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007217 // Instead of simply (possibly) unpoisoning `temp` here, we should
7218 // emit a read barrier for the previous class reference load.
7219 // However this is not required in practice, as this is an
7220 // intermediate/temporary reference and because the current
7221 // concurrent copying collector keeps the from-space memory
7222 // intact/accessible until the end of the marking phase (the
7223 // concurrent copying collector may not in the future).
7224 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007225 // temp = temp->GetMethodAt(method_offset);
7226 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7227 // T9 = temp->GetEntryPoint();
7228 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7229 // T9();
7230 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007231 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007232 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007233}
7234
7235void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7236 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7237 return;
7238 }
7239
7240 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007241 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007242}
7243
7244void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007245 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007246 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007247 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007248 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7249 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007250 return;
7251 }
Vladimir Marko41559982017-01-06 14:04:23 +00007252 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007253 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007254 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7255 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007256 ? LocationSummary::kCallOnSlowPath
7257 : LocationSummary::kNoCall;
7258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007259 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7260 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7261 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007262 switch (load_kind) {
7263 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007264 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007265 case HLoadClass::LoadKind::kBootImageAddress:
7266 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007267 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007268 break;
7269 }
7270 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007271 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007272 locations->SetInAt(0, Location::RequiresRegister());
7273 break;
7274 default:
7275 break;
7276 }
7277 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007278 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7279 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7280 // Rely on the type resolution or initialization and marking to save everything we need.
7281 // Request a temp to hold the BSS entry location for the slow path on R2
7282 // (no benefit for R6).
7283 if (!isR6) {
7284 locations->AddTemp(Location::RequiresRegister());
7285 }
7286 RegisterSet caller_saves = RegisterSet::Empty();
7287 InvokeRuntimeCallingConvention calling_convention;
7288 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7289 locations->SetCustomSlowPathCallerSaves(caller_saves);
7290 } else {
7291 // For non-Baker read barriers we have a temp-clobbering call.
7292 }
7293 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007294}
7295
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007296// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7297// move.
7298void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007299 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007300 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007301 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007302 return;
7303 }
Vladimir Marko41559982017-01-06 14:04:23 +00007304 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007305
Vladimir Marko41559982017-01-06 14:04:23 +00007306 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007307 Location out_loc = locations->Out();
7308 Register out = out_loc.AsRegister<Register>();
7309 Register base_or_current_method_reg;
7310 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7311 switch (load_kind) {
7312 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007313 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007314 case HLoadClass::LoadKind::kBootImageAddress:
7315 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007316 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7317 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007318 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007319 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007320 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7321 break;
7322 default:
7323 base_or_current_method_reg = ZERO;
7324 break;
7325 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007326
Alexey Frunze15958152017-02-09 19:08:30 -08007327 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7328 ? kWithoutReadBarrier
7329 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007330 bool generate_null_check = false;
7331 switch (load_kind) {
7332 case HLoadClass::LoadKind::kReferrersClass: {
7333 DCHECK(!cls->CanCallRuntime());
7334 DCHECK(!cls->MustGenerateClinitCheck());
7335 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7336 GenerateGcRootFieldLoad(cls,
7337 out_loc,
7338 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007339 ArtMethod::DeclaringClassOffset().Int32Value(),
7340 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007341 break;
7342 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007343 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007344 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007345 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007346 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7347 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007348 bool reordering = __ SetReorder(false);
7349 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7350 __ Addiu(out, out, /* placeholder */ 0x5678);
7351 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007352 break;
7353 }
7354 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007355 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007356 uint32_t address = dchecked_integral_cast<uint32_t>(
7357 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7358 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007359 __ LoadLiteral(out,
7360 base_or_current_method_reg,
7361 codegen_->DeduplicateBootImageAddressLiteral(address));
7362 break;
7363 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007364 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007365 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007366 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007367 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7368 if (isR6 || non_baker_read_barrier) {
7369 bool reordering = __ SetReorder(false);
7370 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7371 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7372 __ SetReorder(reordering);
7373 } else {
7374 // On R2 save the BSS entry address in a temporary register instead of
7375 // recalculating it in the slow path.
7376 Register temp = locations->GetTemp(0).AsRegister<Register>();
7377 bool reordering = __ SetReorder(false);
7378 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7379 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7380 __ SetReorder(reordering);
7381 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7382 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007383 generate_null_check = true;
7384 break;
7385 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007386 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007387 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7388 cls->GetTypeIndex(),
7389 cls->GetClass());
7390 bool reordering = __ SetReorder(false);
7391 __ Bind(&info->high_label);
7392 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007393 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007394 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007395 break;
7396 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007397 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007398 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007399 LOG(FATAL) << "UNREACHABLE";
7400 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007401 }
7402
7403 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7404 DCHECK(cls->CanCallRuntime());
7405 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7406 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7407 codegen_->AddSlowPath(slow_path);
7408 if (generate_null_check) {
7409 __ Beqz(out, slow_path->GetEntryLabel());
7410 }
7411 if (cls->MustGenerateClinitCheck()) {
7412 GenerateClassInitializationCheck(slow_path, out);
7413 } else {
7414 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007415 }
7416 }
7417}
7418
7419static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007420 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007421}
7422
7423void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7424 LocationSummary* locations =
7425 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7426 locations->SetOut(Location::RequiresRegister());
7427}
7428
7429void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7430 Register out = load->GetLocations()->Out().AsRegister<Register>();
7431 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7432}
7433
7434void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7435 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7436}
7437
7438void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7439 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7440}
7441
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007442void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007443 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007444 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007445 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007446 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007447 switch (load_kind) {
7448 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007449 case HLoadString::LoadKind::kBootImageAddress:
7450 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007451 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007452 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007453 break;
7454 }
7455 FALLTHROUGH_INTENDED;
7456 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007457 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007458 locations->SetInAt(0, Location::RequiresRegister());
7459 break;
7460 default:
7461 break;
7462 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007463 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007464 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007465 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007466 } else {
7467 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007468 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7469 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7470 // Rely on the pResolveString and marking to save everything we need.
7471 // Request a temp to hold the BSS entry location for the slow path on R2
7472 // (no benefit for R6).
7473 if (!isR6) {
7474 locations->AddTemp(Location::RequiresRegister());
7475 }
7476 RegisterSet caller_saves = RegisterSet::Empty();
7477 InvokeRuntimeCallingConvention calling_convention;
7478 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7479 locations->SetCustomSlowPathCallerSaves(caller_saves);
7480 } else {
7481 // For non-Baker read barriers we have a temp-clobbering call.
7482 }
7483 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007484 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007485}
7486
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007487// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7488// move.
7489void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007490 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007491 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007492 Location out_loc = locations->Out();
7493 Register out = out_loc.AsRegister<Register>();
7494 Register base_or_current_method_reg;
7495 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7496 switch (load_kind) {
7497 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007498 case HLoadString::LoadKind::kBootImageAddress:
7499 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007500 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007501 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7502 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007503 default:
7504 base_or_current_method_reg = ZERO;
7505 break;
7506 }
7507
7508 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007509 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007510 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007511 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007512 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007513 bool reordering = __ SetReorder(false);
7514 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7515 __ Addiu(out, out, /* placeholder */ 0x5678);
7516 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007517 return; // No dex cache slow path.
7518 }
7519 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007520 uint32_t address = dchecked_integral_cast<uint32_t>(
7521 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7522 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007523 __ LoadLiteral(out,
7524 base_or_current_method_reg,
7525 codegen_->DeduplicateBootImageAddressLiteral(address));
7526 return; // No dex cache slow path.
7527 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007528 case HLoadString::LoadKind::kBssEntry: {
7529 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7530 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007531 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007532 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7533 if (isR6 || non_baker_read_barrier) {
7534 bool reordering = __ SetReorder(false);
7535 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7536 GenerateGcRootFieldLoad(load,
7537 out_loc,
7538 out,
7539 /* placeholder */ 0x5678,
7540 kCompilerReadBarrierOption);
7541 __ SetReorder(reordering);
7542 } else {
7543 // On R2 save the BSS entry address in a temporary register instead of
7544 // recalculating it in the slow path.
7545 Register temp = locations->GetTemp(0).AsRegister<Register>();
7546 bool reordering = __ SetReorder(false);
7547 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7548 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7549 __ SetReorder(reordering);
7550 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7551 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007552 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7553 codegen_->AddSlowPath(slow_path);
7554 __ Beqz(out, slow_path->GetEntryLabel());
7555 __ Bind(slow_path->GetExitLabel());
7556 return;
7557 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007558 case HLoadString::LoadKind::kJitTableAddress: {
7559 CodeGeneratorMIPS::JitPatchInfo* info =
7560 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7561 load->GetStringIndex(),
7562 load->GetString());
7563 bool reordering = __ SetReorder(false);
7564 __ Bind(&info->high_label);
7565 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007566 GenerateGcRootFieldLoad(load,
7567 out_loc,
7568 out,
7569 /* placeholder */ 0x5678,
7570 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007571 __ SetReorder(reordering);
7572 return;
7573 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007574 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007575 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007576 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007577
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007578 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007579 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007580 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007581 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007582 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007583 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7584 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007585}
7586
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007587void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7589 locations->SetOut(Location::ConstantLocation(constant));
7590}
7591
7592void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7593 // Will be generated at use site.
7594}
7595
7596void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7597 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007599 InvokeRuntimeCallingConvention calling_convention;
7600 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7601}
7602
7603void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7604 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007605 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007606 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7607 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007608 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 }
7610 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7611}
7612
7613void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7614 LocationSummary* locations =
7615 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7616 switch (mul->GetResultType()) {
7617 case Primitive::kPrimInt:
7618 case Primitive::kPrimLong:
7619 locations->SetInAt(0, Location::RequiresRegister());
7620 locations->SetInAt(1, Location::RequiresRegister());
7621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7622 break;
7623
7624 case Primitive::kPrimFloat:
7625 case Primitive::kPrimDouble:
7626 locations->SetInAt(0, Location::RequiresFpuRegister());
7627 locations->SetInAt(1, Location::RequiresFpuRegister());
7628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7629 break;
7630
7631 default:
7632 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7633 }
7634}
7635
7636void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7637 Primitive::Type type = instruction->GetType();
7638 LocationSummary* locations = instruction->GetLocations();
7639 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7640
7641 switch (type) {
7642 case Primitive::kPrimInt: {
7643 Register dst = locations->Out().AsRegister<Register>();
7644 Register lhs = locations->InAt(0).AsRegister<Register>();
7645 Register rhs = locations->InAt(1).AsRegister<Register>();
7646
7647 if (isR6) {
7648 __ MulR6(dst, lhs, rhs);
7649 } else {
7650 __ MulR2(dst, lhs, rhs);
7651 }
7652 break;
7653 }
7654 case Primitive::kPrimLong: {
7655 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7656 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7657 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7658 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7659 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7660 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7661
7662 // Extra checks to protect caused by the existance of A1_A2.
7663 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7664 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7665 DCHECK_NE(dst_high, lhs_low);
7666 DCHECK_NE(dst_high, rhs_low);
7667
7668 // A_B * C_D
7669 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7670 // dst_lo: [ low(B*D) ]
7671 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7672
7673 if (isR6) {
7674 __ MulR6(TMP, lhs_high, rhs_low);
7675 __ MulR6(dst_high, lhs_low, rhs_high);
7676 __ Addu(dst_high, dst_high, TMP);
7677 __ MuhuR6(TMP, lhs_low, rhs_low);
7678 __ Addu(dst_high, dst_high, TMP);
7679 __ MulR6(dst_low, lhs_low, rhs_low);
7680 } else {
7681 __ MulR2(TMP, lhs_high, rhs_low);
7682 __ MulR2(dst_high, lhs_low, rhs_high);
7683 __ Addu(dst_high, dst_high, TMP);
7684 __ MultuR2(lhs_low, rhs_low);
7685 __ Mfhi(TMP);
7686 __ Addu(dst_high, dst_high, TMP);
7687 __ Mflo(dst_low);
7688 }
7689 break;
7690 }
7691 case Primitive::kPrimFloat:
7692 case Primitive::kPrimDouble: {
7693 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7694 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7695 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7696 if (type == Primitive::kPrimFloat) {
7697 __ MulS(dst, lhs, rhs);
7698 } else {
7699 __ MulD(dst, lhs, rhs);
7700 }
7701 break;
7702 }
7703 default:
7704 LOG(FATAL) << "Unexpected mul type " << type;
7705 }
7706}
7707
7708void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7709 LocationSummary* locations =
7710 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7711 switch (neg->GetResultType()) {
7712 case Primitive::kPrimInt:
7713 case Primitive::kPrimLong:
7714 locations->SetInAt(0, Location::RequiresRegister());
7715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7716 break;
7717
7718 case Primitive::kPrimFloat:
7719 case Primitive::kPrimDouble:
7720 locations->SetInAt(0, Location::RequiresFpuRegister());
7721 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7722 break;
7723
7724 default:
7725 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7726 }
7727}
7728
7729void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7730 Primitive::Type type = instruction->GetType();
7731 LocationSummary* locations = instruction->GetLocations();
7732
7733 switch (type) {
7734 case Primitive::kPrimInt: {
7735 Register dst = locations->Out().AsRegister<Register>();
7736 Register src = locations->InAt(0).AsRegister<Register>();
7737 __ Subu(dst, ZERO, src);
7738 break;
7739 }
7740 case Primitive::kPrimLong: {
7741 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7742 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7743 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7744 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7745 __ Subu(dst_low, ZERO, src_low);
7746 __ Sltu(TMP, ZERO, dst_low);
7747 __ Subu(dst_high, ZERO, src_high);
7748 __ Subu(dst_high, dst_high, TMP);
7749 break;
7750 }
7751 case Primitive::kPrimFloat:
7752 case Primitive::kPrimDouble: {
7753 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7754 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7755 if (type == Primitive::kPrimFloat) {
7756 __ NegS(dst, src);
7757 } else {
7758 __ NegD(dst, src);
7759 }
7760 break;
7761 }
7762 default:
7763 LOG(FATAL) << "Unexpected neg type " << type;
7764 }
7765}
7766
7767void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7768 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007770 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007771 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007772 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7773 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007774}
7775
7776void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007777 // Note: if heap poisoning is enabled, the entry point takes care
7778 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007779 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7780 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007781}
7782
7783void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7784 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007785 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007786 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007787 if (instruction->IsStringAlloc()) {
7788 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7789 } else {
7790 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007792 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7793}
7794
7795void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007796 // Note: if heap poisoning is enabled, the entry point takes care
7797 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007798 if (instruction->IsStringAlloc()) {
7799 // String is allocated through StringFactory. Call NewEmptyString entry point.
7800 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007801 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007802 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7803 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7804 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007805 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007806 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7807 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007808 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007809 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007810 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007811}
7812
7813void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7815 locations->SetInAt(0, Location::RequiresRegister());
7816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7817}
7818
7819void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7820 Primitive::Type type = instruction->GetType();
7821 LocationSummary* locations = instruction->GetLocations();
7822
7823 switch (type) {
7824 case Primitive::kPrimInt: {
7825 Register dst = locations->Out().AsRegister<Register>();
7826 Register src = locations->InAt(0).AsRegister<Register>();
7827 __ Nor(dst, src, ZERO);
7828 break;
7829 }
7830
7831 case Primitive::kPrimLong: {
7832 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7833 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7834 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7835 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7836 __ Nor(dst_high, src_high, ZERO);
7837 __ Nor(dst_low, src_low, ZERO);
7838 break;
7839 }
7840
7841 default:
7842 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7843 }
7844}
7845
7846void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7847 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7848 locations->SetInAt(0, Location::RequiresRegister());
7849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7850}
7851
7852void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7853 LocationSummary* locations = instruction->GetLocations();
7854 __ Xori(locations->Out().AsRegister<Register>(),
7855 locations->InAt(0).AsRegister<Register>(),
7856 1);
7857}
7858
7859void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007860 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7861 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007862}
7863
Calin Juravle2ae48182016-03-16 14:05:09 +00007864void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7865 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007866 return;
7867 }
7868 Location obj = instruction->GetLocations()->InAt(0);
7869
7870 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007871 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007872}
7873
Calin Juravle2ae48182016-03-16 14:05:09 +00007874void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007875 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007876 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007877
7878 Location obj = instruction->GetLocations()->InAt(0);
7879
7880 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7881}
7882
7883void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007884 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007885}
7886
7887void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7888 HandleBinaryOp(instruction);
7889}
7890
7891void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7892 HandleBinaryOp(instruction);
7893}
7894
7895void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7896 LOG(FATAL) << "Unreachable";
7897}
7898
7899void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7900 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7901}
7902
7903void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7905 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7906 if (location.IsStackSlot()) {
7907 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7908 } else if (location.IsDoubleStackSlot()) {
7909 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7910 }
7911 locations->SetOut(location);
7912}
7913
7914void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7915 ATTRIBUTE_UNUSED) {
7916 // Nothing to do, the parameter is already at its location.
7917}
7918
7919void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7920 LocationSummary* locations =
7921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7922 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7923}
7924
7925void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7926 ATTRIBUTE_UNUSED) {
7927 // Nothing to do, the method is already at its location.
7928}
7929
7930void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007932 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007933 locations->SetInAt(i, Location::Any());
7934 }
7935 locations->SetOut(Location::Any());
7936}
7937
7938void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7939 LOG(FATAL) << "Unreachable";
7940}
7941
7942void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7943 Primitive::Type type = rem->GetResultType();
7944 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007945 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7947
7948 switch (type) {
7949 case Primitive::kPrimInt:
7950 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007951 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7953 break;
7954
7955 case Primitive::kPrimLong: {
7956 InvokeRuntimeCallingConvention calling_convention;
7957 locations->SetInAt(0, Location::RegisterPairLocation(
7958 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7959 locations->SetInAt(1, Location::RegisterPairLocation(
7960 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7961 locations->SetOut(calling_convention.GetReturnLocation(type));
7962 break;
7963 }
7964
7965 case Primitive::kPrimFloat:
7966 case Primitive::kPrimDouble: {
7967 InvokeRuntimeCallingConvention calling_convention;
7968 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7969 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
7970 locations->SetOut(calling_convention.GetReturnLocation(type));
7971 break;
7972 }
7973
7974 default:
7975 LOG(FATAL) << "Unexpected rem type " << type;
7976 }
7977}
7978
7979void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
7980 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007981
7982 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08007983 case Primitive::kPrimInt:
7984 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007985 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007986 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007987 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007988 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
7989 break;
7990 }
7991 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007992 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007993 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007994 break;
7995 }
7996 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007997 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007998 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007999 break;
8000 }
8001 default:
8002 LOG(FATAL) << "Unexpected rem type " << type;
8003 }
8004}
8005
Igor Murashkind01745e2017-04-05 16:40:31 -07008006void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8007 constructor_fence->SetLocations(nullptr);
8008}
8009
8010void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8011 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8012 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8013}
8014
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008015void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8016 memory_barrier->SetLocations(nullptr);
8017}
8018
8019void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8020 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8021}
8022
8023void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8025 Primitive::Type return_type = ret->InputAt(0)->GetType();
8026 locations->SetInAt(0, MipsReturnLocation(return_type));
8027}
8028
8029void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8030 codegen_->GenerateFrameExit();
8031}
8032
8033void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8034 ret->SetLocations(nullptr);
8035}
8036
8037void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8038 codegen_->GenerateFrameExit();
8039}
8040
Alexey Frunze92d90602015-12-18 18:16:36 -08008041void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8042 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008043}
8044
Alexey Frunze92d90602015-12-18 18:16:36 -08008045void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8046 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008047}
8048
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008049void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8050 HandleShift(shl);
8051}
8052
8053void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8054 HandleShift(shl);
8055}
8056
8057void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8058 HandleShift(shr);
8059}
8060
8061void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8062 HandleShift(shr);
8063}
8064
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008065void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8066 HandleBinaryOp(instruction);
8067}
8068
8069void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8070 HandleBinaryOp(instruction);
8071}
8072
8073void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8074 HandleFieldGet(instruction, instruction->GetFieldInfo());
8075}
8076
8077void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8078 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8079}
8080
8081void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8082 HandleFieldSet(instruction, instruction->GetFieldInfo());
8083}
8084
8085void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008086 HandleFieldSet(instruction,
8087 instruction->GetFieldInfo(),
8088 instruction->GetDexPc(),
8089 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008090}
8091
8092void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8093 HUnresolvedInstanceFieldGet* instruction) {
8094 FieldAccessCallingConventionMIPS calling_convention;
8095 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8096 instruction->GetFieldType(),
8097 calling_convention);
8098}
8099
8100void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8101 HUnresolvedInstanceFieldGet* instruction) {
8102 FieldAccessCallingConventionMIPS calling_convention;
8103 codegen_->GenerateUnresolvedFieldAccess(instruction,
8104 instruction->GetFieldType(),
8105 instruction->GetFieldIndex(),
8106 instruction->GetDexPc(),
8107 calling_convention);
8108}
8109
8110void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8111 HUnresolvedInstanceFieldSet* instruction) {
8112 FieldAccessCallingConventionMIPS calling_convention;
8113 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8114 instruction->GetFieldType(),
8115 calling_convention);
8116}
8117
8118void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8119 HUnresolvedInstanceFieldSet* instruction) {
8120 FieldAccessCallingConventionMIPS calling_convention;
8121 codegen_->GenerateUnresolvedFieldAccess(instruction,
8122 instruction->GetFieldType(),
8123 instruction->GetFieldIndex(),
8124 instruction->GetDexPc(),
8125 calling_convention);
8126}
8127
8128void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8129 HUnresolvedStaticFieldGet* instruction) {
8130 FieldAccessCallingConventionMIPS calling_convention;
8131 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8132 instruction->GetFieldType(),
8133 calling_convention);
8134}
8135
8136void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8137 HUnresolvedStaticFieldGet* instruction) {
8138 FieldAccessCallingConventionMIPS calling_convention;
8139 codegen_->GenerateUnresolvedFieldAccess(instruction,
8140 instruction->GetFieldType(),
8141 instruction->GetFieldIndex(),
8142 instruction->GetDexPc(),
8143 calling_convention);
8144}
8145
8146void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8147 HUnresolvedStaticFieldSet* instruction) {
8148 FieldAccessCallingConventionMIPS calling_convention;
8149 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8150 instruction->GetFieldType(),
8151 calling_convention);
8152}
8153
8154void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8155 HUnresolvedStaticFieldSet* instruction) {
8156 FieldAccessCallingConventionMIPS calling_convention;
8157 codegen_->GenerateUnresolvedFieldAccess(instruction,
8158 instruction->GetFieldType(),
8159 instruction->GetFieldIndex(),
8160 instruction->GetDexPc(),
8161 calling_convention);
8162}
8163
8164void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008165 LocationSummary* locations =
8166 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008167 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008168}
8169
8170void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8171 HBasicBlock* block = instruction->GetBlock();
8172 if (block->GetLoopInformation() != nullptr) {
8173 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8174 // The back edge will generate the suspend check.
8175 return;
8176 }
8177 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8178 // The goto will generate the suspend check.
8179 return;
8180 }
8181 GenerateSuspendCheck(instruction, nullptr);
8182}
8183
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008184void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8185 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008186 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008187 InvokeRuntimeCallingConvention calling_convention;
8188 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8189}
8190
8191void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008192 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008193 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8194}
8195
8196void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8197 Primitive::Type input_type = conversion->GetInputType();
8198 Primitive::Type result_type = conversion->GetResultType();
8199 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008200 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008201
8202 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8203 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8204 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8205 }
8206
8207 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008208 if (!isR6 &&
8209 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8210 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008211 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008212 }
8213
8214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8215
8216 if (call_kind == LocationSummary::kNoCall) {
8217 if (Primitive::IsFloatingPointType(input_type)) {
8218 locations->SetInAt(0, Location::RequiresFpuRegister());
8219 } else {
8220 locations->SetInAt(0, Location::RequiresRegister());
8221 }
8222
8223 if (Primitive::IsFloatingPointType(result_type)) {
8224 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8225 } else {
8226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8227 }
8228 } else {
8229 InvokeRuntimeCallingConvention calling_convention;
8230
8231 if (Primitive::IsFloatingPointType(input_type)) {
8232 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8233 } else {
8234 DCHECK_EQ(input_type, Primitive::kPrimLong);
8235 locations->SetInAt(0, Location::RegisterPairLocation(
8236 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8237 }
8238
8239 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8240 }
8241}
8242
8243void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8244 LocationSummary* locations = conversion->GetLocations();
8245 Primitive::Type result_type = conversion->GetResultType();
8246 Primitive::Type input_type = conversion->GetInputType();
8247 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008248 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008249
8250 DCHECK_NE(input_type, result_type);
8251
8252 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8253 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8254 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8255 Register src = locations->InAt(0).AsRegister<Register>();
8256
Alexey Frunzea871ef12016-06-27 15:20:11 -07008257 if (dst_low != src) {
8258 __ Move(dst_low, src);
8259 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008260 __ Sra(dst_high, src, 31);
8261 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8262 Register dst = locations->Out().AsRegister<Register>();
8263 Register src = (input_type == Primitive::kPrimLong)
8264 ? locations->InAt(0).AsRegisterPairLow<Register>()
8265 : locations->InAt(0).AsRegister<Register>();
8266
8267 switch (result_type) {
8268 case Primitive::kPrimChar:
8269 __ Andi(dst, src, 0xFFFF);
8270 break;
8271 case Primitive::kPrimByte:
8272 if (has_sign_extension) {
8273 __ Seb(dst, src);
8274 } else {
8275 __ Sll(dst, src, 24);
8276 __ Sra(dst, dst, 24);
8277 }
8278 break;
8279 case Primitive::kPrimShort:
8280 if (has_sign_extension) {
8281 __ Seh(dst, src);
8282 } else {
8283 __ Sll(dst, src, 16);
8284 __ Sra(dst, dst, 16);
8285 }
8286 break;
8287 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008288 if (dst != src) {
8289 __ Move(dst, src);
8290 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008291 break;
8292
8293 default:
8294 LOG(FATAL) << "Unexpected type conversion from " << input_type
8295 << " to " << result_type;
8296 }
8297 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008298 if (input_type == Primitive::kPrimLong) {
8299 if (isR6) {
8300 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8301 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8302 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8303 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8304 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8305 __ Mtc1(src_low, FTMP);
8306 __ Mthc1(src_high, FTMP);
8307 if (result_type == Primitive::kPrimFloat) {
8308 __ Cvtsl(dst, FTMP);
8309 } else {
8310 __ Cvtdl(dst, FTMP);
8311 }
8312 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008313 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8314 : kQuickL2d;
8315 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008316 if (result_type == Primitive::kPrimFloat) {
8317 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8318 } else {
8319 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8320 }
8321 }
8322 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008323 Register src = locations->InAt(0).AsRegister<Register>();
8324 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8325 __ Mtc1(src, FTMP);
8326 if (result_type == Primitive::kPrimFloat) {
8327 __ Cvtsw(dst, FTMP);
8328 } else {
8329 __ Cvtdw(dst, FTMP);
8330 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 }
8332 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8333 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008334
8335 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8336 // value of the output type if the input is outside of the range after the truncation or
8337 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8338 // results. This matches the desired float/double-to-int/long conversion exactly.
8339 //
8340 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8341 // value when the input is either a NaN or is outside of the range of the output type
8342 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8343 // the same result.
8344 //
8345 // The code takes care of the different behaviors by first comparing the input to the
8346 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8347 // If the input is greater than or equal to the minimum, it procedes to the truncate
8348 // instruction, which will handle such an input the same way irrespective of NAN2008.
8349 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8350 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008351 if (result_type == Primitive::kPrimLong) {
8352 if (isR6) {
8353 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8354 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8355 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8356 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8357 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008358
8359 if (input_type == Primitive::kPrimFloat) {
8360 __ TruncLS(FTMP, src);
8361 } else {
8362 __ TruncLD(FTMP, src);
8363 }
8364 __ Mfc1(dst_low, FTMP);
8365 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008367 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8368 : kQuickD2l;
8369 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008370 if (input_type == Primitive::kPrimFloat) {
8371 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8372 } else {
8373 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8374 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008375 }
8376 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008377 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8378 Register dst = locations->Out().AsRegister<Register>();
8379 MipsLabel truncate;
8380 MipsLabel done;
8381
Lena Djokicf4e23a82017-05-09 15:43:45 +02008382 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008383 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008384 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8385 __ LoadConst32(TMP, min_val);
8386 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008387 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008388 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8389 __ LoadConst32(TMP, High32Bits(min_val));
8390 __ Mtc1(ZERO, FTMP);
8391 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008392 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008393
8394 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008395 __ ColeS(0, FTMP, src);
8396 } else {
8397 __ ColeD(0, FTMP, src);
8398 }
8399 __ Bc1t(0, &truncate);
8400
8401 if (input_type == Primitive::kPrimFloat) {
8402 __ CeqS(0, src, src);
8403 } else {
8404 __ CeqD(0, src, src);
8405 }
8406 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8407 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008408
8409 __ B(&done);
8410
8411 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008412 }
8413
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008414 if (input_type == Primitive::kPrimFloat) {
8415 __ TruncWS(FTMP, src);
8416 } else {
8417 __ TruncWD(FTMP, src);
8418 }
8419 __ Mfc1(dst, FTMP);
8420
Lena Djokicf4e23a82017-05-09 15:43:45 +02008421 if (!isR6) {
8422 __ Bind(&done);
8423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008424 }
8425 } else if (Primitive::IsFloatingPointType(result_type) &&
8426 Primitive::IsFloatingPointType(input_type)) {
8427 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8428 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8429 if (result_type == Primitive::kPrimFloat) {
8430 __ Cvtsd(dst, src);
8431 } else {
8432 __ Cvtds(dst, src);
8433 }
8434 } else {
8435 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8436 << " to " << result_type;
8437 }
8438}
8439
8440void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8441 HandleShift(ushr);
8442}
8443
8444void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8445 HandleShift(ushr);
8446}
8447
8448void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8449 HandleBinaryOp(instruction);
8450}
8451
8452void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8453 HandleBinaryOp(instruction);
8454}
8455
8456void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8457 // Nothing to do, this should be removed during prepare for register allocator.
8458 LOG(FATAL) << "Unreachable";
8459}
8460
8461void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8462 // Nothing to do, this should be removed during prepare for register allocator.
8463 LOG(FATAL) << "Unreachable";
8464}
8465
8466void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008467 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008468}
8469
8470void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008471 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008472}
8473
8474void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008475 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008476}
8477
8478void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008479 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008480}
8481
8482void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008483 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008484}
8485
8486void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008487 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008488}
8489
8490void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008491 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008492}
8493
8494void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008495 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496}
8497
8498void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008499 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008500}
8501
8502void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008503 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008504}
8505
8506void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008507 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508}
8509
8510void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008511 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008512}
8513
8514void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008515 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008516}
8517
8518void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008519 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520}
8521
8522void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008523 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008524}
8525
8526void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008527 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008528}
8529
8530void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008531 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008532}
8533
8534void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008535 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008536}
8537
8538void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008539 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008540}
8541
8542void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008543 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008544}
8545
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008546void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8547 LocationSummary* locations =
8548 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8549 locations->SetInAt(0, Location::RequiresRegister());
8550}
8551
Alexey Frunze96b66822016-09-10 02:32:44 -07008552void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8553 int32_t lower_bound,
8554 uint32_t num_entries,
8555 HBasicBlock* switch_block,
8556 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008557 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008558 Register temp_reg = TMP;
8559 __ Addiu32(temp_reg, value_reg, -lower_bound);
8560 // Jump to default if index is negative
8561 // Note: We don't check the case that index is positive while value < lower_bound, because in
8562 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8563 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8564
Alexey Frunze96b66822016-09-10 02:32:44 -07008565 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008566 // Jump to successors[0] if value == lower_bound.
8567 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8568 int32_t last_index = 0;
8569 for (; num_entries - last_index > 2; last_index += 2) {
8570 __ Addiu(temp_reg, temp_reg, -2);
8571 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8572 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8573 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8574 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8575 }
8576 if (num_entries - last_index == 2) {
8577 // The last missing case_value.
8578 __ Addiu(temp_reg, temp_reg, -1);
8579 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008580 }
8581
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008582 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008583 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008584 __ B(codegen_->GetLabelOf(default_block));
8585 }
8586}
8587
Alexey Frunze96b66822016-09-10 02:32:44 -07008588void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8589 Register constant_area,
8590 int32_t lower_bound,
8591 uint32_t num_entries,
8592 HBasicBlock* switch_block,
8593 HBasicBlock* default_block) {
8594 // Create a jump table.
8595 std::vector<MipsLabel*> labels(num_entries);
8596 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8597 for (uint32_t i = 0; i < num_entries; i++) {
8598 labels[i] = codegen_->GetLabelOf(successors[i]);
8599 }
8600 JumpTable* table = __ CreateJumpTable(std::move(labels));
8601
8602 // Is the value in range?
8603 __ Addiu32(TMP, value_reg, -lower_bound);
8604 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8605 __ Sltiu(AT, TMP, num_entries);
8606 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8607 } else {
8608 __ LoadConst32(AT, num_entries);
8609 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8610 }
8611
8612 // We are in the range of the table.
8613 // Load the target address from the jump table, indexing by the value.
8614 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008615 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008616 __ Lw(TMP, TMP, 0);
8617 // Compute the absolute target address by adding the table start address
8618 // (the table contains offsets to targets relative to its start).
8619 __ Addu(TMP, TMP, AT);
8620 // And jump.
8621 __ Jr(TMP);
8622 __ NopIfNoReordering();
8623}
8624
8625void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8626 int32_t lower_bound = switch_instr->GetStartValue();
8627 uint32_t num_entries = switch_instr->GetNumEntries();
8628 LocationSummary* locations = switch_instr->GetLocations();
8629 Register value_reg = locations->InAt(0).AsRegister<Register>();
8630 HBasicBlock* switch_block = switch_instr->GetBlock();
8631 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8632
8633 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8634 num_entries > kPackedSwitchJumpTableThreshold) {
8635 // R6 uses PC-relative addressing to access the jump table.
8636 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8637 // the jump table and it is implemented by changing HPackedSwitch to
8638 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8639 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8640 GenTableBasedPackedSwitch(value_reg,
8641 ZERO,
8642 lower_bound,
8643 num_entries,
8644 switch_block,
8645 default_block);
8646 } else {
8647 GenPackedSwitchWithCompares(value_reg,
8648 lower_bound,
8649 num_entries,
8650 switch_block,
8651 default_block);
8652 }
8653}
8654
8655void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8656 LocationSummary* locations =
8657 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8658 locations->SetInAt(0, Location::RequiresRegister());
8659 // Constant area pointer (HMipsComputeBaseMethodAddress).
8660 locations->SetInAt(1, Location::RequiresRegister());
8661}
8662
8663void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8664 int32_t lower_bound = switch_instr->GetStartValue();
8665 uint32_t num_entries = switch_instr->GetNumEntries();
8666 LocationSummary* locations = switch_instr->GetLocations();
8667 Register value_reg = locations->InAt(0).AsRegister<Register>();
8668 Register constant_area = locations->InAt(1).AsRegister<Register>();
8669 HBasicBlock* switch_block = switch_instr->GetBlock();
8670 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8671
8672 // This is an R2-only path. HPackedSwitch has been changed to
8673 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8674 // required to address the jump table relative to PC.
8675 GenTableBasedPackedSwitch(value_reg,
8676 constant_area,
8677 lower_bound,
8678 num_entries,
8679 switch_block,
8680 default_block);
8681}
8682
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008683void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8684 HMipsComputeBaseMethodAddress* insn) {
8685 LocationSummary* locations =
8686 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8687 locations->SetOut(Location::RequiresRegister());
8688}
8689
8690void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8691 HMipsComputeBaseMethodAddress* insn) {
8692 LocationSummary* locations = insn->GetLocations();
8693 Register reg = locations->Out().AsRegister<Register>();
8694
8695 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8696
8697 // Generate a dummy PC-relative call to obtain PC.
8698 __ Nal();
8699 // Grab the return address off RA.
8700 __ Move(reg, RA);
8701
8702 // Remember this offset (the obtained PC value) for later use with constant area.
8703 __ BindPcRelBaseLabel();
8704}
8705
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008706void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8707 // The trampoline uses the same calling convention as dex calling conventions,
8708 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8709 // the method_idx.
8710 HandleInvoke(invoke);
8711}
8712
8713void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8714 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8715}
8716
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008717void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8718 LocationSummary* locations =
8719 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8720 locations->SetInAt(0, Location::RequiresRegister());
8721 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008722}
8723
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008724void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8725 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008726 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008727 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008728 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008729 __ LoadFromOffset(kLoadWord,
8730 locations->Out().AsRegister<Register>(),
8731 locations->InAt(0).AsRegister<Register>(),
8732 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008733 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008734 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008735 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008736 __ LoadFromOffset(kLoadWord,
8737 locations->Out().AsRegister<Register>(),
8738 locations->InAt(0).AsRegister<Register>(),
8739 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008740 __ LoadFromOffset(kLoadWord,
8741 locations->Out().AsRegister<Register>(),
8742 locations->Out().AsRegister<Register>(),
8743 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008744 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008745}
8746
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008747#undef __
8748#undef QUICK_ENTRY_POINT
8749
8750} // namespace mips
8751} // namespace art