blob: 51f5b969d55db84e7685c99776d6221bd553b577 [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
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070023#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010024#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020025#include "entrypoints/quick/quick_entrypoints.h"
26#include "entrypoints/quick/quick_entrypoints_enum.h"
27#include "gc/accounting/card_table.h"
28#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070029#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "mirror/array-inl.h"
31#include "mirror/class-inl.h"
32#include "offsets.h"
33#include "thread.h"
34#include "utils/assembler.h"
35#include "utils/mips/assembler_mips.h"
36#include "utils/stack_checks.h"
37
38namespace art {
39namespace mips {
40
41static constexpr int kCurrentMethodStackOffset = 0;
42static constexpr Register kMethodRegisterArgument = A0;
43
Alexey Frunze4147fcc2017-06-17 19:57:27 -070044// Flags controlling the use of thunks for Baker read barriers.
45constexpr bool kBakerReadBarrierThunksEnableForFields = true;
46constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
47constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
48
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020049Location MipsReturnLocation(Primitive::Type return_type) {
50 switch (return_type) {
51 case Primitive::kPrimBoolean:
52 case Primitive::kPrimByte:
53 case Primitive::kPrimChar:
54 case Primitive::kPrimShort:
55 case Primitive::kPrimInt:
56 case Primitive::kPrimNot:
57 return Location::RegisterLocation(V0);
58
59 case Primitive::kPrimLong:
60 return Location::RegisterPairLocation(V0, V1);
61
62 case Primitive::kPrimFloat:
63 case Primitive::kPrimDouble:
64 return Location::FpuRegisterLocation(F0);
65
66 case Primitive::kPrimVoid:
67 return Location();
68 }
69 UNREACHABLE();
70}
71
72Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
73 return MipsReturnLocation(type);
74}
75
76Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
77 return Location::RegisterLocation(kMethodRegisterArgument);
78}
79
80Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
81 Location next_location;
82
83 switch (type) {
84 case Primitive::kPrimBoolean:
85 case Primitive::kPrimByte:
86 case Primitive::kPrimChar:
87 case Primitive::kPrimShort:
88 case Primitive::kPrimInt:
89 case Primitive::kPrimNot: {
90 uint32_t gp_index = gp_index_++;
91 if (gp_index < calling_convention.GetNumberOfRegisters()) {
92 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
93 } else {
94 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
95 next_location = Location::StackSlot(stack_offset);
96 }
97 break;
98 }
99
100 case Primitive::kPrimLong: {
101 uint32_t gp_index = gp_index_;
102 gp_index_ += 2;
103 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800104 Register reg = calling_convention.GetRegisterAt(gp_index);
105 if (reg == A1 || reg == A3) {
106 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200107 gp_index++;
108 }
109 Register low_even = calling_convention.GetRegisterAt(gp_index);
110 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
111 DCHECK_EQ(low_even + 1, high_odd);
112 next_location = Location::RegisterPairLocation(low_even, high_odd);
113 } else {
114 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
115 next_location = Location::DoubleStackSlot(stack_offset);
116 }
117 break;
118 }
119
120 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
121 // will take up the even/odd pair, while floats are stored in even regs only.
122 // On 64 bit FPU, both double and float are stored in even registers only.
123 case Primitive::kPrimFloat:
124 case Primitive::kPrimDouble: {
125 uint32_t float_index = float_index_++;
126 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
127 next_location = Location::FpuRegisterLocation(
128 calling_convention.GetFpuRegisterAt(float_index));
129 } else {
130 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
131 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
132 : Location::StackSlot(stack_offset);
133 }
134 break;
135 }
136
137 case Primitive::kPrimVoid:
138 LOG(FATAL) << "Unexpected parameter type " << type;
139 break;
140 }
141
142 // Space on the stack is reserved for all arguments.
143 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
144
145 return next_location;
146}
147
148Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
149 return MipsReturnLocation(type);
150}
151
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100152// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
153#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700154#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
157 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000158 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200159
160 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
161 LocationSummary* locations = instruction_->GetLocations();
162 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
163 __ Bind(GetEntryLabel());
164 if (instruction_->CanThrowIntoCatchBlock()) {
165 // Live registers will be restored in the catch block if caught.
166 SaveLiveRegisters(codegen, instruction_->GetLocations());
167 }
168 // We're moving two locations to locations that could overlap, so we need a parallel
169 // move resolver.
170 InvokeRuntimeCallingConvention calling_convention;
171 codegen->EmitParallelMoves(locations->InAt(0),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
173 Primitive::kPrimInt,
174 locations->InAt(1),
175 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
176 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100177 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
178 ? kQuickThrowStringBounds
179 : kQuickThrowArrayBounds;
180 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183 }
184
185 bool IsFatal() const OVERRIDE { return true; }
186
187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
188
189 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
191};
192
193class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
194 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196
197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
198 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
199 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100200 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
202 }
203
204 bool IsFatal() const OVERRIDE { return true; }
205
206 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
207
208 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200209 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
210};
211
212class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
213 public:
214 LoadClassSlowPathMIPS(HLoadClass* cls,
215 HInstruction* at,
216 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700217 bool do_clinit,
218 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
219 : SlowPathCodeMIPS(at),
220 cls_(cls),
221 dex_pc_(dex_pc),
222 do_clinit_(do_clinit),
223 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200224 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
225 }
226
227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000228 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700229 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700231 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700232 InvokeRuntimeCallingConvention calling_convention;
233 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
234 const bool is_load_class_bss_entry =
235 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 __ Bind(GetEntryLabel());
237 SaveLiveRegisters(codegen, locations);
238
Alexey Frunzec61c0762017-04-10 13:54:23 -0700239 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
240 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700241 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700242 Register temp = locations->GetTemp(0).AsRegister<Register>();
243 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
244 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
245 // kSaveEverything call.
246 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
247 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
248 if (temp_is_a0) {
249 __ Move(entry_address, temp);
250 }
251 }
252
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000253 dex::TypeIndex type_index = cls_->GetTypeIndex();
254 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100255 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
256 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000257 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200258 if (do_clinit_) {
259 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
260 } else {
261 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
262 }
263
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700265 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700266 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700267 DCHECK(bss_info_high_);
268 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
269 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700270 __ Sw(calling_convention.GetRegisterAt(0),
271 entry_address,
272 /* placeholder */ 0x5678,
273 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700274 }
275
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200276 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200277 if (out.IsValid()) {
278 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700280 mips_codegen->MoveLocation(out,
281 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
282 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200284 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700285
286 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700287 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
288 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700289 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700290 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000291 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700292 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000293 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700294 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
295 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700296 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
297 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000298 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200299 __ B(GetExitLabel());
300 }
301
302 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
303
304 private:
305 // The class this slow path will load.
306 HLoadClass* const cls_;
307
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200308 // The dex PC of `at_`.
309 const uint32_t dex_pc_;
310
311 // Whether to initialize the class.
312 const bool do_clinit_;
313
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700314 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
315 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
316
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
318};
319
320class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
321 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700322 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
323 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
324 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200325
326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700327 DCHECK(instruction_->IsLoadString());
328 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 LocationSummary* locations = instruction_->GetLocations();
330 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700331 HLoadString* load = instruction_->AsLoadString();
332 const dex::StringIndex string_index = load->GetStringIndex();
333 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700335 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700336 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 __ Bind(GetEntryLabel());
338 SaveLiveRegisters(codegen, locations);
339
Alexey Frunzec61c0762017-04-10 13:54:23 -0700340 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
341 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700342 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700343 Register temp = locations->GetTemp(0).AsRegister<Register>();
344 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
345 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
346 // kSaveEverything call.
347 entry_address = temp_is_a0 ? out : temp;
348 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
349 if (temp_is_a0) {
350 __ Move(entry_address, temp);
351 }
352 }
353
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100355 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200356 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700357
358 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700359 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700360 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700361 DCHECK(bss_info_high_);
362 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
363 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700364 __ Sw(calling_convention.GetRegisterAt(0),
365 entry_address,
366 /* placeholder */ 0x5678,
367 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700368 }
369
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200370 Primitive::Type type = instruction_->GetType();
371 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700372 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200373 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200374 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000375
Alexey Frunzec61c0762017-04-10 13:54:23 -0700376 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700377 if (!baker_or_no_read_barriers) {
378 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700379 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700380 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700381 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700382 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunzec61c0762017-04-10 13:54:23 -0700383 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700384 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
385 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700386 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
387 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700388 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389 __ B(GetExitLabel());
390 }
391
392 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
393
394 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700395 // Pointer to the high half PC-relative patch info.
396 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
397
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
399};
400
401class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
402 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
407 __ Bind(GetEntryLabel());
408 if (instruction_->CanThrowIntoCatchBlock()) {
409 // Live registers will be restored in the catch block if caught.
410 SaveLiveRegisters(codegen, instruction_->GetLocations());
411 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100412 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 instruction_,
414 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100415 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
417 }
418
419 bool IsFatal() const OVERRIDE { return true; }
420
421 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
422
423 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200424 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
425};
426
427class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
428 public:
429 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000430 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200431
432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200433 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
435 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200436 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100437 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200439 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 if (successor_ == nullptr) {
441 __ B(GetReturnLabel());
442 } else {
443 __ B(mips_codegen->GetLabelOf(successor_));
444 }
445 }
446
447 MipsLabel* GetReturnLabel() {
448 DCHECK(successor_ == nullptr);
449 return &return_label_;
450 }
451
452 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
453
454 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 // If not null, the block to branch to after the suspend check.
456 HBasicBlock* const successor_;
457
458 // If `successor_` is null, the label to branch to after the suspend check.
459 MipsLabel return_label_;
460
461 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
462};
463
464class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
465 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800466 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
467 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200468
469 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
470 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200471 uint32_t dex_pc = instruction_->GetDexPc();
472 DCHECK(instruction_->IsCheckCast()
473 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
474 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
475
476 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800477 if (!is_fatal_) {
478 SaveLiveRegisters(codegen, locations);
479 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200480
481 // We're moving two locations to locations that could overlap, so we need a parallel
482 // move resolver.
483 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800484 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
486 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800487 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200488 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
489 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100491 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800492 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200493 Primitive::Type ret_type = instruction_->GetType();
494 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
495 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200496 } else {
497 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800498 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
499 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200500 }
501
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800502 if (!is_fatal_) {
503 RestoreLiveRegisters(codegen, locations);
504 __ B(GetExitLabel());
505 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200506 }
507
508 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
509
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800510 bool IsFatal() const OVERRIDE { return is_fatal_; }
511
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200512 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800513 const bool is_fatal_;
514
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200515 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
516};
517
518class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
519 public:
Aart Bik42249c32016-01-07 15:33:50 -0800520 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800524 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200525 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100526 LocationSummary* locations = instruction_->GetLocations();
527 SaveLiveRegisters(codegen, locations);
528 InvokeRuntimeCallingConvention calling_convention;
529 __ LoadConst32(calling_convention.GetRegisterAt(0),
530 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100531 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100532 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200533 }
534
535 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
536
537 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200538 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
539};
540
Alexey Frunze15958152017-02-09 19:08:30 -0800541class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
542 public:
543 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
544
545 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
546 LocationSummary* locations = instruction_->GetLocations();
547 __ Bind(GetEntryLabel());
548 SaveLiveRegisters(codegen, locations);
549
550 InvokeRuntimeCallingConvention calling_convention;
551 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
552 parallel_move.AddMove(
553 locations->InAt(0),
554 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
555 Primitive::kPrimNot,
556 nullptr);
557 parallel_move.AddMove(
558 locations->InAt(1),
559 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
560 Primitive::kPrimInt,
561 nullptr);
562 parallel_move.AddMove(
563 locations->InAt(2),
564 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
565 Primitive::kPrimNot,
566 nullptr);
567 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
568
569 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
570 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
571 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
572 RestoreLiveRegisters(codegen, locations);
573 __ B(GetExitLabel());
574 }
575
576 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
577
578 private:
579 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
580};
581
582// Slow path marking an object reference `ref` during a read
583// barrier. The field `obj.field` in the object `obj` holding this
584// reference does not get updated by this slow path after marking (see
585// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
586//
587// This means that after the execution of this slow path, `ref` will
588// always be up-to-date, but `obj.field` may not; i.e., after the
589// flip, `ref` will be a to-space reference, but `obj.field` will
590// probably still be a from-space reference (unless it gets updated by
591// another thread, or if another thread installed another object
592// reference (different from `ref`) in `obj.field`).
593//
594// If `entrypoint` is a valid location it is assumed to already be
595// holding the entrypoint. The case where the entrypoint is passed in
596// is for the GcRoot read barrier.
597class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
598 public:
599 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
600 Location ref,
601 Location entrypoint = Location::NoLocation())
602 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
603 DCHECK(kEmitCompilerReadBarrier);
604 }
605
606 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
607
608 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
609 LocationSummary* locations = instruction_->GetLocations();
610 Register ref_reg = ref_.AsRegister<Register>();
611 DCHECK(locations->CanCall());
612 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
613 DCHECK(instruction_->IsInstanceFieldGet() ||
614 instruction_->IsStaticFieldGet() ||
615 instruction_->IsArrayGet() ||
616 instruction_->IsArraySet() ||
617 instruction_->IsLoadClass() ||
618 instruction_->IsLoadString() ||
619 instruction_->IsInstanceOf() ||
620 instruction_->IsCheckCast() ||
621 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
622 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
623 << "Unexpected instruction in read barrier marking slow path: "
624 << instruction_->DebugName();
625
626 __ Bind(GetEntryLabel());
627 // No need to save live registers; it's taken care of by the
628 // entrypoint. Also, there is no need to update the stack mask,
629 // as this runtime call will not trigger a garbage collection.
630 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
631 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
632 (S2 <= ref_reg && ref_reg <= S7) ||
633 (ref_reg == FP)) << ref_reg;
634 // "Compact" slow path, saving two moves.
635 //
636 // Instead of using the standard runtime calling convention (input
637 // and output in A0 and V0 respectively):
638 //
639 // A0 <- ref
640 // V0 <- ReadBarrierMark(A0)
641 // ref <- V0
642 //
643 // we just use rX (the register containing `ref`) as input and output
644 // of a dedicated entrypoint:
645 //
646 // rX <- ReadBarrierMarkRegX(rX)
647 //
648 if (entrypoint_.IsValid()) {
649 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
650 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
651 __ Jalr(entrypoint_.AsRegister<Register>());
652 __ NopIfNoReordering();
653 } else {
654 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100655 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800656 // This runtime call does not require a stack map.
657 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
658 instruction_,
659 this,
660 /* direct */ false);
661 }
662 __ B(GetExitLabel());
663 }
664
665 private:
666 // The location (register) of the marked object reference.
667 const Location ref_;
668
669 // The location of the entrypoint if already loaded.
670 const Location entrypoint_;
671
672 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
673};
674
675// Slow path marking an object reference `ref` during a read barrier,
676// and if needed, atomically updating the field `obj.field` in the
677// object `obj` holding this reference after marking (contrary to
678// ReadBarrierMarkSlowPathMIPS above, which never tries to update
679// `obj.field`).
680//
681// This means that after the execution of this slow path, both `ref`
682// and `obj.field` will be up-to-date; i.e., after the flip, both will
683// hold the same to-space reference (unless another thread installed
684// another object reference (different from `ref`) in `obj.field`).
685class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
686 public:
687 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
688 Location ref,
689 Register obj,
690 Location field_offset,
691 Register temp1)
692 : SlowPathCodeMIPS(instruction),
693 ref_(ref),
694 obj_(obj),
695 field_offset_(field_offset),
696 temp1_(temp1) {
697 DCHECK(kEmitCompilerReadBarrier);
698 }
699
700 const char* GetDescription() const OVERRIDE {
701 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
702 }
703
704 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
705 LocationSummary* locations = instruction_->GetLocations();
706 Register ref_reg = ref_.AsRegister<Register>();
707 DCHECK(locations->CanCall());
708 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
709 // This slow path is only used by the UnsafeCASObject intrinsic.
710 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
711 << "Unexpected instruction in read barrier marking and field updating slow path: "
712 << instruction_->DebugName();
713 DCHECK(instruction_->GetLocations()->Intrinsified());
714 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
715 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
716
717 __ Bind(GetEntryLabel());
718
719 // Save the old reference.
720 // Note that we cannot use AT or TMP to save the old reference, as those
721 // are used by the code that follows, but we need the old reference after
722 // the call to the ReadBarrierMarkRegX entry point.
723 DCHECK_NE(temp1_, AT);
724 DCHECK_NE(temp1_, TMP);
725 __ Move(temp1_, ref_reg);
726
727 // No need to save live registers; it's taken care of by the
728 // entrypoint. Also, there is no need to update the stack mask,
729 // as this runtime call will not trigger a garbage collection.
730 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
731 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
732 (S2 <= ref_reg && ref_reg <= S7) ||
733 (ref_reg == FP)) << ref_reg;
734 // "Compact" slow path, saving two moves.
735 //
736 // Instead of using the standard runtime calling convention (input
737 // and output in A0 and V0 respectively):
738 //
739 // A0 <- ref
740 // V0 <- ReadBarrierMark(A0)
741 // ref <- V0
742 //
743 // we just use rX (the register containing `ref`) as input and output
744 // of a dedicated entrypoint:
745 //
746 // rX <- ReadBarrierMarkRegX(rX)
747 //
748 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100749 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800750 // This runtime call does not require a stack map.
751 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
752 instruction_,
753 this,
754 /* direct */ false);
755
756 // If the new reference is different from the old reference,
757 // update the field in the holder (`*(obj_ + field_offset_)`).
758 //
759 // Note that this field could also hold a different object, if
760 // another thread had concurrently changed it. In that case, the
761 // the compare-and-set (CAS) loop below would abort, leaving the
762 // field as-is.
763 MipsLabel done;
764 __ Beq(temp1_, ref_reg, &done);
765
766 // Update the the holder's field atomically. This may fail if
767 // mutator updates before us, but it's OK. This is achieved
768 // using a strong compare-and-set (CAS) operation with relaxed
769 // memory synchronization ordering, where the expected value is
770 // the old reference and the desired value is the new reference.
771
772 // Convenience aliases.
773 Register base = obj_;
774 // The UnsafeCASObject intrinsic uses a register pair as field
775 // offset ("long offset"), of which only the low part contains
776 // data.
777 Register offset = field_offset_.AsRegisterPairLow<Register>();
778 Register expected = temp1_;
779 Register value = ref_reg;
780 Register tmp_ptr = TMP; // Pointer to actual memory.
781 Register tmp = AT; // Value in memory.
782
783 __ Addu(tmp_ptr, base, offset);
784
785 if (kPoisonHeapReferences) {
786 __ PoisonHeapReference(expected);
787 // Do not poison `value` if it is the same register as
788 // `expected`, which has just been poisoned.
789 if (value != expected) {
790 __ PoisonHeapReference(value);
791 }
792 }
793
794 // do {
795 // tmp = [r_ptr] - expected;
796 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
797
798 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
799 MipsLabel loop_head, exit_loop;
800 __ Bind(&loop_head);
801 if (is_r6) {
802 __ LlR6(tmp, tmp_ptr);
803 } else {
804 __ LlR2(tmp, tmp_ptr);
805 }
806 __ Bne(tmp, expected, &exit_loop);
807 __ Move(tmp, value);
808 if (is_r6) {
809 __ ScR6(tmp, tmp_ptr);
810 } else {
811 __ ScR2(tmp, tmp_ptr);
812 }
813 __ Beqz(tmp, &loop_head);
814 __ Bind(&exit_loop);
815
816 if (kPoisonHeapReferences) {
817 __ UnpoisonHeapReference(expected);
818 // Do not unpoison `value` if it is the same register as
819 // `expected`, which has just been unpoisoned.
820 if (value != expected) {
821 __ UnpoisonHeapReference(value);
822 }
823 }
824
825 __ Bind(&done);
826 __ B(GetExitLabel());
827 }
828
829 private:
830 // The location (register) of the marked object reference.
831 const Location ref_;
832 // The register containing the object holding the marked object reference field.
833 const Register obj_;
834 // The location of the offset of the marked reference field within `obj_`.
835 Location field_offset_;
836
837 const Register temp1_;
838
839 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
840};
841
842// Slow path generating a read barrier for a heap reference.
843class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
844 public:
845 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
846 Location out,
847 Location ref,
848 Location obj,
849 uint32_t offset,
850 Location index)
851 : SlowPathCodeMIPS(instruction),
852 out_(out),
853 ref_(ref),
854 obj_(obj),
855 offset_(offset),
856 index_(index) {
857 DCHECK(kEmitCompilerReadBarrier);
858 // If `obj` is equal to `out` or `ref`, it means the initial object
859 // has been overwritten by (or after) the heap object reference load
860 // to be instrumented, e.g.:
861 //
862 // __ LoadFromOffset(kLoadWord, out, out, offset);
863 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
864 //
865 // In that case, we have lost the information about the original
866 // object, and the emitted read barrier cannot work properly.
867 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
868 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
869 }
870
871 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
872 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
873 LocationSummary* locations = instruction_->GetLocations();
874 Register reg_out = out_.AsRegister<Register>();
875 DCHECK(locations->CanCall());
876 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
877 DCHECK(instruction_->IsInstanceFieldGet() ||
878 instruction_->IsStaticFieldGet() ||
879 instruction_->IsArrayGet() ||
880 instruction_->IsInstanceOf() ||
881 instruction_->IsCheckCast() ||
882 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
883 << "Unexpected instruction in read barrier for heap reference slow path: "
884 << instruction_->DebugName();
885
886 __ Bind(GetEntryLabel());
887 SaveLiveRegisters(codegen, locations);
888
889 // We may have to change the index's value, but as `index_` is a
890 // constant member (like other "inputs" of this slow path),
891 // introduce a copy of it, `index`.
892 Location index = index_;
893 if (index_.IsValid()) {
894 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
895 if (instruction_->IsArrayGet()) {
896 // Compute the actual memory offset and store it in `index`.
897 Register index_reg = index_.AsRegister<Register>();
898 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
899 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
900 // We are about to change the value of `index_reg` (see the
901 // calls to art::mips::MipsAssembler::Sll and
902 // art::mips::MipsAssembler::Addiu32 below), but it has
903 // not been saved by the previous call to
904 // art::SlowPathCode::SaveLiveRegisters, as it is a
905 // callee-save register --
906 // art::SlowPathCode::SaveLiveRegisters does not consider
907 // callee-save registers, as it has been designed with the
908 // assumption that callee-save registers are supposed to be
909 // handled by the called function. So, as a callee-save
910 // register, `index_reg` _would_ eventually be saved onto
911 // the stack, but it would be too late: we would have
912 // changed its value earlier. Therefore, we manually save
913 // it here into another freely available register,
914 // `free_reg`, chosen of course among the caller-save
915 // registers (as a callee-save `free_reg` register would
916 // exhibit the same problem).
917 //
918 // Note we could have requested a temporary register from
919 // the register allocator instead; but we prefer not to, as
920 // this is a slow path, and we know we can find a
921 // caller-save register that is available.
922 Register free_reg = FindAvailableCallerSaveRegister(codegen);
923 __ Move(free_reg, index_reg);
924 index_reg = free_reg;
925 index = Location::RegisterLocation(index_reg);
926 } else {
927 // The initial register stored in `index_` has already been
928 // saved in the call to art::SlowPathCode::SaveLiveRegisters
929 // (as it is not a callee-save register), so we can freely
930 // use it.
931 }
932 // Shifting the index value contained in `index_reg` by the scale
933 // factor (2) cannot overflow in practice, as the runtime is
934 // unable to allocate object arrays with a size larger than
935 // 2^26 - 1 (that is, 2^28 - 4 bytes).
936 __ Sll(index_reg, index_reg, TIMES_4);
937 static_assert(
938 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
939 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
940 __ Addiu32(index_reg, index_reg, offset_);
941 } else {
942 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
943 // intrinsics, `index_` is not shifted by a scale factor of 2
944 // (as in the case of ArrayGet), as it is actually an offset
945 // to an object field within an object.
946 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
947 DCHECK(instruction_->GetLocations()->Intrinsified());
948 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
949 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
950 << instruction_->AsInvoke()->GetIntrinsic();
951 DCHECK_EQ(offset_, 0U);
952 DCHECK(index_.IsRegisterPair());
953 // UnsafeGet's offset location is a register pair, the low
954 // part contains the correct offset.
955 index = index_.ToLow();
956 }
957 }
958
959 // We're moving two or three locations to locations that could
960 // overlap, so we need a parallel move resolver.
961 InvokeRuntimeCallingConvention calling_convention;
962 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
963 parallel_move.AddMove(ref_,
964 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
965 Primitive::kPrimNot,
966 nullptr);
967 parallel_move.AddMove(obj_,
968 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
969 Primitive::kPrimNot,
970 nullptr);
971 if (index.IsValid()) {
972 parallel_move.AddMove(index,
973 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
974 Primitive::kPrimInt,
975 nullptr);
976 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
977 } else {
978 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
979 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
980 }
981 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
982 instruction_,
983 instruction_->GetDexPc(),
984 this);
985 CheckEntrypointTypes<
986 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200987 mips_codegen->MoveLocation(out_,
988 calling_convention.GetReturnLocation(Primitive::kPrimNot),
989 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -0800990
991 RestoreLiveRegisters(codegen, locations);
992 __ B(GetExitLabel());
993 }
994
995 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
996
997 private:
998 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
999 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1000 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1001 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1002 if (i != ref &&
1003 i != obj &&
1004 !codegen->IsCoreCalleeSaveRegister(i) &&
1005 !codegen->IsBlockedCoreRegister(i)) {
1006 return static_cast<Register>(i);
1007 }
1008 }
1009 // We shall never fail to find a free caller-save register, as
1010 // there are more than two core caller-save registers on MIPS
1011 // (meaning it is possible to find one which is different from
1012 // `ref` and `obj`).
1013 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1014 LOG(FATAL) << "Could not find a free caller-save register";
1015 UNREACHABLE();
1016 }
1017
1018 const Location out_;
1019 const Location ref_;
1020 const Location obj_;
1021 const uint32_t offset_;
1022 // An additional location containing an index to an array.
1023 // Only used for HArrayGet and the UnsafeGetObject &
1024 // UnsafeGetObjectVolatile intrinsics.
1025 const Location index_;
1026
1027 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1028};
1029
1030// Slow path generating a read barrier for a GC root.
1031class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1032 public:
1033 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1034 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1035 DCHECK(kEmitCompilerReadBarrier);
1036 }
1037
1038 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1039 LocationSummary* locations = instruction_->GetLocations();
1040 Register reg_out = out_.AsRegister<Register>();
1041 DCHECK(locations->CanCall());
1042 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1043 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1044 << "Unexpected instruction in read barrier for GC root slow path: "
1045 << instruction_->DebugName();
1046
1047 __ Bind(GetEntryLabel());
1048 SaveLiveRegisters(codegen, locations);
1049
1050 InvokeRuntimeCallingConvention calling_convention;
1051 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001052 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1053 root_,
1054 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001055 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1056 instruction_,
1057 instruction_->GetDexPc(),
1058 this);
1059 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001060 mips_codegen->MoveLocation(out_,
1061 calling_convention.GetReturnLocation(Primitive::kPrimNot),
1062 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001063
1064 RestoreLiveRegisters(codegen, locations);
1065 __ B(GetExitLabel());
1066 }
1067
1068 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1069
1070 private:
1071 const Location out_;
1072 const Location root_;
1073
1074 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1075};
1076
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001077CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1078 const MipsInstructionSetFeatures& isa_features,
1079 const CompilerOptions& compiler_options,
1080 OptimizingCompilerStats* stats)
1081 : CodeGenerator(graph,
1082 kNumberOfCoreRegisters,
1083 kNumberOfFRegisters,
1084 kNumberOfRegisterPairs,
1085 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1086 arraysize(kCoreCalleeSaves)),
1087 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1088 arraysize(kFpuCalleeSaves)),
1089 compiler_options,
1090 stats),
1091 block_labels_(nullptr),
1092 location_builder_(graph, this),
1093 instruction_visitor_(graph, this),
1094 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001095 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001096 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001097 uint32_literals_(std::less<uint32_t>(),
1098 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001099 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001100 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001101 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001102 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001103 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001104 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1105 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001106 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001107 // Save RA (containing the return address) to mimic Quick.
1108 AddAllocatedRegister(Location::RegisterLocation(RA));
1109}
1110
1111#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001112// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1113#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001114#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115
1116void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1117 // Ensure that we fix up branches.
1118 __ FinalizeCode();
1119
1120 // Adjust native pc offsets in stack maps.
1121 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001122 uint32_t old_position =
1123 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 uint32_t new_position = __ GetAdjustedPosition(old_position);
1125 DCHECK_GE(new_position, old_position);
1126 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1127 }
1128
1129 // Adjust pc offsets for the disassembly information.
1130 if (disasm_info_ != nullptr) {
1131 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1132 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1133 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1134 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1135 it.second.start = __ GetAdjustedPosition(it.second.start);
1136 it.second.end = __ GetAdjustedPosition(it.second.end);
1137 }
1138 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1139 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1140 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1141 }
1142 }
1143
1144 CodeGenerator::Finalize(allocator);
1145}
1146
1147MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1148 return codegen_->GetAssembler();
1149}
1150
1151void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1152 DCHECK_LT(index, moves_.size());
1153 MoveOperands* move = moves_[index];
1154 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1155}
1156
1157void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1158 DCHECK_LT(index, moves_.size());
1159 MoveOperands* move = moves_[index];
1160 Primitive::Type type = move->GetType();
1161 Location loc1 = move->GetDestination();
1162 Location loc2 = move->GetSource();
1163
1164 DCHECK(!loc1.IsConstant());
1165 DCHECK(!loc2.IsConstant());
1166
1167 if (loc1.Equals(loc2)) {
1168 return;
1169 }
1170
1171 if (loc1.IsRegister() && loc2.IsRegister()) {
1172 // Swap 2 GPRs.
1173 Register r1 = loc1.AsRegister<Register>();
1174 Register r2 = loc2.AsRegister<Register>();
1175 __ Move(TMP, r2);
1176 __ Move(r2, r1);
1177 __ Move(r1, TMP);
1178 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1179 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1180 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1181 if (type == Primitive::kPrimFloat) {
1182 __ MovS(FTMP, f2);
1183 __ MovS(f2, f1);
1184 __ MovS(f1, FTMP);
1185 } else {
1186 DCHECK_EQ(type, Primitive::kPrimDouble);
1187 __ MovD(FTMP, f2);
1188 __ MovD(f2, f1);
1189 __ MovD(f1, FTMP);
1190 }
1191 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1192 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1193 // Swap FPR and GPR.
1194 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1195 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1196 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001197 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001198 __ Move(TMP, r2);
1199 __ Mfc1(r2, f1);
1200 __ Mtc1(TMP, f1);
1201 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1202 // Swap 2 GPR register pairs.
1203 Register r1 = loc1.AsRegisterPairLow<Register>();
1204 Register r2 = loc2.AsRegisterPairLow<Register>();
1205 __ Move(TMP, r2);
1206 __ Move(r2, r1);
1207 __ Move(r1, TMP);
1208 r1 = loc1.AsRegisterPairHigh<Register>();
1209 r2 = loc2.AsRegisterPairHigh<Register>();
1210 __ Move(TMP, r2);
1211 __ Move(r2, r1);
1212 __ Move(r1, TMP);
1213 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1214 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1215 // Swap FPR and GPR register pair.
1216 DCHECK_EQ(type, Primitive::kPrimDouble);
1217 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1218 : loc2.AsFpuRegister<FRegister>();
1219 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1220 : loc2.AsRegisterPairLow<Register>();
1221 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1222 : loc2.AsRegisterPairHigh<Register>();
1223 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1224 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1225 // unpredictable and the following mfch1 will fail.
1226 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001227 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001228 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001229 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230 __ Move(r2_l, TMP);
1231 __ Move(r2_h, AT);
1232 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1233 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1234 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1235 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001236 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1237 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001238 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1239 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001240 __ Move(TMP, reg);
1241 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1242 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1243 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1244 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1245 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1246 : loc2.AsRegisterPairLow<Register>();
1247 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1248 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001249 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001250 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1251 : loc2.GetHighStackIndex(kMipsWordSize);
1252 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001253 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001254 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001255 __ Move(TMP, reg_h);
1256 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1257 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001258 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1259 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1260 : loc2.AsFpuRegister<FRegister>();
1261 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1262 if (type == Primitive::kPrimFloat) {
1263 __ MovS(FTMP, reg);
1264 __ LoadSFromOffset(reg, SP, offset);
1265 __ StoreSToOffset(FTMP, SP, offset);
1266 } else {
1267 DCHECK_EQ(type, Primitive::kPrimDouble);
1268 __ MovD(FTMP, reg);
1269 __ LoadDFromOffset(reg, SP, offset);
1270 __ StoreDToOffset(FTMP, SP, offset);
1271 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001272 } else {
1273 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1274 }
1275}
1276
1277void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1278 __ Pop(static_cast<Register>(reg));
1279}
1280
1281void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1282 __ Push(static_cast<Register>(reg));
1283}
1284
1285void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1286 // Allocate a scratch register other than TMP, if available.
1287 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1288 // automatically unspilled when the scratch scope object is destroyed).
1289 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1290 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1291 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1292 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1293 __ LoadFromOffset(kLoadWord,
1294 Register(ensure_scratch.GetRegister()),
1295 SP,
1296 index1 + stack_offset);
1297 __ LoadFromOffset(kLoadWord,
1298 TMP,
1299 SP,
1300 index2 + stack_offset);
1301 __ StoreToOffset(kStoreWord,
1302 Register(ensure_scratch.GetRegister()),
1303 SP,
1304 index2 + stack_offset);
1305 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1306 }
1307}
1308
Alexey Frunze73296a72016-06-03 22:51:46 -07001309void CodeGeneratorMIPS::ComputeSpillMask() {
1310 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1311 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1312 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1313 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1314 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1315 // within the stack frame.
1316 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1317 core_spill_mask_ |= (1 << ZERO);
1318 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001319}
1320
1321bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001322 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001323 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1324 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1325 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001326 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001327}
1328
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329static dwarf::Reg DWARFReg(Register reg) {
1330 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1331}
1332
1333// TODO: mapping of floating-point registers to DWARF.
1334
1335void CodeGeneratorMIPS::GenerateFrameEntry() {
1336 __ Bind(&frame_entry_label_);
1337
1338 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1339
1340 if (do_overflow_check) {
1341 __ LoadFromOffset(kLoadWord,
1342 ZERO,
1343 SP,
1344 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1345 RecordPcInfo(nullptr, 0);
1346 }
1347
1348 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001349 CHECK_EQ(fpu_spill_mask_, 0u);
1350 CHECK_EQ(core_spill_mask_, 1u << RA);
1351 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001352 return;
1353 }
1354
1355 // Make sure the frame size isn't unreasonably large.
1356 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1357 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1358 }
1359
1360 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001361
Alexey Frunze73296a72016-06-03 22:51:46 -07001362 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001363 __ IncreaseFrameSize(ofs);
1364
Alexey Frunze73296a72016-06-03 22:51:46 -07001365 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1366 Register reg = static_cast<Register>(MostSignificantBit(mask));
1367 mask ^= 1u << reg;
1368 ofs -= kMipsWordSize;
1369 // The ZERO register is only included for alignment.
1370 if (reg != ZERO) {
1371 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001372 __ cfi().RelOffset(DWARFReg(reg), ofs);
1373 }
1374 }
1375
Alexey Frunze73296a72016-06-03 22:51:46 -07001376 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1377 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1378 mask ^= 1u << reg;
1379 ofs -= kMipsDoublewordSize;
1380 __ StoreDToOffset(reg, SP, ofs);
1381 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001382 }
1383
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001384 // Save the current method if we need it. Note that we do not
1385 // do this in HCurrentMethod, as the instruction might have been removed
1386 // in the SSA graph.
1387 if (RequiresCurrentMethod()) {
1388 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1389 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001390
1391 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1392 // Initialize should deoptimize flag to 0.
1393 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1394 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395}
1396
1397void CodeGeneratorMIPS::GenerateFrameExit() {
1398 __ cfi().RememberState();
1399
1400 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001401 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001402
Alexey Frunze73296a72016-06-03 22:51:46 -07001403 // For better instruction scheduling restore RA before other registers.
1404 uint32_t ofs = GetFrameSize();
1405 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1406 Register reg = static_cast<Register>(MostSignificantBit(mask));
1407 mask ^= 1u << reg;
1408 ofs -= kMipsWordSize;
1409 // The ZERO register is only included for alignment.
1410 if (reg != ZERO) {
1411 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001412 __ cfi().Restore(DWARFReg(reg));
1413 }
1414 }
1415
Alexey Frunze73296a72016-06-03 22:51:46 -07001416 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1417 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1418 mask ^= 1u << reg;
1419 ofs -= kMipsDoublewordSize;
1420 __ LoadDFromOffset(reg, SP, ofs);
1421 // TODO: __ cfi().Restore(DWARFReg(reg));
1422 }
1423
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001424 size_t frame_size = GetFrameSize();
1425 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1426 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1427 bool reordering = __ SetReorder(false);
1428 if (exchange) {
1429 __ Jr(RA);
1430 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1431 } else {
1432 __ DecreaseFrameSize(frame_size);
1433 __ Jr(RA);
1434 __ Nop(); // In delay slot.
1435 }
1436 __ SetReorder(reordering);
1437 } else {
1438 __ Jr(RA);
1439 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001440 }
1441
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001442 __ cfi().RestoreState();
1443 __ cfi().DefCFAOffset(GetFrameSize());
1444}
1445
1446void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1447 __ Bind(GetLabelOf(block));
1448}
1449
Lena Djokicca8c2952017-05-29 11:31:46 +02001450VectorRegister VectorRegisterFrom(Location location) {
1451 DCHECK(location.IsFpuRegister());
1452 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1453}
1454
Lena Djokic8098da92017-06-28 12:07:50 +02001455void CodeGeneratorMIPS::MoveLocation(Location destination,
1456 Location source,
1457 Primitive::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001458 if (source.Equals(destination)) {
1459 return;
1460 }
1461
Lena Djokic8098da92017-06-28 12:07:50 +02001462 if (source.IsConstant()) {
1463 MoveConstant(destination, source.GetConstant());
1464 } else {
1465 if (destination.IsRegister()) {
1466 if (source.IsRegister()) {
1467 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1468 } else if (source.IsFpuRegister()) {
1469 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1470 } else {
1471 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001472 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001473 }
1474 } else if (destination.IsRegisterPair()) {
1475 if (source.IsRegisterPair()) {
1476 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1477 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1478 } else if (source.IsFpuRegister()) {
1479 Register dst_high = destination.AsRegisterPairHigh<Register>();
1480 Register dst_low = destination.AsRegisterPairLow<Register>();
1481 FRegister src = source.AsFpuRegister<FRegister>();
1482 __ Mfc1(dst_low, src);
1483 __ MoveFromFpuHigh(dst_high, src);
1484 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001485 DCHECK(source.IsDoubleStackSlot())
1486 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001487 int32_t off = source.GetStackIndex();
1488 Register r = destination.AsRegisterPairLow<Register>();
1489 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1490 }
1491 } else if (destination.IsFpuRegister()) {
1492 if (source.IsRegister()) {
1493 DCHECK(!Primitive::Is64BitType(dst_type));
1494 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1495 } else if (source.IsRegisterPair()) {
1496 DCHECK(Primitive::Is64BitType(dst_type));
1497 FRegister dst = destination.AsFpuRegister<FRegister>();
1498 Register src_high = source.AsRegisterPairHigh<Register>();
1499 Register src_low = source.AsRegisterPairLow<Register>();
1500 __ Mtc1(src_low, dst);
1501 __ MoveToFpuHigh(src_high, dst);
1502 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001503 if (GetGraph()->HasSIMD()) {
1504 __ MoveV(VectorRegisterFrom(destination),
1505 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001506 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001507 if (Primitive::Is64BitType(dst_type)) {
1508 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1509 } else {
1510 DCHECK_EQ(dst_type, Primitive::kPrimFloat);
1511 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1512 }
Lena Djokic8098da92017-06-28 12:07:50 +02001513 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001514 } else if (source.IsSIMDStackSlot()) {
1515 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001516 } else if (source.IsDoubleStackSlot()) {
1517 DCHECK(Primitive::Is64BitType(dst_type));
1518 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1519 } else {
1520 DCHECK(!Primitive::Is64BitType(dst_type));
1521 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1522 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1523 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001524 } else if (destination.IsSIMDStackSlot()) {
1525 if (source.IsFpuRegister()) {
1526 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1527 } else {
1528 DCHECK(source.IsSIMDStackSlot());
1529 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1530 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1531 }
Lena Djokic8098da92017-06-28 12:07:50 +02001532 } else if (destination.IsDoubleStackSlot()) {
1533 int32_t dst_offset = destination.GetStackIndex();
1534 if (source.IsRegisterPair()) {
1535 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1536 } else if (source.IsFpuRegister()) {
1537 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1538 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001539 DCHECK(source.IsDoubleStackSlot())
1540 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001541 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1542 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1543 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1544 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001546 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001547 DCHECK(destination.IsStackSlot()) << destination;
1548 int32_t dst_offset = destination.GetStackIndex();
1549 if (source.IsRegister()) {
1550 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1551 } else if (source.IsFpuRegister()) {
1552 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1553 } else {
1554 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1555 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1556 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1557 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001558 }
1559 }
1560}
1561
1562void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1563 if (c->IsIntConstant() || c->IsNullConstant()) {
1564 // Move 32 bit constant.
1565 int32_t value = GetInt32ValueOf(c);
1566 if (destination.IsRegister()) {
1567 Register dst = destination.AsRegister<Register>();
1568 __ LoadConst32(dst, value);
1569 } else {
1570 DCHECK(destination.IsStackSlot())
1571 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001572 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001573 }
1574 } else if (c->IsLongConstant()) {
1575 // Move 64 bit constant.
1576 int64_t value = GetInt64ValueOf(c);
1577 if (destination.IsRegisterPair()) {
1578 Register r_h = destination.AsRegisterPairHigh<Register>();
1579 Register r_l = destination.AsRegisterPairLow<Register>();
1580 __ LoadConst64(r_h, r_l, value);
1581 } else {
1582 DCHECK(destination.IsDoubleStackSlot())
1583 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001584 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001585 }
1586 } else if (c->IsFloatConstant()) {
1587 // Move 32 bit float constant.
1588 int32_t value = GetInt32ValueOf(c);
1589 if (destination.IsFpuRegister()) {
1590 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1591 } else {
1592 DCHECK(destination.IsStackSlot())
1593 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001594 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 }
1596 } else {
1597 // Move 64 bit double constant.
1598 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1599 int64_t value = GetInt64ValueOf(c);
1600 if (destination.IsFpuRegister()) {
1601 FRegister fd = destination.AsFpuRegister<FRegister>();
1602 __ LoadDConst64(fd, value, TMP);
1603 } else {
1604 DCHECK(destination.IsDoubleStackSlot())
1605 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001606 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001607 }
1608 }
1609}
1610
1611void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1612 DCHECK(destination.IsRegister());
1613 Register dst = destination.AsRegister<Register>();
1614 __ LoadConst32(dst, value);
1615}
1616
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001617void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1618 if (location.IsRegister()) {
1619 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001620 } else if (location.IsRegisterPair()) {
1621 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1622 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001623 } else {
1624 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1625 }
1626}
1627
Vladimir Markoaad75c62016-10-03 08:46:48 +00001628template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1629inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1630 const ArenaDeque<PcRelativePatchInfo>& infos,
1631 ArenaVector<LinkerPatch>* linker_patches) {
1632 for (const PcRelativePatchInfo& info : infos) {
1633 const DexFile& dex_file = info.target_dex_file;
1634 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001635 DCHECK(info.label.IsBound());
1636 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001637 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1638 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001639 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1640 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1641 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001642 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001643 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001644 }
1645}
1646
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001647void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1648 DCHECK(linker_patches->empty());
1649 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001650 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001651 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001652 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001653 type_bss_entry_patches_.size() +
1654 pc_relative_string_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001655 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001656 if (GetCompilerOptions().IsBootImage()) {
1657 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00001658 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001659 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1660 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001661 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1662 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001663 } else {
1664 DCHECK(pc_relative_method_patches_.empty());
1665 DCHECK(pc_relative_type_patches_.empty());
1666 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1667 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001668 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001669 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1670 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001671 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1672 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001673 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001674}
1675
Vladimir Marko65979462017-05-19 17:25:12 +01001676CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001677 MethodReference target_method,
1678 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001679 return NewPcRelativePatch(*target_method.dex_file,
1680 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001681 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001682 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001683}
1684
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001685CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001686 MethodReference target_method,
1687 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001688 return NewPcRelativePatch(*target_method.dex_file,
1689 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001690 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001691 &method_bss_entry_patches_);
1692}
1693
Alexey Frunze06a46c42016-07-19 15:00:40 -07001694CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001695 const DexFile& dex_file,
1696 dex::TypeIndex type_index,
1697 const PcRelativePatchInfo* info_high) {
1698 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001699}
1700
Vladimir Marko1998cd02017-01-13 13:02:58 +00001701CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001702 const DexFile& dex_file,
1703 dex::TypeIndex type_index,
1704 const PcRelativePatchInfo* info_high) {
1705 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001706}
1707
Vladimir Marko65979462017-05-19 17:25:12 +01001708CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001709 const DexFile& dex_file,
1710 dex::StringIndex string_index,
1711 const PcRelativePatchInfo* info_high) {
1712 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001713}
1714
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001715CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001716 const DexFile& dex_file,
1717 uint32_t offset_or_index,
1718 const PcRelativePatchInfo* info_high,
1719 ArenaDeque<PcRelativePatchInfo>* patches) {
1720 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001721 return &patches->back();
1722}
1723
Alexey Frunze06a46c42016-07-19 15:00:40 -07001724Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1725 return map->GetOrCreate(
1726 value,
1727 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1728}
1729
Alexey Frunze06a46c42016-07-19 15:00:40 -07001730Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001731 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001732}
1733
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001734void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001735 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001736 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001737 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001738 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001739 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001740 if (GetInstructionSetFeatures().IsR6()) {
1741 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001742 __ Bind(&info_high->label);
1743 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001744 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001745 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001746 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001747 } else {
1748 // If base is ZERO, emit NAL to obtain the actual base.
1749 if (base == ZERO) {
1750 // Generate a dummy PC-relative call to obtain PC.
1751 __ Nal();
1752 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001753 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001754 __ Lui(out, /* placeholder */ 0x1234);
1755 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1756 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1757 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001758 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001759 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001760 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001761 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001762 __ Addu(out, out, (base == ZERO) ? RA : base);
1763 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001764 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001765 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001766}
1767
Alexey Frunze627c1a02017-01-30 19:28:14 -08001768CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1769 const DexFile& dex_file,
1770 dex::StringIndex dex_index,
1771 Handle<mirror::String> handle) {
1772 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1773 reinterpret_cast64<uint64_t>(handle.GetReference()));
1774 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1775 return &jit_string_patches_.back();
1776}
1777
1778CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1779 const DexFile& dex_file,
1780 dex::TypeIndex dex_index,
1781 Handle<mirror::Class> handle) {
1782 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1783 reinterpret_cast64<uint64_t>(handle.GetReference()));
1784 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1785 return &jit_class_patches_.back();
1786}
1787
1788void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1789 const uint8_t* roots_data,
1790 const CodeGeneratorMIPS::JitPatchInfo& info,
1791 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001792 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1793 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001794 uintptr_t address =
1795 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1796 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1797 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001798 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1799 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1800 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1801 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001802 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001803 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1804 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001805 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001806 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001807 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1808 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001809 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001810 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1811 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812}
1813
1814void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1815 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001816 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1817 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001818 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001819 uint64_t index_in_table = it->second;
1820 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001821 }
1822 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001823 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1824 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001825 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001826 uint64_t index_in_table = it->second;
1827 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001828 }
1829}
1830
Goran Jakovljevice114da22016-12-26 14:21:43 +01001831void CodeGeneratorMIPS::MarkGCCard(Register object,
1832 Register value,
1833 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001834 MipsLabel done;
1835 Register card = AT;
1836 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001837 if (value_can_be_null) {
1838 __ Beqz(value, &done);
1839 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840 __ LoadFromOffset(kLoadWord,
1841 card,
1842 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001843 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1845 __ Addu(temp, card, temp);
1846 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001847 if (value_can_be_null) {
1848 __ Bind(&done);
1849 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850}
1851
David Brazdil58282f42016-01-14 12:45:10 +00001852void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1854 blocked_core_registers_[ZERO] = true;
1855 blocked_core_registers_[K0] = true;
1856 blocked_core_registers_[K1] = true;
1857 blocked_core_registers_[GP] = true;
1858 blocked_core_registers_[SP] = true;
1859 blocked_core_registers_[RA] = true;
1860
1861 // AT and TMP(T8) are used as temporary/scratch registers
1862 // (similar to how AT is used by MIPS assemblers).
1863 blocked_core_registers_[AT] = true;
1864 blocked_core_registers_[TMP] = true;
1865 blocked_fpu_registers_[FTMP] = true;
1866
1867 // Reserve suspend and thread registers.
1868 blocked_core_registers_[S0] = true;
1869 blocked_core_registers_[TR] = true;
1870
1871 // Reserve T9 for function calls
1872 blocked_core_registers_[T9] = true;
1873
1874 // Reserve odd-numbered FPU registers.
1875 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1876 blocked_fpu_registers_[i] = true;
1877 }
1878
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001879 if (GetGraph()->IsDebuggable()) {
1880 // Stubs do not save callee-save floating point registers. If the graph
1881 // is debuggable, we need to deal with these registers differently. For
1882 // now, just block them.
1883 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1884 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1885 }
1886 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887}
1888
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1890 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1891 return kMipsWordSize;
1892}
1893
1894size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1895 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1896 return kMipsWordSize;
1897}
1898
1899size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001900 if (GetGraph()->HasSIMD()) {
1901 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1902 } else {
1903 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1904 }
1905 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906}
1907
1908size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001909 if (GetGraph()->HasSIMD()) {
1910 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1911 } else {
1912 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1913 }
1914 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915}
1916
1917void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001918 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919}
1920
1921void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001922 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923}
1924
Serban Constantinescufca16662016-07-14 09:21:59 +01001925constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1926
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1928 HInstruction* instruction,
1929 uint32_t dex_pc,
1930 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001931 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001932 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1933 IsDirectEntrypoint(entrypoint));
1934 if (EntrypointRequiresStackMap(entrypoint)) {
1935 RecordPcInfo(instruction, dex_pc, slow_path);
1936 }
1937}
1938
1939void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1940 HInstruction* instruction,
1941 SlowPathCode* slow_path,
1942 bool direct) {
1943 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1944 GenerateInvokeRuntime(entry_point_offset, direct);
1945}
1946
1947void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001948 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001949 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001950 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001951 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 // Reserve argument space on stack (for $a0-$a3) for
1953 // entrypoints that directly reference native implementations.
1954 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001955 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001957 } else {
1958 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001960 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961}
1962
1963void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1964 Register class_reg) {
1965 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1966 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1967 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1968 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1969 __ Sync(0);
1970 __ Bind(slow_path->GetExitLabel());
1971}
1972
1973void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1974 __ Sync(0); // Only stype 0 is supported.
1975}
1976
1977void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1978 HBasicBlock* successor) {
1979 SuspendCheckSlowPathMIPS* slow_path =
1980 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1981 codegen_->AddSlowPath(slow_path);
1982
1983 __ LoadFromOffset(kLoadUnsignedHalfword,
1984 TMP,
1985 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001986 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 if (successor == nullptr) {
1988 __ Bnez(TMP, slow_path->GetEntryLabel());
1989 __ Bind(slow_path->GetReturnLabel());
1990 } else {
1991 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1992 __ B(slow_path->GetEntryLabel());
1993 // slow_path will return to GetLabelOf(successor).
1994 }
1995}
1996
1997InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1998 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001999 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002000 assembler_(codegen->GetAssembler()),
2001 codegen_(codegen) {}
2002
2003void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2004 DCHECK_EQ(instruction->InputCount(), 2U);
2005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2006 Primitive::Type type = instruction->GetResultType();
2007 switch (type) {
2008 case Primitive::kPrimInt: {
2009 locations->SetInAt(0, Location::RequiresRegister());
2010 HInstruction* right = instruction->InputAt(1);
2011 bool can_use_imm = false;
2012 if (right->IsConstant()) {
2013 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2014 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2015 can_use_imm = IsUint<16>(imm);
2016 } else if (instruction->IsAdd()) {
2017 can_use_imm = IsInt<16>(imm);
2018 } else {
2019 DCHECK(instruction->IsSub());
2020 can_use_imm = IsInt<16>(-imm);
2021 }
2022 }
2023 if (can_use_imm)
2024 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2025 else
2026 locations->SetInAt(1, Location::RequiresRegister());
2027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2028 break;
2029 }
2030
2031 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002033 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 break;
2036 }
2037
2038 case Primitive::kPrimFloat:
2039 case Primitive::kPrimDouble:
2040 DCHECK(instruction->IsAdd() || instruction->IsSub());
2041 locations->SetInAt(0, Location::RequiresFpuRegister());
2042 locations->SetInAt(1, Location::RequiresFpuRegister());
2043 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2044 break;
2045
2046 default:
2047 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2048 }
2049}
2050
2051void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2052 Primitive::Type type = instruction->GetType();
2053 LocationSummary* locations = instruction->GetLocations();
2054
2055 switch (type) {
2056 case Primitive::kPrimInt: {
2057 Register dst = locations->Out().AsRegister<Register>();
2058 Register lhs = locations->InAt(0).AsRegister<Register>();
2059 Location rhs_location = locations->InAt(1);
2060
2061 Register rhs_reg = ZERO;
2062 int32_t rhs_imm = 0;
2063 bool use_imm = rhs_location.IsConstant();
2064 if (use_imm) {
2065 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2066 } else {
2067 rhs_reg = rhs_location.AsRegister<Register>();
2068 }
2069
2070 if (instruction->IsAnd()) {
2071 if (use_imm)
2072 __ Andi(dst, lhs, rhs_imm);
2073 else
2074 __ And(dst, lhs, rhs_reg);
2075 } else if (instruction->IsOr()) {
2076 if (use_imm)
2077 __ Ori(dst, lhs, rhs_imm);
2078 else
2079 __ Or(dst, lhs, rhs_reg);
2080 } else if (instruction->IsXor()) {
2081 if (use_imm)
2082 __ Xori(dst, lhs, rhs_imm);
2083 else
2084 __ Xor(dst, lhs, rhs_reg);
2085 } else if (instruction->IsAdd()) {
2086 if (use_imm)
2087 __ Addiu(dst, lhs, rhs_imm);
2088 else
2089 __ Addu(dst, lhs, rhs_reg);
2090 } else {
2091 DCHECK(instruction->IsSub());
2092 if (use_imm)
2093 __ Addiu(dst, lhs, -rhs_imm);
2094 else
2095 __ Subu(dst, lhs, rhs_reg);
2096 }
2097 break;
2098 }
2099
2100 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2102 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2103 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2104 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002105 Location rhs_location = locations->InAt(1);
2106 bool use_imm = rhs_location.IsConstant();
2107 if (!use_imm) {
2108 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2109 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2110 if (instruction->IsAnd()) {
2111 __ And(dst_low, lhs_low, rhs_low);
2112 __ And(dst_high, lhs_high, rhs_high);
2113 } else if (instruction->IsOr()) {
2114 __ Or(dst_low, lhs_low, rhs_low);
2115 __ Or(dst_high, lhs_high, rhs_high);
2116 } else if (instruction->IsXor()) {
2117 __ Xor(dst_low, lhs_low, rhs_low);
2118 __ Xor(dst_high, lhs_high, rhs_high);
2119 } else if (instruction->IsAdd()) {
2120 if (lhs_low == rhs_low) {
2121 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2122 __ Slt(TMP, lhs_low, ZERO);
2123 __ Addu(dst_low, lhs_low, rhs_low);
2124 } else {
2125 __ Addu(dst_low, lhs_low, rhs_low);
2126 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2127 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2128 }
2129 __ Addu(dst_high, lhs_high, rhs_high);
2130 __ Addu(dst_high, dst_high, TMP);
2131 } else {
2132 DCHECK(instruction->IsSub());
2133 __ Sltu(TMP, lhs_low, rhs_low);
2134 __ Subu(dst_low, lhs_low, rhs_low);
2135 __ Subu(dst_high, lhs_high, rhs_high);
2136 __ Subu(dst_high, dst_high, TMP);
2137 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002138 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002139 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2140 if (instruction->IsOr()) {
2141 uint32_t low = Low32Bits(value);
2142 uint32_t high = High32Bits(value);
2143 if (IsUint<16>(low)) {
2144 if (dst_low != lhs_low || low != 0) {
2145 __ Ori(dst_low, lhs_low, low);
2146 }
2147 } else {
2148 __ LoadConst32(TMP, low);
2149 __ Or(dst_low, lhs_low, TMP);
2150 }
2151 if (IsUint<16>(high)) {
2152 if (dst_high != lhs_high || high != 0) {
2153 __ Ori(dst_high, lhs_high, high);
2154 }
2155 } else {
2156 if (high != low) {
2157 __ LoadConst32(TMP, high);
2158 }
2159 __ Or(dst_high, lhs_high, TMP);
2160 }
2161 } else if (instruction->IsXor()) {
2162 uint32_t low = Low32Bits(value);
2163 uint32_t high = High32Bits(value);
2164 if (IsUint<16>(low)) {
2165 if (dst_low != lhs_low || low != 0) {
2166 __ Xori(dst_low, lhs_low, low);
2167 }
2168 } else {
2169 __ LoadConst32(TMP, low);
2170 __ Xor(dst_low, lhs_low, TMP);
2171 }
2172 if (IsUint<16>(high)) {
2173 if (dst_high != lhs_high || high != 0) {
2174 __ Xori(dst_high, lhs_high, high);
2175 }
2176 } else {
2177 if (high != low) {
2178 __ LoadConst32(TMP, high);
2179 }
2180 __ Xor(dst_high, lhs_high, TMP);
2181 }
2182 } else if (instruction->IsAnd()) {
2183 uint32_t low = Low32Bits(value);
2184 uint32_t high = High32Bits(value);
2185 if (IsUint<16>(low)) {
2186 __ Andi(dst_low, lhs_low, low);
2187 } else if (low != 0xFFFFFFFF) {
2188 __ LoadConst32(TMP, low);
2189 __ And(dst_low, lhs_low, TMP);
2190 } else if (dst_low != lhs_low) {
2191 __ Move(dst_low, lhs_low);
2192 }
2193 if (IsUint<16>(high)) {
2194 __ Andi(dst_high, lhs_high, high);
2195 } else if (high != 0xFFFFFFFF) {
2196 if (high != low) {
2197 __ LoadConst32(TMP, high);
2198 }
2199 __ And(dst_high, lhs_high, TMP);
2200 } else if (dst_high != lhs_high) {
2201 __ Move(dst_high, lhs_high);
2202 }
2203 } else {
2204 if (instruction->IsSub()) {
2205 value = -value;
2206 } else {
2207 DCHECK(instruction->IsAdd());
2208 }
2209 int32_t low = Low32Bits(value);
2210 int32_t high = High32Bits(value);
2211 if (IsInt<16>(low)) {
2212 if (dst_low != lhs_low || low != 0) {
2213 __ Addiu(dst_low, lhs_low, low);
2214 }
2215 if (low != 0) {
2216 __ Sltiu(AT, dst_low, low);
2217 }
2218 } else {
2219 __ LoadConst32(TMP, low);
2220 __ Addu(dst_low, lhs_low, TMP);
2221 __ Sltu(AT, dst_low, TMP);
2222 }
2223 if (IsInt<16>(high)) {
2224 if (dst_high != lhs_high || high != 0) {
2225 __ Addiu(dst_high, lhs_high, high);
2226 }
2227 } else {
2228 if (high != low) {
2229 __ LoadConst32(TMP, high);
2230 }
2231 __ Addu(dst_high, lhs_high, TMP);
2232 }
2233 if (low != 0) {
2234 __ Addu(dst_high, dst_high, AT);
2235 }
2236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 }
2238 break;
2239 }
2240
2241 case Primitive::kPrimFloat:
2242 case Primitive::kPrimDouble: {
2243 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2244 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2245 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2246 if (instruction->IsAdd()) {
2247 if (type == Primitive::kPrimFloat) {
2248 __ AddS(dst, lhs, rhs);
2249 } else {
2250 __ AddD(dst, lhs, rhs);
2251 }
2252 } else {
2253 DCHECK(instruction->IsSub());
2254 if (type == Primitive::kPrimFloat) {
2255 __ SubS(dst, lhs, rhs);
2256 } else {
2257 __ SubD(dst, lhs, rhs);
2258 }
2259 }
2260 break;
2261 }
2262
2263 default:
2264 LOG(FATAL) << "Unexpected binary operation type " << type;
2265 }
2266}
2267
2268void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002269 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002270
2271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2272 Primitive::Type type = instr->GetResultType();
2273 switch (type) {
2274 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2278 break;
2279 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002280 locations->SetInAt(0, Location::RequiresRegister());
2281 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2282 locations->SetOut(Location::RequiresRegister());
2283 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284 default:
2285 LOG(FATAL) << "Unexpected shift type " << type;
2286 }
2287}
2288
2289static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2290
2291void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002292 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002293 LocationSummary* locations = instr->GetLocations();
2294 Primitive::Type type = instr->GetType();
2295
2296 Location rhs_location = locations->InAt(1);
2297 bool use_imm = rhs_location.IsConstant();
2298 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2299 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002300 const uint32_t shift_mask =
2301 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002302 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002303 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2304 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002305
2306 switch (type) {
2307 case Primitive::kPrimInt: {
2308 Register dst = locations->Out().AsRegister<Register>();
2309 Register lhs = locations->InAt(0).AsRegister<Register>();
2310 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 if (shift_value == 0) {
2312 if (dst != lhs) {
2313 __ Move(dst, lhs);
2314 }
2315 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316 __ Sll(dst, lhs, shift_value);
2317 } else if (instr->IsShr()) {
2318 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002319 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002320 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002321 } else {
2322 if (has_ins_rotr) {
2323 __ Rotr(dst, lhs, shift_value);
2324 } else {
2325 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2326 __ Srl(dst, lhs, shift_value);
2327 __ Or(dst, dst, TMP);
2328 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329 }
2330 } else {
2331 if (instr->IsShl()) {
2332 __ Sllv(dst, lhs, rhs_reg);
2333 } else if (instr->IsShr()) {
2334 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002335 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002336 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 } else {
2338 if (has_ins_rotr) {
2339 __ Rotrv(dst, lhs, rhs_reg);
2340 } else {
2341 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002342 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2343 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2344 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2345 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2346 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002347 __ Sllv(TMP, lhs, TMP);
2348 __ Srlv(dst, lhs, rhs_reg);
2349 __ Or(dst, dst, TMP);
2350 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002351 }
2352 }
2353 break;
2354 }
2355
2356 case Primitive::kPrimLong: {
2357 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2358 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2359 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2360 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2361 if (use_imm) {
2362 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002363 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002364 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002365 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002366 if (instr->IsShl()) {
2367 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2368 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2369 __ Sll(dst_low, lhs_low, shift_value);
2370 } else if (instr->IsShr()) {
2371 __ Srl(dst_low, lhs_low, shift_value);
2372 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2373 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 } else if (instr->IsUShr()) {
2375 __ Srl(dst_low, lhs_low, shift_value);
2376 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2377 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002378 } else {
2379 __ Srl(dst_low, lhs_low, shift_value);
2380 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2381 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002383 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002385 if (instr->IsShl()) {
2386 __ Sll(dst_low, lhs_low, shift_value);
2387 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2388 __ Sll(dst_high, lhs_high, shift_value);
2389 __ Or(dst_high, dst_high, TMP);
2390 } else if (instr->IsShr()) {
2391 __ Sra(dst_high, lhs_high, shift_value);
2392 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2393 __ Srl(dst_low, lhs_low, shift_value);
2394 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002395 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002396 __ Srl(dst_high, lhs_high, shift_value);
2397 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2398 __ Srl(dst_low, lhs_low, shift_value);
2399 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002400 } else {
2401 __ Srl(TMP, lhs_low, shift_value);
2402 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2403 __ Or(dst_low, dst_low, TMP);
2404 __ Srl(TMP, lhs_high, shift_value);
2405 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2406 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002407 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408 }
2409 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002410 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002412 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 __ Move(dst_low, ZERO);
2414 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002415 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002417 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002418 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002420 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002421 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002422 // 64-bit rotation by 32 is just a swap.
2423 __ Move(dst_low, lhs_high);
2424 __ Move(dst_high, lhs_low);
2425 } else {
2426 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002427 __ Srl(dst_low, lhs_high, shift_value_high);
2428 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2429 __ Srl(dst_high, lhs_low, shift_value_high);
2430 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002431 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002432 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2433 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002435 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2436 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002437 __ Or(dst_high, dst_high, TMP);
2438 }
2439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 }
2441 }
2442 } else {
2443 MipsLabel done;
2444 if (instr->IsShl()) {
2445 __ Sllv(dst_low, lhs_low, rhs_reg);
2446 __ Nor(AT, ZERO, rhs_reg);
2447 __ Srl(TMP, lhs_low, 1);
2448 __ Srlv(TMP, TMP, AT);
2449 __ Sllv(dst_high, lhs_high, rhs_reg);
2450 __ Or(dst_high, dst_high, TMP);
2451 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2452 __ Beqz(TMP, &done);
2453 __ Move(dst_high, dst_low);
2454 __ Move(dst_low, ZERO);
2455 } else if (instr->IsShr()) {
2456 __ Srav(dst_high, lhs_high, rhs_reg);
2457 __ Nor(AT, ZERO, rhs_reg);
2458 __ Sll(TMP, lhs_high, 1);
2459 __ Sllv(TMP, TMP, AT);
2460 __ Srlv(dst_low, lhs_low, rhs_reg);
2461 __ Or(dst_low, dst_low, TMP);
2462 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2463 __ Beqz(TMP, &done);
2464 __ Move(dst_low, dst_high);
2465 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002466 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002467 __ Srlv(dst_high, lhs_high, rhs_reg);
2468 __ Nor(AT, ZERO, rhs_reg);
2469 __ Sll(TMP, lhs_high, 1);
2470 __ Sllv(TMP, TMP, AT);
2471 __ Srlv(dst_low, lhs_low, rhs_reg);
2472 __ Or(dst_low, dst_low, TMP);
2473 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2474 __ Beqz(TMP, &done);
2475 __ Move(dst_low, dst_high);
2476 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002477 } else {
2478 __ Nor(AT, ZERO, rhs_reg);
2479 __ Srlv(TMP, lhs_low, rhs_reg);
2480 __ Sll(dst_low, lhs_high, 1);
2481 __ Sllv(dst_low, dst_low, AT);
2482 __ Or(dst_low, dst_low, TMP);
2483 __ Srlv(TMP, lhs_high, rhs_reg);
2484 __ Sll(dst_high, lhs_low, 1);
2485 __ Sllv(dst_high, dst_high, AT);
2486 __ Or(dst_high, dst_high, TMP);
2487 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2488 __ Beqz(TMP, &done);
2489 __ Move(TMP, dst_high);
2490 __ Move(dst_high, dst_low);
2491 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002492 }
2493 __ Bind(&done);
2494 }
2495 break;
2496 }
2497
2498 default:
2499 LOG(FATAL) << "Unexpected shift operation type " << type;
2500 }
2501}
2502
2503void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2504 HandleBinaryOp(instruction);
2505}
2506
2507void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2508 HandleBinaryOp(instruction);
2509}
2510
2511void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2512 HandleBinaryOp(instruction);
2513}
2514
2515void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2516 HandleBinaryOp(instruction);
2517}
2518
2519void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002520 Primitive::Type type = instruction->GetType();
2521 bool object_array_get_with_read_barrier =
2522 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002523 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002524 new (GetGraph()->GetArena()) LocationSummary(instruction,
2525 object_array_get_with_read_barrier
2526 ? LocationSummary::kCallOnSlowPath
2527 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002528 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2529 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2530 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002531 locations->SetInAt(0, Location::RequiresRegister());
2532 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002533 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002534 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2535 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002536 // The output overlaps in the case of an object array get with
2537 // read barriers enabled: we do not want the move to overwrite the
2538 // array's location, as we need it to emit the read barrier.
2539 locations->SetOut(Location::RequiresRegister(),
2540 object_array_get_with_read_barrier
2541 ? Location::kOutputOverlap
2542 : Location::kNoOutputOverlap);
2543 }
2544 // We need a temporary register for the read barrier marking slow
2545 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2546 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002547 bool temp_needed = instruction->GetIndex()->IsConstant()
2548 ? !kBakerReadBarrierThunksEnableForFields
2549 : !kBakerReadBarrierThunksEnableForArrays;
2550 if (temp_needed) {
2551 locations->AddTemp(Location::RequiresRegister());
2552 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002553 }
2554}
2555
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002556static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2557 auto null_checker = [codegen, instruction]() {
2558 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002559 };
2560 return null_checker;
2561}
2562
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002563void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2564 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002565 Location obj_loc = locations->InAt(0);
2566 Register obj = obj_loc.AsRegister<Register>();
2567 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002568 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002569 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002570 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002572 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002573 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2574 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002575 switch (type) {
2576 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002577 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002578 if (index.IsConstant()) {
2579 size_t offset =
2580 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002581 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002582 } else {
2583 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002584 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585 }
2586 break;
2587 }
2588
2589 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002590 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002591 if (index.IsConstant()) {
2592 size_t offset =
2593 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002594 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002595 } else {
2596 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002597 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002598 }
2599 break;
2600 }
2601
2602 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002603 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 if (index.IsConstant()) {
2605 size_t offset =
2606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002607 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002608 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002609 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002610 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 }
2612 break;
2613 }
2614
2615 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002616 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002617 if (maybe_compressed_char_at) {
2618 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2619 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2620 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2621 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2622 "Expecting 0=compressed, 1=uncompressed");
2623 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002625 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2626 if (maybe_compressed_char_at) {
2627 MipsLabel uncompressed_load, done;
2628 __ Bnez(TMP, &uncompressed_load);
2629 __ LoadFromOffset(kLoadUnsignedByte,
2630 out,
2631 obj,
2632 data_offset + (const_index << TIMES_1));
2633 __ B(&done);
2634 __ Bind(&uncompressed_load);
2635 __ LoadFromOffset(kLoadUnsignedHalfword,
2636 out,
2637 obj,
2638 data_offset + (const_index << TIMES_2));
2639 __ Bind(&done);
2640 } else {
2641 __ LoadFromOffset(kLoadUnsignedHalfword,
2642 out,
2643 obj,
2644 data_offset + (const_index << TIMES_2),
2645 null_checker);
2646 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002647 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002648 Register index_reg = index.AsRegister<Register>();
2649 if (maybe_compressed_char_at) {
2650 MipsLabel uncompressed_load, done;
2651 __ Bnez(TMP, &uncompressed_load);
2652 __ Addu(TMP, obj, index_reg);
2653 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2654 __ B(&done);
2655 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002656 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002657 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2658 __ Bind(&done);
2659 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002660 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002661 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 }
2664 break;
2665 }
2666
Alexey Frunze15958152017-02-09 19:08:30 -08002667 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002669 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002670 if (index.IsConstant()) {
2671 size_t offset =
2672 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002673 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002675 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002676 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 }
2678 break;
2679 }
2680
Alexey Frunze15958152017-02-09 19:08:30 -08002681 case Primitive::kPrimNot: {
2682 static_assert(
2683 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2684 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2685 // /* HeapReference<Object> */ out =
2686 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2687 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002688 bool temp_needed = index.IsConstant()
2689 ? !kBakerReadBarrierThunksEnableForFields
2690 : !kBakerReadBarrierThunksEnableForArrays;
2691 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002692 // Note that a potential implicit null check is handled in this
2693 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002694 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2695 if (index.IsConstant()) {
2696 // Array load with a constant index can be treated as a field load.
2697 size_t offset =
2698 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2699 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2700 out_loc,
2701 obj,
2702 offset,
2703 temp,
2704 /* needs_null_check */ false);
2705 } else {
2706 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2707 out_loc,
2708 obj,
2709 data_offset,
2710 index,
2711 temp,
2712 /* needs_null_check */ false);
2713 }
Alexey Frunze15958152017-02-09 19:08:30 -08002714 } else {
2715 Register out = out_loc.AsRegister<Register>();
2716 if (index.IsConstant()) {
2717 size_t offset =
2718 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2719 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2720 // If read barriers are enabled, emit read barriers other than
2721 // Baker's using a slow path (and also unpoison the loaded
2722 // reference, if heap poisoning is enabled).
2723 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2724 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002725 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002726 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2727 // If read barriers are enabled, emit read barriers other than
2728 // Baker's using a slow path (and also unpoison the loaded
2729 // reference, if heap poisoning is enabled).
2730 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2731 out_loc,
2732 out_loc,
2733 obj_loc,
2734 data_offset,
2735 index);
2736 }
2737 }
2738 break;
2739 }
2740
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002741 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002742 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002743 if (index.IsConstant()) {
2744 size_t offset =
2745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002746 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002747 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002748 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002749 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002750 }
2751 break;
2752 }
2753
2754 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002755 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002756 if (index.IsConstant()) {
2757 size_t offset =
2758 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002759 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002760 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002761 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002762 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002763 }
2764 break;
2765 }
2766
2767 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002768 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002769 if (index.IsConstant()) {
2770 size_t offset =
2771 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002772 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002773 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002774 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002775 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002776 }
2777 break;
2778 }
2779
2780 case Primitive::kPrimVoid:
2781 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2782 UNREACHABLE();
2783 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002784}
2785
2786void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2787 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2788 locations->SetInAt(0, Location::RequiresRegister());
2789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2790}
2791
2792void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2793 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002794 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 Register obj = locations->InAt(0).AsRegister<Register>();
2796 Register out = locations->Out().AsRegister<Register>();
2797 __ LoadFromOffset(kLoadWord, out, obj, offset);
2798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002799 // Mask out compression flag from String's array length.
2800 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2801 __ Srl(out, out, 1u);
2802 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803}
2804
Alexey Frunzef58b2482016-09-02 22:14:06 -07002805Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2806 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2807 ? Location::ConstantLocation(instruction->AsConstant())
2808 : Location::RequiresRegister();
2809}
2810
2811Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2812 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2813 // We can store a non-zero float or double constant without first loading it into the FPU,
2814 // but we should only prefer this if the constant has a single use.
2815 if (instruction->IsConstant() &&
2816 (instruction->AsConstant()->IsZeroBitPattern() ||
2817 instruction->GetUses().HasExactlyOneElement())) {
2818 return Location::ConstantLocation(instruction->AsConstant());
2819 // Otherwise fall through and require an FPU register for the constant.
2820 }
2821 return Location::RequiresFpuRegister();
2822}
2823
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002825 Primitive::Type value_type = instruction->GetComponentType();
2826
2827 bool needs_write_barrier =
2828 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2829 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2830
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2832 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002833 may_need_runtime_call_for_type_check ?
2834 LocationSummary::kCallOnSlowPath :
2835 LocationSummary::kNoCall);
2836
2837 locations->SetInAt(0, Location::RequiresRegister());
2838 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2839 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2840 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002841 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002842 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2843 }
2844 if (needs_write_barrier) {
2845 // Temporary register for the write barrier.
2846 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002847 }
2848}
2849
2850void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2851 LocationSummary* locations = instruction->GetLocations();
2852 Register obj = locations->InAt(0).AsRegister<Register>();
2853 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002854 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002855 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002856 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002857 bool needs_write_barrier =
2858 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002859 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002860 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002861
2862 switch (value_type) {
2863 case Primitive::kPrimBoolean:
2864 case Primitive::kPrimByte: {
2865 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002866 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002867 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002869 __ Addu(base_reg, obj, index.AsRegister<Register>());
2870 }
2871 if (value_location.IsConstant()) {
2872 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2873 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2874 } else {
2875 Register value = value_location.AsRegister<Register>();
2876 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002877 }
2878 break;
2879 }
2880
2881 case Primitive::kPrimShort:
2882 case Primitive::kPrimChar: {
2883 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002884 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002885 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002886 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002887 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002888 }
2889 if (value_location.IsConstant()) {
2890 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2891 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2892 } else {
2893 Register value = value_location.AsRegister<Register>();
2894 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002895 }
2896 break;
2897 }
2898
Alexey Frunze15958152017-02-09 19:08:30 -08002899 case Primitive::kPrimInt: {
2900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2901 if (index.IsConstant()) {
2902 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2903 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002904 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002905 }
2906 if (value_location.IsConstant()) {
2907 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2908 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2909 } else {
2910 Register value = value_location.AsRegister<Register>();
2911 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2912 }
2913 break;
2914 }
2915
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002916 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002917 if (value_location.IsConstant()) {
2918 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002921 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002922 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002923 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002924 }
Alexey Frunze15958152017-02-09 19:08:30 -08002925 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2926 DCHECK_EQ(value, 0);
2927 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2928 DCHECK(!needs_write_barrier);
2929 DCHECK(!may_need_runtime_call_for_type_check);
2930 break;
2931 }
2932
2933 DCHECK(needs_write_barrier);
2934 Register value = value_location.AsRegister<Register>();
2935 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2936 Register temp2 = TMP; // Doesn't need to survive slow path.
2937 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2938 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2939 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2940 MipsLabel done;
2941 SlowPathCodeMIPS* slow_path = nullptr;
2942
2943 if (may_need_runtime_call_for_type_check) {
2944 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2945 codegen_->AddSlowPath(slow_path);
2946 if (instruction->GetValueCanBeNull()) {
2947 MipsLabel non_zero;
2948 __ Bnez(value, &non_zero);
2949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2950 if (index.IsConstant()) {
2951 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002952 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002953 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002954 }
Alexey Frunze15958152017-02-09 19:08:30 -08002955 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2956 __ B(&done);
2957 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 }
Alexey Frunze15958152017-02-09 19:08:30 -08002959
2960 // Note that when read barriers are enabled, the type checks
2961 // are performed without read barriers. This is fine, even in
2962 // the case where a class object is in the from-space after
2963 // the flip, as a comparison involving such a type would not
2964 // produce a false positive; it may of course produce a false
2965 // negative, in which case we would take the ArraySet slow
2966 // path.
2967
2968 // /* HeapReference<Class> */ temp1 = obj->klass_
2969 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2970 __ MaybeUnpoisonHeapReference(temp1);
2971
2972 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2973 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2974 // /* HeapReference<Class> */ temp2 = value->klass_
2975 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2976 // If heap poisoning is enabled, no need to unpoison `temp1`
2977 // nor `temp2`, as we are comparing two poisoned references.
2978
2979 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2980 MipsLabel do_put;
2981 __ Beq(temp1, temp2, &do_put);
2982 // If heap poisoning is enabled, the `temp1` reference has
2983 // not been unpoisoned yet; unpoison it now.
2984 __ MaybeUnpoisonHeapReference(temp1);
2985
2986 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2987 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2988 // If heap poisoning is enabled, no need to unpoison
2989 // `temp1`, as we are comparing against null below.
2990 __ Bnez(temp1, slow_path->GetEntryLabel());
2991 __ Bind(&do_put);
2992 } else {
2993 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2994 }
2995 }
2996
2997 Register source = value;
2998 if (kPoisonHeapReferences) {
2999 // Note that in the case where `value` is a null reference,
3000 // we do not enter this block, as a null reference does not
3001 // need poisoning.
3002 __ Move(temp1, value);
3003 __ PoisonHeapReference(temp1);
3004 source = temp1;
3005 }
3006
3007 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3008 if (index.IsConstant()) {
3009 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003010 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003011 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003012 }
3013 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3014
3015 if (!may_need_runtime_call_for_type_check) {
3016 codegen_->MaybeRecordImplicitNullCheck(instruction);
3017 }
3018
3019 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3020
3021 if (done.IsLinked()) {
3022 __ Bind(&done);
3023 }
3024
3025 if (slow_path != nullptr) {
3026 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003027 }
3028 break;
3029 }
3030
3031 case Primitive::kPrimLong: {
3032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003033 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003034 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003035 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003036 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003037 }
3038 if (value_location.IsConstant()) {
3039 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3040 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3041 } else {
3042 Register value = value_location.AsRegisterPairLow<Register>();
3043 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003044 }
3045 break;
3046 }
3047
3048 case Primitive::kPrimFloat: {
3049 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003050 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003051 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003052 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003053 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003054 }
3055 if (value_location.IsConstant()) {
3056 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3057 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3058 } else {
3059 FRegister value = value_location.AsFpuRegister<FRegister>();
3060 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003061 }
3062 break;
3063 }
3064
3065 case Primitive::kPrimDouble: {
3066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003067 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003068 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003069 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003070 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003071 }
3072 if (value_location.IsConstant()) {
3073 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3074 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3075 } else {
3076 FRegister value = value_location.AsFpuRegister<FRegister>();
3077 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078 }
3079 break;
3080 }
3081
3082 case Primitive::kPrimVoid:
3083 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3084 UNREACHABLE();
3085 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003086}
3087
3088void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003089 RegisterSet caller_saves = RegisterSet::Empty();
3090 InvokeRuntimeCallingConvention calling_convention;
3091 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3092 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3093 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003094 locations->SetInAt(0, Location::RequiresRegister());
3095 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003096}
3097
3098void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3099 LocationSummary* locations = instruction->GetLocations();
3100 BoundsCheckSlowPathMIPS* slow_path =
3101 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3102 codegen_->AddSlowPath(slow_path);
3103
3104 Register index = locations->InAt(0).AsRegister<Register>();
3105 Register length = locations->InAt(1).AsRegister<Register>();
3106
3107 // length is limited by the maximum positive signed 32-bit integer.
3108 // Unsigned comparison of length and index checks for index < 0
3109 // and for length <= index simultaneously.
3110 __ Bgeu(index, length, slow_path->GetEntryLabel());
3111}
3112
Alexey Frunze15958152017-02-09 19:08:30 -08003113// Temp is used for read barrier.
3114static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3115 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003116 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003117 (kUseBakerReadBarrier ||
3118 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3119 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3120 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3121 return 1;
3122 }
3123 return 0;
3124}
3125
3126// Extra temp is used for read barrier.
3127static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3128 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3129}
3130
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003132 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3133 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3134
3135 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3136 switch (type_check_kind) {
3137 case TypeCheckKind::kExactCheck:
3138 case TypeCheckKind::kAbstractClassCheck:
3139 case TypeCheckKind::kClassHierarchyCheck:
3140 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003141 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003142 ? LocationSummary::kCallOnSlowPath
3143 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3144 break;
3145 case TypeCheckKind::kArrayCheck:
3146 case TypeCheckKind::kUnresolvedCheck:
3147 case TypeCheckKind::kInterfaceCheck:
3148 call_kind = LocationSummary::kCallOnSlowPath;
3149 break;
3150 }
3151
3152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003153 locations->SetInAt(0, Location::RequiresRegister());
3154 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003155 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003156}
3157
3158void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003159 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003160 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003161 Location obj_loc = locations->InAt(0);
3162 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003163 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003164 Location temp_loc = locations->GetTemp(0);
3165 Register temp = temp_loc.AsRegister<Register>();
3166 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3167 DCHECK_LE(num_temps, 2u);
3168 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003169 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3170 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3171 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3172 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3173 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3174 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3175 const uint32_t object_array_data_offset =
3176 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3177 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003178
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003179 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3180 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3181 // read barriers is done for performance and code size reasons.
3182 bool is_type_check_slow_path_fatal = false;
3183 if (!kEmitCompilerReadBarrier) {
3184 is_type_check_slow_path_fatal =
3185 (type_check_kind == TypeCheckKind::kExactCheck ||
3186 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3187 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3188 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3189 !instruction->CanThrowIntoCatchBlock();
3190 }
3191 SlowPathCodeMIPS* slow_path =
3192 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3193 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003194 codegen_->AddSlowPath(slow_path);
3195
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003196 // Avoid this check if we know `obj` is not null.
3197 if (instruction->MustDoNullCheck()) {
3198 __ Beqz(obj, &done);
3199 }
3200
3201 switch (type_check_kind) {
3202 case TypeCheckKind::kExactCheck:
3203 case TypeCheckKind::kArrayCheck: {
3204 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003205 GenerateReferenceLoadTwoRegisters(instruction,
3206 temp_loc,
3207 obj_loc,
3208 class_offset,
3209 maybe_temp2_loc,
3210 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003211 // Jump to slow path for throwing the exception or doing a
3212 // more involved array check.
3213 __ Bne(temp, cls, slow_path->GetEntryLabel());
3214 break;
3215 }
3216
3217 case TypeCheckKind::kAbstractClassCheck: {
3218 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003219 GenerateReferenceLoadTwoRegisters(instruction,
3220 temp_loc,
3221 obj_loc,
3222 class_offset,
3223 maybe_temp2_loc,
3224 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003225 // If the class is abstract, we eagerly fetch the super class of the
3226 // object to avoid doing a comparison we know will fail.
3227 MipsLabel loop;
3228 __ Bind(&loop);
3229 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003230 GenerateReferenceLoadOneRegister(instruction,
3231 temp_loc,
3232 super_offset,
3233 maybe_temp2_loc,
3234 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003235 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3236 // exception.
3237 __ Beqz(temp, slow_path->GetEntryLabel());
3238 // Otherwise, compare the classes.
3239 __ Bne(temp, cls, &loop);
3240 break;
3241 }
3242
3243 case TypeCheckKind::kClassHierarchyCheck: {
3244 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003245 GenerateReferenceLoadTwoRegisters(instruction,
3246 temp_loc,
3247 obj_loc,
3248 class_offset,
3249 maybe_temp2_loc,
3250 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003251 // Walk over the class hierarchy to find a match.
3252 MipsLabel loop;
3253 __ Bind(&loop);
3254 __ Beq(temp, cls, &done);
3255 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003256 GenerateReferenceLoadOneRegister(instruction,
3257 temp_loc,
3258 super_offset,
3259 maybe_temp2_loc,
3260 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003261 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3262 // exception. Otherwise, jump to the beginning of the loop.
3263 __ Bnez(temp, &loop);
3264 __ B(slow_path->GetEntryLabel());
3265 break;
3266 }
3267
3268 case TypeCheckKind::kArrayObjectCheck: {
3269 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003270 GenerateReferenceLoadTwoRegisters(instruction,
3271 temp_loc,
3272 obj_loc,
3273 class_offset,
3274 maybe_temp2_loc,
3275 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003276 // Do an exact check.
3277 __ Beq(temp, cls, &done);
3278 // Otherwise, we need to check that the object's class is a non-primitive array.
3279 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003280 GenerateReferenceLoadOneRegister(instruction,
3281 temp_loc,
3282 component_offset,
3283 maybe_temp2_loc,
3284 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003285 // If the component type is null, jump to the slow path to throw the exception.
3286 __ Beqz(temp, slow_path->GetEntryLabel());
3287 // Otherwise, the object is indeed an array, further check that this component
3288 // type is not a primitive type.
3289 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3290 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3291 __ Bnez(temp, slow_path->GetEntryLabel());
3292 break;
3293 }
3294
3295 case TypeCheckKind::kUnresolvedCheck:
3296 // We always go into the type check slow path for the unresolved check case.
3297 // We cannot directly call the CheckCast runtime entry point
3298 // without resorting to a type checking slow path here (i.e. by
3299 // calling InvokeRuntime directly), as it would require to
3300 // assign fixed registers for the inputs of this HInstanceOf
3301 // instruction (following the runtime calling convention), which
3302 // might be cluttered by the potential first read barrier
3303 // emission at the beginning of this method.
3304 __ B(slow_path->GetEntryLabel());
3305 break;
3306
3307 case TypeCheckKind::kInterfaceCheck: {
3308 // Avoid read barriers to improve performance of the fast path. We can not get false
3309 // positives by doing this.
3310 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003311 GenerateReferenceLoadTwoRegisters(instruction,
3312 temp_loc,
3313 obj_loc,
3314 class_offset,
3315 maybe_temp2_loc,
3316 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003317 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003318 GenerateReferenceLoadTwoRegisters(instruction,
3319 temp_loc,
3320 temp_loc,
3321 iftable_offset,
3322 maybe_temp2_loc,
3323 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003324 // Iftable is never null.
3325 __ Lw(TMP, temp, array_length_offset);
3326 // Loop through the iftable and check if any class matches.
3327 MipsLabel loop;
3328 __ Bind(&loop);
3329 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3330 __ Beqz(TMP, slow_path->GetEntryLabel());
3331 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3332 __ MaybeUnpoisonHeapReference(AT);
3333 // Go to next interface.
3334 __ Addiu(TMP, TMP, -2);
3335 // Compare the classes and continue the loop if they do not match.
3336 __ Bne(AT, cls, &loop);
3337 break;
3338 }
3339 }
3340
3341 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003342 __ Bind(slow_path->GetExitLabel());
3343}
3344
3345void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3346 LocationSummary* locations =
3347 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3348 locations->SetInAt(0, Location::RequiresRegister());
3349 if (check->HasUses()) {
3350 locations->SetOut(Location::SameAsFirstInput());
3351 }
3352}
3353
3354void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3355 // We assume the class is not null.
3356 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3357 check->GetLoadClass(),
3358 check,
3359 check->GetDexPc(),
3360 true);
3361 codegen_->AddSlowPath(slow_path);
3362 GenerateClassInitializationCheck(slow_path,
3363 check->GetLocations()->InAt(0).AsRegister<Register>());
3364}
3365
3366void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3367 Primitive::Type in_type = compare->InputAt(0)->GetType();
3368
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003369 LocationSummary* locations =
3370 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003371
3372 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003373 case Primitive::kPrimBoolean:
3374 case Primitive::kPrimByte:
3375 case Primitive::kPrimShort:
3376 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003377 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003378 locations->SetInAt(0, Location::RequiresRegister());
3379 locations->SetInAt(1, Location::RequiresRegister());
3380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3381 break;
3382
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003383 case Primitive::kPrimLong:
3384 locations->SetInAt(0, Location::RequiresRegister());
3385 locations->SetInAt(1, Location::RequiresRegister());
3386 // Output overlaps because it is written before doing the low comparison.
3387 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3388 break;
3389
3390 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003391 case Primitive::kPrimDouble:
3392 locations->SetInAt(0, Location::RequiresFpuRegister());
3393 locations->SetInAt(1, Location::RequiresFpuRegister());
3394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003395 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003396
3397 default:
3398 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3399 }
3400}
3401
3402void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3403 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003404 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003405 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003406 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003407
3408 // 0 if: left == right
3409 // 1 if: left > right
3410 // -1 if: left < right
3411 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003412 case Primitive::kPrimBoolean:
3413 case Primitive::kPrimByte:
3414 case Primitive::kPrimShort:
3415 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003416 case Primitive::kPrimInt: {
3417 Register lhs = locations->InAt(0).AsRegister<Register>();
3418 Register rhs = locations->InAt(1).AsRegister<Register>();
3419 __ Slt(TMP, lhs, rhs);
3420 __ Slt(res, rhs, lhs);
3421 __ Subu(res, res, TMP);
3422 break;
3423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003424 case Primitive::kPrimLong: {
3425 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003426 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3427 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3428 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3429 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3430 // TODO: more efficient (direct) comparison with a constant.
3431 __ Slt(TMP, lhs_high, rhs_high);
3432 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3433 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3434 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3435 __ Sltu(TMP, lhs_low, rhs_low);
3436 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3437 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3438 __ Bind(&done);
3439 break;
3440 }
3441
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003442 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003443 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003444 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3445 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3446 MipsLabel done;
3447 if (isR6) {
3448 __ CmpEqS(FTMP, lhs, rhs);
3449 __ LoadConst32(res, 0);
3450 __ Bc1nez(FTMP, &done);
3451 if (gt_bias) {
3452 __ CmpLtS(FTMP, lhs, rhs);
3453 __ LoadConst32(res, -1);
3454 __ Bc1nez(FTMP, &done);
3455 __ LoadConst32(res, 1);
3456 } else {
3457 __ CmpLtS(FTMP, rhs, lhs);
3458 __ LoadConst32(res, 1);
3459 __ Bc1nez(FTMP, &done);
3460 __ LoadConst32(res, -1);
3461 }
3462 } else {
3463 if (gt_bias) {
3464 __ ColtS(0, lhs, rhs);
3465 __ LoadConst32(res, -1);
3466 __ Bc1t(0, &done);
3467 __ CeqS(0, lhs, rhs);
3468 __ LoadConst32(res, 1);
3469 __ Movt(res, ZERO, 0);
3470 } else {
3471 __ ColtS(0, rhs, lhs);
3472 __ LoadConst32(res, 1);
3473 __ Bc1t(0, &done);
3474 __ CeqS(0, lhs, rhs);
3475 __ LoadConst32(res, -1);
3476 __ Movt(res, ZERO, 0);
3477 }
3478 }
3479 __ Bind(&done);
3480 break;
3481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003483 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003484 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3485 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3486 MipsLabel done;
3487 if (isR6) {
3488 __ CmpEqD(FTMP, lhs, rhs);
3489 __ LoadConst32(res, 0);
3490 __ Bc1nez(FTMP, &done);
3491 if (gt_bias) {
3492 __ CmpLtD(FTMP, lhs, rhs);
3493 __ LoadConst32(res, -1);
3494 __ Bc1nez(FTMP, &done);
3495 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003497 __ CmpLtD(FTMP, rhs, lhs);
3498 __ LoadConst32(res, 1);
3499 __ Bc1nez(FTMP, &done);
3500 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003501 }
3502 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003503 if (gt_bias) {
3504 __ ColtD(0, lhs, rhs);
3505 __ LoadConst32(res, -1);
3506 __ Bc1t(0, &done);
3507 __ CeqD(0, lhs, rhs);
3508 __ LoadConst32(res, 1);
3509 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003510 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003511 __ ColtD(0, rhs, lhs);
3512 __ LoadConst32(res, 1);
3513 __ Bc1t(0, &done);
3514 __ CeqD(0, lhs, rhs);
3515 __ LoadConst32(res, -1);
3516 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003517 }
3518 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003519 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003520 break;
3521 }
3522
3523 default:
3524 LOG(FATAL) << "Unimplemented compare type " << in_type;
3525 }
3526}
3527
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003528void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003530 switch (instruction->InputAt(0)->GetType()) {
3531 default:
3532 case Primitive::kPrimLong:
3533 locations->SetInAt(0, Location::RequiresRegister());
3534 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3535 break;
3536
3537 case Primitive::kPrimFloat:
3538 case Primitive::kPrimDouble:
3539 locations->SetInAt(0, Location::RequiresFpuRegister());
3540 locations->SetInAt(1, Location::RequiresFpuRegister());
3541 break;
3542 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003543 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3545 }
3546}
3547
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003548void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003549 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003550 return;
3551 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003553 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003554 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003555
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003556 switch (type) {
3557 default:
3558 // Integer case.
3559 GenerateIntCompare(instruction->GetCondition(), locations);
3560 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003561
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003562 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003563 GenerateLongCompare(instruction->GetCondition(), locations);
3564 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003565
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003566 case Primitive::kPrimFloat:
3567 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003568 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3569 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003570 }
3571}
3572
Alexey Frunze7e99e052015-11-24 19:28:01 -08003573void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3574 DCHECK(instruction->IsDiv() || instruction->IsRem());
3575 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3576
3577 LocationSummary* locations = instruction->GetLocations();
3578 Location second = locations->InAt(1);
3579 DCHECK(second.IsConstant());
3580
3581 Register out = locations->Out().AsRegister<Register>();
3582 Register dividend = locations->InAt(0).AsRegister<Register>();
3583 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3584 DCHECK(imm == 1 || imm == -1);
3585
3586 if (instruction->IsRem()) {
3587 __ Move(out, ZERO);
3588 } else {
3589 if (imm == -1) {
3590 __ Subu(out, ZERO, dividend);
3591 } else if (out != dividend) {
3592 __ Move(out, dividend);
3593 }
3594 }
3595}
3596
3597void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3598 DCHECK(instruction->IsDiv() || instruction->IsRem());
3599 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3600
3601 LocationSummary* locations = instruction->GetLocations();
3602 Location second = locations->InAt(1);
3603 DCHECK(second.IsConstant());
3604
3605 Register out = locations->Out().AsRegister<Register>();
3606 Register dividend = locations->InAt(0).AsRegister<Register>();
3607 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003608 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003609 int ctz_imm = CTZ(abs_imm);
3610
3611 if (instruction->IsDiv()) {
3612 if (ctz_imm == 1) {
3613 // Fast path for division by +/-2, which is very common.
3614 __ Srl(TMP, dividend, 31);
3615 } else {
3616 __ Sra(TMP, dividend, 31);
3617 __ Srl(TMP, TMP, 32 - ctz_imm);
3618 }
3619 __ Addu(out, dividend, TMP);
3620 __ Sra(out, out, ctz_imm);
3621 if (imm < 0) {
3622 __ Subu(out, ZERO, out);
3623 }
3624 } else {
3625 if (ctz_imm == 1) {
3626 // Fast path for modulo +/-2, which is very common.
3627 __ Sra(TMP, dividend, 31);
3628 __ Subu(out, dividend, TMP);
3629 __ Andi(out, out, 1);
3630 __ Addu(out, out, TMP);
3631 } else {
3632 __ Sra(TMP, dividend, 31);
3633 __ Srl(TMP, TMP, 32 - ctz_imm);
3634 __ Addu(out, dividend, TMP);
3635 if (IsUint<16>(abs_imm - 1)) {
3636 __ Andi(out, out, abs_imm - 1);
3637 } else {
3638 __ Sll(out, out, 32 - ctz_imm);
3639 __ Srl(out, out, 32 - ctz_imm);
3640 }
3641 __ Subu(out, out, TMP);
3642 }
3643 }
3644}
3645
3646void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3647 DCHECK(instruction->IsDiv() || instruction->IsRem());
3648 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3649
3650 LocationSummary* locations = instruction->GetLocations();
3651 Location second = locations->InAt(1);
3652 DCHECK(second.IsConstant());
3653
3654 Register out = locations->Out().AsRegister<Register>();
3655 Register dividend = locations->InAt(0).AsRegister<Register>();
3656 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3657
3658 int64_t magic;
3659 int shift;
3660 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3661
3662 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3663
3664 __ LoadConst32(TMP, magic);
3665 if (isR6) {
3666 __ MuhR6(TMP, dividend, TMP);
3667 } else {
3668 __ MultR2(dividend, TMP);
3669 __ Mfhi(TMP);
3670 }
3671 if (imm > 0 && magic < 0) {
3672 __ Addu(TMP, TMP, dividend);
3673 } else if (imm < 0 && magic > 0) {
3674 __ Subu(TMP, TMP, dividend);
3675 }
3676
3677 if (shift != 0) {
3678 __ Sra(TMP, TMP, shift);
3679 }
3680
3681 if (instruction->IsDiv()) {
3682 __ Sra(out, TMP, 31);
3683 __ Subu(out, TMP, out);
3684 } else {
3685 __ Sra(AT, TMP, 31);
3686 __ Subu(AT, TMP, AT);
3687 __ LoadConst32(TMP, imm);
3688 if (isR6) {
3689 __ MulR6(TMP, AT, TMP);
3690 } else {
3691 __ MulR2(TMP, AT, TMP);
3692 }
3693 __ Subu(out, dividend, TMP);
3694 }
3695}
3696
3697void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3698 DCHECK(instruction->IsDiv() || instruction->IsRem());
3699 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3700
3701 LocationSummary* locations = instruction->GetLocations();
3702 Register out = locations->Out().AsRegister<Register>();
3703 Location second = locations->InAt(1);
3704
3705 if (second.IsConstant()) {
3706 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3707 if (imm == 0) {
3708 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3709 } else if (imm == 1 || imm == -1) {
3710 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003711 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003712 DivRemByPowerOfTwo(instruction);
3713 } else {
3714 DCHECK(imm <= -2 || imm >= 2);
3715 GenerateDivRemWithAnyConstant(instruction);
3716 }
3717 } else {
3718 Register dividend = locations->InAt(0).AsRegister<Register>();
3719 Register divisor = second.AsRegister<Register>();
3720 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3721 if (instruction->IsDiv()) {
3722 if (isR6) {
3723 __ DivR6(out, dividend, divisor);
3724 } else {
3725 __ DivR2(out, dividend, divisor);
3726 }
3727 } else {
3728 if (isR6) {
3729 __ ModR6(out, dividend, divisor);
3730 } else {
3731 __ ModR2(out, dividend, divisor);
3732 }
3733 }
3734 }
3735}
3736
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003737void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3738 Primitive::Type type = div->GetResultType();
3739 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003740 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003741 : LocationSummary::kNoCall;
3742
3743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3744
3745 switch (type) {
3746 case Primitive::kPrimInt:
3747 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003748 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3750 break;
3751
3752 case Primitive::kPrimLong: {
3753 InvokeRuntimeCallingConvention calling_convention;
3754 locations->SetInAt(0, Location::RegisterPairLocation(
3755 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3756 locations->SetInAt(1, Location::RegisterPairLocation(
3757 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3758 locations->SetOut(calling_convention.GetReturnLocation(type));
3759 break;
3760 }
3761
3762 case Primitive::kPrimFloat:
3763 case Primitive::kPrimDouble:
3764 locations->SetInAt(0, Location::RequiresFpuRegister());
3765 locations->SetInAt(1, Location::RequiresFpuRegister());
3766 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3767 break;
3768
3769 default:
3770 LOG(FATAL) << "Unexpected div type " << type;
3771 }
3772}
3773
3774void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3775 Primitive::Type type = instruction->GetType();
3776 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003777
3778 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003779 case Primitive::kPrimInt:
3780 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003781 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003782 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003783 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003784 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3785 break;
3786 }
3787 case Primitive::kPrimFloat:
3788 case Primitive::kPrimDouble: {
3789 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3790 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3791 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3792 if (type == Primitive::kPrimFloat) {
3793 __ DivS(dst, lhs, rhs);
3794 } else {
3795 __ DivD(dst, lhs, rhs);
3796 }
3797 break;
3798 }
3799 default:
3800 LOG(FATAL) << "Unexpected div type " << type;
3801 }
3802}
3803
3804void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003805 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003806 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003807}
3808
3809void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3810 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3811 codegen_->AddSlowPath(slow_path);
3812 Location value = instruction->GetLocations()->InAt(0);
3813 Primitive::Type type = instruction->GetType();
3814
3815 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003816 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 case Primitive::kPrimByte:
3818 case Primitive::kPrimChar:
3819 case Primitive::kPrimShort:
3820 case Primitive::kPrimInt: {
3821 if (value.IsConstant()) {
3822 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3823 __ B(slow_path->GetEntryLabel());
3824 } else {
3825 // A division by a non-null constant is valid. We don't need to perform
3826 // any check, so simply fall through.
3827 }
3828 } else {
3829 DCHECK(value.IsRegister()) << value;
3830 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3831 }
3832 break;
3833 }
3834 case Primitive::kPrimLong: {
3835 if (value.IsConstant()) {
3836 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3837 __ B(slow_path->GetEntryLabel());
3838 } else {
3839 // A division by a non-null constant is valid. We don't need to perform
3840 // any check, so simply fall through.
3841 }
3842 } else {
3843 DCHECK(value.IsRegisterPair()) << value;
3844 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3845 __ Beqz(TMP, slow_path->GetEntryLabel());
3846 }
3847 break;
3848 }
3849 default:
3850 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3851 }
3852}
3853
3854void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3855 LocationSummary* locations =
3856 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3857 locations->SetOut(Location::ConstantLocation(constant));
3858}
3859
3860void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3861 // Will be generated at use site.
3862}
3863
3864void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3865 exit->SetLocations(nullptr);
3866}
3867
3868void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3869}
3870
3871void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3872 LocationSummary* locations =
3873 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3874 locations->SetOut(Location::ConstantLocation(constant));
3875}
3876
3877void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3878 // Will be generated at use site.
3879}
3880
3881void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3882 got->SetLocations(nullptr);
3883}
3884
3885void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3886 DCHECK(!successor->IsExitBlock());
3887 HBasicBlock* block = got->GetBlock();
3888 HInstruction* previous = got->GetPrevious();
3889 HLoopInformation* info = block->GetLoopInformation();
3890
3891 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3892 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3893 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3894 return;
3895 }
3896 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3897 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3898 }
3899 if (!codegen_->GoesToNextBlock(block, successor)) {
3900 __ B(codegen_->GetLabelOf(successor));
3901 }
3902}
3903
3904void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3905 HandleGoto(got, got->GetSuccessor());
3906}
3907
3908void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3909 try_boundary->SetLocations(nullptr);
3910}
3911
3912void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3913 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3914 if (!successor->IsExitBlock()) {
3915 HandleGoto(try_boundary, successor);
3916 }
3917}
3918
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003919void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3920 LocationSummary* locations) {
3921 Register dst = locations->Out().AsRegister<Register>();
3922 Register lhs = locations->InAt(0).AsRegister<Register>();
3923 Location rhs_location = locations->InAt(1);
3924 Register rhs_reg = ZERO;
3925 int64_t rhs_imm = 0;
3926 bool use_imm = rhs_location.IsConstant();
3927 if (use_imm) {
3928 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3929 } else {
3930 rhs_reg = rhs_location.AsRegister<Register>();
3931 }
3932
3933 switch (cond) {
3934 case kCondEQ:
3935 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003936 if (use_imm && IsInt<16>(-rhs_imm)) {
3937 if (rhs_imm == 0) {
3938 if (cond == kCondEQ) {
3939 __ Sltiu(dst, lhs, 1);
3940 } else {
3941 __ Sltu(dst, ZERO, lhs);
3942 }
3943 } else {
3944 __ Addiu(dst, lhs, -rhs_imm);
3945 if (cond == kCondEQ) {
3946 __ Sltiu(dst, dst, 1);
3947 } else {
3948 __ Sltu(dst, ZERO, dst);
3949 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003950 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003951 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003952 if (use_imm && IsUint<16>(rhs_imm)) {
3953 __ Xori(dst, lhs, rhs_imm);
3954 } else {
3955 if (use_imm) {
3956 rhs_reg = TMP;
3957 __ LoadConst32(rhs_reg, rhs_imm);
3958 }
3959 __ Xor(dst, lhs, rhs_reg);
3960 }
3961 if (cond == kCondEQ) {
3962 __ Sltiu(dst, dst, 1);
3963 } else {
3964 __ Sltu(dst, ZERO, dst);
3965 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003966 }
3967 break;
3968
3969 case kCondLT:
3970 case kCondGE:
3971 if (use_imm && IsInt<16>(rhs_imm)) {
3972 __ Slti(dst, lhs, rhs_imm);
3973 } else {
3974 if (use_imm) {
3975 rhs_reg = TMP;
3976 __ LoadConst32(rhs_reg, rhs_imm);
3977 }
3978 __ Slt(dst, lhs, rhs_reg);
3979 }
3980 if (cond == kCondGE) {
3981 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3982 // only the slt instruction but no sge.
3983 __ Xori(dst, dst, 1);
3984 }
3985 break;
3986
3987 case kCondLE:
3988 case kCondGT:
3989 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3990 // Simulate lhs <= rhs via lhs < rhs + 1.
3991 __ Slti(dst, lhs, rhs_imm + 1);
3992 if (cond == kCondGT) {
3993 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3994 // only the slti instruction but no sgti.
3995 __ Xori(dst, dst, 1);
3996 }
3997 } else {
3998 if (use_imm) {
3999 rhs_reg = TMP;
4000 __ LoadConst32(rhs_reg, rhs_imm);
4001 }
4002 __ Slt(dst, rhs_reg, lhs);
4003 if (cond == kCondLE) {
4004 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4005 // only the slt instruction but no sle.
4006 __ Xori(dst, dst, 1);
4007 }
4008 }
4009 break;
4010
4011 case kCondB:
4012 case kCondAE:
4013 if (use_imm && IsInt<16>(rhs_imm)) {
4014 // Sltiu sign-extends its 16-bit immediate operand before
4015 // the comparison and thus lets us compare directly with
4016 // unsigned values in the ranges [0, 0x7fff] and
4017 // [0xffff8000, 0xffffffff].
4018 __ Sltiu(dst, lhs, rhs_imm);
4019 } else {
4020 if (use_imm) {
4021 rhs_reg = TMP;
4022 __ LoadConst32(rhs_reg, rhs_imm);
4023 }
4024 __ Sltu(dst, lhs, rhs_reg);
4025 }
4026 if (cond == kCondAE) {
4027 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4028 // only the sltu instruction but no sgeu.
4029 __ Xori(dst, dst, 1);
4030 }
4031 break;
4032
4033 case kCondBE:
4034 case kCondA:
4035 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4036 // Simulate lhs <= rhs via lhs < rhs + 1.
4037 // Note that this only works if rhs + 1 does not overflow
4038 // to 0, hence the check above.
4039 // Sltiu sign-extends its 16-bit immediate operand before
4040 // the comparison and thus lets us compare directly with
4041 // unsigned values in the ranges [0, 0x7fff] and
4042 // [0xffff8000, 0xffffffff].
4043 __ Sltiu(dst, lhs, rhs_imm + 1);
4044 if (cond == kCondA) {
4045 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4046 // only the sltiu instruction but no sgtiu.
4047 __ Xori(dst, dst, 1);
4048 }
4049 } else {
4050 if (use_imm) {
4051 rhs_reg = TMP;
4052 __ LoadConst32(rhs_reg, rhs_imm);
4053 }
4054 __ Sltu(dst, rhs_reg, lhs);
4055 if (cond == kCondBE) {
4056 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4057 // only the sltu instruction but no sleu.
4058 __ Xori(dst, dst, 1);
4059 }
4060 }
4061 break;
4062 }
4063}
4064
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004065bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4066 LocationSummary* input_locations,
4067 Register dst) {
4068 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4069 Location rhs_location = input_locations->InAt(1);
4070 Register rhs_reg = ZERO;
4071 int64_t rhs_imm = 0;
4072 bool use_imm = rhs_location.IsConstant();
4073 if (use_imm) {
4074 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4075 } else {
4076 rhs_reg = rhs_location.AsRegister<Register>();
4077 }
4078
4079 switch (cond) {
4080 case kCondEQ:
4081 case kCondNE:
4082 if (use_imm && IsInt<16>(-rhs_imm)) {
4083 __ Addiu(dst, lhs, -rhs_imm);
4084 } else if (use_imm && IsUint<16>(rhs_imm)) {
4085 __ Xori(dst, lhs, rhs_imm);
4086 } else {
4087 if (use_imm) {
4088 rhs_reg = TMP;
4089 __ LoadConst32(rhs_reg, rhs_imm);
4090 }
4091 __ Xor(dst, lhs, rhs_reg);
4092 }
4093 return (cond == kCondEQ);
4094
4095 case kCondLT:
4096 case kCondGE:
4097 if (use_imm && IsInt<16>(rhs_imm)) {
4098 __ Slti(dst, lhs, rhs_imm);
4099 } else {
4100 if (use_imm) {
4101 rhs_reg = TMP;
4102 __ LoadConst32(rhs_reg, rhs_imm);
4103 }
4104 __ Slt(dst, lhs, rhs_reg);
4105 }
4106 return (cond == kCondGE);
4107
4108 case kCondLE:
4109 case kCondGT:
4110 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4111 // Simulate lhs <= rhs via lhs < rhs + 1.
4112 __ Slti(dst, lhs, rhs_imm + 1);
4113 return (cond == kCondGT);
4114 } else {
4115 if (use_imm) {
4116 rhs_reg = TMP;
4117 __ LoadConst32(rhs_reg, rhs_imm);
4118 }
4119 __ Slt(dst, rhs_reg, lhs);
4120 return (cond == kCondLE);
4121 }
4122
4123 case kCondB:
4124 case kCondAE:
4125 if (use_imm && IsInt<16>(rhs_imm)) {
4126 // Sltiu sign-extends its 16-bit immediate operand before
4127 // the comparison and thus lets us compare directly with
4128 // unsigned values in the ranges [0, 0x7fff] and
4129 // [0xffff8000, 0xffffffff].
4130 __ Sltiu(dst, lhs, rhs_imm);
4131 } else {
4132 if (use_imm) {
4133 rhs_reg = TMP;
4134 __ LoadConst32(rhs_reg, rhs_imm);
4135 }
4136 __ Sltu(dst, lhs, rhs_reg);
4137 }
4138 return (cond == kCondAE);
4139
4140 case kCondBE:
4141 case kCondA:
4142 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4143 // Simulate lhs <= rhs via lhs < rhs + 1.
4144 // Note that this only works if rhs + 1 does not overflow
4145 // to 0, hence the check above.
4146 // Sltiu sign-extends its 16-bit immediate operand before
4147 // the comparison and thus lets us compare directly with
4148 // unsigned values in the ranges [0, 0x7fff] and
4149 // [0xffff8000, 0xffffffff].
4150 __ Sltiu(dst, lhs, rhs_imm + 1);
4151 return (cond == kCondA);
4152 } else {
4153 if (use_imm) {
4154 rhs_reg = TMP;
4155 __ LoadConst32(rhs_reg, rhs_imm);
4156 }
4157 __ Sltu(dst, rhs_reg, lhs);
4158 return (cond == kCondBE);
4159 }
4160 }
4161}
4162
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004163void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4164 LocationSummary* locations,
4165 MipsLabel* label) {
4166 Register lhs = locations->InAt(0).AsRegister<Register>();
4167 Location rhs_location = locations->InAt(1);
4168 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004169 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004170 bool use_imm = rhs_location.IsConstant();
4171 if (use_imm) {
4172 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4173 } else {
4174 rhs_reg = rhs_location.AsRegister<Register>();
4175 }
4176
4177 if (use_imm && rhs_imm == 0) {
4178 switch (cond) {
4179 case kCondEQ:
4180 case kCondBE: // <= 0 if zero
4181 __ Beqz(lhs, label);
4182 break;
4183 case kCondNE:
4184 case kCondA: // > 0 if non-zero
4185 __ Bnez(lhs, label);
4186 break;
4187 case kCondLT:
4188 __ Bltz(lhs, label);
4189 break;
4190 case kCondGE:
4191 __ Bgez(lhs, label);
4192 break;
4193 case kCondLE:
4194 __ Blez(lhs, label);
4195 break;
4196 case kCondGT:
4197 __ Bgtz(lhs, label);
4198 break;
4199 case kCondB: // always false
4200 break;
4201 case kCondAE: // always true
4202 __ B(label);
4203 break;
4204 }
4205 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004206 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4207 if (isR6 || !use_imm) {
4208 if (use_imm) {
4209 rhs_reg = TMP;
4210 __ LoadConst32(rhs_reg, rhs_imm);
4211 }
4212 switch (cond) {
4213 case kCondEQ:
4214 __ Beq(lhs, rhs_reg, label);
4215 break;
4216 case kCondNE:
4217 __ Bne(lhs, rhs_reg, label);
4218 break;
4219 case kCondLT:
4220 __ Blt(lhs, rhs_reg, label);
4221 break;
4222 case kCondGE:
4223 __ Bge(lhs, rhs_reg, label);
4224 break;
4225 case kCondLE:
4226 __ Bge(rhs_reg, lhs, label);
4227 break;
4228 case kCondGT:
4229 __ Blt(rhs_reg, lhs, label);
4230 break;
4231 case kCondB:
4232 __ Bltu(lhs, rhs_reg, label);
4233 break;
4234 case kCondAE:
4235 __ Bgeu(lhs, rhs_reg, label);
4236 break;
4237 case kCondBE:
4238 __ Bgeu(rhs_reg, lhs, label);
4239 break;
4240 case kCondA:
4241 __ Bltu(rhs_reg, lhs, label);
4242 break;
4243 }
4244 } else {
4245 // Special cases for more efficient comparison with constants on R2.
4246 switch (cond) {
4247 case kCondEQ:
4248 __ LoadConst32(TMP, rhs_imm);
4249 __ Beq(lhs, TMP, label);
4250 break;
4251 case kCondNE:
4252 __ LoadConst32(TMP, rhs_imm);
4253 __ Bne(lhs, TMP, label);
4254 break;
4255 case kCondLT:
4256 if (IsInt<16>(rhs_imm)) {
4257 __ Slti(TMP, lhs, rhs_imm);
4258 __ Bnez(TMP, label);
4259 } else {
4260 __ LoadConst32(TMP, rhs_imm);
4261 __ Blt(lhs, TMP, label);
4262 }
4263 break;
4264 case kCondGE:
4265 if (IsInt<16>(rhs_imm)) {
4266 __ Slti(TMP, lhs, rhs_imm);
4267 __ Beqz(TMP, label);
4268 } else {
4269 __ LoadConst32(TMP, rhs_imm);
4270 __ Bge(lhs, TMP, label);
4271 }
4272 break;
4273 case kCondLE:
4274 if (IsInt<16>(rhs_imm + 1)) {
4275 // Simulate lhs <= rhs via lhs < rhs + 1.
4276 __ Slti(TMP, lhs, rhs_imm + 1);
4277 __ Bnez(TMP, label);
4278 } else {
4279 __ LoadConst32(TMP, rhs_imm);
4280 __ Bge(TMP, lhs, label);
4281 }
4282 break;
4283 case kCondGT:
4284 if (IsInt<16>(rhs_imm + 1)) {
4285 // Simulate lhs > rhs via !(lhs < rhs + 1).
4286 __ Slti(TMP, lhs, rhs_imm + 1);
4287 __ Beqz(TMP, label);
4288 } else {
4289 __ LoadConst32(TMP, rhs_imm);
4290 __ Blt(TMP, lhs, label);
4291 }
4292 break;
4293 case kCondB:
4294 if (IsInt<16>(rhs_imm)) {
4295 __ Sltiu(TMP, lhs, rhs_imm);
4296 __ Bnez(TMP, label);
4297 } else {
4298 __ LoadConst32(TMP, rhs_imm);
4299 __ Bltu(lhs, TMP, label);
4300 }
4301 break;
4302 case kCondAE:
4303 if (IsInt<16>(rhs_imm)) {
4304 __ Sltiu(TMP, lhs, rhs_imm);
4305 __ Beqz(TMP, label);
4306 } else {
4307 __ LoadConst32(TMP, rhs_imm);
4308 __ Bgeu(lhs, TMP, label);
4309 }
4310 break;
4311 case kCondBE:
4312 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4313 // Simulate lhs <= rhs via lhs < rhs + 1.
4314 // Note that this only works if rhs + 1 does not overflow
4315 // to 0, hence the check above.
4316 __ Sltiu(TMP, lhs, rhs_imm + 1);
4317 __ Bnez(TMP, label);
4318 } else {
4319 __ LoadConst32(TMP, rhs_imm);
4320 __ Bgeu(TMP, lhs, label);
4321 }
4322 break;
4323 case kCondA:
4324 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4325 // Simulate lhs > rhs via !(lhs < rhs + 1).
4326 // Note that this only works if rhs + 1 does not overflow
4327 // to 0, hence the check above.
4328 __ Sltiu(TMP, lhs, rhs_imm + 1);
4329 __ Beqz(TMP, label);
4330 } else {
4331 __ LoadConst32(TMP, rhs_imm);
4332 __ Bltu(TMP, lhs, label);
4333 }
4334 break;
4335 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004336 }
4337 }
4338}
4339
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004340void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4341 LocationSummary* locations) {
4342 Register dst = locations->Out().AsRegister<Register>();
4343 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4344 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4345 Location rhs_location = locations->InAt(1);
4346 Register rhs_high = ZERO;
4347 Register rhs_low = ZERO;
4348 int64_t imm = 0;
4349 uint32_t imm_high = 0;
4350 uint32_t imm_low = 0;
4351 bool use_imm = rhs_location.IsConstant();
4352 if (use_imm) {
4353 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4354 imm_high = High32Bits(imm);
4355 imm_low = Low32Bits(imm);
4356 } else {
4357 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4358 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4359 }
4360 if (use_imm && imm == 0) {
4361 switch (cond) {
4362 case kCondEQ:
4363 case kCondBE: // <= 0 if zero
4364 __ Or(dst, lhs_high, lhs_low);
4365 __ Sltiu(dst, dst, 1);
4366 break;
4367 case kCondNE:
4368 case kCondA: // > 0 if non-zero
4369 __ Or(dst, lhs_high, lhs_low);
4370 __ Sltu(dst, ZERO, dst);
4371 break;
4372 case kCondLT:
4373 __ Slt(dst, lhs_high, ZERO);
4374 break;
4375 case kCondGE:
4376 __ Slt(dst, lhs_high, ZERO);
4377 __ Xori(dst, dst, 1);
4378 break;
4379 case kCondLE:
4380 __ Or(TMP, lhs_high, lhs_low);
4381 __ Sra(AT, lhs_high, 31);
4382 __ Sltu(dst, AT, TMP);
4383 __ Xori(dst, dst, 1);
4384 break;
4385 case kCondGT:
4386 __ Or(TMP, lhs_high, lhs_low);
4387 __ Sra(AT, lhs_high, 31);
4388 __ Sltu(dst, AT, TMP);
4389 break;
4390 case kCondB: // always false
4391 __ Andi(dst, dst, 0);
4392 break;
4393 case kCondAE: // always true
4394 __ Ori(dst, ZERO, 1);
4395 break;
4396 }
4397 } else if (use_imm) {
4398 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4399 switch (cond) {
4400 case kCondEQ:
4401 __ LoadConst32(TMP, imm_high);
4402 __ Xor(TMP, TMP, lhs_high);
4403 __ LoadConst32(AT, imm_low);
4404 __ Xor(AT, AT, lhs_low);
4405 __ Or(dst, TMP, AT);
4406 __ Sltiu(dst, dst, 1);
4407 break;
4408 case kCondNE:
4409 __ LoadConst32(TMP, imm_high);
4410 __ Xor(TMP, TMP, lhs_high);
4411 __ LoadConst32(AT, imm_low);
4412 __ Xor(AT, AT, lhs_low);
4413 __ Or(dst, TMP, AT);
4414 __ Sltu(dst, ZERO, dst);
4415 break;
4416 case kCondLT:
4417 case kCondGE:
4418 if (dst == lhs_low) {
4419 __ LoadConst32(TMP, imm_low);
4420 __ Sltu(dst, lhs_low, TMP);
4421 }
4422 __ LoadConst32(TMP, imm_high);
4423 __ Slt(AT, lhs_high, TMP);
4424 __ Slt(TMP, TMP, lhs_high);
4425 if (dst != lhs_low) {
4426 __ LoadConst32(dst, imm_low);
4427 __ Sltu(dst, lhs_low, dst);
4428 }
4429 __ Slt(dst, TMP, dst);
4430 __ Or(dst, dst, AT);
4431 if (cond == kCondGE) {
4432 __ Xori(dst, dst, 1);
4433 }
4434 break;
4435 case kCondGT:
4436 case kCondLE:
4437 if (dst == lhs_low) {
4438 __ LoadConst32(TMP, imm_low);
4439 __ Sltu(dst, TMP, lhs_low);
4440 }
4441 __ LoadConst32(TMP, imm_high);
4442 __ Slt(AT, TMP, lhs_high);
4443 __ Slt(TMP, lhs_high, TMP);
4444 if (dst != lhs_low) {
4445 __ LoadConst32(dst, imm_low);
4446 __ Sltu(dst, dst, lhs_low);
4447 }
4448 __ Slt(dst, TMP, dst);
4449 __ Or(dst, dst, AT);
4450 if (cond == kCondLE) {
4451 __ Xori(dst, dst, 1);
4452 }
4453 break;
4454 case kCondB:
4455 case kCondAE:
4456 if (dst == lhs_low) {
4457 __ LoadConst32(TMP, imm_low);
4458 __ Sltu(dst, lhs_low, TMP);
4459 }
4460 __ LoadConst32(TMP, imm_high);
4461 __ Sltu(AT, lhs_high, TMP);
4462 __ Sltu(TMP, TMP, lhs_high);
4463 if (dst != lhs_low) {
4464 __ LoadConst32(dst, imm_low);
4465 __ Sltu(dst, lhs_low, dst);
4466 }
4467 __ Slt(dst, TMP, dst);
4468 __ Or(dst, dst, AT);
4469 if (cond == kCondAE) {
4470 __ Xori(dst, dst, 1);
4471 }
4472 break;
4473 case kCondA:
4474 case kCondBE:
4475 if (dst == lhs_low) {
4476 __ LoadConst32(TMP, imm_low);
4477 __ Sltu(dst, TMP, lhs_low);
4478 }
4479 __ LoadConst32(TMP, imm_high);
4480 __ Sltu(AT, TMP, lhs_high);
4481 __ Sltu(TMP, lhs_high, TMP);
4482 if (dst != lhs_low) {
4483 __ LoadConst32(dst, imm_low);
4484 __ Sltu(dst, dst, lhs_low);
4485 }
4486 __ Slt(dst, TMP, dst);
4487 __ Or(dst, dst, AT);
4488 if (cond == kCondBE) {
4489 __ Xori(dst, dst, 1);
4490 }
4491 break;
4492 }
4493 } else {
4494 switch (cond) {
4495 case kCondEQ:
4496 __ Xor(TMP, lhs_high, rhs_high);
4497 __ Xor(AT, lhs_low, rhs_low);
4498 __ Or(dst, TMP, AT);
4499 __ Sltiu(dst, dst, 1);
4500 break;
4501 case kCondNE:
4502 __ Xor(TMP, lhs_high, rhs_high);
4503 __ Xor(AT, lhs_low, rhs_low);
4504 __ Or(dst, TMP, AT);
4505 __ Sltu(dst, ZERO, dst);
4506 break;
4507 case kCondLT:
4508 case kCondGE:
4509 __ Slt(TMP, rhs_high, lhs_high);
4510 __ Sltu(AT, lhs_low, rhs_low);
4511 __ Slt(TMP, TMP, AT);
4512 __ Slt(AT, lhs_high, rhs_high);
4513 __ Or(dst, AT, TMP);
4514 if (cond == kCondGE) {
4515 __ Xori(dst, dst, 1);
4516 }
4517 break;
4518 case kCondGT:
4519 case kCondLE:
4520 __ Slt(TMP, lhs_high, rhs_high);
4521 __ Sltu(AT, rhs_low, lhs_low);
4522 __ Slt(TMP, TMP, AT);
4523 __ Slt(AT, rhs_high, lhs_high);
4524 __ Or(dst, AT, TMP);
4525 if (cond == kCondLE) {
4526 __ Xori(dst, dst, 1);
4527 }
4528 break;
4529 case kCondB:
4530 case kCondAE:
4531 __ Sltu(TMP, rhs_high, lhs_high);
4532 __ Sltu(AT, lhs_low, rhs_low);
4533 __ Slt(TMP, TMP, AT);
4534 __ Sltu(AT, lhs_high, rhs_high);
4535 __ Or(dst, AT, TMP);
4536 if (cond == kCondAE) {
4537 __ Xori(dst, dst, 1);
4538 }
4539 break;
4540 case kCondA:
4541 case kCondBE:
4542 __ Sltu(TMP, lhs_high, rhs_high);
4543 __ Sltu(AT, rhs_low, lhs_low);
4544 __ Slt(TMP, TMP, AT);
4545 __ Sltu(AT, rhs_high, lhs_high);
4546 __ Or(dst, AT, TMP);
4547 if (cond == kCondBE) {
4548 __ Xori(dst, dst, 1);
4549 }
4550 break;
4551 }
4552 }
4553}
4554
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004555void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4556 LocationSummary* locations,
4557 MipsLabel* label) {
4558 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4559 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4560 Location rhs_location = locations->InAt(1);
4561 Register rhs_high = ZERO;
4562 Register rhs_low = ZERO;
4563 int64_t imm = 0;
4564 uint32_t imm_high = 0;
4565 uint32_t imm_low = 0;
4566 bool use_imm = rhs_location.IsConstant();
4567 if (use_imm) {
4568 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4569 imm_high = High32Bits(imm);
4570 imm_low = Low32Bits(imm);
4571 } else {
4572 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4573 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4574 }
4575
4576 if (use_imm && imm == 0) {
4577 switch (cond) {
4578 case kCondEQ:
4579 case kCondBE: // <= 0 if zero
4580 __ Or(TMP, lhs_high, lhs_low);
4581 __ Beqz(TMP, label);
4582 break;
4583 case kCondNE:
4584 case kCondA: // > 0 if non-zero
4585 __ Or(TMP, lhs_high, lhs_low);
4586 __ Bnez(TMP, label);
4587 break;
4588 case kCondLT:
4589 __ Bltz(lhs_high, label);
4590 break;
4591 case kCondGE:
4592 __ Bgez(lhs_high, label);
4593 break;
4594 case kCondLE:
4595 __ Or(TMP, lhs_high, lhs_low);
4596 __ Sra(AT, lhs_high, 31);
4597 __ Bgeu(AT, TMP, label);
4598 break;
4599 case kCondGT:
4600 __ Or(TMP, lhs_high, lhs_low);
4601 __ Sra(AT, lhs_high, 31);
4602 __ Bltu(AT, TMP, label);
4603 break;
4604 case kCondB: // always false
4605 break;
4606 case kCondAE: // always true
4607 __ B(label);
4608 break;
4609 }
4610 } else if (use_imm) {
4611 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4612 switch (cond) {
4613 case kCondEQ:
4614 __ LoadConst32(TMP, imm_high);
4615 __ Xor(TMP, TMP, lhs_high);
4616 __ LoadConst32(AT, imm_low);
4617 __ Xor(AT, AT, lhs_low);
4618 __ Or(TMP, TMP, AT);
4619 __ Beqz(TMP, label);
4620 break;
4621 case kCondNE:
4622 __ LoadConst32(TMP, imm_high);
4623 __ Xor(TMP, TMP, lhs_high);
4624 __ LoadConst32(AT, imm_low);
4625 __ Xor(AT, AT, lhs_low);
4626 __ Or(TMP, TMP, AT);
4627 __ Bnez(TMP, label);
4628 break;
4629 case kCondLT:
4630 __ LoadConst32(TMP, imm_high);
4631 __ Blt(lhs_high, TMP, label);
4632 __ Slt(TMP, TMP, lhs_high);
4633 __ LoadConst32(AT, imm_low);
4634 __ Sltu(AT, lhs_low, AT);
4635 __ Blt(TMP, AT, label);
4636 break;
4637 case kCondGE:
4638 __ LoadConst32(TMP, imm_high);
4639 __ Blt(TMP, lhs_high, label);
4640 __ Slt(TMP, lhs_high, TMP);
4641 __ LoadConst32(AT, imm_low);
4642 __ Sltu(AT, lhs_low, AT);
4643 __ Or(TMP, TMP, AT);
4644 __ Beqz(TMP, label);
4645 break;
4646 case kCondLE:
4647 __ LoadConst32(TMP, imm_high);
4648 __ Blt(lhs_high, TMP, label);
4649 __ Slt(TMP, TMP, lhs_high);
4650 __ LoadConst32(AT, imm_low);
4651 __ Sltu(AT, AT, lhs_low);
4652 __ Or(TMP, TMP, AT);
4653 __ Beqz(TMP, label);
4654 break;
4655 case kCondGT:
4656 __ LoadConst32(TMP, imm_high);
4657 __ Blt(TMP, lhs_high, label);
4658 __ Slt(TMP, lhs_high, TMP);
4659 __ LoadConst32(AT, imm_low);
4660 __ Sltu(AT, AT, lhs_low);
4661 __ Blt(TMP, AT, label);
4662 break;
4663 case kCondB:
4664 __ LoadConst32(TMP, imm_high);
4665 __ Bltu(lhs_high, TMP, label);
4666 __ Sltu(TMP, TMP, lhs_high);
4667 __ LoadConst32(AT, imm_low);
4668 __ Sltu(AT, lhs_low, AT);
4669 __ Blt(TMP, AT, label);
4670 break;
4671 case kCondAE:
4672 __ LoadConst32(TMP, imm_high);
4673 __ Bltu(TMP, lhs_high, label);
4674 __ Sltu(TMP, lhs_high, TMP);
4675 __ LoadConst32(AT, imm_low);
4676 __ Sltu(AT, lhs_low, AT);
4677 __ Or(TMP, TMP, AT);
4678 __ Beqz(TMP, label);
4679 break;
4680 case kCondBE:
4681 __ LoadConst32(TMP, imm_high);
4682 __ Bltu(lhs_high, TMP, label);
4683 __ Sltu(TMP, TMP, lhs_high);
4684 __ LoadConst32(AT, imm_low);
4685 __ Sltu(AT, AT, lhs_low);
4686 __ Or(TMP, TMP, AT);
4687 __ Beqz(TMP, label);
4688 break;
4689 case kCondA:
4690 __ LoadConst32(TMP, imm_high);
4691 __ Bltu(TMP, lhs_high, label);
4692 __ Sltu(TMP, lhs_high, TMP);
4693 __ LoadConst32(AT, imm_low);
4694 __ Sltu(AT, AT, lhs_low);
4695 __ Blt(TMP, AT, label);
4696 break;
4697 }
4698 } else {
4699 switch (cond) {
4700 case kCondEQ:
4701 __ Xor(TMP, lhs_high, rhs_high);
4702 __ Xor(AT, lhs_low, rhs_low);
4703 __ Or(TMP, TMP, AT);
4704 __ Beqz(TMP, label);
4705 break;
4706 case kCondNE:
4707 __ Xor(TMP, lhs_high, rhs_high);
4708 __ Xor(AT, lhs_low, rhs_low);
4709 __ Or(TMP, TMP, AT);
4710 __ Bnez(TMP, label);
4711 break;
4712 case kCondLT:
4713 __ Blt(lhs_high, rhs_high, label);
4714 __ Slt(TMP, rhs_high, lhs_high);
4715 __ Sltu(AT, lhs_low, rhs_low);
4716 __ Blt(TMP, AT, label);
4717 break;
4718 case kCondGE:
4719 __ Blt(rhs_high, lhs_high, label);
4720 __ Slt(TMP, lhs_high, rhs_high);
4721 __ Sltu(AT, lhs_low, rhs_low);
4722 __ Or(TMP, TMP, AT);
4723 __ Beqz(TMP, label);
4724 break;
4725 case kCondLE:
4726 __ Blt(lhs_high, rhs_high, label);
4727 __ Slt(TMP, rhs_high, lhs_high);
4728 __ Sltu(AT, rhs_low, lhs_low);
4729 __ Or(TMP, TMP, AT);
4730 __ Beqz(TMP, label);
4731 break;
4732 case kCondGT:
4733 __ Blt(rhs_high, lhs_high, label);
4734 __ Slt(TMP, lhs_high, rhs_high);
4735 __ Sltu(AT, rhs_low, lhs_low);
4736 __ Blt(TMP, AT, label);
4737 break;
4738 case kCondB:
4739 __ Bltu(lhs_high, rhs_high, label);
4740 __ Sltu(TMP, rhs_high, lhs_high);
4741 __ Sltu(AT, lhs_low, rhs_low);
4742 __ Blt(TMP, AT, label);
4743 break;
4744 case kCondAE:
4745 __ Bltu(rhs_high, lhs_high, label);
4746 __ Sltu(TMP, lhs_high, rhs_high);
4747 __ Sltu(AT, lhs_low, rhs_low);
4748 __ Or(TMP, TMP, AT);
4749 __ Beqz(TMP, label);
4750 break;
4751 case kCondBE:
4752 __ Bltu(lhs_high, rhs_high, label);
4753 __ Sltu(TMP, rhs_high, lhs_high);
4754 __ Sltu(AT, rhs_low, lhs_low);
4755 __ Or(TMP, TMP, AT);
4756 __ Beqz(TMP, label);
4757 break;
4758 case kCondA:
4759 __ Bltu(rhs_high, lhs_high, label);
4760 __ Sltu(TMP, lhs_high, rhs_high);
4761 __ Sltu(AT, rhs_low, lhs_low);
4762 __ Blt(TMP, AT, label);
4763 break;
4764 }
4765 }
4766}
4767
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004768void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4769 bool gt_bias,
4770 Primitive::Type type,
4771 LocationSummary* locations) {
4772 Register dst = locations->Out().AsRegister<Register>();
4773 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4774 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4775 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4776 if (type == Primitive::kPrimFloat) {
4777 if (isR6) {
4778 switch (cond) {
4779 case kCondEQ:
4780 __ CmpEqS(FTMP, lhs, rhs);
4781 __ Mfc1(dst, FTMP);
4782 __ Andi(dst, dst, 1);
4783 break;
4784 case kCondNE:
4785 __ CmpEqS(FTMP, lhs, rhs);
4786 __ Mfc1(dst, FTMP);
4787 __ Addiu(dst, dst, 1);
4788 break;
4789 case kCondLT:
4790 if (gt_bias) {
4791 __ CmpLtS(FTMP, lhs, rhs);
4792 } else {
4793 __ CmpUltS(FTMP, lhs, rhs);
4794 }
4795 __ Mfc1(dst, FTMP);
4796 __ Andi(dst, dst, 1);
4797 break;
4798 case kCondLE:
4799 if (gt_bias) {
4800 __ CmpLeS(FTMP, lhs, rhs);
4801 } else {
4802 __ CmpUleS(FTMP, lhs, rhs);
4803 }
4804 __ Mfc1(dst, FTMP);
4805 __ Andi(dst, dst, 1);
4806 break;
4807 case kCondGT:
4808 if (gt_bias) {
4809 __ CmpUltS(FTMP, rhs, lhs);
4810 } else {
4811 __ CmpLtS(FTMP, rhs, lhs);
4812 }
4813 __ Mfc1(dst, FTMP);
4814 __ Andi(dst, dst, 1);
4815 break;
4816 case kCondGE:
4817 if (gt_bias) {
4818 __ CmpUleS(FTMP, rhs, lhs);
4819 } else {
4820 __ CmpLeS(FTMP, rhs, lhs);
4821 }
4822 __ Mfc1(dst, FTMP);
4823 __ Andi(dst, dst, 1);
4824 break;
4825 default:
4826 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4827 UNREACHABLE();
4828 }
4829 } else {
4830 switch (cond) {
4831 case kCondEQ:
4832 __ CeqS(0, lhs, rhs);
4833 __ LoadConst32(dst, 1);
4834 __ Movf(dst, ZERO, 0);
4835 break;
4836 case kCondNE:
4837 __ CeqS(0, lhs, rhs);
4838 __ LoadConst32(dst, 1);
4839 __ Movt(dst, ZERO, 0);
4840 break;
4841 case kCondLT:
4842 if (gt_bias) {
4843 __ ColtS(0, lhs, rhs);
4844 } else {
4845 __ CultS(0, lhs, rhs);
4846 }
4847 __ LoadConst32(dst, 1);
4848 __ Movf(dst, ZERO, 0);
4849 break;
4850 case kCondLE:
4851 if (gt_bias) {
4852 __ ColeS(0, lhs, rhs);
4853 } else {
4854 __ CuleS(0, lhs, rhs);
4855 }
4856 __ LoadConst32(dst, 1);
4857 __ Movf(dst, ZERO, 0);
4858 break;
4859 case kCondGT:
4860 if (gt_bias) {
4861 __ CultS(0, rhs, lhs);
4862 } else {
4863 __ ColtS(0, rhs, lhs);
4864 }
4865 __ LoadConst32(dst, 1);
4866 __ Movf(dst, ZERO, 0);
4867 break;
4868 case kCondGE:
4869 if (gt_bias) {
4870 __ CuleS(0, rhs, lhs);
4871 } else {
4872 __ ColeS(0, rhs, lhs);
4873 }
4874 __ LoadConst32(dst, 1);
4875 __ Movf(dst, ZERO, 0);
4876 break;
4877 default:
4878 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4879 UNREACHABLE();
4880 }
4881 }
4882 } else {
4883 DCHECK_EQ(type, Primitive::kPrimDouble);
4884 if (isR6) {
4885 switch (cond) {
4886 case kCondEQ:
4887 __ CmpEqD(FTMP, lhs, rhs);
4888 __ Mfc1(dst, FTMP);
4889 __ Andi(dst, dst, 1);
4890 break;
4891 case kCondNE:
4892 __ CmpEqD(FTMP, lhs, rhs);
4893 __ Mfc1(dst, FTMP);
4894 __ Addiu(dst, dst, 1);
4895 break;
4896 case kCondLT:
4897 if (gt_bias) {
4898 __ CmpLtD(FTMP, lhs, rhs);
4899 } else {
4900 __ CmpUltD(FTMP, lhs, rhs);
4901 }
4902 __ Mfc1(dst, FTMP);
4903 __ Andi(dst, dst, 1);
4904 break;
4905 case kCondLE:
4906 if (gt_bias) {
4907 __ CmpLeD(FTMP, lhs, rhs);
4908 } else {
4909 __ CmpUleD(FTMP, lhs, rhs);
4910 }
4911 __ Mfc1(dst, FTMP);
4912 __ Andi(dst, dst, 1);
4913 break;
4914 case kCondGT:
4915 if (gt_bias) {
4916 __ CmpUltD(FTMP, rhs, lhs);
4917 } else {
4918 __ CmpLtD(FTMP, rhs, lhs);
4919 }
4920 __ Mfc1(dst, FTMP);
4921 __ Andi(dst, dst, 1);
4922 break;
4923 case kCondGE:
4924 if (gt_bias) {
4925 __ CmpUleD(FTMP, rhs, lhs);
4926 } else {
4927 __ CmpLeD(FTMP, rhs, lhs);
4928 }
4929 __ Mfc1(dst, FTMP);
4930 __ Andi(dst, dst, 1);
4931 break;
4932 default:
4933 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4934 UNREACHABLE();
4935 }
4936 } else {
4937 switch (cond) {
4938 case kCondEQ:
4939 __ CeqD(0, lhs, rhs);
4940 __ LoadConst32(dst, 1);
4941 __ Movf(dst, ZERO, 0);
4942 break;
4943 case kCondNE:
4944 __ CeqD(0, lhs, rhs);
4945 __ LoadConst32(dst, 1);
4946 __ Movt(dst, ZERO, 0);
4947 break;
4948 case kCondLT:
4949 if (gt_bias) {
4950 __ ColtD(0, lhs, rhs);
4951 } else {
4952 __ CultD(0, lhs, rhs);
4953 }
4954 __ LoadConst32(dst, 1);
4955 __ Movf(dst, ZERO, 0);
4956 break;
4957 case kCondLE:
4958 if (gt_bias) {
4959 __ ColeD(0, lhs, rhs);
4960 } else {
4961 __ CuleD(0, lhs, rhs);
4962 }
4963 __ LoadConst32(dst, 1);
4964 __ Movf(dst, ZERO, 0);
4965 break;
4966 case kCondGT:
4967 if (gt_bias) {
4968 __ CultD(0, rhs, lhs);
4969 } else {
4970 __ ColtD(0, rhs, lhs);
4971 }
4972 __ LoadConst32(dst, 1);
4973 __ Movf(dst, ZERO, 0);
4974 break;
4975 case kCondGE:
4976 if (gt_bias) {
4977 __ CuleD(0, rhs, lhs);
4978 } else {
4979 __ ColeD(0, rhs, lhs);
4980 }
4981 __ LoadConst32(dst, 1);
4982 __ Movf(dst, ZERO, 0);
4983 break;
4984 default:
4985 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4986 UNREACHABLE();
4987 }
4988 }
4989 }
4990}
4991
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004992bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4993 bool gt_bias,
4994 Primitive::Type type,
4995 LocationSummary* input_locations,
4996 int cc) {
4997 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4998 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4999 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5000 if (type == Primitive::kPrimFloat) {
5001 switch (cond) {
5002 case kCondEQ:
5003 __ CeqS(cc, lhs, rhs);
5004 return false;
5005 case kCondNE:
5006 __ CeqS(cc, lhs, rhs);
5007 return true;
5008 case kCondLT:
5009 if (gt_bias) {
5010 __ ColtS(cc, lhs, rhs);
5011 } else {
5012 __ CultS(cc, lhs, rhs);
5013 }
5014 return false;
5015 case kCondLE:
5016 if (gt_bias) {
5017 __ ColeS(cc, lhs, rhs);
5018 } else {
5019 __ CuleS(cc, lhs, rhs);
5020 }
5021 return false;
5022 case kCondGT:
5023 if (gt_bias) {
5024 __ CultS(cc, rhs, lhs);
5025 } else {
5026 __ ColtS(cc, rhs, lhs);
5027 }
5028 return false;
5029 case kCondGE:
5030 if (gt_bias) {
5031 __ CuleS(cc, rhs, lhs);
5032 } else {
5033 __ ColeS(cc, rhs, lhs);
5034 }
5035 return false;
5036 default:
5037 LOG(FATAL) << "Unexpected non-floating-point condition";
5038 UNREACHABLE();
5039 }
5040 } else {
5041 DCHECK_EQ(type, Primitive::kPrimDouble);
5042 switch (cond) {
5043 case kCondEQ:
5044 __ CeqD(cc, lhs, rhs);
5045 return false;
5046 case kCondNE:
5047 __ CeqD(cc, lhs, rhs);
5048 return true;
5049 case kCondLT:
5050 if (gt_bias) {
5051 __ ColtD(cc, lhs, rhs);
5052 } else {
5053 __ CultD(cc, lhs, rhs);
5054 }
5055 return false;
5056 case kCondLE:
5057 if (gt_bias) {
5058 __ ColeD(cc, lhs, rhs);
5059 } else {
5060 __ CuleD(cc, lhs, rhs);
5061 }
5062 return false;
5063 case kCondGT:
5064 if (gt_bias) {
5065 __ CultD(cc, rhs, lhs);
5066 } else {
5067 __ ColtD(cc, rhs, lhs);
5068 }
5069 return false;
5070 case kCondGE:
5071 if (gt_bias) {
5072 __ CuleD(cc, rhs, lhs);
5073 } else {
5074 __ ColeD(cc, rhs, lhs);
5075 }
5076 return false;
5077 default:
5078 LOG(FATAL) << "Unexpected non-floating-point condition";
5079 UNREACHABLE();
5080 }
5081 }
5082}
5083
5084bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5085 bool gt_bias,
5086 Primitive::Type type,
5087 LocationSummary* input_locations,
5088 FRegister dst) {
5089 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5090 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5091 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
5092 if (type == Primitive::kPrimFloat) {
5093 switch (cond) {
5094 case kCondEQ:
5095 __ CmpEqS(dst, lhs, rhs);
5096 return false;
5097 case kCondNE:
5098 __ CmpEqS(dst, lhs, rhs);
5099 return true;
5100 case kCondLT:
5101 if (gt_bias) {
5102 __ CmpLtS(dst, lhs, rhs);
5103 } else {
5104 __ CmpUltS(dst, lhs, rhs);
5105 }
5106 return false;
5107 case kCondLE:
5108 if (gt_bias) {
5109 __ CmpLeS(dst, lhs, rhs);
5110 } else {
5111 __ CmpUleS(dst, lhs, rhs);
5112 }
5113 return false;
5114 case kCondGT:
5115 if (gt_bias) {
5116 __ CmpUltS(dst, rhs, lhs);
5117 } else {
5118 __ CmpLtS(dst, rhs, lhs);
5119 }
5120 return false;
5121 case kCondGE:
5122 if (gt_bias) {
5123 __ CmpUleS(dst, rhs, lhs);
5124 } else {
5125 __ CmpLeS(dst, rhs, lhs);
5126 }
5127 return false;
5128 default:
5129 LOG(FATAL) << "Unexpected non-floating-point condition";
5130 UNREACHABLE();
5131 }
5132 } else {
5133 DCHECK_EQ(type, Primitive::kPrimDouble);
5134 switch (cond) {
5135 case kCondEQ:
5136 __ CmpEqD(dst, lhs, rhs);
5137 return false;
5138 case kCondNE:
5139 __ CmpEqD(dst, lhs, rhs);
5140 return true;
5141 case kCondLT:
5142 if (gt_bias) {
5143 __ CmpLtD(dst, lhs, rhs);
5144 } else {
5145 __ CmpUltD(dst, lhs, rhs);
5146 }
5147 return false;
5148 case kCondLE:
5149 if (gt_bias) {
5150 __ CmpLeD(dst, lhs, rhs);
5151 } else {
5152 __ CmpUleD(dst, lhs, rhs);
5153 }
5154 return false;
5155 case kCondGT:
5156 if (gt_bias) {
5157 __ CmpUltD(dst, rhs, lhs);
5158 } else {
5159 __ CmpLtD(dst, rhs, lhs);
5160 }
5161 return false;
5162 case kCondGE:
5163 if (gt_bias) {
5164 __ CmpUleD(dst, rhs, lhs);
5165 } else {
5166 __ CmpLeD(dst, rhs, lhs);
5167 }
5168 return false;
5169 default:
5170 LOG(FATAL) << "Unexpected non-floating-point condition";
5171 UNREACHABLE();
5172 }
5173 }
5174}
5175
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005176void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5177 bool gt_bias,
5178 Primitive::Type type,
5179 LocationSummary* locations,
5180 MipsLabel* label) {
5181 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5182 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5183 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5184 if (type == Primitive::kPrimFloat) {
5185 if (isR6) {
5186 switch (cond) {
5187 case kCondEQ:
5188 __ CmpEqS(FTMP, lhs, rhs);
5189 __ Bc1nez(FTMP, label);
5190 break;
5191 case kCondNE:
5192 __ CmpEqS(FTMP, lhs, rhs);
5193 __ Bc1eqz(FTMP, label);
5194 break;
5195 case kCondLT:
5196 if (gt_bias) {
5197 __ CmpLtS(FTMP, lhs, rhs);
5198 } else {
5199 __ CmpUltS(FTMP, lhs, rhs);
5200 }
5201 __ Bc1nez(FTMP, label);
5202 break;
5203 case kCondLE:
5204 if (gt_bias) {
5205 __ CmpLeS(FTMP, lhs, rhs);
5206 } else {
5207 __ CmpUleS(FTMP, lhs, rhs);
5208 }
5209 __ Bc1nez(FTMP, label);
5210 break;
5211 case kCondGT:
5212 if (gt_bias) {
5213 __ CmpUltS(FTMP, rhs, lhs);
5214 } else {
5215 __ CmpLtS(FTMP, rhs, lhs);
5216 }
5217 __ Bc1nez(FTMP, label);
5218 break;
5219 case kCondGE:
5220 if (gt_bias) {
5221 __ CmpUleS(FTMP, rhs, lhs);
5222 } else {
5223 __ CmpLeS(FTMP, rhs, lhs);
5224 }
5225 __ Bc1nez(FTMP, label);
5226 break;
5227 default:
5228 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005229 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005230 }
5231 } else {
5232 switch (cond) {
5233 case kCondEQ:
5234 __ CeqS(0, lhs, rhs);
5235 __ Bc1t(0, label);
5236 break;
5237 case kCondNE:
5238 __ CeqS(0, lhs, rhs);
5239 __ Bc1f(0, label);
5240 break;
5241 case kCondLT:
5242 if (gt_bias) {
5243 __ ColtS(0, lhs, rhs);
5244 } else {
5245 __ CultS(0, lhs, rhs);
5246 }
5247 __ Bc1t(0, label);
5248 break;
5249 case kCondLE:
5250 if (gt_bias) {
5251 __ ColeS(0, lhs, rhs);
5252 } else {
5253 __ CuleS(0, lhs, rhs);
5254 }
5255 __ Bc1t(0, label);
5256 break;
5257 case kCondGT:
5258 if (gt_bias) {
5259 __ CultS(0, rhs, lhs);
5260 } else {
5261 __ ColtS(0, rhs, lhs);
5262 }
5263 __ Bc1t(0, label);
5264 break;
5265 case kCondGE:
5266 if (gt_bias) {
5267 __ CuleS(0, rhs, lhs);
5268 } else {
5269 __ ColeS(0, rhs, lhs);
5270 }
5271 __ Bc1t(0, label);
5272 break;
5273 default:
5274 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005275 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005276 }
5277 }
5278 } else {
5279 DCHECK_EQ(type, Primitive::kPrimDouble);
5280 if (isR6) {
5281 switch (cond) {
5282 case kCondEQ:
5283 __ CmpEqD(FTMP, lhs, rhs);
5284 __ Bc1nez(FTMP, label);
5285 break;
5286 case kCondNE:
5287 __ CmpEqD(FTMP, lhs, rhs);
5288 __ Bc1eqz(FTMP, label);
5289 break;
5290 case kCondLT:
5291 if (gt_bias) {
5292 __ CmpLtD(FTMP, lhs, rhs);
5293 } else {
5294 __ CmpUltD(FTMP, lhs, rhs);
5295 }
5296 __ Bc1nez(FTMP, label);
5297 break;
5298 case kCondLE:
5299 if (gt_bias) {
5300 __ CmpLeD(FTMP, lhs, rhs);
5301 } else {
5302 __ CmpUleD(FTMP, lhs, rhs);
5303 }
5304 __ Bc1nez(FTMP, label);
5305 break;
5306 case kCondGT:
5307 if (gt_bias) {
5308 __ CmpUltD(FTMP, rhs, lhs);
5309 } else {
5310 __ CmpLtD(FTMP, rhs, lhs);
5311 }
5312 __ Bc1nez(FTMP, label);
5313 break;
5314 case kCondGE:
5315 if (gt_bias) {
5316 __ CmpUleD(FTMP, rhs, lhs);
5317 } else {
5318 __ CmpLeD(FTMP, rhs, lhs);
5319 }
5320 __ Bc1nez(FTMP, label);
5321 break;
5322 default:
5323 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005324 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005325 }
5326 } else {
5327 switch (cond) {
5328 case kCondEQ:
5329 __ CeqD(0, lhs, rhs);
5330 __ Bc1t(0, label);
5331 break;
5332 case kCondNE:
5333 __ CeqD(0, lhs, rhs);
5334 __ Bc1f(0, label);
5335 break;
5336 case kCondLT:
5337 if (gt_bias) {
5338 __ ColtD(0, lhs, rhs);
5339 } else {
5340 __ CultD(0, lhs, rhs);
5341 }
5342 __ Bc1t(0, label);
5343 break;
5344 case kCondLE:
5345 if (gt_bias) {
5346 __ ColeD(0, lhs, rhs);
5347 } else {
5348 __ CuleD(0, lhs, rhs);
5349 }
5350 __ Bc1t(0, label);
5351 break;
5352 case kCondGT:
5353 if (gt_bias) {
5354 __ CultD(0, rhs, lhs);
5355 } else {
5356 __ ColtD(0, rhs, lhs);
5357 }
5358 __ Bc1t(0, label);
5359 break;
5360 case kCondGE:
5361 if (gt_bias) {
5362 __ CuleD(0, rhs, lhs);
5363 } else {
5364 __ ColeD(0, rhs, lhs);
5365 }
5366 __ Bc1t(0, label);
5367 break;
5368 default:
5369 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005370 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005371 }
5372 }
5373 }
5374}
5375
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005376void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005377 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005378 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005379 MipsLabel* false_target) {
5380 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005381
David Brazdil0debae72015-11-12 18:37:00 +00005382 if (true_target == nullptr && false_target == nullptr) {
5383 // Nothing to do. The code always falls through.
5384 return;
5385 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005386 // Constant condition, statically compared against "true" (integer value 1).
5387 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005388 if (true_target != nullptr) {
5389 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005390 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005392 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005393 if (false_target != nullptr) {
5394 __ B(false_target);
5395 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005396 }
David Brazdil0debae72015-11-12 18:37:00 +00005397 return;
5398 }
5399
5400 // The following code generates these patterns:
5401 // (1) true_target == nullptr && false_target != nullptr
5402 // - opposite condition true => branch to false_target
5403 // (2) true_target != nullptr && false_target == nullptr
5404 // - condition true => branch to true_target
5405 // (3) true_target != nullptr && false_target != nullptr
5406 // - condition true => branch to true_target
5407 // - branch to false_target
5408 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005409 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005410 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005411 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005412 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005413 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5414 } else {
5415 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005417 } else {
5418 // The condition instruction has not been materialized, use its inputs as
5419 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005420 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005421 Primitive::Type type = condition->InputAt(0)->GetType();
5422 LocationSummary* locations = cond->GetLocations();
5423 IfCondition if_cond = condition->GetCondition();
5424 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005425
David Brazdil0debae72015-11-12 18:37:00 +00005426 if (true_target == nullptr) {
5427 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005428 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005429 }
5430
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005431 switch (type) {
5432 default:
5433 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5434 break;
5435 case Primitive::kPrimLong:
5436 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5437 break;
5438 case Primitive::kPrimFloat:
5439 case Primitive::kPrimDouble:
5440 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5441 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005442 }
5443 }
David Brazdil0debae72015-11-12 18:37:00 +00005444
5445 // If neither branch falls through (case 3), the conditional branch to `true_target`
5446 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5447 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005448 __ B(false_target);
5449 }
5450}
5451
5452void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5453 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005454 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005455 locations->SetInAt(0, Location::RequiresRegister());
5456 }
5457}
5458
5459void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005460 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5461 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5462 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5463 nullptr : codegen_->GetLabelOf(true_successor);
5464 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5465 nullptr : codegen_->GetLabelOf(false_successor);
5466 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005467}
5468
5469void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5470 LocationSummary* locations = new (GetGraph()->GetArena())
5471 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005472 InvokeRuntimeCallingConvention calling_convention;
5473 RegisterSet caller_saves = RegisterSet::Empty();
5474 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5475 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005476 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005477 locations->SetInAt(0, Location::RequiresRegister());
5478 }
5479}
5480
5481void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005482 SlowPathCodeMIPS* slow_path =
5483 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005484 GenerateTestAndBranch(deoptimize,
5485 /* condition_input_index */ 0,
5486 slow_path->GetEntryLabel(),
5487 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005488}
5489
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005490// This function returns true if a conditional move can be generated for HSelect.
5491// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5492// branches and regular moves.
5493//
5494// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5495//
5496// While determining feasibility of a conditional move and setting inputs/outputs
5497// are two distinct tasks, this function does both because they share quite a bit
5498// of common logic.
5499static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5500 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5501 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5502 HCondition* condition = cond->AsCondition();
5503
5504 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5505 Primitive::Type dst_type = select->GetType();
5506
5507 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5508 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5509 bool is_true_value_zero_constant =
5510 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5511 bool is_false_value_zero_constant =
5512 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5513
5514 bool can_move_conditionally = false;
5515 bool use_const_for_false_in = false;
5516 bool use_const_for_true_in = false;
5517
5518 if (!cond->IsConstant()) {
5519 switch (cond_type) {
5520 default:
5521 switch (dst_type) {
5522 default:
5523 // Moving int on int condition.
5524 if (is_r6) {
5525 if (is_true_value_zero_constant) {
5526 // seleqz out_reg, false_reg, cond_reg
5527 can_move_conditionally = true;
5528 use_const_for_true_in = true;
5529 } else if (is_false_value_zero_constant) {
5530 // selnez out_reg, true_reg, cond_reg
5531 can_move_conditionally = true;
5532 use_const_for_false_in = true;
5533 } else if (materialized) {
5534 // Not materializing unmaterialized int conditions
5535 // to keep the instruction count low.
5536 // selnez AT, true_reg, cond_reg
5537 // seleqz TMP, false_reg, cond_reg
5538 // or out_reg, AT, TMP
5539 can_move_conditionally = true;
5540 }
5541 } else {
5542 // movn out_reg, true_reg/ZERO, cond_reg
5543 can_move_conditionally = true;
5544 use_const_for_true_in = is_true_value_zero_constant;
5545 }
5546 break;
5547 case Primitive::kPrimLong:
5548 // Moving long on int condition.
5549 if (is_r6) {
5550 if (is_true_value_zero_constant) {
5551 // seleqz out_reg_lo, false_reg_lo, cond_reg
5552 // seleqz out_reg_hi, false_reg_hi, cond_reg
5553 can_move_conditionally = true;
5554 use_const_for_true_in = true;
5555 } else if (is_false_value_zero_constant) {
5556 // selnez out_reg_lo, true_reg_lo, cond_reg
5557 // selnez out_reg_hi, true_reg_hi, cond_reg
5558 can_move_conditionally = true;
5559 use_const_for_false_in = true;
5560 }
5561 // Other long conditional moves would generate 6+ instructions,
5562 // which is too many.
5563 } else {
5564 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5565 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5566 can_move_conditionally = true;
5567 use_const_for_true_in = is_true_value_zero_constant;
5568 }
5569 break;
5570 case Primitive::kPrimFloat:
5571 case Primitive::kPrimDouble:
5572 // Moving float/double on int condition.
5573 if (is_r6) {
5574 if (materialized) {
5575 // Not materializing unmaterialized int conditions
5576 // to keep the instruction count low.
5577 can_move_conditionally = true;
5578 if (is_true_value_zero_constant) {
5579 // sltu TMP, ZERO, cond_reg
5580 // mtc1 TMP, temp_cond_reg
5581 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5582 use_const_for_true_in = true;
5583 } else if (is_false_value_zero_constant) {
5584 // sltu TMP, ZERO, cond_reg
5585 // mtc1 TMP, temp_cond_reg
5586 // selnez.fmt out_reg, true_reg, temp_cond_reg
5587 use_const_for_false_in = true;
5588 } else {
5589 // sltu TMP, ZERO, cond_reg
5590 // mtc1 TMP, temp_cond_reg
5591 // sel.fmt temp_cond_reg, false_reg, true_reg
5592 // mov.fmt out_reg, temp_cond_reg
5593 }
5594 }
5595 } else {
5596 // movn.fmt out_reg, true_reg, cond_reg
5597 can_move_conditionally = true;
5598 }
5599 break;
5600 }
5601 break;
5602 case Primitive::kPrimLong:
5603 // We don't materialize long comparison now
5604 // and use conditional branches instead.
5605 break;
5606 case Primitive::kPrimFloat:
5607 case Primitive::kPrimDouble:
5608 switch (dst_type) {
5609 default:
5610 // Moving int on float/double condition.
5611 if (is_r6) {
5612 if (is_true_value_zero_constant) {
5613 // mfc1 TMP, temp_cond_reg
5614 // seleqz out_reg, false_reg, TMP
5615 can_move_conditionally = true;
5616 use_const_for_true_in = true;
5617 } else if (is_false_value_zero_constant) {
5618 // mfc1 TMP, temp_cond_reg
5619 // selnez out_reg, true_reg, TMP
5620 can_move_conditionally = true;
5621 use_const_for_false_in = true;
5622 } else {
5623 // mfc1 TMP, temp_cond_reg
5624 // selnez AT, true_reg, TMP
5625 // seleqz TMP, false_reg, TMP
5626 // or out_reg, AT, TMP
5627 can_move_conditionally = true;
5628 }
5629 } else {
5630 // movt out_reg, true_reg/ZERO, cc
5631 can_move_conditionally = true;
5632 use_const_for_true_in = is_true_value_zero_constant;
5633 }
5634 break;
5635 case Primitive::kPrimLong:
5636 // Moving long on float/double condition.
5637 if (is_r6) {
5638 if (is_true_value_zero_constant) {
5639 // mfc1 TMP, temp_cond_reg
5640 // seleqz out_reg_lo, false_reg_lo, TMP
5641 // seleqz out_reg_hi, false_reg_hi, TMP
5642 can_move_conditionally = true;
5643 use_const_for_true_in = true;
5644 } else if (is_false_value_zero_constant) {
5645 // mfc1 TMP, temp_cond_reg
5646 // selnez out_reg_lo, true_reg_lo, TMP
5647 // selnez out_reg_hi, true_reg_hi, TMP
5648 can_move_conditionally = true;
5649 use_const_for_false_in = true;
5650 }
5651 // Other long conditional moves would generate 6+ instructions,
5652 // which is too many.
5653 } else {
5654 // movt out_reg_lo, true_reg_lo/ZERO, cc
5655 // movt out_reg_hi, true_reg_hi/ZERO, cc
5656 can_move_conditionally = true;
5657 use_const_for_true_in = is_true_value_zero_constant;
5658 }
5659 break;
5660 case Primitive::kPrimFloat:
5661 case Primitive::kPrimDouble:
5662 // Moving float/double on float/double condition.
5663 if (is_r6) {
5664 can_move_conditionally = true;
5665 if (is_true_value_zero_constant) {
5666 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5667 use_const_for_true_in = true;
5668 } else if (is_false_value_zero_constant) {
5669 // selnez.fmt out_reg, true_reg, temp_cond_reg
5670 use_const_for_false_in = true;
5671 } else {
5672 // sel.fmt temp_cond_reg, false_reg, true_reg
5673 // mov.fmt out_reg, temp_cond_reg
5674 }
5675 } else {
5676 // movt.fmt out_reg, true_reg, cc
5677 can_move_conditionally = true;
5678 }
5679 break;
5680 }
5681 break;
5682 }
5683 }
5684
5685 if (can_move_conditionally) {
5686 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5687 } else {
5688 DCHECK(!use_const_for_false_in);
5689 DCHECK(!use_const_for_true_in);
5690 }
5691
5692 if (locations_to_set != nullptr) {
5693 if (use_const_for_false_in) {
5694 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5695 } else {
5696 locations_to_set->SetInAt(0,
5697 Primitive::IsFloatingPointType(dst_type)
5698 ? Location::RequiresFpuRegister()
5699 : Location::RequiresRegister());
5700 }
5701 if (use_const_for_true_in) {
5702 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5703 } else {
5704 locations_to_set->SetInAt(1,
5705 Primitive::IsFloatingPointType(dst_type)
5706 ? Location::RequiresFpuRegister()
5707 : Location::RequiresRegister());
5708 }
5709 if (materialized) {
5710 locations_to_set->SetInAt(2, Location::RequiresRegister());
5711 }
5712 // On R6 we don't require the output to be the same as the
5713 // first input for conditional moves unlike on R2.
5714 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5715 if (is_out_same_as_first_in) {
5716 locations_to_set->SetOut(Location::SameAsFirstInput());
5717 } else {
5718 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5719 ? Location::RequiresFpuRegister()
5720 : Location::RequiresRegister());
5721 }
5722 }
5723
5724 return can_move_conditionally;
5725}
5726
5727void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5728 LocationSummary* locations = select->GetLocations();
5729 Location dst = locations->Out();
5730 Location src = locations->InAt(1);
5731 Register src_reg = ZERO;
5732 Register src_reg_high = ZERO;
5733 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5734 Register cond_reg = TMP;
5735 int cond_cc = 0;
5736 Primitive::Type cond_type = Primitive::kPrimInt;
5737 bool cond_inverted = false;
5738 Primitive::Type dst_type = select->GetType();
5739
5740 if (IsBooleanValueOrMaterializedCondition(cond)) {
5741 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5742 } else {
5743 HCondition* condition = cond->AsCondition();
5744 LocationSummary* cond_locations = cond->GetLocations();
5745 IfCondition if_cond = condition->GetCondition();
5746 cond_type = condition->InputAt(0)->GetType();
5747 switch (cond_type) {
5748 default:
5749 DCHECK_NE(cond_type, Primitive::kPrimLong);
5750 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5751 break;
5752 case Primitive::kPrimFloat:
5753 case Primitive::kPrimDouble:
5754 cond_inverted = MaterializeFpCompareR2(if_cond,
5755 condition->IsGtBias(),
5756 cond_type,
5757 cond_locations,
5758 cond_cc);
5759 break;
5760 }
5761 }
5762
5763 DCHECK(dst.Equals(locations->InAt(0)));
5764 if (src.IsRegister()) {
5765 src_reg = src.AsRegister<Register>();
5766 } else if (src.IsRegisterPair()) {
5767 src_reg = src.AsRegisterPairLow<Register>();
5768 src_reg_high = src.AsRegisterPairHigh<Register>();
5769 } else if (src.IsConstant()) {
5770 DCHECK(src.GetConstant()->IsZeroBitPattern());
5771 }
5772
5773 switch (cond_type) {
5774 default:
5775 switch (dst_type) {
5776 default:
5777 if (cond_inverted) {
5778 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5779 } else {
5780 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5781 }
5782 break;
5783 case Primitive::kPrimLong:
5784 if (cond_inverted) {
5785 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5786 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5787 } else {
5788 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5789 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5790 }
5791 break;
5792 case Primitive::kPrimFloat:
5793 if (cond_inverted) {
5794 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5795 } else {
5796 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5797 }
5798 break;
5799 case Primitive::kPrimDouble:
5800 if (cond_inverted) {
5801 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5802 } else {
5803 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5804 }
5805 break;
5806 }
5807 break;
5808 case Primitive::kPrimLong:
5809 LOG(FATAL) << "Unreachable";
5810 UNREACHABLE();
5811 case Primitive::kPrimFloat:
5812 case Primitive::kPrimDouble:
5813 switch (dst_type) {
5814 default:
5815 if (cond_inverted) {
5816 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5817 } else {
5818 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5819 }
5820 break;
5821 case Primitive::kPrimLong:
5822 if (cond_inverted) {
5823 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5824 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5825 } else {
5826 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5827 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5828 }
5829 break;
5830 case Primitive::kPrimFloat:
5831 if (cond_inverted) {
5832 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5833 } else {
5834 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5835 }
5836 break;
5837 case Primitive::kPrimDouble:
5838 if (cond_inverted) {
5839 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5840 } else {
5841 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5842 }
5843 break;
5844 }
5845 break;
5846 }
5847}
5848
5849void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5850 LocationSummary* locations = select->GetLocations();
5851 Location dst = locations->Out();
5852 Location false_src = locations->InAt(0);
5853 Location true_src = locations->InAt(1);
5854 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5855 Register cond_reg = TMP;
5856 FRegister fcond_reg = FTMP;
5857 Primitive::Type cond_type = Primitive::kPrimInt;
5858 bool cond_inverted = false;
5859 Primitive::Type dst_type = select->GetType();
5860
5861 if (IsBooleanValueOrMaterializedCondition(cond)) {
5862 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5863 } else {
5864 HCondition* condition = cond->AsCondition();
5865 LocationSummary* cond_locations = cond->GetLocations();
5866 IfCondition if_cond = condition->GetCondition();
5867 cond_type = condition->InputAt(0)->GetType();
5868 switch (cond_type) {
5869 default:
5870 DCHECK_NE(cond_type, Primitive::kPrimLong);
5871 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5872 break;
5873 case Primitive::kPrimFloat:
5874 case Primitive::kPrimDouble:
5875 cond_inverted = MaterializeFpCompareR6(if_cond,
5876 condition->IsGtBias(),
5877 cond_type,
5878 cond_locations,
5879 fcond_reg);
5880 break;
5881 }
5882 }
5883
5884 if (true_src.IsConstant()) {
5885 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5886 }
5887 if (false_src.IsConstant()) {
5888 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5889 }
5890
5891 switch (dst_type) {
5892 default:
5893 if (Primitive::IsFloatingPointType(cond_type)) {
5894 __ Mfc1(cond_reg, fcond_reg);
5895 }
5896 if (true_src.IsConstant()) {
5897 if (cond_inverted) {
5898 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5899 } else {
5900 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5901 }
5902 } else if (false_src.IsConstant()) {
5903 if (cond_inverted) {
5904 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5905 } else {
5906 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5907 }
5908 } else {
5909 DCHECK_NE(cond_reg, AT);
5910 if (cond_inverted) {
5911 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5912 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5913 } else {
5914 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5915 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5916 }
5917 __ Or(dst.AsRegister<Register>(), AT, TMP);
5918 }
5919 break;
5920 case Primitive::kPrimLong: {
5921 if (Primitive::IsFloatingPointType(cond_type)) {
5922 __ Mfc1(cond_reg, fcond_reg);
5923 }
5924 Register dst_lo = dst.AsRegisterPairLow<Register>();
5925 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5926 if (true_src.IsConstant()) {
5927 Register src_lo = false_src.AsRegisterPairLow<Register>();
5928 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5929 if (cond_inverted) {
5930 __ Selnez(dst_lo, src_lo, cond_reg);
5931 __ Selnez(dst_hi, src_hi, cond_reg);
5932 } else {
5933 __ Seleqz(dst_lo, src_lo, cond_reg);
5934 __ Seleqz(dst_hi, src_hi, cond_reg);
5935 }
5936 } else {
5937 DCHECK(false_src.IsConstant());
5938 Register src_lo = true_src.AsRegisterPairLow<Register>();
5939 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5940 if (cond_inverted) {
5941 __ Seleqz(dst_lo, src_lo, cond_reg);
5942 __ Seleqz(dst_hi, src_hi, cond_reg);
5943 } else {
5944 __ Selnez(dst_lo, src_lo, cond_reg);
5945 __ Selnez(dst_hi, src_hi, cond_reg);
5946 }
5947 }
5948 break;
5949 }
5950 case Primitive::kPrimFloat: {
5951 if (!Primitive::IsFloatingPointType(cond_type)) {
5952 // sel*.fmt tests bit 0 of the condition register, account for that.
5953 __ Sltu(TMP, ZERO, cond_reg);
5954 __ Mtc1(TMP, fcond_reg);
5955 }
5956 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5957 if (true_src.IsConstant()) {
5958 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5959 if (cond_inverted) {
5960 __ SelnezS(dst_reg, src_reg, fcond_reg);
5961 } else {
5962 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5963 }
5964 } else if (false_src.IsConstant()) {
5965 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5966 if (cond_inverted) {
5967 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5968 } else {
5969 __ SelnezS(dst_reg, src_reg, fcond_reg);
5970 }
5971 } else {
5972 if (cond_inverted) {
5973 __ SelS(fcond_reg,
5974 true_src.AsFpuRegister<FRegister>(),
5975 false_src.AsFpuRegister<FRegister>());
5976 } else {
5977 __ SelS(fcond_reg,
5978 false_src.AsFpuRegister<FRegister>(),
5979 true_src.AsFpuRegister<FRegister>());
5980 }
5981 __ MovS(dst_reg, fcond_reg);
5982 }
5983 break;
5984 }
5985 case Primitive::kPrimDouble: {
5986 if (!Primitive::IsFloatingPointType(cond_type)) {
5987 // sel*.fmt tests bit 0 of the condition register, account for that.
5988 __ Sltu(TMP, ZERO, cond_reg);
5989 __ Mtc1(TMP, fcond_reg);
5990 }
5991 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5992 if (true_src.IsConstant()) {
5993 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5994 if (cond_inverted) {
5995 __ SelnezD(dst_reg, src_reg, fcond_reg);
5996 } else {
5997 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5998 }
5999 } else if (false_src.IsConstant()) {
6000 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6001 if (cond_inverted) {
6002 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6003 } else {
6004 __ SelnezD(dst_reg, src_reg, fcond_reg);
6005 }
6006 } else {
6007 if (cond_inverted) {
6008 __ SelD(fcond_reg,
6009 true_src.AsFpuRegister<FRegister>(),
6010 false_src.AsFpuRegister<FRegister>());
6011 } else {
6012 __ SelD(fcond_reg,
6013 false_src.AsFpuRegister<FRegister>(),
6014 true_src.AsFpuRegister<FRegister>());
6015 }
6016 __ MovD(dst_reg, fcond_reg);
6017 }
6018 break;
6019 }
6020 }
6021}
6022
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006023void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6024 LocationSummary* locations = new (GetGraph()->GetArena())
6025 LocationSummary(flag, LocationSummary::kNoCall);
6026 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006027}
6028
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006029void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6030 __ LoadFromOffset(kLoadWord,
6031 flag->GetLocations()->Out().AsRegister<Register>(),
6032 SP,
6033 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006034}
6035
David Brazdil74eb1b22015-12-14 11:44:01 +00006036void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
6037 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006038 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006039}
6040
6041void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006042 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6043 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6044 if (is_r6) {
6045 GenConditionalMoveR6(select);
6046 } else {
6047 GenConditionalMoveR2(select);
6048 }
6049 } else {
6050 LocationSummary* locations = select->GetLocations();
6051 MipsLabel false_target;
6052 GenerateTestAndBranch(select,
6053 /* condition_input_index */ 2,
6054 /* true_target */ nullptr,
6055 &false_target);
6056 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6057 __ Bind(&false_target);
6058 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006059}
6060
David Srbecky0cf44932015-12-09 14:09:59 +00006061void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
6062 new (GetGraph()->GetArena()) LocationSummary(info);
6063}
6064
David Srbeckyd28f4a02016-03-14 17:14:24 +00006065void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6066 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006067}
6068
6069void CodeGeneratorMIPS::GenerateNop() {
6070 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006071}
6072
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006073void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
6074 Primitive::Type field_type = field_info.GetFieldType();
6075 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6076 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006077 bool object_field_get_with_read_barrier =
6078 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006080 instruction,
6081 generate_volatile
6082 ? LocationSummary::kCallOnMainOnly
6083 : (object_field_get_with_read_barrier
6084 ? LocationSummary::kCallOnSlowPath
6085 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006086
Alexey Frunzec61c0762017-04-10 13:54:23 -07006087 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6088 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6089 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006090 locations->SetInAt(0, Location::RequiresRegister());
6091 if (generate_volatile) {
6092 InvokeRuntimeCallingConvention calling_convention;
6093 // need A0 to hold base + offset
6094 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6095 if (field_type == Primitive::kPrimLong) {
6096 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6097 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006098 // Use Location::Any() to prevent situations when running out of available fp registers.
6099 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006100 // Need some temp core regs since FP results are returned in core registers
6101 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6102 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6103 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6104 }
6105 } else {
6106 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6107 locations->SetOut(Location::RequiresFpuRegister());
6108 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006109 // The output overlaps in the case of an object field get with
6110 // read barriers enabled: we do not want the move to overwrite the
6111 // object's location, as we need it to emit the read barrier.
6112 locations->SetOut(Location::RequiresRegister(),
6113 object_field_get_with_read_barrier
6114 ? Location::kOutputOverlap
6115 : Location::kNoOutputOverlap);
6116 }
6117 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6118 // We need a temporary register for the read barrier marking slow
6119 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006120 if (!kBakerReadBarrierThunksEnableForFields) {
6121 locations->AddTemp(Location::RequiresRegister());
6122 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006123 }
6124 }
6125}
6126
6127void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6128 const FieldInfo& field_info,
6129 uint32_t dex_pc) {
6130 Primitive::Type type = field_info.GetFieldType();
6131 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006132 Location obj_loc = locations->InAt(0);
6133 Register obj = obj_loc.AsRegister<Register>();
6134 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006135 LoadOperandType load_type = kLoadUnsignedByte;
6136 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006137 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006138 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006139
6140 switch (type) {
6141 case Primitive::kPrimBoolean:
6142 load_type = kLoadUnsignedByte;
6143 break;
6144 case Primitive::kPrimByte:
6145 load_type = kLoadSignedByte;
6146 break;
6147 case Primitive::kPrimShort:
6148 load_type = kLoadSignedHalfword;
6149 break;
6150 case Primitive::kPrimChar:
6151 load_type = kLoadUnsignedHalfword;
6152 break;
6153 case Primitive::kPrimInt:
6154 case Primitive::kPrimFloat:
6155 case Primitive::kPrimNot:
6156 load_type = kLoadWord;
6157 break;
6158 case Primitive::kPrimLong:
6159 case Primitive::kPrimDouble:
6160 load_type = kLoadDoubleword;
6161 break;
6162 case Primitive::kPrimVoid:
6163 LOG(FATAL) << "Unreachable type " << type;
6164 UNREACHABLE();
6165 }
6166
6167 if (is_volatile && load_type == kLoadDoubleword) {
6168 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006169 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006170 // Do implicit Null check
6171 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6172 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006173 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006174 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6175 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006176 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006177 if (dst_loc.IsFpuRegister()) {
6178 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006179 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006180 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006181 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006182 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006183 __ StoreToOffset(kStoreWord,
6184 locations->GetTemp(1).AsRegister<Register>(),
6185 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006186 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006187 __ StoreToOffset(kStoreWord,
6188 locations->GetTemp(2).AsRegister<Register>(),
6189 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006190 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006191 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006192 }
6193 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006194 if (type == Primitive::kPrimNot) {
6195 // /* HeapReference<Object> */ dst = *(obj + offset)
6196 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006197 Location temp_loc =
6198 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006199 // Note that a potential implicit null check is handled in this
6200 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6201 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6202 dst_loc,
6203 obj,
6204 offset,
6205 temp_loc,
6206 /* needs_null_check */ true);
6207 if (is_volatile) {
6208 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6209 }
6210 } else {
6211 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6212 if (is_volatile) {
6213 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6214 }
6215 // If read barriers are enabled, emit read barriers other than
6216 // Baker's using a slow path (and also unpoison the loaded
6217 // reference, if heap poisoning is enabled).
6218 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6219 }
6220 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 Register dst;
6222 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006223 DCHECK(dst_loc.IsRegisterPair());
6224 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006225 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006226 DCHECK(dst_loc.IsRegister());
6227 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006228 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006229 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006230 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006231 DCHECK(dst_loc.IsFpuRegister());
6232 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006233 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006234 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006236 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006237 }
6238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006239 }
6240
Alexey Frunze15958152017-02-09 19:08:30 -08006241 // Memory barriers, in the case of references, are handled in the
6242 // previous switch statement.
6243 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006244 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6245 }
6246}
6247
6248void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6249 Primitive::Type field_type = field_info.GetFieldType();
6250 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6251 bool generate_volatile = field_info.IsVolatile() && is_wide;
6252 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006253 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006254
6255 locations->SetInAt(0, Location::RequiresRegister());
6256 if (generate_volatile) {
6257 InvokeRuntimeCallingConvention calling_convention;
6258 // need A0 to hold base + offset
6259 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6260 if (field_type == Primitive::kPrimLong) {
6261 locations->SetInAt(1, Location::RegisterPairLocation(
6262 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6263 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006264 // Use Location::Any() to prevent situations when running out of available fp registers.
6265 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266 // Pass FP parameters in core registers.
6267 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6268 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6269 }
6270 } else {
6271 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006272 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006273 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006274 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006275 }
6276 }
6277}
6278
6279void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6280 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006281 uint32_t dex_pc,
6282 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006283 Primitive::Type type = field_info.GetFieldType();
6284 LocationSummary* locations = instruction->GetLocations();
6285 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006286 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006287 StoreOperandType store_type = kStoreByte;
6288 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006289 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006290 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006291 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006292
6293 switch (type) {
6294 case Primitive::kPrimBoolean:
6295 case Primitive::kPrimByte:
6296 store_type = kStoreByte;
6297 break;
6298 case Primitive::kPrimShort:
6299 case Primitive::kPrimChar:
6300 store_type = kStoreHalfword;
6301 break;
6302 case Primitive::kPrimInt:
6303 case Primitive::kPrimFloat:
6304 case Primitive::kPrimNot:
6305 store_type = kStoreWord;
6306 break;
6307 case Primitive::kPrimLong:
6308 case Primitive::kPrimDouble:
6309 store_type = kStoreDoubleword;
6310 break;
6311 case Primitive::kPrimVoid:
6312 LOG(FATAL) << "Unreachable type " << type;
6313 UNREACHABLE();
6314 }
6315
6316 if (is_volatile) {
6317 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6318 }
6319
6320 if (is_volatile && store_type == kStoreDoubleword) {
6321 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006322 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006323 // Do implicit Null check.
6324 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6325 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6326 if (type == Primitive::kPrimDouble) {
6327 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006328 if (value_location.IsFpuRegister()) {
6329 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6330 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006331 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006332 value_location.AsFpuRegister<FRegister>());
6333 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006334 __ LoadFromOffset(kLoadWord,
6335 locations->GetTemp(1).AsRegister<Register>(),
6336 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006337 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006338 __ LoadFromOffset(kLoadWord,
6339 locations->GetTemp(2).AsRegister<Register>(),
6340 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006341 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006342 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006343 DCHECK(value_location.IsConstant());
6344 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6345 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006346 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6347 locations->GetTemp(1).AsRegister<Register>(),
6348 value);
6349 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006350 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006351 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006352 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6353 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006354 if (value_location.IsConstant()) {
6355 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6356 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6357 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006358 Register src;
6359 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006360 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006361 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006362 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006363 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006364 if (kPoisonHeapReferences && needs_write_barrier) {
6365 // Note that in the case where `value` is a null reference,
6366 // we do not enter this block, as a null reference does not
6367 // need poisoning.
6368 DCHECK_EQ(type, Primitive::kPrimNot);
6369 __ PoisonHeapReference(TMP, src);
6370 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6371 } else {
6372 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6373 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006374 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006375 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006377 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006379 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 }
6381 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006382 }
6383
Alexey Frunzec061de12017-02-14 13:27:23 -08006384 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006385 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006386 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387 }
6388
6389 if (is_volatile) {
6390 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6391 }
6392}
6393
6394void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6395 HandleFieldGet(instruction, instruction->GetFieldInfo());
6396}
6397
6398void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6399 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6400}
6401
6402void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6403 HandleFieldSet(instruction, instruction->GetFieldInfo());
6404}
6405
6406void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006407 HandleFieldSet(instruction,
6408 instruction->GetFieldInfo(),
6409 instruction->GetDexPc(),
6410 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006411}
6412
Alexey Frunze15958152017-02-09 19:08:30 -08006413void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6414 HInstruction* instruction,
6415 Location out,
6416 uint32_t offset,
6417 Location maybe_temp,
6418 ReadBarrierOption read_barrier_option) {
6419 Register out_reg = out.AsRegister<Register>();
6420 if (read_barrier_option == kWithReadBarrier) {
6421 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006422 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6423 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6424 }
Alexey Frunze15958152017-02-09 19:08:30 -08006425 if (kUseBakerReadBarrier) {
6426 // Load with fast path based Baker's read barrier.
6427 // /* HeapReference<Object> */ out = *(out + offset)
6428 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6429 out,
6430 out_reg,
6431 offset,
6432 maybe_temp,
6433 /* needs_null_check */ false);
6434 } else {
6435 // Load with slow path based read barrier.
6436 // Save the value of `out` into `maybe_temp` before overwriting it
6437 // in the following move operation, as we will need it for the
6438 // read barrier below.
6439 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6440 // /* HeapReference<Object> */ out = *(out + offset)
6441 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6442 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6443 }
6444 } else {
6445 // Plain load with no read barrier.
6446 // /* HeapReference<Object> */ out = *(out + offset)
6447 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6448 __ MaybeUnpoisonHeapReference(out_reg);
6449 }
6450}
6451
6452void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6453 HInstruction* instruction,
6454 Location out,
6455 Location obj,
6456 uint32_t offset,
6457 Location maybe_temp,
6458 ReadBarrierOption read_barrier_option) {
6459 Register out_reg = out.AsRegister<Register>();
6460 Register obj_reg = obj.AsRegister<Register>();
6461 if (read_barrier_option == kWithReadBarrier) {
6462 CHECK(kEmitCompilerReadBarrier);
6463 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006464 if (!kBakerReadBarrierThunksEnableForFields) {
6465 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6466 }
Alexey Frunze15958152017-02-09 19:08:30 -08006467 // Load with fast path based Baker's read barrier.
6468 // /* HeapReference<Object> */ out = *(obj + offset)
6469 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6470 out,
6471 obj_reg,
6472 offset,
6473 maybe_temp,
6474 /* needs_null_check */ false);
6475 } else {
6476 // Load with slow path based read barrier.
6477 // /* HeapReference<Object> */ out = *(obj + offset)
6478 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6479 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6480 }
6481 } else {
6482 // Plain load with no read barrier.
6483 // /* HeapReference<Object> */ out = *(obj + offset)
6484 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6485 __ MaybeUnpoisonHeapReference(out_reg);
6486 }
6487}
6488
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006489static inline int GetBakerMarkThunkNumber(Register reg) {
6490 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6491 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6492 return reg - V0;
6493 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6494 return 14 + (reg - S2);
6495 } else if (reg == FP) { // One more.
6496 return 20;
6497 }
6498 LOG(FATAL) << "Unexpected register " << reg;
6499 UNREACHABLE();
6500}
6501
6502static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6503 int num = GetBakerMarkThunkNumber(reg) +
6504 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6505 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6506}
6507
6508static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6509 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6510 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6511}
6512
Alexey Frunze15958152017-02-09 19:08:30 -08006513void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6514 Location root,
6515 Register obj,
6516 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006517 ReadBarrierOption read_barrier_option,
6518 MipsLabel* label_low) {
6519 bool reordering;
6520 if (label_low != nullptr) {
6521 DCHECK_EQ(offset, 0x5678u);
6522 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006523 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006524 if (read_barrier_option == kWithReadBarrier) {
6525 DCHECK(kEmitCompilerReadBarrier);
6526 if (kUseBakerReadBarrier) {
6527 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6528 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006529 if (kBakerReadBarrierThunksEnableForGcRoots) {
6530 // Note that we do not actually check the value of `GetIsGcMarking()`
6531 // to decide whether to mark the loaded GC root or not. Instead, we
6532 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6533 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6534 // vice versa.
6535 //
6536 // We use thunks for the slow path. That thunk checks the reference
6537 // and jumps to the entrypoint if needed.
6538 //
6539 // temp = Thread::Current()->pReadBarrierMarkReg00
6540 // // AKA &art_quick_read_barrier_mark_introspection.
6541 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6542 // if (temp != nullptr) {
6543 // temp = &gc_root_thunk<root_reg>
6544 // root = temp(root)
6545 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006546
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006547 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6548 const int32_t entry_point_offset =
6549 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6550 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6551 int16_t offset_low = Low16Bits(offset);
6552 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6553 // extension in lw.
6554 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6555 Register base = short_offset ? obj : TMP;
6556 // Loading the entrypoint does not require a load acquire since it is only changed when
6557 // threads are suspended or running a checkpoint.
6558 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6559 reordering = __ SetReorder(false);
6560 if (!short_offset) {
6561 DCHECK(!label_low);
6562 __ AddUpper(base, obj, offset_high);
6563 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006564 MipsLabel skip_call;
6565 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006566 if (label_low != nullptr) {
6567 DCHECK(short_offset);
6568 __ Bind(label_low);
6569 }
6570 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6571 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6572 // in delay slot.
6573 if (isR6) {
6574 __ Jialc(T9, thunk_disp);
6575 } else {
6576 __ Addiu(T9, T9, thunk_disp);
6577 __ Jalr(T9);
6578 __ Nop();
6579 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006580 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006581 __ SetReorder(reordering);
6582 } else {
6583 // Note that we do not actually check the value of `GetIsGcMarking()`
6584 // to decide whether to mark the loaded GC root or not. Instead, we
6585 // load into `temp` (T9) the read barrier mark entry point corresponding
6586 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6587 // is false, and vice versa.
6588 //
6589 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6590 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6591 // if (temp != null) {
6592 // root = temp(root)
6593 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006594
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006595 if (label_low != nullptr) {
6596 reordering = __ SetReorder(false);
6597 __ Bind(label_low);
6598 }
6599 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6600 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6601 if (label_low != nullptr) {
6602 __ SetReorder(reordering);
6603 }
6604 static_assert(
6605 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6606 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6607 "have different sizes.");
6608 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6609 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6610 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006611
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006612 // Slow path marking the GC root `root`.
6613 Location temp = Location::RegisterLocation(T9);
6614 SlowPathCodeMIPS* slow_path =
6615 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6616 instruction,
6617 root,
6618 /*entrypoint*/ temp);
6619 codegen_->AddSlowPath(slow_path);
6620
6621 const int32_t entry_point_offset =
6622 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6623 // Loading the entrypoint does not require a load acquire since it is only changed when
6624 // threads are suspended or running a checkpoint.
6625 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6626 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6627 __ Bind(slow_path->GetExitLabel());
6628 }
Alexey Frunze15958152017-02-09 19:08:30 -08006629 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006630 if (label_low != nullptr) {
6631 reordering = __ SetReorder(false);
6632 __ Bind(label_low);
6633 }
Alexey Frunze15958152017-02-09 19:08:30 -08006634 // GC root loaded through a slow path for read barriers other
6635 // than Baker's.
6636 // /* GcRoot<mirror::Object>* */ root = obj + offset
6637 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006638 if (label_low != nullptr) {
6639 __ SetReorder(reordering);
6640 }
Alexey Frunze15958152017-02-09 19:08:30 -08006641 // /* mirror::Object* */ root = root->Read()
6642 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6643 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006644 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006645 if (label_low != nullptr) {
6646 reordering = __ SetReorder(false);
6647 __ Bind(label_low);
6648 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006649 // Plain GC root load with no read barrier.
6650 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6651 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6652 // Note that GC roots are not affected by heap poisoning, thus we
6653 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006654 if (label_low != nullptr) {
6655 __ SetReorder(reordering);
6656 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006657 }
6658}
6659
Alexey Frunze15958152017-02-09 19:08:30 -08006660void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6661 Location ref,
6662 Register obj,
6663 uint32_t offset,
6664 Location temp,
6665 bool needs_null_check) {
6666 DCHECK(kEmitCompilerReadBarrier);
6667 DCHECK(kUseBakerReadBarrier);
6668
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006669 if (kBakerReadBarrierThunksEnableForFields) {
6670 // Note that we do not actually check the value of `GetIsGcMarking()`
6671 // to decide whether to mark the loaded reference or not. Instead, we
6672 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6673 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6674 // vice versa.
6675 //
6676 // We use thunks for the slow path. That thunk checks the reference
6677 // and jumps to the entrypoint if needed. If the holder is not gray,
6678 // it issues a load-load memory barrier and returns to the original
6679 // reference load.
6680 //
6681 // temp = Thread::Current()->pReadBarrierMarkReg00
6682 // // AKA &art_quick_read_barrier_mark_introspection.
6683 // if (temp != nullptr) {
6684 // temp = &field_array_thunk<holder_reg>
6685 // temp()
6686 // }
6687 // not_gray_return_address:
6688 // // If the offset is too large to fit into the lw instruction, we
6689 // // use an adjusted base register (TMP) here. This register
6690 // // receives bits 16 ... 31 of the offset before the thunk invocation
6691 // // and the thunk benefits from it.
6692 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6693 // gray_return_address:
6694
6695 DCHECK(temp.IsInvalid());
6696 bool isR6 = GetInstructionSetFeatures().IsR6();
6697 int16_t offset_low = Low16Bits(offset);
6698 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6699 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6700 bool reordering = __ SetReorder(false);
6701 const int32_t entry_point_offset =
6702 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6703 // There may have or may have not been a null check if the field offset is smaller than
6704 // the page size.
6705 // There must've been a null check in case it's actually a load from an array.
6706 // We will, however, perform an explicit null check in the thunk as it's easier to
6707 // do it than not.
6708 if (instruction->IsArrayGet()) {
6709 DCHECK(!needs_null_check);
6710 }
6711 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6712 // Loading the entrypoint does not require a load acquire since it is only changed when
6713 // threads are suspended or running a checkpoint.
6714 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6715 Register ref_reg = ref.AsRegister<Register>();
6716 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006717 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006718 if (short_offset) {
6719 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006720 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006721 __ Nop(); // In forbidden slot.
6722 __ Jialc(T9, thunk_disp);
6723 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006724 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006725 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6726 __ Jalr(T9);
6727 __ Nop(); // In delay slot.
6728 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006729 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006730 } else {
6731 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006732 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006733 __ Aui(base, obj, offset_high); // In delay slot.
6734 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006735 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006736 } else {
6737 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006738 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006739 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6740 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006741 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006742 __ Addu(base, base, obj); // In delay slot.
6743 }
6744 }
6745 // /* HeapReference<Object> */ ref = *(obj + offset)
6746 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6747 if (needs_null_check) {
6748 MaybeRecordImplicitNullCheck(instruction);
6749 }
6750 __ MaybeUnpoisonHeapReference(ref_reg);
6751 __ SetReorder(reordering);
6752 return;
6753 }
6754
Alexey Frunze15958152017-02-09 19:08:30 -08006755 // /* HeapReference<Object> */ ref = *(obj + offset)
6756 Location no_index = Location::NoLocation();
6757 ScaleFactor no_scale_factor = TIMES_1;
6758 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6759 ref,
6760 obj,
6761 offset,
6762 no_index,
6763 no_scale_factor,
6764 temp,
6765 needs_null_check);
6766}
6767
6768void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6769 Location ref,
6770 Register obj,
6771 uint32_t data_offset,
6772 Location index,
6773 Location temp,
6774 bool needs_null_check) {
6775 DCHECK(kEmitCompilerReadBarrier);
6776 DCHECK(kUseBakerReadBarrier);
6777
6778 static_assert(
6779 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6780 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006781 ScaleFactor scale_factor = TIMES_4;
6782
6783 if (kBakerReadBarrierThunksEnableForArrays) {
6784 // Note that we do not actually check the value of `GetIsGcMarking()`
6785 // to decide whether to mark the loaded reference or not. Instead, we
6786 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6787 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6788 // vice versa.
6789 //
6790 // We use thunks for the slow path. That thunk checks the reference
6791 // and jumps to the entrypoint if needed. If the holder is not gray,
6792 // it issues a load-load memory barrier and returns to the original
6793 // reference load.
6794 //
6795 // temp = Thread::Current()->pReadBarrierMarkReg00
6796 // // AKA &art_quick_read_barrier_mark_introspection.
6797 // if (temp != nullptr) {
6798 // temp = &field_array_thunk<holder_reg>
6799 // temp()
6800 // }
6801 // not_gray_return_address:
6802 // // The element address is pre-calculated in the TMP register before the
6803 // // thunk invocation and the thunk benefits from it.
6804 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6805 // gray_return_address:
6806
6807 DCHECK(temp.IsInvalid());
6808 DCHECK(index.IsValid());
6809 bool reordering = __ SetReorder(false);
6810 const int32_t entry_point_offset =
6811 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6812 // We will not do the explicit null check in the thunk as some form of a null check
6813 // must've been done earlier.
6814 DCHECK(!needs_null_check);
6815 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6816 // Loading the entrypoint does not require a load acquire since it is only changed when
6817 // threads are suspended or running a checkpoint.
6818 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6819 Register ref_reg = ref.AsRegister<Register>();
6820 Register index_reg = index.IsRegisterPair()
6821 ? index.AsRegisterPairLow<Register>()
6822 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006823 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006824 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006825 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006826 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6827 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006828 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006829 } else {
6830 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006831 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006832 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6833 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006834 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006835 __ Addu(TMP, TMP, obj); // In delay slot.
6836 }
6837 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6838 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6839 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6840 __ MaybeUnpoisonHeapReference(ref_reg);
6841 __ SetReorder(reordering);
6842 return;
6843 }
6844
Alexey Frunze15958152017-02-09 19:08:30 -08006845 // /* HeapReference<Object> */ ref =
6846 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006847 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6848 ref,
6849 obj,
6850 data_offset,
6851 index,
6852 scale_factor,
6853 temp,
6854 needs_null_check);
6855}
6856
6857void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6858 Location ref,
6859 Register obj,
6860 uint32_t offset,
6861 Location index,
6862 ScaleFactor scale_factor,
6863 Location temp,
6864 bool needs_null_check,
6865 bool always_update_field) {
6866 DCHECK(kEmitCompilerReadBarrier);
6867 DCHECK(kUseBakerReadBarrier);
6868
6869 // In slow path based read barriers, the read barrier call is
6870 // inserted after the original load. However, in fast path based
6871 // Baker's read barriers, we need to perform the load of
6872 // mirror::Object::monitor_ *before* the original reference load.
6873 // This load-load ordering is required by the read barrier.
6874 // The fast path/slow path (for Baker's algorithm) should look like:
6875 //
6876 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6877 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6878 // HeapReference<Object> ref = *src; // Original reference load.
6879 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6880 // if (is_gray) {
6881 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6882 // }
6883 //
6884 // Note: the original implementation in ReadBarrier::Barrier is
6885 // slightly more complex as it performs additional checks that we do
6886 // not do here for performance reasons.
6887
6888 Register ref_reg = ref.AsRegister<Register>();
6889 Register temp_reg = temp.AsRegister<Register>();
6890 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6891
6892 // /* int32_t */ monitor = obj->monitor_
6893 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6894 if (needs_null_check) {
6895 MaybeRecordImplicitNullCheck(instruction);
6896 }
6897 // /* LockWord */ lock_word = LockWord(monitor)
6898 static_assert(sizeof(LockWord) == sizeof(int32_t),
6899 "art::LockWord and int32_t have different sizes.");
6900
6901 __ Sync(0); // Barrier to prevent load-load reordering.
6902
6903 // The actual reference load.
6904 if (index.IsValid()) {
6905 // Load types involving an "index": ArrayGet,
6906 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6907 // intrinsics.
6908 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6909 if (index.IsConstant()) {
6910 size_t computed_offset =
6911 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6912 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6913 } else {
6914 // Handle the special case of the
6915 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6916 // intrinsics, which use a register pair as index ("long
6917 // offset"), of which only the low part contains data.
6918 Register index_reg = index.IsRegisterPair()
6919 ? index.AsRegisterPairLow<Register>()
6920 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006921 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006922 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6923 }
6924 } else {
6925 // /* HeapReference<Object> */ ref = *(obj + offset)
6926 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6927 }
6928
6929 // Object* ref = ref_addr->AsMirrorPtr()
6930 __ MaybeUnpoisonHeapReference(ref_reg);
6931
6932 // Slow path marking the object `ref` when it is gray.
6933 SlowPathCodeMIPS* slow_path;
6934 if (always_update_field) {
6935 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6936 // of the form `obj + field_offset`, where `obj` is a register and
6937 // `field_offset` is a register pair (of which only the lower half
6938 // is used). Thus `offset` and `scale_factor` above are expected
6939 // to be null in this code path.
6940 DCHECK_EQ(offset, 0u);
6941 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6942 slow_path = new (GetGraph()->GetArena())
6943 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6944 ref,
6945 obj,
6946 /* field_offset */ index,
6947 temp_reg);
6948 } else {
6949 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6950 }
6951 AddSlowPath(slow_path);
6952
6953 // if (rb_state == ReadBarrier::GrayState())
6954 // ref = ReadBarrier::Mark(ref);
6955 // Given the numeric representation, it's enough to check the low bit of the
6956 // rb_state. We do that by shifting the bit into the sign bit (31) and
6957 // performing a branch on less than zero.
6958 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6959 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6960 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6961 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6962 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6963 __ Bind(slow_path->GetExitLabel());
6964}
6965
6966void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6967 Location out,
6968 Location ref,
6969 Location obj,
6970 uint32_t offset,
6971 Location index) {
6972 DCHECK(kEmitCompilerReadBarrier);
6973
6974 // Insert a slow path based read barrier *after* the reference load.
6975 //
6976 // If heap poisoning is enabled, the unpoisoning of the loaded
6977 // reference will be carried out by the runtime within the slow
6978 // path.
6979 //
6980 // Note that `ref` currently does not get unpoisoned (when heap
6981 // poisoning is enabled), which is alright as the `ref` argument is
6982 // not used by the artReadBarrierSlow entry point.
6983 //
6984 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6985 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6986 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6987 AddSlowPath(slow_path);
6988
6989 __ B(slow_path->GetEntryLabel());
6990 __ Bind(slow_path->GetExitLabel());
6991}
6992
6993void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6994 Location out,
6995 Location ref,
6996 Location obj,
6997 uint32_t offset,
6998 Location index) {
6999 if (kEmitCompilerReadBarrier) {
7000 // Baker's read barriers shall be handled by the fast path
7001 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7002 DCHECK(!kUseBakerReadBarrier);
7003 // If heap poisoning is enabled, unpoisoning will be taken care of
7004 // by the runtime within the slow path.
7005 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7006 } else if (kPoisonHeapReferences) {
7007 __ UnpoisonHeapReference(out.AsRegister<Register>());
7008 }
7009}
7010
7011void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7012 Location out,
7013 Location root) {
7014 DCHECK(kEmitCompilerReadBarrier);
7015
7016 // Insert a slow path based read barrier *after* the GC root load.
7017 //
7018 // Note that GC roots are not affected by heap poisoning, so we do
7019 // not need to do anything special for this here.
7020 SlowPathCodeMIPS* slow_path =
7021 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
7022 AddSlowPath(slow_path);
7023
7024 __ B(slow_path->GetEntryLabel());
7025 __ Bind(slow_path->GetExitLabel());
7026}
7027
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007028void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007029 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7030 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007031 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007032 switch (type_check_kind) {
7033 case TypeCheckKind::kExactCheck:
7034 case TypeCheckKind::kAbstractClassCheck:
7035 case TypeCheckKind::kClassHierarchyCheck:
7036 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007037 call_kind =
7038 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007039 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007040 break;
7041 case TypeCheckKind::kArrayCheck:
7042 case TypeCheckKind::kUnresolvedCheck:
7043 case TypeCheckKind::kInterfaceCheck:
7044 call_kind = LocationSummary::kCallOnSlowPath;
7045 break;
7046 }
7047
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007049 if (baker_read_barrier_slow_path) {
7050 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7051 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007052 locations->SetInAt(0, Location::RequiresRegister());
7053 locations->SetInAt(1, Location::RequiresRegister());
7054 // The output does overlap inputs.
7055 // Note that TypeCheckSlowPathMIPS uses this register too.
7056 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007057 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007058}
7059
7060void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007061 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007062 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007063 Location obj_loc = locations->InAt(0);
7064 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007065 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007066 Location out_loc = locations->Out();
7067 Register out = out_loc.AsRegister<Register>();
7068 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7069 DCHECK_LE(num_temps, 1u);
7070 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007071 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7072 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7073 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7074 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007075 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007076 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007077
7078 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007079 // Avoid this check if we know `obj` is not null.
7080 if (instruction->MustDoNullCheck()) {
7081 __ Move(out, ZERO);
7082 __ Beqz(obj, &done);
7083 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007084
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007085 switch (type_check_kind) {
7086 case TypeCheckKind::kExactCheck: {
7087 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007088 GenerateReferenceLoadTwoRegisters(instruction,
7089 out_loc,
7090 obj_loc,
7091 class_offset,
7092 maybe_temp_loc,
7093 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007094 // Classes must be equal for the instanceof to succeed.
7095 __ Xor(out, out, cls);
7096 __ Sltiu(out, out, 1);
7097 break;
7098 }
7099
7100 case TypeCheckKind::kAbstractClassCheck: {
7101 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007102 GenerateReferenceLoadTwoRegisters(instruction,
7103 out_loc,
7104 obj_loc,
7105 class_offset,
7106 maybe_temp_loc,
7107 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007108 // If the class is abstract, we eagerly fetch the super class of the
7109 // object to avoid doing a comparison we know will fail.
7110 MipsLabel loop;
7111 __ Bind(&loop);
7112 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007113 GenerateReferenceLoadOneRegister(instruction,
7114 out_loc,
7115 super_offset,
7116 maybe_temp_loc,
7117 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007118 // If `out` is null, we use it for the result, and jump to `done`.
7119 __ Beqz(out, &done);
7120 __ Bne(out, cls, &loop);
7121 __ LoadConst32(out, 1);
7122 break;
7123 }
7124
7125 case TypeCheckKind::kClassHierarchyCheck: {
7126 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007127 GenerateReferenceLoadTwoRegisters(instruction,
7128 out_loc,
7129 obj_loc,
7130 class_offset,
7131 maybe_temp_loc,
7132 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007133 // Walk over the class hierarchy to find a match.
7134 MipsLabel loop, success;
7135 __ Bind(&loop);
7136 __ Beq(out, cls, &success);
7137 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007138 GenerateReferenceLoadOneRegister(instruction,
7139 out_loc,
7140 super_offset,
7141 maybe_temp_loc,
7142 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007143 __ Bnez(out, &loop);
7144 // If `out` is null, we use it for the result, and jump to `done`.
7145 __ B(&done);
7146 __ Bind(&success);
7147 __ LoadConst32(out, 1);
7148 break;
7149 }
7150
7151 case TypeCheckKind::kArrayObjectCheck: {
7152 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007153 GenerateReferenceLoadTwoRegisters(instruction,
7154 out_loc,
7155 obj_loc,
7156 class_offset,
7157 maybe_temp_loc,
7158 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007159 // Do an exact check.
7160 MipsLabel success;
7161 __ Beq(out, cls, &success);
7162 // Otherwise, we need to check that the object's class is a non-primitive array.
7163 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007164 GenerateReferenceLoadOneRegister(instruction,
7165 out_loc,
7166 component_offset,
7167 maybe_temp_loc,
7168 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007169 // If `out` is null, we use it for the result, and jump to `done`.
7170 __ Beqz(out, &done);
7171 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7172 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7173 __ Sltiu(out, out, 1);
7174 __ B(&done);
7175 __ Bind(&success);
7176 __ LoadConst32(out, 1);
7177 break;
7178 }
7179
7180 case TypeCheckKind::kArrayCheck: {
7181 // No read barrier since the slow path will retry upon failure.
7182 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007183 GenerateReferenceLoadTwoRegisters(instruction,
7184 out_loc,
7185 obj_loc,
7186 class_offset,
7187 maybe_temp_loc,
7188 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007189 DCHECK(locations->OnlyCallsOnSlowPath());
7190 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7191 /* is_fatal */ false);
7192 codegen_->AddSlowPath(slow_path);
7193 __ Bne(out, cls, slow_path->GetEntryLabel());
7194 __ LoadConst32(out, 1);
7195 break;
7196 }
7197
7198 case TypeCheckKind::kUnresolvedCheck:
7199 case TypeCheckKind::kInterfaceCheck: {
7200 // Note that we indeed only call on slow path, but we always go
7201 // into the slow path for the unresolved and interface check
7202 // cases.
7203 //
7204 // We cannot directly call the InstanceofNonTrivial runtime
7205 // entry point without resorting to a type checking slow path
7206 // here (i.e. by calling InvokeRuntime directly), as it would
7207 // require to assign fixed registers for the inputs of this
7208 // HInstanceOf instruction (following the runtime calling
7209 // convention), which might be cluttered by the potential first
7210 // read barrier emission at the beginning of this method.
7211 //
7212 // TODO: Introduce a new runtime entry point taking the object
7213 // to test (instead of its class) as argument, and let it deal
7214 // with the read barrier issues. This will let us refactor this
7215 // case of the `switch` code as it was previously (with a direct
7216 // call to the runtime not using a type checking slow path).
7217 // This should also be beneficial for the other cases above.
7218 DCHECK(locations->OnlyCallsOnSlowPath());
7219 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7220 /* is_fatal */ false);
7221 codegen_->AddSlowPath(slow_path);
7222 __ B(slow_path->GetEntryLabel());
7223 break;
7224 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007225 }
7226
7227 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007228
7229 if (slow_path != nullptr) {
7230 __ Bind(slow_path->GetExitLabel());
7231 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007232}
7233
7234void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
7235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7236 locations->SetOut(Location::ConstantLocation(constant));
7237}
7238
7239void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7240 // Will be generated at use site.
7241}
7242
7243void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
7244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7245 locations->SetOut(Location::ConstantLocation(constant));
7246}
7247
7248void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7249 // Will be generated at use site.
7250}
7251
7252void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7253 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7254 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7255}
7256
7257void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7258 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007259 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007260 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007261 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007262}
7263
7264void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7265 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7266 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007267 Location receiver = invoke->GetLocations()->InAt(0);
7268 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007269 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007270
7271 // Set the hidden argument.
7272 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7273 invoke->GetDexMethodIndex());
7274
7275 // temp = object->GetClass();
7276 if (receiver.IsStackSlot()) {
7277 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7278 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7279 } else {
7280 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7281 }
7282 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007283 // Instead of simply (possibly) unpoisoning `temp` here, we should
7284 // emit a read barrier for the previous class reference load.
7285 // However this is not required in practice, as this is an
7286 // intermediate/temporary reference and because the current
7287 // concurrent copying collector keeps the from-space memory
7288 // intact/accessible until the end of the marking phase (the
7289 // concurrent copying collector may not in the future).
7290 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007291 __ LoadFromOffset(kLoadWord, temp, temp,
7292 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7293 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007294 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007295 // temp = temp->GetImtEntryAt(method_offset);
7296 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7297 // T9 = temp->GetEntryPoint();
7298 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7299 // T9();
7300 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007301 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007302 DCHECK(!codegen_->IsLeafMethod());
7303 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7304}
7305
7306void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007307 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7308 if (intrinsic.TryDispatch(invoke)) {
7309 return;
7310 }
7311
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007312 HandleInvoke(invoke);
7313}
7314
7315void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007316 // Explicit clinit checks triggered by static invokes must have been pruned by
7317 // art::PrepareForRegisterAllocation.
7318 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007319
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007320 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007321 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007322
Chris Larsen701566a2015-10-27 15:29:13 -07007323 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7324 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007325 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7326 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7327 }
Chris Larsen701566a2015-10-27 15:29:13 -07007328 return;
7329 }
7330
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007331 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007332
7333 // Add the extra input register if either the dex cache array base register
7334 // or the PC-relative base register for accessing literals is needed.
7335 if (has_extra_input) {
7336 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007338}
7339
Orion Hodsonac141392017-01-13 11:53:47 +00007340void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7341 HandleInvoke(invoke);
7342}
7343
7344void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7345 codegen_->GenerateInvokePolymorphicCall(invoke);
7346}
7347
Chris Larsen701566a2015-10-27 15:29:13 -07007348static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007349 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007350 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7351 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007352 return true;
7353 }
7354 return false;
7355}
7356
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007357HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007358 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007359 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007360 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007361 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
Vladimir Markoaad75c62016-10-03 08:46:48 +00007362 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007363 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007364 bool is_r6 = GetInstructionSetFeatures().IsR6();
7365 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007366 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007367 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007368 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007369 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007370 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007371 case HLoadString::LoadKind::kBootImageAddress:
7372 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007373 case HLoadString::LoadKind::kJitTableAddress:
7374 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007375 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007376 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007377 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007378 fallback_load = false;
7379 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007380 }
7381 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007382 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007383 }
7384 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007385}
7386
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007387HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7388 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007389 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007390 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007391 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7392 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007393 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007394 bool is_r6 = GetInstructionSetFeatures().IsR6();
7395 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007396 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007397 case HLoadClass::LoadKind::kInvalid:
7398 LOG(FATAL) << "UNREACHABLE";
7399 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007400 case HLoadClass::LoadKind::kReferrersClass:
7401 fallback_load = false;
7402 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007403 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007404 case HLoadClass::LoadKind::kBssEntry:
7405 DCHECK(!Runtime::Current()->UseJitCompilation());
7406 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007407 case HLoadClass::LoadKind::kBootImageAddress:
7408 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007409 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007410 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007411 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007412 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007413 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007414 fallback_load = false;
7415 break;
7416 }
7417 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007418 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007419 }
7420 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007421}
7422
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007423Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7424 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007425 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007426 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7427 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7428 if (!invoke->GetLocations()->Intrinsified()) {
7429 return location.AsRegister<Register>();
7430 }
7431 // For intrinsics we allow any location, so it may be on the stack.
7432 if (!location.IsRegister()) {
7433 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7434 return temp;
7435 }
7436 // For register locations, check if the register was saved. If so, get it from the stack.
7437 // Note: There is a chance that the register was saved but not overwritten, so we could
7438 // save one load. However, since this is just an intrinsic slow path we prefer this
7439 // simple and more robust approach rather that trying to determine if that's the case.
7440 SlowPathCode* slow_path = GetCurrentSlowPath();
7441 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7442 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7443 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7444 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7445 return temp;
7446 }
7447 return location.AsRegister<Register>();
7448}
7449
Vladimir Markodc151b22015-10-15 18:02:30 +01007450HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7451 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007452 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007453 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007454 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007455 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007456 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7457 // with irreducible loops.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007458 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007459 bool is_r6 = GetInstructionSetFeatures().IsR6();
7460 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007461 switch (dispatch_info.method_load_kind) {
Vladimir Marko65979462017-05-19 17:25:12 +01007462 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007463 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007464 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007465 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007466 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007467 break;
7468 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007469 if (fallback_load) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007470 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007471 dispatch_info.method_load_data = 0;
7472 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007473 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007474}
7475
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007476void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7477 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007478 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007479 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007480 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7481 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007482 bool is_r6 = GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007483 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007484 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7485 : ZERO;
7486
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007487 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007488 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007489 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007490 uint32_t offset =
7491 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007492 __ LoadFromOffset(kLoadWord,
7493 temp.AsRegister<Register>(),
7494 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007495 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007496 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007497 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007498 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007499 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007500 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007501 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7502 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007503 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7504 PcRelativePatchInfo* info_low =
7505 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007506 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007507 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7508 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007509 break;
7510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007511 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7512 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7513 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007514 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007515 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007516 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007517 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7518 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007519 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007520 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7521 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007522 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007523 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007524 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7525 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7526 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007527 }
7528 }
7529
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007530 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007531 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007532 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007534 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7535 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007536 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007537 T9,
7538 callee_method.AsRegister<Register>(),
7539 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007540 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007541 // T9()
7542 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007543 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007544 break;
7545 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007546 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7547
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007548 DCHECK(!IsLeafMethod());
7549}
7550
7551void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007552 // Explicit clinit checks triggered by static invokes must have been pruned by
7553 // art::PrepareForRegisterAllocation.
7554 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007555
7556 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7557 return;
7558 }
7559
7560 LocationSummary* locations = invoke->GetLocations();
7561 codegen_->GenerateStaticOrDirectCall(invoke,
7562 locations->HasTemps()
7563 ? locations->GetTemp(0)
7564 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007565}
7566
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007567void CodeGeneratorMIPS::GenerateVirtualCall(
7568 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007569 // Use the calling convention instead of the location of the receiver, as
7570 // intrinsics may have put the receiver in a different register. In the intrinsics
7571 // slow path, the arguments have been moved to the right place, so here we are
7572 // guaranteed that the receiver is the first register of the calling convention.
7573 InvokeDexCallingConvention calling_convention;
7574 Register receiver = calling_convention.GetRegisterAt(0);
7575
Chris Larsen3acee732015-11-18 13:31:08 -08007576 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007577 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7578 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7579 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007580 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007581
7582 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007583 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007584 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007585 // Instead of simply (possibly) unpoisoning `temp` here, we should
7586 // emit a read barrier for the previous class reference load.
7587 // However this is not required in practice, as this is an
7588 // intermediate/temporary reference and because the current
7589 // concurrent copying collector keeps the from-space memory
7590 // intact/accessible until the end of the marking phase (the
7591 // concurrent copying collector may not in the future).
7592 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007593 // temp = temp->GetMethodAt(method_offset);
7594 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7595 // T9 = temp->GetEntryPoint();
7596 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7597 // T9();
7598 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007599 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007600 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007601}
7602
7603void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7604 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7605 return;
7606 }
7607
7608 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007610}
7611
7612void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007613 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007614 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007615 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007616 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7617 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007618 return;
7619 }
Vladimir Marko41559982017-01-06 14:04:23 +00007620 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007621 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007622 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7623 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007624 ? LocationSummary::kCallOnSlowPath
7625 : LocationSummary::kNoCall;
7626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007627 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7628 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7629 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007630 switch (load_kind) {
7631 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007632 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007633 case HLoadClass::LoadKind::kBootImageAddress:
7634 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007635 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007636 break;
7637 }
7638 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007639 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007640 locations->SetInAt(0, Location::RequiresRegister());
7641 break;
7642 default:
7643 break;
7644 }
7645 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007646 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7647 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7648 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007649 // Request a temp to hold the BSS entry location for the slow path.
7650 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007651 RegisterSet caller_saves = RegisterSet::Empty();
7652 InvokeRuntimeCallingConvention calling_convention;
7653 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7654 locations->SetCustomSlowPathCallerSaves(caller_saves);
7655 } else {
7656 // For non-Baker read barriers we have a temp-clobbering call.
7657 }
7658 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007659}
7660
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007661// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7662// move.
7663void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007664 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007665 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007666 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007667 return;
7668 }
Vladimir Marko41559982017-01-06 14:04:23 +00007669 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007670
Vladimir Marko41559982017-01-06 14:04:23 +00007671 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007672 Location out_loc = locations->Out();
7673 Register out = out_loc.AsRegister<Register>();
7674 Register base_or_current_method_reg;
7675 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7676 switch (load_kind) {
7677 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007678 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007679 case HLoadClass::LoadKind::kBootImageAddress:
7680 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007681 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7682 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007683 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007684 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007685 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7686 break;
7687 default:
7688 base_or_current_method_reg = ZERO;
7689 break;
7690 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007691
Alexey Frunze15958152017-02-09 19:08:30 -08007692 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7693 ? kWithoutReadBarrier
7694 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007695 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007696 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007697 switch (load_kind) {
7698 case HLoadClass::LoadKind::kReferrersClass: {
7699 DCHECK(!cls->CanCallRuntime());
7700 DCHECK(!cls->MustGenerateClinitCheck());
7701 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7702 GenerateGcRootFieldLoad(cls,
7703 out_loc,
7704 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007705 ArtMethod::DeclaringClassOffset().Int32Value(),
7706 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007707 break;
7708 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007709 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007710 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007711 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007712 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007713 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007714 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7715 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007716 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7717 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007718 base_or_current_method_reg);
7719 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007720 break;
7721 }
7722 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007723 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007724 uint32_t address = dchecked_integral_cast<uint32_t>(
7725 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7726 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 __ LoadLiteral(out,
7728 base_or_current_method_reg,
7729 codegen_->DeduplicateBootImageAddressLiteral(address));
7730 break;
7731 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007732 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007733 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7734 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7735 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007736 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007737 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007738 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7739 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007740 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007741 GenerateGcRootFieldLoad(cls,
7742 out_loc,
7743 temp,
7744 /* placeholder */ 0x5678,
7745 read_barrier_option,
7746 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007747 generate_null_check = true;
7748 break;
7749 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007750 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007751 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7752 cls->GetTypeIndex(),
7753 cls->GetClass());
7754 bool reordering = __ SetReorder(false);
7755 __ Bind(&info->high_label);
7756 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007757 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007758 GenerateGcRootFieldLoad(cls,
7759 out_loc,
7760 out,
7761 /* placeholder */ 0x5678,
7762 read_barrier_option,
7763 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007764 break;
7765 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007766 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007767 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007768 LOG(FATAL) << "UNREACHABLE";
7769 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007770 }
7771
7772 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7773 DCHECK(cls->CanCallRuntime());
7774 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007775 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007776 codegen_->AddSlowPath(slow_path);
7777 if (generate_null_check) {
7778 __ Beqz(out, slow_path->GetEntryLabel());
7779 }
7780 if (cls->MustGenerateClinitCheck()) {
7781 GenerateClassInitializationCheck(slow_path, out);
7782 } else {
7783 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007784 }
7785 }
7786}
7787
7788static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007789 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007790}
7791
7792void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7793 LocationSummary* locations =
7794 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7795 locations->SetOut(Location::RequiresRegister());
7796}
7797
7798void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7799 Register out = load->GetLocations()->Out().AsRegister<Register>();
7800 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7801}
7802
7803void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7804 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7805}
7806
7807void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7808 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7809}
7810
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007811void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007812 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007814 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007815 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007816 switch (load_kind) {
7817 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007818 case HLoadString::LoadKind::kBootImageAddress:
7819 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007820 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007821 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007822 break;
7823 }
7824 FALLTHROUGH_INTENDED;
7825 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007826 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007827 locations->SetInAt(0, Location::RequiresRegister());
7828 break;
7829 default:
7830 break;
7831 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007832 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007833 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007834 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007835 } else {
7836 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007837 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7838 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7839 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007840 // Request a temp to hold the BSS entry location for the slow path.
7841 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007842 RegisterSet caller_saves = RegisterSet::Empty();
7843 InvokeRuntimeCallingConvention calling_convention;
7844 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7845 locations->SetCustomSlowPathCallerSaves(caller_saves);
7846 } else {
7847 // For non-Baker read barriers we have a temp-clobbering call.
7848 }
7849 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007850 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007851}
7852
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007853// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7854// move.
7855void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007856 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007857 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007858 Location out_loc = locations->Out();
7859 Register out = out_loc.AsRegister<Register>();
7860 Register base_or_current_method_reg;
7861 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7862 switch (load_kind) {
7863 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007864 case HLoadString::LoadKind::kBootImageAddress:
7865 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007866 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007867 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7868 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007869 default:
7870 base_or_current_method_reg = ZERO;
7871 break;
7872 }
7873
7874 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007875 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007876 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007877 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007878 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007879 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7880 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007881 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7882 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007883 base_or_current_method_reg);
7884 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 return; // No dex cache slow path.
7886 }
7887 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007888 uint32_t address = dchecked_integral_cast<uint32_t>(
7889 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7890 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007891 __ LoadLiteral(out,
7892 base_or_current_method_reg,
7893 codegen_->DeduplicateBootImageAddressLiteral(address));
7894 return; // No dex cache slow path.
7895 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007896 case HLoadString::LoadKind::kBssEntry: {
7897 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007898 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007899 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007900 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7901 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007902 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007903 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007904 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7905 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007906 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007907 GenerateGcRootFieldLoad(load,
7908 out_loc,
7909 temp,
7910 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007911 kCompilerReadBarrierOption,
7912 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007913 SlowPathCodeMIPS* slow_path =
7914 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007915 codegen_->AddSlowPath(slow_path);
7916 __ Beqz(out, slow_path->GetEntryLabel());
7917 __ Bind(slow_path->GetExitLabel());
7918 return;
7919 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007920 case HLoadString::LoadKind::kJitTableAddress: {
7921 CodeGeneratorMIPS::JitPatchInfo* info =
7922 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7923 load->GetStringIndex(),
7924 load->GetString());
7925 bool reordering = __ SetReorder(false);
7926 __ Bind(&info->high_label);
7927 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007928 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08007929 GenerateGcRootFieldLoad(load,
7930 out_loc,
7931 out,
7932 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007933 kCompilerReadBarrierOption,
7934 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007935 return;
7936 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007937 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007938 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007939 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007940
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007941 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007942 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007943 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007944 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007945 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007946 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7947 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007948}
7949
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007950void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7951 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7952 locations->SetOut(Location::ConstantLocation(constant));
7953}
7954
7955void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7956 // Will be generated at use site.
7957}
7958
7959void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7960 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007961 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007962 InvokeRuntimeCallingConvention calling_convention;
7963 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7964}
7965
7966void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7967 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007968 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007969 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7970 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007971 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007972 }
7973 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7974}
7975
7976void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7977 LocationSummary* locations =
7978 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7979 switch (mul->GetResultType()) {
7980 case Primitive::kPrimInt:
7981 case Primitive::kPrimLong:
7982 locations->SetInAt(0, Location::RequiresRegister());
7983 locations->SetInAt(1, Location::RequiresRegister());
7984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7985 break;
7986
7987 case Primitive::kPrimFloat:
7988 case Primitive::kPrimDouble:
7989 locations->SetInAt(0, Location::RequiresFpuRegister());
7990 locations->SetInAt(1, Location::RequiresFpuRegister());
7991 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7992 break;
7993
7994 default:
7995 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7996 }
7997}
7998
7999void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
8000 Primitive::Type type = instruction->GetType();
8001 LocationSummary* locations = instruction->GetLocations();
8002 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8003
8004 switch (type) {
8005 case Primitive::kPrimInt: {
8006 Register dst = locations->Out().AsRegister<Register>();
8007 Register lhs = locations->InAt(0).AsRegister<Register>();
8008 Register rhs = locations->InAt(1).AsRegister<Register>();
8009
8010 if (isR6) {
8011 __ MulR6(dst, lhs, rhs);
8012 } else {
8013 __ MulR2(dst, lhs, rhs);
8014 }
8015 break;
8016 }
8017 case Primitive::kPrimLong: {
8018 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8019 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8020 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8021 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8022 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8023 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8024
8025 // Extra checks to protect caused by the existance of A1_A2.
8026 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8027 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8028 DCHECK_NE(dst_high, lhs_low);
8029 DCHECK_NE(dst_high, rhs_low);
8030
8031 // A_B * C_D
8032 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8033 // dst_lo: [ low(B*D) ]
8034 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8035
8036 if (isR6) {
8037 __ MulR6(TMP, lhs_high, rhs_low);
8038 __ MulR6(dst_high, lhs_low, rhs_high);
8039 __ Addu(dst_high, dst_high, TMP);
8040 __ MuhuR6(TMP, lhs_low, rhs_low);
8041 __ Addu(dst_high, dst_high, TMP);
8042 __ MulR6(dst_low, lhs_low, rhs_low);
8043 } else {
8044 __ MulR2(TMP, lhs_high, rhs_low);
8045 __ MulR2(dst_high, lhs_low, rhs_high);
8046 __ Addu(dst_high, dst_high, TMP);
8047 __ MultuR2(lhs_low, rhs_low);
8048 __ Mfhi(TMP);
8049 __ Addu(dst_high, dst_high, TMP);
8050 __ Mflo(dst_low);
8051 }
8052 break;
8053 }
8054 case Primitive::kPrimFloat:
8055 case Primitive::kPrimDouble: {
8056 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8057 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8058 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
8059 if (type == Primitive::kPrimFloat) {
8060 __ MulS(dst, lhs, rhs);
8061 } else {
8062 __ MulD(dst, lhs, rhs);
8063 }
8064 break;
8065 }
8066 default:
8067 LOG(FATAL) << "Unexpected mul type " << type;
8068 }
8069}
8070
8071void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8072 LocationSummary* locations =
8073 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
8074 switch (neg->GetResultType()) {
8075 case Primitive::kPrimInt:
8076 case Primitive::kPrimLong:
8077 locations->SetInAt(0, Location::RequiresRegister());
8078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8079 break;
8080
8081 case Primitive::kPrimFloat:
8082 case Primitive::kPrimDouble:
8083 locations->SetInAt(0, Location::RequiresFpuRegister());
8084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8085 break;
8086
8087 default:
8088 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8089 }
8090}
8091
8092void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
8093 Primitive::Type type = instruction->GetType();
8094 LocationSummary* locations = instruction->GetLocations();
8095
8096 switch (type) {
8097 case Primitive::kPrimInt: {
8098 Register dst = locations->Out().AsRegister<Register>();
8099 Register src = locations->InAt(0).AsRegister<Register>();
8100 __ Subu(dst, ZERO, src);
8101 break;
8102 }
8103 case Primitive::kPrimLong: {
8104 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8105 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8106 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8107 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8108 __ Subu(dst_low, ZERO, src_low);
8109 __ Sltu(TMP, ZERO, dst_low);
8110 __ Subu(dst_high, ZERO, src_high);
8111 __ Subu(dst_high, dst_high, TMP);
8112 break;
8113 }
8114 case Primitive::kPrimFloat:
8115 case Primitive::kPrimDouble: {
8116 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8117 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8118 if (type == Primitive::kPrimFloat) {
8119 __ NegS(dst, src);
8120 } else {
8121 __ NegD(dst, src);
8122 }
8123 break;
8124 }
8125 default:
8126 LOG(FATAL) << "Unexpected neg type " << type;
8127 }
8128}
8129
8130void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
8131 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008133 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008134 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008135 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8136 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008137}
8138
8139void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008140 // Note: if heap poisoning is enabled, the entry point takes care
8141 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008142 QuickEntrypointEnum entrypoint =
8143 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8144 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008145 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008146 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008147}
8148
8149void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
8150 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008151 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008152 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008153 if (instruction->IsStringAlloc()) {
8154 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8155 } else {
8156 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008157 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008158 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
8159}
8160
8161void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008162 // Note: if heap poisoning is enabled, the entry point takes care
8163 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008164 if (instruction->IsStringAlloc()) {
8165 // String is allocated through StringFactory. Call NewEmptyString entry point.
8166 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008167 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008168 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8169 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8170 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008171 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008172 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8173 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008174 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008175 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008176 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008177}
8178
8179void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
8180 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8181 locations->SetInAt(0, Location::RequiresRegister());
8182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8183}
8184
8185void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
8186 Primitive::Type type = instruction->GetType();
8187 LocationSummary* locations = instruction->GetLocations();
8188
8189 switch (type) {
8190 case Primitive::kPrimInt: {
8191 Register dst = locations->Out().AsRegister<Register>();
8192 Register src = locations->InAt(0).AsRegister<Register>();
8193 __ Nor(dst, src, ZERO);
8194 break;
8195 }
8196
8197 case Primitive::kPrimLong: {
8198 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8199 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8200 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8201 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8202 __ Nor(dst_high, src_high, ZERO);
8203 __ Nor(dst_low, src_low, ZERO);
8204 break;
8205 }
8206
8207 default:
8208 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8209 }
8210}
8211
8212void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8214 locations->SetInAt(0, Location::RequiresRegister());
8215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8216}
8217
8218void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8219 LocationSummary* locations = instruction->GetLocations();
8220 __ Xori(locations->Out().AsRegister<Register>(),
8221 locations->InAt(0).AsRegister<Register>(),
8222 1);
8223}
8224
8225void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008226 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8227 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008228}
8229
Calin Juravle2ae48182016-03-16 14:05:09 +00008230void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8231 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008232 return;
8233 }
8234 Location obj = instruction->GetLocations()->InAt(0);
8235
8236 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008237 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008238}
8239
Calin Juravle2ae48182016-03-16 14:05:09 +00008240void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008241 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008242 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008243
8244 Location obj = instruction->GetLocations()->InAt(0);
8245
8246 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8247}
8248
8249void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008250 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008251}
8252
8253void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8254 HandleBinaryOp(instruction);
8255}
8256
8257void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8258 HandleBinaryOp(instruction);
8259}
8260
8261void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8262 LOG(FATAL) << "Unreachable";
8263}
8264
8265void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
8266 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8267}
8268
8269void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
8270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8271 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8272 if (location.IsStackSlot()) {
8273 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8274 } else if (location.IsDoubleStackSlot()) {
8275 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8276 }
8277 locations->SetOut(location);
8278}
8279
8280void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8281 ATTRIBUTE_UNUSED) {
8282 // Nothing to do, the parameter is already at its location.
8283}
8284
8285void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8286 LocationSummary* locations =
8287 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8288 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8289}
8290
8291void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8292 ATTRIBUTE_UNUSED) {
8293 // Nothing to do, the method is already at its location.
8294}
8295
8296void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008298 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008299 locations->SetInAt(i, Location::Any());
8300 }
8301 locations->SetOut(Location::Any());
8302}
8303
8304void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8305 LOG(FATAL) << "Unreachable";
8306}
8307
8308void LocationsBuilderMIPS::VisitRem(HRem* rem) {
8309 Primitive::Type type = rem->GetResultType();
8310 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008311 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008312 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8313
8314 switch (type) {
8315 case Primitive::kPrimInt:
8316 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008317 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8319 break;
8320
8321 case Primitive::kPrimLong: {
8322 InvokeRuntimeCallingConvention calling_convention;
8323 locations->SetInAt(0, Location::RegisterPairLocation(
8324 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8325 locations->SetInAt(1, Location::RegisterPairLocation(
8326 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8327 locations->SetOut(calling_convention.GetReturnLocation(type));
8328 break;
8329 }
8330
8331 case Primitive::kPrimFloat:
8332 case Primitive::kPrimDouble: {
8333 InvokeRuntimeCallingConvention calling_convention;
8334 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8335 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8336 locations->SetOut(calling_convention.GetReturnLocation(type));
8337 break;
8338 }
8339
8340 default:
8341 LOG(FATAL) << "Unexpected rem type " << type;
8342 }
8343}
8344
8345void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
8346 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008347
8348 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08008349 case Primitive::kPrimInt:
8350 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008351 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008352 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008353 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008354 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8355 break;
8356 }
8357 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008358 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008359 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008360 break;
8361 }
8362 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008363 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008364 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008365 break;
8366 }
8367 default:
8368 LOG(FATAL) << "Unexpected rem type " << type;
8369 }
8370}
8371
Igor Murashkind01745e2017-04-05 16:40:31 -07008372void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8373 constructor_fence->SetLocations(nullptr);
8374}
8375
8376void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8377 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8378 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8379}
8380
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008381void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8382 memory_barrier->SetLocations(nullptr);
8383}
8384
8385void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8386 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8387}
8388
8389void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8390 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8391 Primitive::Type return_type = ret->InputAt(0)->GetType();
8392 locations->SetInAt(0, MipsReturnLocation(return_type));
8393}
8394
8395void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8396 codegen_->GenerateFrameExit();
8397}
8398
8399void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8400 ret->SetLocations(nullptr);
8401}
8402
8403void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8404 codegen_->GenerateFrameExit();
8405}
8406
Alexey Frunze92d90602015-12-18 18:16:36 -08008407void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8408 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008409}
8410
Alexey Frunze92d90602015-12-18 18:16:36 -08008411void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8412 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008413}
8414
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008415void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8416 HandleShift(shl);
8417}
8418
8419void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8420 HandleShift(shl);
8421}
8422
8423void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8424 HandleShift(shr);
8425}
8426
8427void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8428 HandleShift(shr);
8429}
8430
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008431void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8432 HandleBinaryOp(instruction);
8433}
8434
8435void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8436 HandleBinaryOp(instruction);
8437}
8438
8439void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8440 HandleFieldGet(instruction, instruction->GetFieldInfo());
8441}
8442
8443void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8444 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8445}
8446
8447void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8448 HandleFieldSet(instruction, instruction->GetFieldInfo());
8449}
8450
8451void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008452 HandleFieldSet(instruction,
8453 instruction->GetFieldInfo(),
8454 instruction->GetDexPc(),
8455 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008456}
8457
8458void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8459 HUnresolvedInstanceFieldGet* instruction) {
8460 FieldAccessCallingConventionMIPS calling_convention;
8461 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8462 instruction->GetFieldType(),
8463 calling_convention);
8464}
8465
8466void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8467 HUnresolvedInstanceFieldGet* instruction) {
8468 FieldAccessCallingConventionMIPS calling_convention;
8469 codegen_->GenerateUnresolvedFieldAccess(instruction,
8470 instruction->GetFieldType(),
8471 instruction->GetFieldIndex(),
8472 instruction->GetDexPc(),
8473 calling_convention);
8474}
8475
8476void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8477 HUnresolvedInstanceFieldSet* instruction) {
8478 FieldAccessCallingConventionMIPS calling_convention;
8479 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8480 instruction->GetFieldType(),
8481 calling_convention);
8482}
8483
8484void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8485 HUnresolvedInstanceFieldSet* instruction) {
8486 FieldAccessCallingConventionMIPS calling_convention;
8487 codegen_->GenerateUnresolvedFieldAccess(instruction,
8488 instruction->GetFieldType(),
8489 instruction->GetFieldIndex(),
8490 instruction->GetDexPc(),
8491 calling_convention);
8492}
8493
8494void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8495 HUnresolvedStaticFieldGet* instruction) {
8496 FieldAccessCallingConventionMIPS calling_convention;
8497 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8498 instruction->GetFieldType(),
8499 calling_convention);
8500}
8501
8502void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8503 HUnresolvedStaticFieldGet* instruction) {
8504 FieldAccessCallingConventionMIPS calling_convention;
8505 codegen_->GenerateUnresolvedFieldAccess(instruction,
8506 instruction->GetFieldType(),
8507 instruction->GetFieldIndex(),
8508 instruction->GetDexPc(),
8509 calling_convention);
8510}
8511
8512void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8513 HUnresolvedStaticFieldSet* instruction) {
8514 FieldAccessCallingConventionMIPS calling_convention;
8515 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8516 instruction->GetFieldType(),
8517 calling_convention);
8518}
8519
8520void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8521 HUnresolvedStaticFieldSet* instruction) {
8522 FieldAccessCallingConventionMIPS calling_convention;
8523 codegen_->GenerateUnresolvedFieldAccess(instruction,
8524 instruction->GetFieldType(),
8525 instruction->GetFieldIndex(),
8526 instruction->GetDexPc(),
8527 calling_convention);
8528}
8529
8530void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008531 LocationSummary* locations =
8532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008533 // In suspend check slow path, usually there are no caller-save registers at all.
8534 // If SIMD instructions are present, however, we force spilling all live SIMD
8535 // registers in full width (since the runtime only saves/restores lower part).
8536 locations->SetCustomSlowPathCallerSaves(
8537 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538}
8539
8540void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8541 HBasicBlock* block = instruction->GetBlock();
8542 if (block->GetLoopInformation() != nullptr) {
8543 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8544 // The back edge will generate the suspend check.
8545 return;
8546 }
8547 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8548 // The goto will generate the suspend check.
8549 return;
8550 }
8551 GenerateSuspendCheck(instruction, nullptr);
8552}
8553
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008554void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8555 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008557 InvokeRuntimeCallingConvention calling_convention;
8558 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8559}
8560
8561void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008562 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008563 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8564}
8565
8566void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8567 Primitive::Type input_type = conversion->GetInputType();
8568 Primitive::Type result_type = conversion->GetResultType();
8569 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008570 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008571
8572 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8573 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8574 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8575 }
8576
8577 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008578 if (!isR6 &&
8579 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8580 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008581 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008582 }
8583
8584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8585
8586 if (call_kind == LocationSummary::kNoCall) {
8587 if (Primitive::IsFloatingPointType(input_type)) {
8588 locations->SetInAt(0, Location::RequiresFpuRegister());
8589 } else {
8590 locations->SetInAt(0, Location::RequiresRegister());
8591 }
8592
8593 if (Primitive::IsFloatingPointType(result_type)) {
8594 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8595 } else {
8596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8597 }
8598 } else {
8599 InvokeRuntimeCallingConvention calling_convention;
8600
8601 if (Primitive::IsFloatingPointType(input_type)) {
8602 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8603 } else {
8604 DCHECK_EQ(input_type, Primitive::kPrimLong);
8605 locations->SetInAt(0, Location::RegisterPairLocation(
8606 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8607 }
8608
8609 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8610 }
8611}
8612
8613void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8614 LocationSummary* locations = conversion->GetLocations();
8615 Primitive::Type result_type = conversion->GetResultType();
8616 Primitive::Type input_type = conversion->GetInputType();
8617 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008618 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008619
8620 DCHECK_NE(input_type, result_type);
8621
8622 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8623 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8624 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8625 Register src = locations->InAt(0).AsRegister<Register>();
8626
Alexey Frunzea871ef12016-06-27 15:20:11 -07008627 if (dst_low != src) {
8628 __ Move(dst_low, src);
8629 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008630 __ Sra(dst_high, src, 31);
8631 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8632 Register dst = locations->Out().AsRegister<Register>();
8633 Register src = (input_type == Primitive::kPrimLong)
8634 ? locations->InAt(0).AsRegisterPairLow<Register>()
8635 : locations->InAt(0).AsRegister<Register>();
8636
8637 switch (result_type) {
8638 case Primitive::kPrimChar:
8639 __ Andi(dst, src, 0xFFFF);
8640 break;
8641 case Primitive::kPrimByte:
8642 if (has_sign_extension) {
8643 __ Seb(dst, src);
8644 } else {
8645 __ Sll(dst, src, 24);
8646 __ Sra(dst, dst, 24);
8647 }
8648 break;
8649 case Primitive::kPrimShort:
8650 if (has_sign_extension) {
8651 __ Seh(dst, src);
8652 } else {
8653 __ Sll(dst, src, 16);
8654 __ Sra(dst, dst, 16);
8655 }
8656 break;
8657 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008658 if (dst != src) {
8659 __ Move(dst, src);
8660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008661 break;
8662
8663 default:
8664 LOG(FATAL) << "Unexpected type conversion from " << input_type
8665 << " to " << result_type;
8666 }
8667 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008668 if (input_type == Primitive::kPrimLong) {
8669 if (isR6) {
8670 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8671 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8672 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8673 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8674 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8675 __ Mtc1(src_low, FTMP);
8676 __ Mthc1(src_high, FTMP);
8677 if (result_type == Primitive::kPrimFloat) {
8678 __ Cvtsl(dst, FTMP);
8679 } else {
8680 __ Cvtdl(dst, FTMP);
8681 }
8682 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008683 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8684 : kQuickL2d;
8685 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008686 if (result_type == Primitive::kPrimFloat) {
8687 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8688 } else {
8689 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8690 }
8691 }
8692 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693 Register src = locations->InAt(0).AsRegister<Register>();
8694 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8695 __ Mtc1(src, FTMP);
8696 if (result_type == Primitive::kPrimFloat) {
8697 __ Cvtsw(dst, FTMP);
8698 } else {
8699 __ Cvtdw(dst, FTMP);
8700 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008701 }
8702 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8703 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008704
8705 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8706 // value of the output type if the input is outside of the range after the truncation or
8707 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8708 // results. This matches the desired float/double-to-int/long conversion exactly.
8709 //
8710 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8711 // value when the input is either a NaN or is outside of the range of the output type
8712 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8713 // the same result.
8714 //
8715 // The code takes care of the different behaviors by first comparing the input to the
8716 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8717 // If the input is greater than or equal to the minimum, it procedes to the truncate
8718 // instruction, which will handle such an input the same way irrespective of NAN2008.
8719 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8720 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008721 if (result_type == Primitive::kPrimLong) {
8722 if (isR6) {
8723 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8724 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8725 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8726 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8727 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008728
8729 if (input_type == Primitive::kPrimFloat) {
8730 __ TruncLS(FTMP, src);
8731 } else {
8732 __ TruncLD(FTMP, src);
8733 }
8734 __ Mfc1(dst_low, FTMP);
8735 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008736 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008737 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8738 : kQuickD2l;
8739 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008740 if (input_type == Primitive::kPrimFloat) {
8741 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8742 } else {
8743 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8744 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008745 }
8746 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008747 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8748 Register dst = locations->Out().AsRegister<Register>();
8749 MipsLabel truncate;
8750 MipsLabel done;
8751
Lena Djokicf4e23a82017-05-09 15:43:45 +02008752 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008753 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008754 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8755 __ LoadConst32(TMP, min_val);
8756 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008757 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008758 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8759 __ LoadConst32(TMP, High32Bits(min_val));
8760 __ Mtc1(ZERO, FTMP);
8761 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008762 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008763
8764 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008765 __ ColeS(0, FTMP, src);
8766 } else {
8767 __ ColeD(0, FTMP, src);
8768 }
8769 __ Bc1t(0, &truncate);
8770
8771 if (input_type == Primitive::kPrimFloat) {
8772 __ CeqS(0, src, src);
8773 } else {
8774 __ CeqD(0, src, src);
8775 }
8776 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8777 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008778
8779 __ B(&done);
8780
8781 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008782 }
8783
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008784 if (input_type == Primitive::kPrimFloat) {
8785 __ TruncWS(FTMP, src);
8786 } else {
8787 __ TruncWD(FTMP, src);
8788 }
8789 __ Mfc1(dst, FTMP);
8790
Lena Djokicf4e23a82017-05-09 15:43:45 +02008791 if (!isR6) {
8792 __ Bind(&done);
8793 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 }
8795 } else if (Primitive::IsFloatingPointType(result_type) &&
8796 Primitive::IsFloatingPointType(input_type)) {
8797 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8798 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8799 if (result_type == Primitive::kPrimFloat) {
8800 __ Cvtsd(dst, src);
8801 } else {
8802 __ Cvtds(dst, src);
8803 }
8804 } else {
8805 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8806 << " to " << result_type;
8807 }
8808}
8809
8810void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8811 HandleShift(ushr);
8812}
8813
8814void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8815 HandleShift(ushr);
8816}
8817
8818void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8819 HandleBinaryOp(instruction);
8820}
8821
8822void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8823 HandleBinaryOp(instruction);
8824}
8825
8826void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8827 // Nothing to do, this should be removed during prepare for register allocator.
8828 LOG(FATAL) << "Unreachable";
8829}
8830
8831void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8832 // Nothing to do, this should be removed during prepare for register allocator.
8833 LOG(FATAL) << "Unreachable";
8834}
8835
8836void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008837 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008838}
8839
8840void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008841 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008842}
8843
8844void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008845 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008846}
8847
8848void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008849 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008850}
8851
8852void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008853 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008854}
8855
8856void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008857 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008858}
8859
8860void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008861 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008862}
8863
8864void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008865 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008866}
8867
8868void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008869 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008870}
8871
8872void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008873 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008874}
8875
8876void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008877 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008878}
8879
8880void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008881 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008882}
8883
8884void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008885 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008886}
8887
8888void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008889 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008890}
8891
8892void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008893 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008894}
8895
8896void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008897 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008898}
8899
8900void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008901 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008902}
8903
8904void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008905 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008906}
8907
8908void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008909 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008910}
8911
8912void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008913 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008914}
8915
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008916void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8917 LocationSummary* locations =
8918 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8919 locations->SetInAt(0, Location::RequiresRegister());
8920}
8921
Alexey Frunze96b66822016-09-10 02:32:44 -07008922void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8923 int32_t lower_bound,
8924 uint32_t num_entries,
8925 HBasicBlock* switch_block,
8926 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008927 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008928 Register temp_reg = TMP;
8929 __ Addiu32(temp_reg, value_reg, -lower_bound);
8930 // Jump to default if index is negative
8931 // Note: We don't check the case that index is positive while value < lower_bound, because in
8932 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8933 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8934
Alexey Frunze96b66822016-09-10 02:32:44 -07008935 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008936 // Jump to successors[0] if value == lower_bound.
8937 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8938 int32_t last_index = 0;
8939 for (; num_entries - last_index > 2; last_index += 2) {
8940 __ Addiu(temp_reg, temp_reg, -2);
8941 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8942 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8943 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8944 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8945 }
8946 if (num_entries - last_index == 2) {
8947 // The last missing case_value.
8948 __ Addiu(temp_reg, temp_reg, -1);
8949 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008950 }
8951
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008952 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008953 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008954 __ B(codegen_->GetLabelOf(default_block));
8955 }
8956}
8957
Alexey Frunze96b66822016-09-10 02:32:44 -07008958void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8959 Register constant_area,
8960 int32_t lower_bound,
8961 uint32_t num_entries,
8962 HBasicBlock* switch_block,
8963 HBasicBlock* default_block) {
8964 // Create a jump table.
8965 std::vector<MipsLabel*> labels(num_entries);
8966 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8967 for (uint32_t i = 0; i < num_entries; i++) {
8968 labels[i] = codegen_->GetLabelOf(successors[i]);
8969 }
8970 JumpTable* table = __ CreateJumpTable(std::move(labels));
8971
8972 // Is the value in range?
8973 __ Addiu32(TMP, value_reg, -lower_bound);
8974 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8975 __ Sltiu(AT, TMP, num_entries);
8976 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8977 } else {
8978 __ LoadConst32(AT, num_entries);
8979 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8980 }
8981
8982 // We are in the range of the table.
8983 // Load the target address from the jump table, indexing by the value.
8984 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008985 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008986 __ Lw(TMP, TMP, 0);
8987 // Compute the absolute target address by adding the table start address
8988 // (the table contains offsets to targets relative to its start).
8989 __ Addu(TMP, TMP, AT);
8990 // And jump.
8991 __ Jr(TMP);
8992 __ NopIfNoReordering();
8993}
8994
8995void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8996 int32_t lower_bound = switch_instr->GetStartValue();
8997 uint32_t num_entries = switch_instr->GetNumEntries();
8998 LocationSummary* locations = switch_instr->GetLocations();
8999 Register value_reg = locations->InAt(0).AsRegister<Register>();
9000 HBasicBlock* switch_block = switch_instr->GetBlock();
9001 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9002
9003 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9004 num_entries > kPackedSwitchJumpTableThreshold) {
9005 // R6 uses PC-relative addressing to access the jump table.
9006 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9007 // the jump table and it is implemented by changing HPackedSwitch to
9008 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9009 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9010 GenTableBasedPackedSwitch(value_reg,
9011 ZERO,
9012 lower_bound,
9013 num_entries,
9014 switch_block,
9015 default_block);
9016 } else {
9017 GenPackedSwitchWithCompares(value_reg,
9018 lower_bound,
9019 num_entries,
9020 switch_block,
9021 default_block);
9022 }
9023}
9024
9025void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9026 LocationSummary* locations =
9027 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9028 locations->SetInAt(0, Location::RequiresRegister());
9029 // Constant area pointer (HMipsComputeBaseMethodAddress).
9030 locations->SetInAt(1, Location::RequiresRegister());
9031}
9032
9033void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9034 int32_t lower_bound = switch_instr->GetStartValue();
9035 uint32_t num_entries = switch_instr->GetNumEntries();
9036 LocationSummary* locations = switch_instr->GetLocations();
9037 Register value_reg = locations->InAt(0).AsRegister<Register>();
9038 Register constant_area = locations->InAt(1).AsRegister<Register>();
9039 HBasicBlock* switch_block = switch_instr->GetBlock();
9040 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9041
9042 // This is an R2-only path. HPackedSwitch has been changed to
9043 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9044 // required to address the jump table relative to PC.
9045 GenTableBasedPackedSwitch(value_reg,
9046 constant_area,
9047 lower_bound,
9048 num_entries,
9049 switch_block,
9050 default_block);
9051}
9052
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009053void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9054 HMipsComputeBaseMethodAddress* insn) {
9055 LocationSummary* locations =
9056 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
9057 locations->SetOut(Location::RequiresRegister());
9058}
9059
9060void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9061 HMipsComputeBaseMethodAddress* insn) {
9062 LocationSummary* locations = insn->GetLocations();
9063 Register reg = locations->Out().AsRegister<Register>();
9064
9065 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9066
9067 // Generate a dummy PC-relative call to obtain PC.
9068 __ Nal();
9069 // Grab the return address off RA.
9070 __ Move(reg, RA);
9071
9072 // Remember this offset (the obtained PC value) for later use with constant area.
9073 __ BindPcRelBaseLabel();
9074}
9075
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009076void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9077 // The trampoline uses the same calling convention as dex calling conventions,
9078 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9079 // the method_idx.
9080 HandleInvoke(invoke);
9081}
9082
9083void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9084 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9085}
9086
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009087void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9088 LocationSummary* locations =
9089 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9090 locations->SetInAt(0, Location::RequiresRegister());
9091 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009092}
9093
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009094void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9095 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009096 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009097 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009098 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009099 __ LoadFromOffset(kLoadWord,
9100 locations->Out().AsRegister<Register>(),
9101 locations->InAt(0).AsRegister<Register>(),
9102 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009103 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009104 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009105 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009106 __ LoadFromOffset(kLoadWord,
9107 locations->Out().AsRegister<Register>(),
9108 locations->InAt(0).AsRegister<Register>(),
9109 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009110 __ LoadFromOffset(kLoadWord,
9111 locations->Out().AsRegister<Register>(),
9112 locations->Out().AsRegister<Register>(),
9113 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009114 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009115}
9116
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009117#undef __
9118#undef QUICK_ENTRY_POINT
9119
9120} // namespace mips
9121} // namespace art