blob: 2f65e8c95878d6b1fe06a99e87b7e724b78dacf0 [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"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
61 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020062 return Location::RegisterLocation(V0);
63
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010064 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020065 return Location::RegisterPairLocation(V0, V1);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kFloat32:
68 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020069 return Location::FpuRegisterLocation(F0);
70
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010071 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location();
73 }
74 UNREACHABLE();
75}
76
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010077Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020078 return MipsReturnLocation(type);
79}
80
81Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
82 return Location::RegisterLocation(kMethodRegisterArgument);
83}
84
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020086 Location next_location;
87
88 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010089 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kInt8:
93 case DataType::Type::kUint16:
94 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010095 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020096 uint32_t gp_index = gp_index_++;
97 if (gp_index < calling_convention.GetNumberOfRegisters()) {
98 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
99 } else {
100 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
101 next_location = Location::StackSlot(stack_offset);
102 }
103 break;
104 }
105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200107 uint32_t gp_index = gp_index_;
108 gp_index_ += 2;
109 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800110 Register reg = calling_convention.GetRegisterAt(gp_index);
111 if (reg == A1 || reg == A3) {
112 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200113 gp_index++;
114 }
115 Register low_even = calling_convention.GetRegisterAt(gp_index);
116 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
117 DCHECK_EQ(low_even + 1, high_odd);
118 next_location = Location::RegisterPairLocation(low_even, high_odd);
119 } else {
120 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
121 next_location = Location::DoubleStackSlot(stack_offset);
122 }
123 break;
124 }
125
126 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
127 // will take up the even/odd pair, while floats are stored in even regs only.
128 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 case DataType::Type::kFloat32:
130 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200131 uint32_t float_index = float_index_++;
132 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
133 next_location = Location::FpuRegisterLocation(
134 calling_convention.GetFpuRegisterAt(float_index));
135 } else {
136 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
138 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200139 }
140 break;
141 }
142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200144 LOG(FATAL) << "Unexpected parameter type " << type;
145 break;
146 }
147
148 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 return next_location;
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155 return MipsReturnLocation(type);
156}
157
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100158// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
159#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700160#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
163 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000164 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
167 LocationSummary* locations = instruction_->GetLocations();
168 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
169 __ Bind(GetEntryLabel());
170 if (instruction_->CanThrowIntoCatchBlock()) {
171 // Live registers will be restored in the catch block if caught.
172 SaveLiveRegisters(codegen, instruction_->GetLocations());
173 }
174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
176 InvokeRuntimeCallingConvention calling_convention;
177 codegen->EmitParallelMoves(locations->InAt(0),
178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100179 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 locations->InAt(1),
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100183 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
184 ? kQuickThrowStringBounds
185 : kQuickThrowArrayBounds;
186 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100187 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
189 }
190
191 bool IsFatal() const OVERRIDE { return true; }
192
193 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
194
195 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
197};
198
199class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
200 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202
203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
204 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
205 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100206 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
208 }
209
210 bool IsFatal() const OVERRIDE { return true; }
211
212 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
213
214 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200215 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
216};
217
218class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
219 public:
220 LoadClassSlowPathMIPS(HLoadClass* cls,
221 HInstruction* at,
222 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700223 bool do_clinit,
224 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
225 : SlowPathCodeMIPS(at),
226 cls_(cls),
227 dex_pc_(dex_pc),
228 do_clinit_(do_clinit),
229 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
231 }
232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000234 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700237 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700238 InvokeRuntimeCallingConvention calling_convention;
239 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
240 const bool is_load_class_bss_entry =
241 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200242 __ Bind(GetEntryLabel());
243 SaveLiveRegisters(codegen, locations);
244
Alexey Frunzec61c0762017-04-10 13:54:23 -0700245 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
246 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700247 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700248 Register temp = locations->GetTemp(0).AsRegister<Register>();
249 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
250 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
251 // kSaveEverything call.
252 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
253 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
254 if (temp_is_a0) {
255 __ Move(entry_address, temp);
256 }
257 }
258
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000259 dex::TypeIndex type_index = cls_->GetTypeIndex();
260 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100261 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
262 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
269
Alexey Frunzec61c0762017-04-10 13:54:23 -0700270 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700271 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700272 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700273 DCHECK(bss_info_high_);
274 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
275 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700276 __ Sw(calling_convention.GetRegisterAt(0),
277 entry_address,
278 /* placeholder */ 0x5678,
279 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700280 }
281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283 if (out.IsValid()) {
284 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100285 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700286 mips_codegen->MoveLocation(out,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
288 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291
292 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700293 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
294 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700296 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200297 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
298 Register base =
299 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700300 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000301 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700302 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
303 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700304 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
305 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 __ B(GetExitLabel());
308 }
309
310 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
311
312 private:
313 // The class this slow path will load.
314 HLoadClass* const cls_;
315
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 // The dex PC of `at_`.
317 const uint32_t dex_pc_;
318
319 // Whether to initialize the class.
320 const bool do_clinit_;
321
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700322 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
323 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
324
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200325 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
326};
327
328class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
329 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700330 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
331 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
332 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333
334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700335 DCHECK(instruction_->IsLoadString());
336 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 LocationSummary* locations = instruction_->GetLocations();
338 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700339 HLoadString* load = instruction_->AsLoadString();
340 const dex::StringIndex string_index = load->GetStringIndex();
341 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700343 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700344 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 __ Bind(GetEntryLabel());
346 SaveLiveRegisters(codegen, locations);
347
Alexey Frunzec61c0762017-04-10 13:54:23 -0700348 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
349 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700350 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700351 Register temp = locations->GetTemp(0).AsRegister<Register>();
352 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
353 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
354 // kSaveEverything call.
355 entry_address = temp_is_a0 ? out : temp;
356 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
357 if (temp_is_a0) {
358 __ Move(entry_address, temp);
359 }
360 }
361
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100363 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200364 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700365
366 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700367 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700368 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700369 DCHECK(bss_info_high_);
370 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100371 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700372 __ Sw(calling_convention.GetRegisterAt(0),
373 entry_address,
374 /* placeholder */ 0x5678,
375 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700376 }
377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100378 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700380 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000383
Alexey Frunzec61c0762017-04-10 13:54:23 -0700384 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700385 if (!baker_or_no_read_barriers) {
386 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700387 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700388 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200389 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
390 Register base =
391 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700392 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100393 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700394 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100395 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700396 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
397 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700398 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 __ B(GetExitLabel());
400 }
401
402 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
403
404 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700405 // Pointer to the high half PC-relative patch info.
406 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
407
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200408 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
409};
410
411class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
412 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000413 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
417 __ Bind(GetEntryLabel());
418 if (instruction_->CanThrowIntoCatchBlock()) {
419 // Live registers will be restored in the catch block if caught.
420 SaveLiveRegisters(codegen, instruction_->GetLocations());
421 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100422 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 instruction_,
424 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100425 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200426 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
427 }
428
429 bool IsFatal() const OVERRIDE { return true; }
430
431 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
432
433 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
435};
436
437class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
438 public:
439 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000440 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441
442 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200443 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
445 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200446 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100447 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200449 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 if (successor_ == nullptr) {
451 __ B(GetReturnLabel());
452 } else {
453 __ B(mips_codegen->GetLabelOf(successor_));
454 }
455 }
456
457 MipsLabel* GetReturnLabel() {
458 DCHECK(successor_ == nullptr);
459 return &return_label_;
460 }
461
462 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
463
464 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200465 // If not null, the block to branch to after the suspend check.
466 HBasicBlock* const successor_;
467
468 // If `successor_` is null, the label to branch to after the suspend check.
469 MipsLabel return_label_;
470
471 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
472};
473
474class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
475 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800476 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
477 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200478
479 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
480 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200481 uint32_t dex_pc = instruction_->GetDexPc();
482 DCHECK(instruction_->IsCheckCast()
483 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
484 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
485
486 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800487 if (!is_fatal_) {
488 SaveLiveRegisters(codegen, locations);
489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490
491 // We're moving two locations to locations that could overlap, so we need a parallel
492 // move resolver.
493 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200495 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800497 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100499 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200500 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100501 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800502 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100503 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200504 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
505 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200506 } else {
507 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800508 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
509 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200510 }
511
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800512 if (!is_fatal_) {
513 RestoreLiveRegisters(codegen, locations);
514 __ B(GetExitLabel());
515 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200516 }
517
518 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
519
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800520 bool IsFatal() const OVERRIDE { return is_fatal_; }
521
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200522 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800523 const bool is_fatal_;
524
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200525 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
526};
527
528class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
529 public:
Aart Bik42249c32016-01-07 15:33:50 -0800530 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800534 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200535 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100536 LocationSummary* locations = instruction_->GetLocations();
537 SaveLiveRegisters(codegen, locations);
538 InvokeRuntimeCallingConvention calling_convention;
539 __ LoadConst32(calling_convention.GetRegisterAt(0),
540 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100541 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100542 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200543 }
544
545 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
546
547 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200548 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
549};
550
Alexey Frunze15958152017-02-09 19:08:30 -0800551class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
552 public:
553 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
554
555 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
556 LocationSummary* locations = instruction_->GetLocations();
557 __ Bind(GetEntryLabel());
558 SaveLiveRegisters(codegen, locations);
559
560 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100561 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800562 parallel_move.AddMove(
563 locations->InAt(0),
564 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100565 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800566 nullptr);
567 parallel_move.AddMove(
568 locations->InAt(1),
569 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100570 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800571 nullptr);
572 parallel_move.AddMove(
573 locations->InAt(2),
574 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100575 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800576 nullptr);
577 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
578
579 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
580 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
581 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
582 RestoreLiveRegisters(codegen, locations);
583 __ B(GetExitLabel());
584 }
585
586 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
587
588 private:
589 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
590};
591
592// Slow path marking an object reference `ref` during a read
593// barrier. The field `obj.field` in the object `obj` holding this
594// reference does not get updated by this slow path after marking (see
595// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
596//
597// This means that after the execution of this slow path, `ref` will
598// always be up-to-date, but `obj.field` may not; i.e., after the
599// flip, `ref` will be a to-space reference, but `obj.field` will
600// probably still be a from-space reference (unless it gets updated by
601// another thread, or if another thread installed another object
602// reference (different from `ref`) in `obj.field`).
603//
604// If `entrypoint` is a valid location it is assumed to already be
605// holding the entrypoint. The case where the entrypoint is passed in
606// is for the GcRoot read barrier.
607class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
608 public:
609 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
610 Location ref,
611 Location entrypoint = Location::NoLocation())
612 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
613 DCHECK(kEmitCompilerReadBarrier);
614 }
615
616 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
617
618 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
619 LocationSummary* locations = instruction_->GetLocations();
620 Register ref_reg = ref_.AsRegister<Register>();
621 DCHECK(locations->CanCall());
622 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
623 DCHECK(instruction_->IsInstanceFieldGet() ||
624 instruction_->IsStaticFieldGet() ||
625 instruction_->IsArrayGet() ||
626 instruction_->IsArraySet() ||
627 instruction_->IsLoadClass() ||
628 instruction_->IsLoadString() ||
629 instruction_->IsInstanceOf() ||
630 instruction_->IsCheckCast() ||
631 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
632 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
633 << "Unexpected instruction in read barrier marking slow path: "
634 << instruction_->DebugName();
635
636 __ Bind(GetEntryLabel());
637 // No need to save live registers; it's taken care of by the
638 // entrypoint. Also, there is no need to update the stack mask,
639 // as this runtime call will not trigger a garbage collection.
640 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
641 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
642 (S2 <= ref_reg && ref_reg <= S7) ||
643 (ref_reg == FP)) << ref_reg;
644 // "Compact" slow path, saving two moves.
645 //
646 // Instead of using the standard runtime calling convention (input
647 // and output in A0 and V0 respectively):
648 //
649 // A0 <- ref
650 // V0 <- ReadBarrierMark(A0)
651 // ref <- V0
652 //
653 // we just use rX (the register containing `ref`) as input and output
654 // of a dedicated entrypoint:
655 //
656 // rX <- ReadBarrierMarkRegX(rX)
657 //
658 if (entrypoint_.IsValid()) {
659 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
660 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
661 __ Jalr(entrypoint_.AsRegister<Register>());
662 __ NopIfNoReordering();
663 } else {
664 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100665 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800666 // This runtime call does not require a stack map.
667 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
668 instruction_,
669 this,
670 /* direct */ false);
671 }
672 __ B(GetExitLabel());
673 }
674
675 private:
676 // The location (register) of the marked object reference.
677 const Location ref_;
678
679 // The location of the entrypoint if already loaded.
680 const Location entrypoint_;
681
682 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
683};
684
685// Slow path marking an object reference `ref` during a read barrier,
686// and if needed, atomically updating the field `obj.field` in the
687// object `obj` holding this reference after marking (contrary to
688// ReadBarrierMarkSlowPathMIPS above, which never tries to update
689// `obj.field`).
690//
691// This means that after the execution of this slow path, both `ref`
692// and `obj.field` will be up-to-date; i.e., after the flip, both will
693// hold the same to-space reference (unless another thread installed
694// another object reference (different from `ref`) in `obj.field`).
695class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
696 public:
697 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
698 Location ref,
699 Register obj,
700 Location field_offset,
701 Register temp1)
702 : SlowPathCodeMIPS(instruction),
703 ref_(ref),
704 obj_(obj),
705 field_offset_(field_offset),
706 temp1_(temp1) {
707 DCHECK(kEmitCompilerReadBarrier);
708 }
709
710 const char* GetDescription() const OVERRIDE {
711 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
712 }
713
714 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
715 LocationSummary* locations = instruction_->GetLocations();
716 Register ref_reg = ref_.AsRegister<Register>();
717 DCHECK(locations->CanCall());
718 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
719 // This slow path is only used by the UnsafeCASObject intrinsic.
720 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
721 << "Unexpected instruction in read barrier marking and field updating slow path: "
722 << instruction_->DebugName();
723 DCHECK(instruction_->GetLocations()->Intrinsified());
724 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
725 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
726
727 __ Bind(GetEntryLabel());
728
729 // Save the old reference.
730 // Note that we cannot use AT or TMP to save the old reference, as those
731 // are used by the code that follows, but we need the old reference after
732 // the call to the ReadBarrierMarkRegX entry point.
733 DCHECK_NE(temp1_, AT);
734 DCHECK_NE(temp1_, TMP);
735 __ Move(temp1_, ref_reg);
736
737 // No need to save live registers; it's taken care of by the
738 // entrypoint. Also, there is no need to update the stack mask,
739 // as this runtime call will not trigger a garbage collection.
740 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
741 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
742 (S2 <= ref_reg && ref_reg <= S7) ||
743 (ref_reg == FP)) << ref_reg;
744 // "Compact" slow path, saving two moves.
745 //
746 // Instead of using the standard runtime calling convention (input
747 // and output in A0 and V0 respectively):
748 //
749 // A0 <- ref
750 // V0 <- ReadBarrierMark(A0)
751 // ref <- V0
752 //
753 // we just use rX (the register containing `ref`) as input and output
754 // of a dedicated entrypoint:
755 //
756 // rX <- ReadBarrierMarkRegX(rX)
757 //
758 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100759 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800760 // This runtime call does not require a stack map.
761 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
762 instruction_,
763 this,
764 /* direct */ false);
765
766 // If the new reference is different from the old reference,
767 // update the field in the holder (`*(obj_ + field_offset_)`).
768 //
769 // Note that this field could also hold a different object, if
770 // another thread had concurrently changed it. In that case, the
771 // the compare-and-set (CAS) loop below would abort, leaving the
772 // field as-is.
773 MipsLabel done;
774 __ Beq(temp1_, ref_reg, &done);
775
776 // Update the the holder's field atomically. This may fail if
777 // mutator updates before us, but it's OK. This is achieved
778 // using a strong compare-and-set (CAS) operation with relaxed
779 // memory synchronization ordering, where the expected value is
780 // the old reference and the desired value is the new reference.
781
782 // Convenience aliases.
783 Register base = obj_;
784 // The UnsafeCASObject intrinsic uses a register pair as field
785 // offset ("long offset"), of which only the low part contains
786 // data.
787 Register offset = field_offset_.AsRegisterPairLow<Register>();
788 Register expected = temp1_;
789 Register value = ref_reg;
790 Register tmp_ptr = TMP; // Pointer to actual memory.
791 Register tmp = AT; // Value in memory.
792
793 __ Addu(tmp_ptr, base, offset);
794
795 if (kPoisonHeapReferences) {
796 __ PoisonHeapReference(expected);
797 // Do not poison `value` if it is the same register as
798 // `expected`, which has just been poisoned.
799 if (value != expected) {
800 __ PoisonHeapReference(value);
801 }
802 }
803
804 // do {
805 // tmp = [r_ptr] - expected;
806 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
807
808 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
809 MipsLabel loop_head, exit_loop;
810 __ Bind(&loop_head);
811 if (is_r6) {
812 __ LlR6(tmp, tmp_ptr);
813 } else {
814 __ LlR2(tmp, tmp_ptr);
815 }
816 __ Bne(tmp, expected, &exit_loop);
817 __ Move(tmp, value);
818 if (is_r6) {
819 __ ScR6(tmp, tmp_ptr);
820 } else {
821 __ ScR2(tmp, tmp_ptr);
822 }
823 __ Beqz(tmp, &loop_head);
824 __ Bind(&exit_loop);
825
826 if (kPoisonHeapReferences) {
827 __ UnpoisonHeapReference(expected);
828 // Do not unpoison `value` if it is the same register as
829 // `expected`, which has just been unpoisoned.
830 if (value != expected) {
831 __ UnpoisonHeapReference(value);
832 }
833 }
834
835 __ Bind(&done);
836 __ B(GetExitLabel());
837 }
838
839 private:
840 // The location (register) of the marked object reference.
841 const Location ref_;
842 // The register containing the object holding the marked object reference field.
843 const Register obj_;
844 // The location of the offset of the marked reference field within `obj_`.
845 Location field_offset_;
846
847 const Register temp1_;
848
849 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
850};
851
852// Slow path generating a read barrier for a heap reference.
853class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
854 public:
855 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
856 Location out,
857 Location ref,
858 Location obj,
859 uint32_t offset,
860 Location index)
861 : SlowPathCodeMIPS(instruction),
862 out_(out),
863 ref_(ref),
864 obj_(obj),
865 offset_(offset),
866 index_(index) {
867 DCHECK(kEmitCompilerReadBarrier);
868 // If `obj` is equal to `out` or `ref`, it means the initial object
869 // has been overwritten by (or after) the heap object reference load
870 // to be instrumented, e.g.:
871 //
872 // __ LoadFromOffset(kLoadWord, out, out, offset);
873 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
874 //
875 // In that case, we have lost the information about the original
876 // object, and the emitted read barrier cannot work properly.
877 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
878 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
879 }
880
881 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
882 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
883 LocationSummary* locations = instruction_->GetLocations();
884 Register reg_out = out_.AsRegister<Register>();
885 DCHECK(locations->CanCall());
886 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
887 DCHECK(instruction_->IsInstanceFieldGet() ||
888 instruction_->IsStaticFieldGet() ||
889 instruction_->IsArrayGet() ||
890 instruction_->IsInstanceOf() ||
891 instruction_->IsCheckCast() ||
892 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
893 << "Unexpected instruction in read barrier for heap reference slow path: "
894 << instruction_->DebugName();
895
896 __ Bind(GetEntryLabel());
897 SaveLiveRegisters(codegen, locations);
898
899 // We may have to change the index's value, but as `index_` is a
900 // constant member (like other "inputs" of this slow path),
901 // introduce a copy of it, `index`.
902 Location index = index_;
903 if (index_.IsValid()) {
904 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
905 if (instruction_->IsArrayGet()) {
906 // Compute the actual memory offset and store it in `index`.
907 Register index_reg = index_.AsRegister<Register>();
908 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
909 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
910 // We are about to change the value of `index_reg` (see the
911 // calls to art::mips::MipsAssembler::Sll and
912 // art::mips::MipsAssembler::Addiu32 below), but it has
913 // not been saved by the previous call to
914 // art::SlowPathCode::SaveLiveRegisters, as it is a
915 // callee-save register --
916 // art::SlowPathCode::SaveLiveRegisters does not consider
917 // callee-save registers, as it has been designed with the
918 // assumption that callee-save registers are supposed to be
919 // handled by the called function. So, as a callee-save
920 // register, `index_reg` _would_ eventually be saved onto
921 // the stack, but it would be too late: we would have
922 // changed its value earlier. Therefore, we manually save
923 // it here into another freely available register,
924 // `free_reg`, chosen of course among the caller-save
925 // registers (as a callee-save `free_reg` register would
926 // exhibit the same problem).
927 //
928 // Note we could have requested a temporary register from
929 // the register allocator instead; but we prefer not to, as
930 // this is a slow path, and we know we can find a
931 // caller-save register that is available.
932 Register free_reg = FindAvailableCallerSaveRegister(codegen);
933 __ Move(free_reg, index_reg);
934 index_reg = free_reg;
935 index = Location::RegisterLocation(index_reg);
936 } else {
937 // The initial register stored in `index_` has already been
938 // saved in the call to art::SlowPathCode::SaveLiveRegisters
939 // (as it is not a callee-save register), so we can freely
940 // use it.
941 }
942 // Shifting the index value contained in `index_reg` by the scale
943 // factor (2) cannot overflow in practice, as the runtime is
944 // unable to allocate object arrays with a size larger than
945 // 2^26 - 1 (that is, 2^28 - 4 bytes).
946 __ Sll(index_reg, index_reg, TIMES_4);
947 static_assert(
948 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
949 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
950 __ Addiu32(index_reg, index_reg, offset_);
951 } else {
952 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
953 // intrinsics, `index_` is not shifted by a scale factor of 2
954 // (as in the case of ArrayGet), as it is actually an offset
955 // to an object field within an object.
956 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
957 DCHECK(instruction_->GetLocations()->Intrinsified());
958 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
959 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
960 << instruction_->AsInvoke()->GetIntrinsic();
961 DCHECK_EQ(offset_, 0U);
962 DCHECK(index_.IsRegisterPair());
963 // UnsafeGet's offset location is a register pair, the low
964 // part contains the correct offset.
965 index = index_.ToLow();
966 }
967 }
968
969 // We're moving two or three locations to locations that could
970 // overlap, so we need a parallel move resolver.
971 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800973 parallel_move.AddMove(ref_,
974 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800976 nullptr);
977 parallel_move.AddMove(obj_,
978 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100979 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800980 nullptr);
981 if (index.IsValid()) {
982 parallel_move.AddMove(index,
983 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100984 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800985 nullptr);
986 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
987 } else {
988 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
989 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
990 }
991 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
992 instruction_,
993 instruction_->GetDexPc(),
994 this);
995 CheckEntrypointTypes<
996 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200997 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100998 calling_convention.GetReturnLocation(DataType::Type::kReference),
999 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001000
1001 RestoreLiveRegisters(codegen, locations);
1002 __ B(GetExitLabel());
1003 }
1004
1005 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1006
1007 private:
1008 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1009 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1010 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1011 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1012 if (i != ref &&
1013 i != obj &&
1014 !codegen->IsCoreCalleeSaveRegister(i) &&
1015 !codegen->IsBlockedCoreRegister(i)) {
1016 return static_cast<Register>(i);
1017 }
1018 }
1019 // We shall never fail to find a free caller-save register, as
1020 // there are more than two core caller-save registers on MIPS
1021 // (meaning it is possible to find one which is different from
1022 // `ref` and `obj`).
1023 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1024 LOG(FATAL) << "Could not find a free caller-save register";
1025 UNREACHABLE();
1026 }
1027
1028 const Location out_;
1029 const Location ref_;
1030 const Location obj_;
1031 const uint32_t offset_;
1032 // An additional location containing an index to an array.
1033 // Only used for HArrayGet and the UnsafeGetObject &
1034 // UnsafeGetObjectVolatile intrinsics.
1035 const Location index_;
1036
1037 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1038};
1039
1040// Slow path generating a read barrier for a GC root.
1041class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1042 public:
1043 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1044 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1045 DCHECK(kEmitCompilerReadBarrier);
1046 }
1047
1048 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1049 LocationSummary* locations = instruction_->GetLocations();
1050 Register reg_out = out_.AsRegister<Register>();
1051 DCHECK(locations->CanCall());
1052 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1053 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1054 << "Unexpected instruction in read barrier for GC root slow path: "
1055 << instruction_->DebugName();
1056
1057 __ Bind(GetEntryLabel());
1058 SaveLiveRegisters(codegen, locations);
1059
1060 InvokeRuntimeCallingConvention calling_convention;
1061 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001062 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1063 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001064 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001065 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1066 instruction_,
1067 instruction_->GetDexPc(),
1068 this);
1069 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001070 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001071 calling_convention.GetReturnLocation(DataType::Type::kReference),
1072 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001073
1074 RestoreLiveRegisters(codegen, locations);
1075 __ B(GetExitLabel());
1076 }
1077
1078 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1079
1080 private:
1081 const Location out_;
1082 const Location root_;
1083
1084 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1085};
1086
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001087CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1088 const MipsInstructionSetFeatures& isa_features,
1089 const CompilerOptions& compiler_options,
1090 OptimizingCompilerStats* stats)
1091 : CodeGenerator(graph,
1092 kNumberOfCoreRegisters,
1093 kNumberOfFRegisters,
1094 kNumberOfRegisterPairs,
1095 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1096 arraysize(kCoreCalleeSaves)),
1097 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1098 arraysize(kFpuCalleeSaves)),
1099 compiler_options,
1100 stats),
1101 block_labels_(nullptr),
1102 location_builder_(graph, this),
1103 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001104 move_resolver_(graph->GetAllocator(), this),
1105 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001106 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001107 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1109 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1110 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1111 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1112 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1113 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1114 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1115 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1116 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001117 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001118 // Save RA (containing the return address) to mimic Quick.
1119 AddAllocatedRegister(Location::RegisterLocation(RA));
1120}
1121
1122#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001123// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1124#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001125#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001126
1127void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1128 // Ensure that we fix up branches.
1129 __ FinalizeCode();
1130
1131 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001132 StackMapStream* stack_map_stream = GetStackMapStream();
1133 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001134 uint32_t old_position =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001135 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001136 uint32_t new_position = __ GetAdjustedPosition(old_position);
1137 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001138 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001139 }
1140
1141 // Adjust pc offsets for the disassembly information.
1142 if (disasm_info_ != nullptr) {
1143 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1144 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1145 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1146 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1147 it.second.start = __ GetAdjustedPosition(it.second.start);
1148 it.second.end = __ GetAdjustedPosition(it.second.end);
1149 }
1150 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1151 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1152 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1153 }
1154 }
1155
1156 CodeGenerator::Finalize(allocator);
1157}
1158
1159MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1160 return codegen_->GetAssembler();
1161}
1162
1163void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1164 DCHECK_LT(index, moves_.size());
1165 MoveOperands* move = moves_[index];
1166 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1167}
1168
1169void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1170 DCHECK_LT(index, moves_.size());
1171 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001172 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001173 Location loc1 = move->GetDestination();
1174 Location loc2 = move->GetSource();
1175
1176 DCHECK(!loc1.IsConstant());
1177 DCHECK(!loc2.IsConstant());
1178
1179 if (loc1.Equals(loc2)) {
1180 return;
1181 }
1182
1183 if (loc1.IsRegister() && loc2.IsRegister()) {
1184 // Swap 2 GPRs.
1185 Register r1 = loc1.AsRegister<Register>();
1186 Register r2 = loc2.AsRegister<Register>();
1187 __ Move(TMP, r2);
1188 __ Move(r2, r1);
1189 __ Move(r1, TMP);
1190 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1191 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1192 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194 __ MovS(FTMP, f2);
1195 __ MovS(f2, f1);
1196 __ MovS(f1, FTMP);
1197 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001198 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001199 __ MovD(FTMP, f2);
1200 __ MovD(f2, f1);
1201 __ MovD(f1, FTMP);
1202 }
1203 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1204 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1205 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001207 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1208 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001209 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 __ Move(TMP, r2);
1211 __ Mfc1(r2, f1);
1212 __ Mtc1(TMP, f1);
1213 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1214 // Swap 2 GPR register pairs.
1215 Register r1 = loc1.AsRegisterPairLow<Register>();
1216 Register r2 = loc2.AsRegisterPairLow<Register>();
1217 __ Move(TMP, r2);
1218 __ Move(r2, r1);
1219 __ Move(r1, TMP);
1220 r1 = loc1.AsRegisterPairHigh<Register>();
1221 r2 = loc2.AsRegisterPairHigh<Register>();
1222 __ Move(TMP, r2);
1223 __ Move(r2, r1);
1224 __ Move(r1, TMP);
1225 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1226 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1227 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001228 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001229 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1230 : loc2.AsFpuRegister<FRegister>();
1231 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1232 : loc2.AsRegisterPairLow<Register>();
1233 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1234 : loc2.AsRegisterPairHigh<Register>();
1235 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1236 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1237 // unpredictable and the following mfch1 will fail.
1238 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001239 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001241 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242 __ Move(r2_l, TMP);
1243 __ Move(r2_h, AT);
1244 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1245 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1246 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1247 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001248 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1249 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001250 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1251 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001252 __ Move(TMP, reg);
1253 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1254 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1255 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1256 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1257 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1258 : loc2.AsRegisterPairLow<Register>();
1259 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1260 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001261 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001262 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1263 : loc2.GetHighStackIndex(kMipsWordSize);
1264 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001265 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001266 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001267 __ Move(TMP, reg_h);
1268 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1269 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001270 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1271 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1272 : loc2.AsFpuRegister<FRegister>();
1273 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001275 __ MovS(FTMP, reg);
1276 __ LoadSFromOffset(reg, SP, offset);
1277 __ StoreSToOffset(FTMP, SP, offset);
1278 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001280 __ MovD(FTMP, reg);
1281 __ LoadDFromOffset(reg, SP, offset);
1282 __ StoreDToOffset(FTMP, SP, offset);
1283 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284 } else {
1285 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1286 }
1287}
1288
1289void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1290 __ Pop(static_cast<Register>(reg));
1291}
1292
1293void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1294 __ Push(static_cast<Register>(reg));
1295}
1296
1297void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1298 // Allocate a scratch register other than TMP, if available.
1299 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1300 // automatically unspilled when the scratch scope object is destroyed).
1301 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1302 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001303 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1305 __ LoadFromOffset(kLoadWord,
1306 Register(ensure_scratch.GetRegister()),
1307 SP,
1308 index1 + stack_offset);
1309 __ LoadFromOffset(kLoadWord,
1310 TMP,
1311 SP,
1312 index2 + stack_offset);
1313 __ StoreToOffset(kStoreWord,
1314 Register(ensure_scratch.GetRegister()),
1315 SP,
1316 index2 + stack_offset);
1317 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1318 }
1319}
1320
Alexey Frunze73296a72016-06-03 22:51:46 -07001321void CodeGeneratorMIPS::ComputeSpillMask() {
1322 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1323 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1324 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1325 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1326 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1327 // within the stack frame.
1328 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1329 core_spill_mask_ |= (1 << ZERO);
1330 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001331}
1332
1333bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001334 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001335 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1336 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1337 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001338 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001339}
1340
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001341static dwarf::Reg DWARFReg(Register reg) {
1342 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1343}
1344
1345// TODO: mapping of floating-point registers to DWARF.
1346
1347void CodeGeneratorMIPS::GenerateFrameEntry() {
1348 __ Bind(&frame_entry_label_);
1349
1350 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1351
1352 if (do_overflow_check) {
1353 __ LoadFromOffset(kLoadWord,
1354 ZERO,
1355 SP,
1356 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1357 RecordPcInfo(nullptr, 0);
1358 }
1359
1360 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001361 CHECK_EQ(fpu_spill_mask_, 0u);
1362 CHECK_EQ(core_spill_mask_, 1u << RA);
1363 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001364 return;
1365 }
1366
1367 // Make sure the frame size isn't unreasonably large.
1368 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1369 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1370 }
1371
1372 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001373
Alexey Frunze73296a72016-06-03 22:51:46 -07001374 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001375 __ IncreaseFrameSize(ofs);
1376
Alexey Frunze73296a72016-06-03 22:51:46 -07001377 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1378 Register reg = static_cast<Register>(MostSignificantBit(mask));
1379 mask ^= 1u << reg;
1380 ofs -= kMipsWordSize;
1381 // The ZERO register is only included for alignment.
1382 if (reg != ZERO) {
1383 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001384 __ cfi().RelOffset(DWARFReg(reg), ofs);
1385 }
1386 }
1387
Alexey Frunze73296a72016-06-03 22:51:46 -07001388 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1389 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1390 mask ^= 1u << reg;
1391 ofs -= kMipsDoublewordSize;
1392 __ StoreDToOffset(reg, SP, ofs);
1393 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001394 }
1395
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001396 // Save the current method if we need it. Note that we do not
1397 // do this in HCurrentMethod, as the instruction might have been removed
1398 // in the SSA graph.
1399 if (RequiresCurrentMethod()) {
1400 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1401 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001402
1403 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1404 // Initialize should deoptimize flag to 0.
1405 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1406 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001407}
1408
1409void CodeGeneratorMIPS::GenerateFrameExit() {
1410 __ cfi().RememberState();
1411
1412 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001414
Alexey Frunze73296a72016-06-03 22:51:46 -07001415 // For better instruction scheduling restore RA before other registers.
1416 uint32_t ofs = GetFrameSize();
1417 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1418 Register reg = static_cast<Register>(MostSignificantBit(mask));
1419 mask ^= 1u << reg;
1420 ofs -= kMipsWordSize;
1421 // The ZERO register is only included for alignment.
1422 if (reg != ZERO) {
1423 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001424 __ cfi().Restore(DWARFReg(reg));
1425 }
1426 }
1427
Alexey Frunze73296a72016-06-03 22:51:46 -07001428 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1429 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1430 mask ^= 1u << reg;
1431 ofs -= kMipsDoublewordSize;
1432 __ LoadDFromOffset(reg, SP, ofs);
1433 // TODO: __ cfi().Restore(DWARFReg(reg));
1434 }
1435
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001436 size_t frame_size = GetFrameSize();
1437 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1438 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1439 bool reordering = __ SetReorder(false);
1440 if (exchange) {
1441 __ Jr(RA);
1442 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1443 } else {
1444 __ DecreaseFrameSize(frame_size);
1445 __ Jr(RA);
1446 __ Nop(); // In delay slot.
1447 }
1448 __ SetReorder(reordering);
1449 } else {
1450 __ Jr(RA);
1451 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001452 }
1453
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001454 __ cfi().RestoreState();
1455 __ cfi().DefCFAOffset(GetFrameSize());
1456}
1457
1458void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1459 __ Bind(GetLabelOf(block));
1460}
1461
Lena Djokicca8c2952017-05-29 11:31:46 +02001462VectorRegister VectorRegisterFrom(Location location) {
1463 DCHECK(location.IsFpuRegister());
1464 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1465}
1466
Lena Djokic8098da92017-06-28 12:07:50 +02001467void CodeGeneratorMIPS::MoveLocation(Location destination,
1468 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001469 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001470 if (source.Equals(destination)) {
1471 return;
1472 }
1473
Lena Djokic8098da92017-06-28 12:07:50 +02001474 if (source.IsConstant()) {
1475 MoveConstant(destination, source.GetConstant());
1476 } else {
1477 if (destination.IsRegister()) {
1478 if (source.IsRegister()) {
1479 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1480 } else if (source.IsFpuRegister()) {
1481 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1482 } else {
1483 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001484 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001485 }
1486 } else if (destination.IsRegisterPair()) {
1487 if (source.IsRegisterPair()) {
1488 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1489 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1490 } else if (source.IsFpuRegister()) {
1491 Register dst_high = destination.AsRegisterPairHigh<Register>();
1492 Register dst_low = destination.AsRegisterPairLow<Register>();
1493 FRegister src = source.AsFpuRegister<FRegister>();
1494 __ Mfc1(dst_low, src);
1495 __ MoveFromFpuHigh(dst_high, src);
1496 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001497 DCHECK(source.IsDoubleStackSlot())
1498 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001499 int32_t off = source.GetStackIndex();
1500 Register r = destination.AsRegisterPairLow<Register>();
1501 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1502 }
1503 } else if (destination.IsFpuRegister()) {
1504 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001506 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1507 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001508 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001509 FRegister dst = destination.AsFpuRegister<FRegister>();
1510 Register src_high = source.AsRegisterPairHigh<Register>();
1511 Register src_low = source.AsRegisterPairLow<Register>();
1512 __ Mtc1(src_low, dst);
1513 __ MoveToFpuHigh(src_high, dst);
1514 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001515 if (GetGraph()->HasSIMD()) {
1516 __ MoveV(VectorRegisterFrom(destination),
1517 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001518 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001519 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001520 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1521 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001522 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001523 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1524 }
Lena Djokic8098da92017-06-28 12:07:50 +02001525 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001526 } else if (source.IsSIMDStackSlot()) {
1527 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001528 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001530 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1531 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001532 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001533 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1534 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1535 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001536 } else if (destination.IsSIMDStackSlot()) {
1537 if (source.IsFpuRegister()) {
1538 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1539 } else {
1540 DCHECK(source.IsSIMDStackSlot());
1541 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1542 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1543 }
Lena Djokic8098da92017-06-28 12:07:50 +02001544 } else if (destination.IsDoubleStackSlot()) {
1545 int32_t dst_offset = destination.GetStackIndex();
1546 if (source.IsRegisterPair()) {
1547 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1548 } else if (source.IsFpuRegister()) {
1549 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1550 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001551 DCHECK(source.IsDoubleStackSlot())
1552 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001553 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1554 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1555 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1556 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1557 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001558 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001559 DCHECK(destination.IsStackSlot()) << destination;
1560 int32_t dst_offset = destination.GetStackIndex();
1561 if (source.IsRegister()) {
1562 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1563 } else if (source.IsFpuRegister()) {
1564 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1565 } else {
1566 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1567 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1568 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570 }
1571 }
1572}
1573
1574void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1575 if (c->IsIntConstant() || c->IsNullConstant()) {
1576 // Move 32 bit constant.
1577 int32_t value = GetInt32ValueOf(c);
1578 if (destination.IsRegister()) {
1579 Register dst = destination.AsRegister<Register>();
1580 __ LoadConst32(dst, value);
1581 } else {
1582 DCHECK(destination.IsStackSlot())
1583 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001584 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001585 }
1586 } else if (c->IsLongConstant()) {
1587 // Move 64 bit constant.
1588 int64_t value = GetInt64ValueOf(c);
1589 if (destination.IsRegisterPair()) {
1590 Register r_h = destination.AsRegisterPairHigh<Register>();
1591 Register r_l = destination.AsRegisterPairLow<Register>();
1592 __ LoadConst64(r_h, r_l, value);
1593 } else {
1594 DCHECK(destination.IsDoubleStackSlot())
1595 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001596 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001597 }
1598 } else if (c->IsFloatConstant()) {
1599 // Move 32 bit float constant.
1600 int32_t value = GetInt32ValueOf(c);
1601 if (destination.IsFpuRegister()) {
1602 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1603 } else {
1604 DCHECK(destination.IsStackSlot())
1605 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001606 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001607 }
1608 } else {
1609 // Move 64 bit double constant.
1610 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1611 int64_t value = GetInt64ValueOf(c);
1612 if (destination.IsFpuRegister()) {
1613 FRegister fd = destination.AsFpuRegister<FRegister>();
1614 __ LoadDConst64(fd, value, TMP);
1615 } else {
1616 DCHECK(destination.IsDoubleStackSlot())
1617 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001618 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001619 }
1620 }
1621}
1622
1623void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1624 DCHECK(destination.IsRegister());
1625 Register dst = destination.AsRegister<Register>();
1626 __ LoadConst32(dst, value);
1627}
1628
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001629void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1630 if (location.IsRegister()) {
1631 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001632 } else if (location.IsRegisterPair()) {
1633 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1634 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001635 } else {
1636 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1637 }
1638}
1639
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001640template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001641inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1642 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001643 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001644 for (const PcRelativePatchInfo& info : infos) {
1645 const DexFile& dex_file = info.target_dex_file;
1646 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001647 DCHECK(info.label.IsBound());
1648 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001649 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1650 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001651 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1652 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1653 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001654 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001656 }
1657}
1658
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001659void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001660 DCHECK(linker_patches->empty());
1661 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001662 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001663 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001664 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001665 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001666 pc_relative_string_patches_.size() +
1667 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001668 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001669 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1671 pc_relative_method_patches_, linker_patches);
1672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1673 pc_relative_type_patches_, linker_patches);
1674 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1675 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001676 } else {
1677 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1679 pc_relative_type_patches_, linker_patches);
1680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1681 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001682 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1684 method_bss_entry_patches_, linker_patches);
1685 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1686 type_bss_entry_patches_, linker_patches);
1687 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1688 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001689 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001690}
1691
Vladimir Marko65979462017-05-19 17:25:12 +01001692CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001693 MethodReference target_method,
1694 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001695 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001696 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001697 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001698 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001699}
1700
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001701CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001702 MethodReference target_method,
1703 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001704 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001705 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001706 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001707 &method_bss_entry_patches_);
1708}
1709
Alexey Frunze06a46c42016-07-19 15:00:40 -07001710CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001711 const DexFile& dex_file,
1712 dex::TypeIndex type_index,
1713 const PcRelativePatchInfo* info_high) {
1714 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001715}
1716
Vladimir Marko1998cd02017-01-13 13:02:58 +00001717CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001718 const DexFile& dex_file,
1719 dex::TypeIndex type_index,
1720 const PcRelativePatchInfo* info_high) {
1721 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001722}
1723
Vladimir Marko65979462017-05-19 17:25:12 +01001724CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001725 const DexFile& dex_file,
1726 dex::StringIndex string_index,
1727 const PcRelativePatchInfo* info_high) {
1728 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001729}
1730
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001731CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1732 const DexFile& dex_file,
1733 dex::StringIndex string_index,
1734 const PcRelativePatchInfo* info_high) {
1735 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1736}
1737
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001738CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001739 const DexFile& dex_file,
1740 uint32_t offset_or_index,
1741 const PcRelativePatchInfo* info_high,
1742 ArenaDeque<PcRelativePatchInfo>* patches) {
1743 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001744 return &patches->back();
1745}
1746
Alexey Frunze06a46c42016-07-19 15:00:40 -07001747Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1748 return map->GetOrCreate(
1749 value,
1750 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1751}
1752
Alexey Frunze06a46c42016-07-19 15:00:40 -07001753Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001754 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001755}
1756
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001757void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001758 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001759 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001760 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001761 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001762 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001763 if (GetInstructionSetFeatures().IsR6()) {
1764 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001765 __ Bind(&info_high->label);
1766 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001767 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001768 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001769 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001770 } else {
1771 // If base is ZERO, emit NAL to obtain the actual base.
1772 if (base == ZERO) {
1773 // Generate a dummy PC-relative call to obtain PC.
1774 __ Nal();
1775 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001776 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001777 __ Lui(out, /* placeholder */ 0x1234);
1778 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1779 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1780 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001781 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001782 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001783 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001784 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001785 __ Addu(out, out, (base == ZERO) ? RA : base);
1786 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001787 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001788 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001789}
1790
Alexey Frunze627c1a02017-01-30 19:28:14 -08001791CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1792 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001793 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001794 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001795 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1796 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797 return &jit_string_patches_.back();
1798}
1799
1800CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1801 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001802 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001803 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001804 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1805 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001806 return &jit_class_patches_.back();
1807}
1808
1809void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1810 const uint8_t* roots_data,
1811 const CodeGeneratorMIPS::JitPatchInfo& info,
1812 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001813 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1814 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001815 uintptr_t address =
1816 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1817 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1818 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1820 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1821 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1822 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001823 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001824 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1825 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001826 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001827 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001828 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1829 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001830 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001831 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1832 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833}
1834
1835void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1836 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001837 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1838 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001839 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001840 }
1841 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001842 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1843 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001844 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001845 }
1846}
1847
Goran Jakovljevice114da22016-12-26 14:21:43 +01001848void CodeGeneratorMIPS::MarkGCCard(Register object,
1849 Register value,
1850 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851 MipsLabel done;
1852 Register card = AT;
1853 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001854 if (value_can_be_null) {
1855 __ Beqz(value, &done);
1856 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 __ LoadFromOffset(kLoadWord,
1858 card,
1859 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001860 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1862 __ Addu(temp, card, temp);
1863 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001864 if (value_can_be_null) {
1865 __ Bind(&done);
1866 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867}
1868
David Brazdil58282f42016-01-14 12:45:10 +00001869void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001870 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1871 blocked_core_registers_[ZERO] = true;
1872 blocked_core_registers_[K0] = true;
1873 blocked_core_registers_[K1] = true;
1874 blocked_core_registers_[GP] = true;
1875 blocked_core_registers_[SP] = true;
1876 blocked_core_registers_[RA] = true;
1877
1878 // AT and TMP(T8) are used as temporary/scratch registers
1879 // (similar to how AT is used by MIPS assemblers).
1880 blocked_core_registers_[AT] = true;
1881 blocked_core_registers_[TMP] = true;
1882 blocked_fpu_registers_[FTMP] = true;
1883
1884 // Reserve suspend and thread registers.
1885 blocked_core_registers_[S0] = true;
1886 blocked_core_registers_[TR] = true;
1887
1888 // Reserve T9 for function calls
1889 blocked_core_registers_[T9] = true;
1890
1891 // Reserve odd-numbered FPU registers.
1892 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1893 blocked_fpu_registers_[i] = true;
1894 }
1895
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001896 if (GetGraph()->IsDebuggable()) {
1897 // Stubs do not save callee-save floating point registers. If the graph
1898 // is debuggable, we need to deal with these registers differently. For
1899 // now, just block them.
1900 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1901 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1902 }
1903 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001904}
1905
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1907 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1908 return kMipsWordSize;
1909}
1910
1911size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1912 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1913 return kMipsWordSize;
1914}
1915
1916size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001917 if (GetGraph()->HasSIMD()) {
1918 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1919 } else {
1920 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1921 }
1922 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923}
1924
1925size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001926 if (GetGraph()->HasSIMD()) {
1927 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1928 } else {
1929 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1930 }
1931 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001932}
1933
1934void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001935 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936}
1937
1938void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001939 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940}
1941
Serban Constantinescufca16662016-07-14 09:21:59 +01001942constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1943
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1945 HInstruction* instruction,
1946 uint32_t dex_pc,
1947 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001948 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001949 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1950 IsDirectEntrypoint(entrypoint));
1951 if (EntrypointRequiresStackMap(entrypoint)) {
1952 RecordPcInfo(instruction, dex_pc, slow_path);
1953 }
1954}
1955
1956void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1957 HInstruction* instruction,
1958 SlowPathCode* slow_path,
1959 bool direct) {
1960 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1961 GenerateInvokeRuntime(entry_point_offset, direct);
1962}
1963
1964void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001965 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001966 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001967 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001968 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969 // Reserve argument space on stack (for $a0-$a3) for
1970 // entrypoints that directly reference native implementations.
1971 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001972 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001974 } else {
1975 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001976 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001977 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001978}
1979
1980void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1981 Register class_reg) {
1982 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1983 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1984 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1985 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1986 __ Sync(0);
1987 __ Bind(slow_path->GetExitLabel());
1988}
1989
1990void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1991 __ Sync(0); // Only stype 0 is supported.
1992}
1993
1994void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1995 HBasicBlock* successor) {
1996 SuspendCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001997 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 codegen_->AddSlowPath(slow_path);
1999
2000 __ LoadFromOffset(kLoadUnsignedHalfword,
2001 TMP,
2002 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002003 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002004 if (successor == nullptr) {
2005 __ Bnez(TMP, slow_path->GetEntryLabel());
2006 __ Bind(slow_path->GetReturnLabel());
2007 } else {
2008 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2009 __ B(slow_path->GetEntryLabel());
2010 // slow_path will return to GetLabelOf(successor).
2011 }
2012}
2013
2014InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2015 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002016 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 assembler_(codegen->GetAssembler()),
2018 codegen_(codegen) {}
2019
2020void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2021 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002022 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 locations->SetInAt(0, Location::RequiresRegister());
2027 HInstruction* right = instruction->InputAt(1);
2028 bool can_use_imm = false;
2029 if (right->IsConstant()) {
2030 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2031 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2032 can_use_imm = IsUint<16>(imm);
2033 } else if (instruction->IsAdd()) {
2034 can_use_imm = IsInt<16>(imm);
2035 } else {
2036 DCHECK(instruction->IsSub());
2037 can_use_imm = IsInt<16>(-imm);
2038 }
2039 }
2040 if (can_use_imm)
2041 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2042 else
2043 locations->SetInAt(1, Location::RequiresRegister());
2044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2045 break;
2046 }
2047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002050 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 break;
2053 }
2054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002055 case DataType::Type::kFloat32:
2056 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 DCHECK(instruction->IsAdd() || instruction->IsSub());
2058 locations->SetInAt(0, Location::RequiresFpuRegister());
2059 locations->SetInAt(1, Location::RequiresFpuRegister());
2060 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2061 break;
2062
2063 default:
2064 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2065 }
2066}
2067
2068void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002070 LocationSummary* locations = instruction->GetLocations();
2071
2072 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 Register dst = locations->Out().AsRegister<Register>();
2075 Register lhs = locations->InAt(0).AsRegister<Register>();
2076 Location rhs_location = locations->InAt(1);
2077
2078 Register rhs_reg = ZERO;
2079 int32_t rhs_imm = 0;
2080 bool use_imm = rhs_location.IsConstant();
2081 if (use_imm) {
2082 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2083 } else {
2084 rhs_reg = rhs_location.AsRegister<Register>();
2085 }
2086
2087 if (instruction->IsAnd()) {
2088 if (use_imm)
2089 __ Andi(dst, lhs, rhs_imm);
2090 else
2091 __ And(dst, lhs, rhs_reg);
2092 } else if (instruction->IsOr()) {
2093 if (use_imm)
2094 __ Ori(dst, lhs, rhs_imm);
2095 else
2096 __ Or(dst, lhs, rhs_reg);
2097 } else if (instruction->IsXor()) {
2098 if (use_imm)
2099 __ Xori(dst, lhs, rhs_imm);
2100 else
2101 __ Xor(dst, lhs, rhs_reg);
2102 } else if (instruction->IsAdd()) {
2103 if (use_imm)
2104 __ Addiu(dst, lhs, rhs_imm);
2105 else
2106 __ Addu(dst, lhs, rhs_reg);
2107 } else {
2108 DCHECK(instruction->IsSub());
2109 if (use_imm)
2110 __ Addiu(dst, lhs, -rhs_imm);
2111 else
2112 __ Subu(dst, lhs, rhs_reg);
2113 }
2114 break;
2115 }
2116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2119 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2120 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2121 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002122 Location rhs_location = locations->InAt(1);
2123 bool use_imm = rhs_location.IsConstant();
2124 if (!use_imm) {
2125 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2126 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2127 if (instruction->IsAnd()) {
2128 __ And(dst_low, lhs_low, rhs_low);
2129 __ And(dst_high, lhs_high, rhs_high);
2130 } else if (instruction->IsOr()) {
2131 __ Or(dst_low, lhs_low, rhs_low);
2132 __ Or(dst_high, lhs_high, rhs_high);
2133 } else if (instruction->IsXor()) {
2134 __ Xor(dst_low, lhs_low, rhs_low);
2135 __ Xor(dst_high, lhs_high, rhs_high);
2136 } else if (instruction->IsAdd()) {
2137 if (lhs_low == rhs_low) {
2138 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2139 __ Slt(TMP, lhs_low, ZERO);
2140 __ Addu(dst_low, lhs_low, rhs_low);
2141 } else {
2142 __ Addu(dst_low, lhs_low, rhs_low);
2143 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2144 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2145 }
2146 __ Addu(dst_high, lhs_high, rhs_high);
2147 __ Addu(dst_high, dst_high, TMP);
2148 } else {
2149 DCHECK(instruction->IsSub());
2150 __ Sltu(TMP, lhs_low, rhs_low);
2151 __ Subu(dst_low, lhs_low, rhs_low);
2152 __ Subu(dst_high, lhs_high, rhs_high);
2153 __ Subu(dst_high, dst_high, TMP);
2154 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002156 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2157 if (instruction->IsOr()) {
2158 uint32_t low = Low32Bits(value);
2159 uint32_t high = High32Bits(value);
2160 if (IsUint<16>(low)) {
2161 if (dst_low != lhs_low || low != 0) {
2162 __ Ori(dst_low, lhs_low, low);
2163 }
2164 } else {
2165 __ LoadConst32(TMP, low);
2166 __ Or(dst_low, lhs_low, TMP);
2167 }
2168 if (IsUint<16>(high)) {
2169 if (dst_high != lhs_high || high != 0) {
2170 __ Ori(dst_high, lhs_high, high);
2171 }
2172 } else {
2173 if (high != low) {
2174 __ LoadConst32(TMP, high);
2175 }
2176 __ Or(dst_high, lhs_high, TMP);
2177 }
2178 } else if (instruction->IsXor()) {
2179 uint32_t low = Low32Bits(value);
2180 uint32_t high = High32Bits(value);
2181 if (IsUint<16>(low)) {
2182 if (dst_low != lhs_low || low != 0) {
2183 __ Xori(dst_low, lhs_low, low);
2184 }
2185 } else {
2186 __ LoadConst32(TMP, low);
2187 __ Xor(dst_low, lhs_low, TMP);
2188 }
2189 if (IsUint<16>(high)) {
2190 if (dst_high != lhs_high || high != 0) {
2191 __ Xori(dst_high, lhs_high, high);
2192 }
2193 } else {
2194 if (high != low) {
2195 __ LoadConst32(TMP, high);
2196 }
2197 __ Xor(dst_high, lhs_high, TMP);
2198 }
2199 } else if (instruction->IsAnd()) {
2200 uint32_t low = Low32Bits(value);
2201 uint32_t high = High32Bits(value);
2202 if (IsUint<16>(low)) {
2203 __ Andi(dst_low, lhs_low, low);
2204 } else if (low != 0xFFFFFFFF) {
2205 __ LoadConst32(TMP, low);
2206 __ And(dst_low, lhs_low, TMP);
2207 } else if (dst_low != lhs_low) {
2208 __ Move(dst_low, lhs_low);
2209 }
2210 if (IsUint<16>(high)) {
2211 __ Andi(dst_high, lhs_high, high);
2212 } else if (high != 0xFFFFFFFF) {
2213 if (high != low) {
2214 __ LoadConst32(TMP, high);
2215 }
2216 __ And(dst_high, lhs_high, TMP);
2217 } else if (dst_high != lhs_high) {
2218 __ Move(dst_high, lhs_high);
2219 }
2220 } else {
2221 if (instruction->IsSub()) {
2222 value = -value;
2223 } else {
2224 DCHECK(instruction->IsAdd());
2225 }
2226 int32_t low = Low32Bits(value);
2227 int32_t high = High32Bits(value);
2228 if (IsInt<16>(low)) {
2229 if (dst_low != lhs_low || low != 0) {
2230 __ Addiu(dst_low, lhs_low, low);
2231 }
2232 if (low != 0) {
2233 __ Sltiu(AT, dst_low, low);
2234 }
2235 } else {
2236 __ LoadConst32(TMP, low);
2237 __ Addu(dst_low, lhs_low, TMP);
2238 __ Sltu(AT, dst_low, TMP);
2239 }
2240 if (IsInt<16>(high)) {
2241 if (dst_high != lhs_high || high != 0) {
2242 __ Addiu(dst_high, lhs_high, high);
2243 }
2244 } else {
2245 if (high != low) {
2246 __ LoadConst32(TMP, high);
2247 }
2248 __ Addu(dst_high, lhs_high, TMP);
2249 }
2250 if (low != 0) {
2251 __ Addu(dst_high, dst_high, AT);
2252 }
2253 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254 }
2255 break;
2256 }
2257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 case DataType::Type::kFloat32:
2259 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2261 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2262 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2263 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002265 __ AddS(dst, lhs, rhs);
2266 } else {
2267 __ AddD(dst, lhs, rhs);
2268 }
2269 } else {
2270 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 __ SubS(dst, lhs, rhs);
2273 } else {
2274 __ SubD(dst, lhs, rhs);
2275 }
2276 }
2277 break;
2278 }
2279
2280 default:
2281 LOG(FATAL) << "Unexpected binary operation type " << type;
2282 }
2283}
2284
2285void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002286 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287
Vladimir Markoca6fff82017-10-03 14:49:14 +01002288 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002290 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002292 locations->SetInAt(0, Location::RequiresRegister());
2293 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2295 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297 locations->SetInAt(0, Location::RequiresRegister());
2298 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2299 locations->SetOut(Location::RequiresRegister());
2300 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 default:
2302 LOG(FATAL) << "Unexpected shift type " << type;
2303 }
2304}
2305
2306static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2307
2308void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002309 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312
2313 Location rhs_location = locations->InAt(1);
2314 bool use_imm = rhs_location.IsConstant();
2315 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2316 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002317 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002319 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002320 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2321 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002322
2323 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 Register dst = locations->Out().AsRegister<Register>();
2326 Register lhs = locations->InAt(0).AsRegister<Register>();
2327 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002328 if (shift_value == 0) {
2329 if (dst != lhs) {
2330 __ Move(dst, lhs);
2331 }
2332 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002333 __ Sll(dst, lhs, shift_value);
2334 } else if (instr->IsShr()) {
2335 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002336 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002338 } else {
2339 if (has_ins_rotr) {
2340 __ Rotr(dst, lhs, shift_value);
2341 } else {
2342 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2343 __ Srl(dst, lhs, shift_value);
2344 __ Or(dst, dst, TMP);
2345 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 }
2347 } else {
2348 if (instr->IsShl()) {
2349 __ Sllv(dst, lhs, rhs_reg);
2350 } else if (instr->IsShr()) {
2351 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002352 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 } else {
2355 if (has_ins_rotr) {
2356 __ Rotrv(dst, lhs, rhs_reg);
2357 } else {
2358 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002359 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2360 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2361 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2362 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2363 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002364 __ Sllv(TMP, lhs, TMP);
2365 __ Srlv(dst, lhs, rhs_reg);
2366 __ Or(dst, dst, TMP);
2367 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 }
2369 }
2370 break;
2371 }
2372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2375 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2376 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2377 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2378 if (use_imm) {
2379 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002380 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002383 if (instr->IsShl()) {
2384 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2385 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2386 __ Sll(dst_low, lhs_low, shift_value);
2387 } else if (instr->IsShr()) {
2388 __ Srl(dst_low, lhs_low, shift_value);
2389 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2390 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002391 } else if (instr->IsUShr()) {
2392 __ Srl(dst_low, lhs_low, shift_value);
2393 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2394 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002395 } else {
2396 __ Srl(dst_low, lhs_low, shift_value);
2397 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2398 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002399 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002402 if (instr->IsShl()) {
2403 __ Sll(dst_low, lhs_low, shift_value);
2404 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2405 __ Sll(dst_high, lhs_high, shift_value);
2406 __ Or(dst_high, dst_high, TMP);
2407 } else if (instr->IsShr()) {
2408 __ Sra(dst_high, lhs_high, shift_value);
2409 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2410 __ Srl(dst_low, lhs_low, shift_value);
2411 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002412 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002413 __ Srl(dst_high, lhs_high, shift_value);
2414 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2415 __ Srl(dst_low, lhs_low, shift_value);
2416 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002417 } else {
2418 __ Srl(TMP, lhs_low, shift_value);
2419 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2420 __ Or(dst_low, dst_low, TMP);
2421 __ Srl(TMP, lhs_high, shift_value);
2422 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2423 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002424 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 }
2426 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002427 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002428 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 __ Move(dst_low, ZERO);
2431 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002432 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002435 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002437 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002438 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 // 64-bit rotation by 32 is just a swap.
2440 __ Move(dst_low, lhs_high);
2441 __ Move(dst_high, lhs_low);
2442 } else {
2443 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002444 __ Srl(dst_low, lhs_high, shift_value_high);
2445 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2446 __ Srl(dst_high, lhs_low, shift_value_high);
2447 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002448 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002449 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2450 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002451 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002452 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2453 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002454 __ Or(dst_high, dst_high, TMP);
2455 }
2456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002457 }
2458 }
2459 } else {
2460 MipsLabel done;
2461 if (instr->IsShl()) {
2462 __ Sllv(dst_low, lhs_low, rhs_reg);
2463 __ Nor(AT, ZERO, rhs_reg);
2464 __ Srl(TMP, lhs_low, 1);
2465 __ Srlv(TMP, TMP, AT);
2466 __ Sllv(dst_high, lhs_high, rhs_reg);
2467 __ Or(dst_high, dst_high, TMP);
2468 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2469 __ Beqz(TMP, &done);
2470 __ Move(dst_high, dst_low);
2471 __ Move(dst_low, ZERO);
2472 } else if (instr->IsShr()) {
2473 __ Srav(dst_high, lhs_high, rhs_reg);
2474 __ Nor(AT, ZERO, rhs_reg);
2475 __ Sll(TMP, lhs_high, 1);
2476 __ Sllv(TMP, TMP, AT);
2477 __ Srlv(dst_low, lhs_low, rhs_reg);
2478 __ Or(dst_low, dst_low, TMP);
2479 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2480 __ Beqz(TMP, &done);
2481 __ Move(dst_low, dst_high);
2482 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002483 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002484 __ Srlv(dst_high, lhs_high, rhs_reg);
2485 __ Nor(AT, ZERO, rhs_reg);
2486 __ Sll(TMP, lhs_high, 1);
2487 __ Sllv(TMP, TMP, AT);
2488 __ Srlv(dst_low, lhs_low, rhs_reg);
2489 __ Or(dst_low, dst_low, TMP);
2490 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2491 __ Beqz(TMP, &done);
2492 __ Move(dst_low, dst_high);
2493 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002494 } else {
2495 __ Nor(AT, ZERO, rhs_reg);
2496 __ Srlv(TMP, lhs_low, rhs_reg);
2497 __ Sll(dst_low, lhs_high, 1);
2498 __ Sllv(dst_low, dst_low, AT);
2499 __ Or(dst_low, dst_low, TMP);
2500 __ Srlv(TMP, lhs_high, rhs_reg);
2501 __ Sll(dst_high, lhs_low, 1);
2502 __ Sllv(dst_high, dst_high, AT);
2503 __ Or(dst_high, dst_high, TMP);
2504 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2505 __ Beqz(TMP, &done);
2506 __ Move(TMP, dst_high);
2507 __ Move(dst_high, dst_low);
2508 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002509 }
2510 __ Bind(&done);
2511 }
2512 break;
2513 }
2514
2515 default:
2516 LOG(FATAL) << "Unexpected shift operation type " << type;
2517 }
2518}
2519
2520void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2521 HandleBinaryOp(instruction);
2522}
2523
2524void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2525 HandleBinaryOp(instruction);
2526}
2527
2528void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2529 HandleBinaryOp(instruction);
2530}
2531
2532void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2533 HandleBinaryOp(instruction);
2534}
2535
2536void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002538 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002540 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002541 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2542 object_array_get_with_read_barrier
2543 ? LocationSummary::kCallOnSlowPath
2544 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002545 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2546 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002548 locations->SetInAt(0, Location::RequiresRegister());
2549 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2552 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002553 // The output overlaps in the case of an object array get with
2554 // read barriers enabled: we do not want the move to overwrite the
2555 // array's location, as we need it to emit the read barrier.
2556 locations->SetOut(Location::RequiresRegister(),
2557 object_array_get_with_read_barrier
2558 ? Location::kOutputOverlap
2559 : Location::kNoOutputOverlap);
2560 }
2561 // We need a temporary register for the read barrier marking slow
2562 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2563 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002564 bool temp_needed = instruction->GetIndex()->IsConstant()
2565 ? !kBakerReadBarrierThunksEnableForFields
2566 : !kBakerReadBarrierThunksEnableForArrays;
2567 if (temp_needed) {
2568 locations->AddTemp(Location::RequiresRegister());
2569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002570 }
2571}
2572
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002573static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2574 auto null_checker = [codegen, instruction]() {
2575 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002576 };
2577 return null_checker;
2578}
2579
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2581 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002582 Location obj_loc = locations->InAt(0);
2583 Register obj = obj_loc.AsRegister<Register>();
2584 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002586 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002587 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002590 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2591 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002593 case DataType::Type::kBool:
2594 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002595 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 if (index.IsConstant()) {
2597 size_t offset =
2598 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002599 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 } else {
2601 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002602 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 }
2604 break;
2605 }
2606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002608 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 if (index.IsConstant()) {
2610 size_t offset =
2611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002612 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 } else {
2614 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002615 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002616 }
2617 break;
2618 }
2619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002621 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002622 if (maybe_compressed_char_at) {
2623 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2624 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2625 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2626 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2627 "Expecting 0=compressed, 1=uncompressed");
2628 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002630 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2631 if (maybe_compressed_char_at) {
2632 MipsLabel uncompressed_load, done;
2633 __ Bnez(TMP, &uncompressed_load);
2634 __ LoadFromOffset(kLoadUnsignedByte,
2635 out,
2636 obj,
2637 data_offset + (const_index << TIMES_1));
2638 __ B(&done);
2639 __ Bind(&uncompressed_load);
2640 __ LoadFromOffset(kLoadUnsignedHalfword,
2641 out,
2642 obj,
2643 data_offset + (const_index << TIMES_2));
2644 __ Bind(&done);
2645 } else {
2646 __ LoadFromOffset(kLoadUnsignedHalfword,
2647 out,
2648 obj,
2649 data_offset + (const_index << TIMES_2),
2650 null_checker);
2651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002652 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002653 Register index_reg = index.AsRegister<Register>();
2654 if (maybe_compressed_char_at) {
2655 MipsLabel uncompressed_load, done;
2656 __ Bnez(TMP, &uncompressed_load);
2657 __ Addu(TMP, obj, index_reg);
2658 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2659 __ B(&done);
2660 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002661 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002662 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2663 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002664 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2665 __ Addu(TMP, index_reg, obj);
2666 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002667 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002668 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002669 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2670 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 }
2672 break;
2673 }
2674
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002675 case DataType::Type::kInt16: {
2676 Register out = out_loc.AsRegister<Register>();
2677 if (index.IsConstant()) {
2678 size_t offset =
2679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2680 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002681 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2682 __ Addu(TMP, index.AsRegister<Register>(), obj);
2683 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002684 } else {
2685 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2686 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2687 }
2688 break;
2689 }
2690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002693 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694 if (index.IsConstant()) {
2695 size_t offset =
2696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002697 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002698 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2699 __ Addu(TMP, index.AsRegister<Register>(), obj);
2700 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002702 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002703 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 }
2705 break;
2706 }
2707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002709 static_assert(
2710 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2711 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2712 // /* HeapReference<Object> */ out =
2713 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2714 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002715 bool temp_needed = index.IsConstant()
2716 ? !kBakerReadBarrierThunksEnableForFields
2717 : !kBakerReadBarrierThunksEnableForArrays;
2718 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002719 // Note that a potential implicit null check is handled in this
2720 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002721 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2722 if (index.IsConstant()) {
2723 // Array load with a constant index can be treated as a field load.
2724 size_t offset =
2725 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2726 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2727 out_loc,
2728 obj,
2729 offset,
2730 temp,
2731 /* needs_null_check */ false);
2732 } else {
2733 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2734 out_loc,
2735 obj,
2736 data_offset,
2737 index,
2738 temp,
2739 /* needs_null_check */ false);
2740 }
Alexey Frunze15958152017-02-09 19:08:30 -08002741 } else {
2742 Register out = out_loc.AsRegister<Register>();
2743 if (index.IsConstant()) {
2744 size_t offset =
2745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2746 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2747 // If read barriers are enabled, emit read barriers other than
2748 // Baker's using a slow path (and also unpoison the loaded
2749 // reference, if heap poisoning is enabled).
2750 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2751 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002752 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002753 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2754 // If read barriers are enabled, emit read barriers other than
2755 // Baker's using a slow path (and also unpoison the loaded
2756 // reference, if heap poisoning is enabled).
2757 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2758 out_loc,
2759 out_loc,
2760 obj_loc,
2761 data_offset,
2762 index);
2763 }
2764 }
2765 break;
2766 }
2767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002769 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002770 if (index.IsConstant()) {
2771 size_t offset =
2772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002773 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002774 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2775 __ Addu(TMP, index.AsRegister<Register>(), obj);
2776 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002777 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002778 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002779 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002780 }
2781 break;
2782 }
2783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002784 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002785 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002786 if (index.IsConstant()) {
2787 size_t offset =
2788 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002789 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002790 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2791 __ Addu(TMP, index.AsRegister<Register>(), obj);
2792 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002793 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002794 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002795 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002796 }
2797 break;
2798 }
2799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002801 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802 if (index.IsConstant()) {
2803 size_t offset =
2804 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002805 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002806 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2807 __ Addu(TMP, index.AsRegister<Register>(), obj);
2808 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002809 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002810 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002811 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 }
2813 break;
2814 }
2815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2818 UNREACHABLE();
2819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820}
2821
2822void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002823 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824 locations->SetInAt(0, Location::RequiresRegister());
2825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2826}
2827
2828void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2829 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002830 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002831 Register obj = locations->InAt(0).AsRegister<Register>();
2832 Register out = locations->Out().AsRegister<Register>();
2833 __ LoadFromOffset(kLoadWord, out, obj, offset);
2834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002835 // Mask out compression flag from String's array length.
2836 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2837 __ Srl(out, out, 1u);
2838 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839}
2840
Alexey Frunzef58b2482016-09-02 22:14:06 -07002841Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2842 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2843 ? Location::ConstantLocation(instruction->AsConstant())
2844 : Location::RequiresRegister();
2845}
2846
2847Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2848 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2849 // We can store a non-zero float or double constant without first loading it into the FPU,
2850 // but we should only prefer this if the constant has a single use.
2851 if (instruction->IsConstant() &&
2852 (instruction->AsConstant()->IsZeroBitPattern() ||
2853 instruction->GetUses().HasExactlyOneElement())) {
2854 return Location::ConstantLocation(instruction->AsConstant());
2855 // Otherwise fall through and require an FPU register for the constant.
2856 }
2857 return Location::RequiresFpuRegister();
2858}
2859
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002862
2863 bool needs_write_barrier =
2864 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2865 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2866
Vladimir Markoca6fff82017-10-03 14:49:14 +01002867 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002869 may_need_runtime_call_for_type_check ?
2870 LocationSummary::kCallOnSlowPath :
2871 LocationSummary::kNoCall);
2872
2873 locations->SetInAt(0, Location::RequiresRegister());
2874 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002876 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002877 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002878 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2879 }
2880 if (needs_write_barrier) {
2881 // Temporary register for the write barrier.
2882 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002883 }
2884}
2885
2886void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2887 LocationSummary* locations = instruction->GetLocations();
2888 Register obj = locations->InAt(0).AsRegister<Register>();
2889 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002890 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002892 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002893 bool needs_write_barrier =
2894 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002895 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002896 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897
2898 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002900 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002902 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002903 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002904 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002906 __ Addu(base_reg, obj, index.AsRegister<Register>());
2907 }
2908 if (value_location.IsConstant()) {
2909 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2910 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2911 } else {
2912 Register value = value_location.AsRegister<Register>();
2913 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914 }
2915 break;
2916 }
2917
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002918 case DataType::Type::kUint16:
2919 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002921 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002922 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002923 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2924 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002925 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002926 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002927 }
2928 if (value_location.IsConstant()) {
2929 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2930 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2931 } else {
2932 Register value = value_location.AsRegister<Register>();
2933 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 }
2935 break;
2936 }
2937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2940 if (index.IsConstant()) {
2941 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002942 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2943 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002944 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002945 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002946 }
2947 if (value_location.IsConstant()) {
2948 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2949 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2950 } else {
2951 Register value = value_location.AsRegister<Register>();
2952 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2953 }
2954 break;
2955 }
2956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002958 if (value_location.IsConstant()) {
2959 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002960 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002961 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002962 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002964 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 }
Alexey Frunze15958152017-02-09 19:08:30 -08002966 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2967 DCHECK_EQ(value, 0);
2968 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2969 DCHECK(!needs_write_barrier);
2970 DCHECK(!may_need_runtime_call_for_type_check);
2971 break;
2972 }
2973
2974 DCHECK(needs_write_barrier);
2975 Register value = value_location.AsRegister<Register>();
2976 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2977 Register temp2 = TMP; // Doesn't need to survive slow path.
2978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2979 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2980 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2981 MipsLabel done;
2982 SlowPathCodeMIPS* slow_path = nullptr;
2983
2984 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002985 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002986 codegen_->AddSlowPath(slow_path);
2987 if (instruction->GetValueCanBeNull()) {
2988 MipsLabel non_zero;
2989 __ Bnez(value, &non_zero);
2990 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2991 if (index.IsConstant()) {
2992 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002993 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2994 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002995 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002996 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002997 }
Alexey Frunze15958152017-02-09 19:08:30 -08002998 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2999 __ B(&done);
3000 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003001 }
Alexey Frunze15958152017-02-09 19:08:30 -08003002
3003 // Note that when read barriers are enabled, the type checks
3004 // are performed without read barriers. This is fine, even in
3005 // the case where a class object is in the from-space after
3006 // the flip, as a comparison involving such a type would not
3007 // produce a false positive; it may of course produce a false
3008 // negative, in which case we would take the ArraySet slow
3009 // path.
3010
3011 // /* HeapReference<Class> */ temp1 = obj->klass_
3012 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3013 __ MaybeUnpoisonHeapReference(temp1);
3014
3015 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3016 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3017 // /* HeapReference<Class> */ temp2 = value->klass_
3018 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3019 // If heap poisoning is enabled, no need to unpoison `temp1`
3020 // nor `temp2`, as we are comparing two poisoned references.
3021
3022 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3023 MipsLabel do_put;
3024 __ Beq(temp1, temp2, &do_put);
3025 // If heap poisoning is enabled, the `temp1` reference has
3026 // not been unpoisoned yet; unpoison it now.
3027 __ MaybeUnpoisonHeapReference(temp1);
3028
3029 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3030 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3031 // If heap poisoning is enabled, no need to unpoison
3032 // `temp1`, as we are comparing against null below.
3033 __ Bnez(temp1, slow_path->GetEntryLabel());
3034 __ Bind(&do_put);
3035 } else {
3036 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3037 }
3038 }
3039
3040 Register source = value;
3041 if (kPoisonHeapReferences) {
3042 // Note that in the case where `value` is a null reference,
3043 // we do not enter this block, as a null reference does not
3044 // need poisoning.
3045 __ Move(temp1, value);
3046 __ PoisonHeapReference(temp1);
3047 source = temp1;
3048 }
3049
3050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3051 if (index.IsConstant()) {
3052 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003053 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003054 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003055 }
3056 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3057
3058 if (!may_need_runtime_call_for_type_check) {
3059 codegen_->MaybeRecordImplicitNullCheck(instruction);
3060 }
3061
3062 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3063
3064 if (done.IsLinked()) {
3065 __ Bind(&done);
3066 }
3067
3068 if (slow_path != nullptr) {
3069 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003070 }
3071 break;
3072 }
3073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003076 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003077 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003078 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3079 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003081 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003082 }
3083 if (value_location.IsConstant()) {
3084 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3085 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3086 } else {
3087 Register value = value_location.AsRegisterPairLow<Register>();
3088 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 }
3090 break;
3091 }
3092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003093 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003094 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003095 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003096 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003097 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3098 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003100 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003101 }
3102 if (value_location.IsConstant()) {
3103 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3104 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3105 } else {
3106 FRegister value = value_location.AsFpuRegister<FRegister>();
3107 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 }
3109 break;
3110 }
3111
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003115 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003116 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3117 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003118 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003119 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003120 }
3121 if (value_location.IsConstant()) {
3122 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3123 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3124 } else {
3125 FRegister value = value_location.AsFpuRegister<FRegister>();
3126 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 }
3128 break;
3129 }
3130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003132 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3133 UNREACHABLE();
3134 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135}
3136
Lena Djokica2901602017-09-21 13:50:52 +02003137void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3138 HIntermediateArrayAddressIndex* instruction) {
3139 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003140 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003141
3142 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3143
3144 locations->SetInAt(0, Location::RequiresRegister());
3145 locations->SetInAt(1, Location::ConstantLocation(shift));
3146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3147}
3148
3149void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3150 HIntermediateArrayAddressIndex* instruction) {
3151 LocationSummary* locations = instruction->GetLocations();
3152 Register index_reg = locations->InAt(0).AsRegister<Register>();
3153 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3154 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3155}
3156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003157void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003158 RegisterSet caller_saves = RegisterSet::Empty();
3159 InvokeRuntimeCallingConvention calling_convention;
3160 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3162 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003163 locations->SetInAt(0, Location::RequiresRegister());
3164 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003165}
3166
3167void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3168 LocationSummary* locations = instruction->GetLocations();
3169 BoundsCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003170 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003171 codegen_->AddSlowPath(slow_path);
3172
3173 Register index = locations->InAt(0).AsRegister<Register>();
3174 Register length = locations->InAt(1).AsRegister<Register>();
3175
3176 // length is limited by the maximum positive signed 32-bit integer.
3177 // Unsigned comparison of length and index checks for index < 0
3178 // and for length <= index simultaneously.
3179 __ Bgeu(index, length, slow_path->GetEntryLabel());
3180}
3181
Alexey Frunze15958152017-02-09 19:08:30 -08003182// Temp is used for read barrier.
3183static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3184 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003185 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003186 (kUseBakerReadBarrier ||
3187 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3188 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3189 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3190 return 1;
3191 }
3192 return 0;
3193}
3194
3195// Extra temp is used for read barrier.
3196static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3197 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3198}
3199
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003200void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003201 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3202 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3203
3204 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3205 switch (type_check_kind) {
3206 case TypeCheckKind::kExactCheck:
3207 case TypeCheckKind::kAbstractClassCheck:
3208 case TypeCheckKind::kClassHierarchyCheck:
3209 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003210 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003211 ? LocationSummary::kCallOnSlowPath
3212 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3213 break;
3214 case TypeCheckKind::kArrayCheck:
3215 case TypeCheckKind::kUnresolvedCheck:
3216 case TypeCheckKind::kInterfaceCheck:
3217 call_kind = LocationSummary::kCallOnSlowPath;
3218 break;
3219 }
3220
Vladimir Markoca6fff82017-10-03 14:49:14 +01003221 LocationSummary* locations =
3222 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003223 locations->SetInAt(0, Location::RequiresRegister());
3224 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003225 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003226}
3227
3228void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003229 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003230 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003231 Location obj_loc = locations->InAt(0);
3232 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003233 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003234 Location temp_loc = locations->GetTemp(0);
3235 Register temp = temp_loc.AsRegister<Register>();
3236 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3237 DCHECK_LE(num_temps, 2u);
3238 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003239 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3240 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3241 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3242 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3243 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3244 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3245 const uint32_t object_array_data_offset =
3246 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3247 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003248
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003249 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3250 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3251 // read barriers is done for performance and code size reasons.
3252 bool is_type_check_slow_path_fatal = false;
3253 if (!kEmitCompilerReadBarrier) {
3254 is_type_check_slow_path_fatal =
3255 (type_check_kind == TypeCheckKind::kExactCheck ||
3256 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3257 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3258 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3259 !instruction->CanThrowIntoCatchBlock();
3260 }
3261 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003262 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3263 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003264 codegen_->AddSlowPath(slow_path);
3265
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003266 // Avoid this check if we know `obj` is not null.
3267 if (instruction->MustDoNullCheck()) {
3268 __ Beqz(obj, &done);
3269 }
3270
3271 switch (type_check_kind) {
3272 case TypeCheckKind::kExactCheck:
3273 case TypeCheckKind::kArrayCheck: {
3274 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003275 GenerateReferenceLoadTwoRegisters(instruction,
3276 temp_loc,
3277 obj_loc,
3278 class_offset,
3279 maybe_temp2_loc,
3280 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003281 // Jump to slow path for throwing the exception or doing a
3282 // more involved array check.
3283 __ Bne(temp, cls, slow_path->GetEntryLabel());
3284 break;
3285 }
3286
3287 case TypeCheckKind::kAbstractClassCheck: {
3288 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003289 GenerateReferenceLoadTwoRegisters(instruction,
3290 temp_loc,
3291 obj_loc,
3292 class_offset,
3293 maybe_temp2_loc,
3294 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003295 // If the class is abstract, we eagerly fetch the super class of the
3296 // object to avoid doing a comparison we know will fail.
3297 MipsLabel loop;
3298 __ Bind(&loop);
3299 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003300 GenerateReferenceLoadOneRegister(instruction,
3301 temp_loc,
3302 super_offset,
3303 maybe_temp2_loc,
3304 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003305 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3306 // exception.
3307 __ Beqz(temp, slow_path->GetEntryLabel());
3308 // Otherwise, compare the classes.
3309 __ Bne(temp, cls, &loop);
3310 break;
3311 }
3312
3313 case TypeCheckKind::kClassHierarchyCheck: {
3314 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003315 GenerateReferenceLoadTwoRegisters(instruction,
3316 temp_loc,
3317 obj_loc,
3318 class_offset,
3319 maybe_temp2_loc,
3320 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003321 // Walk over the class hierarchy to find a match.
3322 MipsLabel loop;
3323 __ Bind(&loop);
3324 __ Beq(temp, cls, &done);
3325 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003326 GenerateReferenceLoadOneRegister(instruction,
3327 temp_loc,
3328 super_offset,
3329 maybe_temp2_loc,
3330 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003331 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3332 // exception. Otherwise, jump to the beginning of the loop.
3333 __ Bnez(temp, &loop);
3334 __ B(slow_path->GetEntryLabel());
3335 break;
3336 }
3337
3338 case TypeCheckKind::kArrayObjectCheck: {
3339 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003340 GenerateReferenceLoadTwoRegisters(instruction,
3341 temp_loc,
3342 obj_loc,
3343 class_offset,
3344 maybe_temp2_loc,
3345 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003346 // Do an exact check.
3347 __ Beq(temp, cls, &done);
3348 // Otherwise, we need to check that the object's class is a non-primitive array.
3349 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003350 GenerateReferenceLoadOneRegister(instruction,
3351 temp_loc,
3352 component_offset,
3353 maybe_temp2_loc,
3354 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003355 // If the component type is null, jump to the slow path to throw the exception.
3356 __ Beqz(temp, slow_path->GetEntryLabel());
3357 // Otherwise, the object is indeed an array, further check that this component
3358 // type is not a primitive type.
3359 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3360 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3361 __ Bnez(temp, slow_path->GetEntryLabel());
3362 break;
3363 }
3364
3365 case TypeCheckKind::kUnresolvedCheck:
3366 // We always go into the type check slow path for the unresolved check case.
3367 // We cannot directly call the CheckCast runtime entry point
3368 // without resorting to a type checking slow path here (i.e. by
3369 // calling InvokeRuntime directly), as it would require to
3370 // assign fixed registers for the inputs of this HInstanceOf
3371 // instruction (following the runtime calling convention), which
3372 // might be cluttered by the potential first read barrier
3373 // emission at the beginning of this method.
3374 __ B(slow_path->GetEntryLabel());
3375 break;
3376
3377 case TypeCheckKind::kInterfaceCheck: {
3378 // Avoid read barriers to improve performance of the fast path. We can not get false
3379 // positives by doing this.
3380 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003381 GenerateReferenceLoadTwoRegisters(instruction,
3382 temp_loc,
3383 obj_loc,
3384 class_offset,
3385 maybe_temp2_loc,
3386 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003387 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003388 GenerateReferenceLoadTwoRegisters(instruction,
3389 temp_loc,
3390 temp_loc,
3391 iftable_offset,
3392 maybe_temp2_loc,
3393 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003394 // Iftable is never null.
3395 __ Lw(TMP, temp, array_length_offset);
3396 // Loop through the iftable and check if any class matches.
3397 MipsLabel loop;
3398 __ Bind(&loop);
3399 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3400 __ Beqz(TMP, slow_path->GetEntryLabel());
3401 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3402 __ MaybeUnpoisonHeapReference(AT);
3403 // Go to next interface.
3404 __ Addiu(TMP, TMP, -2);
3405 // Compare the classes and continue the loop if they do not match.
3406 __ Bne(AT, cls, &loop);
3407 break;
3408 }
3409 }
3410
3411 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 __ Bind(slow_path->GetExitLabel());
3413}
3414
3415void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3416 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003417 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003418 locations->SetInAt(0, Location::RequiresRegister());
3419 if (check->HasUses()) {
3420 locations->SetOut(Location::SameAsFirstInput());
3421 }
3422}
3423
3424void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3425 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003426 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003427 check->GetLoadClass(),
3428 check,
3429 check->GetDexPc(),
3430 true);
3431 codegen_->AddSlowPath(slow_path);
3432 GenerateClassInitializationCheck(slow_path,
3433 check->GetLocations()->InAt(0).AsRegister<Register>());
3434}
3435
3436void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003437 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003439 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003440 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003441
3442 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003443 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003444 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003447 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003448 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003449 locations->SetInAt(0, Location::RequiresRegister());
3450 locations->SetInAt(1, Location::RequiresRegister());
3451 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3452 break;
3453
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003454 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003455 locations->SetInAt(0, Location::RequiresRegister());
3456 locations->SetInAt(1, Location::RequiresRegister());
3457 // Output overlaps because it is written before doing the low comparison.
3458 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3459 break;
3460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kFloat32:
3462 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003463 locations->SetInAt(0, Location::RequiresFpuRegister());
3464 locations->SetInAt(1, Location::RequiresFpuRegister());
3465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003466 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467
3468 default:
3469 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3470 }
3471}
3472
3473void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3474 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003475 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003477 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003478
3479 // 0 if: left == right
3480 // 1 if: left > right
3481 // -1 if: left < right
3482 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003483 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003484 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003485 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003487 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003488 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003489 Register lhs = locations->InAt(0).AsRegister<Register>();
3490 Register rhs = locations->InAt(1).AsRegister<Register>();
3491 __ Slt(TMP, lhs, rhs);
3492 __ Slt(res, rhs, lhs);
3493 __ Subu(res, res, TMP);
3494 break;
3495 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003497 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003498 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3499 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3500 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3501 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3502 // TODO: more efficient (direct) comparison with a constant.
3503 __ Slt(TMP, lhs_high, rhs_high);
3504 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3505 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3506 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3507 __ Sltu(TMP, lhs_low, rhs_low);
3508 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3509 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3510 __ Bind(&done);
3511 break;
3512 }
3513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003515 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003516 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3517 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3518 MipsLabel done;
3519 if (isR6) {
3520 __ CmpEqS(FTMP, lhs, rhs);
3521 __ LoadConst32(res, 0);
3522 __ Bc1nez(FTMP, &done);
3523 if (gt_bias) {
3524 __ CmpLtS(FTMP, lhs, rhs);
3525 __ LoadConst32(res, -1);
3526 __ Bc1nez(FTMP, &done);
3527 __ LoadConst32(res, 1);
3528 } else {
3529 __ CmpLtS(FTMP, rhs, lhs);
3530 __ LoadConst32(res, 1);
3531 __ Bc1nez(FTMP, &done);
3532 __ LoadConst32(res, -1);
3533 }
3534 } else {
3535 if (gt_bias) {
3536 __ ColtS(0, lhs, rhs);
3537 __ LoadConst32(res, -1);
3538 __ Bc1t(0, &done);
3539 __ CeqS(0, lhs, rhs);
3540 __ LoadConst32(res, 1);
3541 __ Movt(res, ZERO, 0);
3542 } else {
3543 __ ColtS(0, rhs, lhs);
3544 __ LoadConst32(res, 1);
3545 __ Bc1t(0, &done);
3546 __ CeqS(0, lhs, rhs);
3547 __ LoadConst32(res, -1);
3548 __ Movt(res, ZERO, 0);
3549 }
3550 }
3551 __ Bind(&done);
3552 break;
3553 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003555 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003556 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3557 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3558 MipsLabel done;
3559 if (isR6) {
3560 __ CmpEqD(FTMP, lhs, rhs);
3561 __ LoadConst32(res, 0);
3562 __ Bc1nez(FTMP, &done);
3563 if (gt_bias) {
3564 __ CmpLtD(FTMP, lhs, rhs);
3565 __ LoadConst32(res, -1);
3566 __ Bc1nez(FTMP, &done);
3567 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003568 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003569 __ CmpLtD(FTMP, rhs, lhs);
3570 __ LoadConst32(res, 1);
3571 __ Bc1nez(FTMP, &done);
3572 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003573 }
3574 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003575 if (gt_bias) {
3576 __ ColtD(0, lhs, rhs);
3577 __ LoadConst32(res, -1);
3578 __ Bc1t(0, &done);
3579 __ CeqD(0, lhs, rhs);
3580 __ LoadConst32(res, 1);
3581 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003582 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003583 __ ColtD(0, rhs, lhs);
3584 __ LoadConst32(res, 1);
3585 __ Bc1t(0, &done);
3586 __ CeqD(0, lhs, rhs);
3587 __ LoadConst32(res, -1);
3588 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003589 }
3590 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003591 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003592 break;
3593 }
3594
3595 default:
3596 LOG(FATAL) << "Unimplemented compare type " << in_type;
3597 }
3598}
3599
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003600void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003601 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003602 switch (instruction->InputAt(0)->GetType()) {
3603 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003604 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003605 locations->SetInAt(0, Location::RequiresRegister());
3606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3607 break;
3608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kFloat32:
3610 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003611 locations->SetInAt(0, Location::RequiresFpuRegister());
3612 locations->SetInAt(1, Location::RequiresFpuRegister());
3613 break;
3614 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003615 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3617 }
3618}
3619
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003620void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003621 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003622 return;
3623 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003627
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003628 switch (type) {
3629 default:
3630 // Integer case.
3631 GenerateIntCompare(instruction->GetCondition(), locations);
3632 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003634 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003635 GenerateLongCompare(instruction->GetCondition(), locations);
3636 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 case DataType::Type::kFloat32:
3639 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003640 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3641 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642 }
3643}
3644
Alexey Frunze7e99e052015-11-24 19:28:01 -08003645void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3646 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003647 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003648
3649 LocationSummary* locations = instruction->GetLocations();
3650 Location second = locations->InAt(1);
3651 DCHECK(second.IsConstant());
3652
3653 Register out = locations->Out().AsRegister<Register>();
3654 Register dividend = locations->InAt(0).AsRegister<Register>();
3655 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3656 DCHECK(imm == 1 || imm == -1);
3657
3658 if (instruction->IsRem()) {
3659 __ Move(out, ZERO);
3660 } else {
3661 if (imm == -1) {
3662 __ Subu(out, ZERO, dividend);
3663 } else if (out != dividend) {
3664 __ Move(out, dividend);
3665 }
3666 }
3667}
3668
3669void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3670 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003672
3673 LocationSummary* locations = instruction->GetLocations();
3674 Location second = locations->InAt(1);
3675 DCHECK(second.IsConstant());
3676
3677 Register out = locations->Out().AsRegister<Register>();
3678 Register dividend = locations->InAt(0).AsRegister<Register>();
3679 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003680 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003681 int ctz_imm = CTZ(abs_imm);
3682
3683 if (instruction->IsDiv()) {
3684 if (ctz_imm == 1) {
3685 // Fast path for division by +/-2, which is very common.
3686 __ Srl(TMP, dividend, 31);
3687 } else {
3688 __ Sra(TMP, dividend, 31);
3689 __ Srl(TMP, TMP, 32 - ctz_imm);
3690 }
3691 __ Addu(out, dividend, TMP);
3692 __ Sra(out, out, ctz_imm);
3693 if (imm < 0) {
3694 __ Subu(out, ZERO, out);
3695 }
3696 } else {
3697 if (ctz_imm == 1) {
3698 // Fast path for modulo +/-2, which is very common.
3699 __ Sra(TMP, dividend, 31);
3700 __ Subu(out, dividend, TMP);
3701 __ Andi(out, out, 1);
3702 __ Addu(out, out, TMP);
3703 } else {
3704 __ Sra(TMP, dividend, 31);
3705 __ Srl(TMP, TMP, 32 - ctz_imm);
3706 __ Addu(out, dividend, TMP);
3707 if (IsUint<16>(abs_imm - 1)) {
3708 __ Andi(out, out, abs_imm - 1);
3709 } else {
3710 __ Sll(out, out, 32 - ctz_imm);
3711 __ Srl(out, out, 32 - ctz_imm);
3712 }
3713 __ Subu(out, out, TMP);
3714 }
3715 }
3716}
3717
3718void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3719 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003721
3722 LocationSummary* locations = instruction->GetLocations();
3723 Location second = locations->InAt(1);
3724 DCHECK(second.IsConstant());
3725
3726 Register out = locations->Out().AsRegister<Register>();
3727 Register dividend = locations->InAt(0).AsRegister<Register>();
3728 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3729
3730 int64_t magic;
3731 int shift;
3732 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3733
3734 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3735
3736 __ LoadConst32(TMP, magic);
3737 if (isR6) {
3738 __ MuhR6(TMP, dividend, TMP);
3739 } else {
3740 __ MultR2(dividend, TMP);
3741 __ Mfhi(TMP);
3742 }
3743 if (imm > 0 && magic < 0) {
3744 __ Addu(TMP, TMP, dividend);
3745 } else if (imm < 0 && magic > 0) {
3746 __ Subu(TMP, TMP, dividend);
3747 }
3748
3749 if (shift != 0) {
3750 __ Sra(TMP, TMP, shift);
3751 }
3752
3753 if (instruction->IsDiv()) {
3754 __ Sra(out, TMP, 31);
3755 __ Subu(out, TMP, out);
3756 } else {
3757 __ Sra(AT, TMP, 31);
3758 __ Subu(AT, TMP, AT);
3759 __ LoadConst32(TMP, imm);
3760 if (isR6) {
3761 __ MulR6(TMP, AT, TMP);
3762 } else {
3763 __ MulR2(TMP, AT, TMP);
3764 }
3765 __ Subu(out, dividend, TMP);
3766 }
3767}
3768
3769void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3770 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003772
3773 LocationSummary* locations = instruction->GetLocations();
3774 Register out = locations->Out().AsRegister<Register>();
3775 Location second = locations->InAt(1);
3776
3777 if (second.IsConstant()) {
3778 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3779 if (imm == 0) {
3780 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3781 } else if (imm == 1 || imm == -1) {
3782 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003783 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003784 DivRemByPowerOfTwo(instruction);
3785 } else {
3786 DCHECK(imm <= -2 || imm >= 2);
3787 GenerateDivRemWithAnyConstant(instruction);
3788 }
3789 } else {
3790 Register dividend = locations->InAt(0).AsRegister<Register>();
3791 Register divisor = second.AsRegister<Register>();
3792 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3793 if (instruction->IsDiv()) {
3794 if (isR6) {
3795 __ DivR6(out, dividend, divisor);
3796 } else {
3797 __ DivR2(out, dividend, divisor);
3798 }
3799 } else {
3800 if (isR6) {
3801 __ ModR6(out, dividend, divisor);
3802 } else {
3803 __ ModR2(out, dividend, divisor);
3804 }
3805 }
3806 }
3807}
3808
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 DataType::Type type = div->GetResultType();
3811 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003812 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003813 : LocationSummary::kNoCall;
3814
Vladimir Markoca6fff82017-10-03 14:49:14 +01003815 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003816
3817 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003818 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003819 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003820 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3822 break;
3823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003824 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825 InvokeRuntimeCallingConvention calling_convention;
3826 locations->SetInAt(0, Location::RegisterPairLocation(
3827 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3828 locations->SetInAt(1, Location::RegisterPairLocation(
3829 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3830 locations->SetOut(calling_convention.GetReturnLocation(type));
3831 break;
3832 }
3833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kFloat32:
3835 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836 locations->SetInAt(0, Location::RequiresFpuRegister());
3837 locations->SetInAt(1, Location::RequiresFpuRegister());
3838 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3839 break;
3840
3841 default:
3842 LOG(FATAL) << "Unexpected div type " << type;
3843 }
3844}
3845
3846void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003847 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003848 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849
3850 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003852 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003855 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3857 break;
3858 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 case DataType::Type::kFloat32:
3860 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3862 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3863 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 __ DivS(dst, lhs, rhs);
3866 } else {
3867 __ DivD(dst, lhs, rhs);
3868 }
3869 break;
3870 }
3871 default:
3872 LOG(FATAL) << "Unexpected div type " << type;
3873 }
3874}
3875
3876void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003877 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003878 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003879}
3880
3881void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003882 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003883 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003884 codegen_->AddSlowPath(slow_path);
3885 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003887
3888 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003890 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt8:
3892 case DataType::Type::kUint16:
3893 case DataType::Type::kInt16:
3894 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003895 if (value.IsConstant()) {
3896 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3897 __ B(slow_path->GetEntryLabel());
3898 } else {
3899 // A division by a non-null constant is valid. We don't need to perform
3900 // any check, so simply fall through.
3901 }
3902 } else {
3903 DCHECK(value.IsRegister()) << value;
3904 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3905 }
3906 break;
3907 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003908 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 if (value.IsConstant()) {
3910 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3911 __ B(slow_path->GetEntryLabel());
3912 } else {
3913 // A division by a non-null constant is valid. We don't need to perform
3914 // any check, so simply fall through.
3915 }
3916 } else {
3917 DCHECK(value.IsRegisterPair()) << value;
3918 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3919 __ Beqz(TMP, slow_path->GetEntryLabel());
3920 }
3921 break;
3922 }
3923 default:
3924 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3925 }
3926}
3927
3928void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003930 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003931 locations->SetOut(Location::ConstantLocation(constant));
3932}
3933
3934void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3935 // Will be generated at use site.
3936}
3937
3938void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3939 exit->SetLocations(nullptr);
3940}
3941
3942void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3943}
3944
3945void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3946 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003947 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948 locations->SetOut(Location::ConstantLocation(constant));
3949}
3950
3951void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3952 // Will be generated at use site.
3953}
3954
3955void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3956 got->SetLocations(nullptr);
3957}
3958
3959void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3960 DCHECK(!successor->IsExitBlock());
3961 HBasicBlock* block = got->GetBlock();
3962 HInstruction* previous = got->GetPrevious();
3963 HLoopInformation* info = block->GetLoopInformation();
3964
3965 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003966 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3967 return;
3968 }
3969 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3970 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3971 }
3972 if (!codegen_->GoesToNextBlock(block, successor)) {
3973 __ B(codegen_->GetLabelOf(successor));
3974 }
3975}
3976
3977void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3978 HandleGoto(got, got->GetSuccessor());
3979}
3980
3981void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3982 try_boundary->SetLocations(nullptr);
3983}
3984
3985void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3986 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3987 if (!successor->IsExitBlock()) {
3988 HandleGoto(try_boundary, successor);
3989 }
3990}
3991
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003992void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3993 LocationSummary* locations) {
3994 Register dst = locations->Out().AsRegister<Register>();
3995 Register lhs = locations->InAt(0).AsRegister<Register>();
3996 Location rhs_location = locations->InAt(1);
3997 Register rhs_reg = ZERO;
3998 int64_t rhs_imm = 0;
3999 bool use_imm = rhs_location.IsConstant();
4000 if (use_imm) {
4001 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4002 } else {
4003 rhs_reg = rhs_location.AsRegister<Register>();
4004 }
4005
4006 switch (cond) {
4007 case kCondEQ:
4008 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004009 if (use_imm && IsInt<16>(-rhs_imm)) {
4010 if (rhs_imm == 0) {
4011 if (cond == kCondEQ) {
4012 __ Sltiu(dst, lhs, 1);
4013 } else {
4014 __ Sltu(dst, ZERO, lhs);
4015 }
4016 } else {
4017 __ Addiu(dst, lhs, -rhs_imm);
4018 if (cond == kCondEQ) {
4019 __ Sltiu(dst, dst, 1);
4020 } else {
4021 __ Sltu(dst, ZERO, dst);
4022 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004023 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004024 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004025 if (use_imm && IsUint<16>(rhs_imm)) {
4026 __ Xori(dst, lhs, rhs_imm);
4027 } else {
4028 if (use_imm) {
4029 rhs_reg = TMP;
4030 __ LoadConst32(rhs_reg, rhs_imm);
4031 }
4032 __ Xor(dst, lhs, rhs_reg);
4033 }
4034 if (cond == kCondEQ) {
4035 __ Sltiu(dst, dst, 1);
4036 } else {
4037 __ Sltu(dst, ZERO, dst);
4038 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004039 }
4040 break;
4041
4042 case kCondLT:
4043 case kCondGE:
4044 if (use_imm && IsInt<16>(rhs_imm)) {
4045 __ Slti(dst, lhs, rhs_imm);
4046 } else {
4047 if (use_imm) {
4048 rhs_reg = TMP;
4049 __ LoadConst32(rhs_reg, rhs_imm);
4050 }
4051 __ Slt(dst, lhs, rhs_reg);
4052 }
4053 if (cond == kCondGE) {
4054 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4055 // only the slt instruction but no sge.
4056 __ Xori(dst, dst, 1);
4057 }
4058 break;
4059
4060 case kCondLE:
4061 case kCondGT:
4062 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4063 // Simulate lhs <= rhs via lhs < rhs + 1.
4064 __ Slti(dst, lhs, rhs_imm + 1);
4065 if (cond == kCondGT) {
4066 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4067 // only the slti instruction but no sgti.
4068 __ Xori(dst, dst, 1);
4069 }
4070 } else {
4071 if (use_imm) {
4072 rhs_reg = TMP;
4073 __ LoadConst32(rhs_reg, rhs_imm);
4074 }
4075 __ Slt(dst, rhs_reg, lhs);
4076 if (cond == kCondLE) {
4077 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4078 // only the slt instruction but no sle.
4079 __ Xori(dst, dst, 1);
4080 }
4081 }
4082 break;
4083
4084 case kCondB:
4085 case kCondAE:
4086 if (use_imm && IsInt<16>(rhs_imm)) {
4087 // Sltiu sign-extends its 16-bit immediate operand before
4088 // the comparison and thus lets us compare directly with
4089 // unsigned values in the ranges [0, 0x7fff] and
4090 // [0xffff8000, 0xffffffff].
4091 __ Sltiu(dst, lhs, rhs_imm);
4092 } else {
4093 if (use_imm) {
4094 rhs_reg = TMP;
4095 __ LoadConst32(rhs_reg, rhs_imm);
4096 }
4097 __ Sltu(dst, lhs, rhs_reg);
4098 }
4099 if (cond == kCondAE) {
4100 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4101 // only the sltu instruction but no sgeu.
4102 __ Xori(dst, dst, 1);
4103 }
4104 break;
4105
4106 case kCondBE:
4107 case kCondA:
4108 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4109 // Simulate lhs <= rhs via lhs < rhs + 1.
4110 // Note that this only works if rhs + 1 does not overflow
4111 // to 0, hence the check above.
4112 // Sltiu sign-extends its 16-bit immediate operand before
4113 // the comparison and thus lets us compare directly with
4114 // unsigned values in the ranges [0, 0x7fff] and
4115 // [0xffff8000, 0xffffffff].
4116 __ Sltiu(dst, lhs, rhs_imm + 1);
4117 if (cond == kCondA) {
4118 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4119 // only the sltiu instruction but no sgtiu.
4120 __ Xori(dst, dst, 1);
4121 }
4122 } else {
4123 if (use_imm) {
4124 rhs_reg = TMP;
4125 __ LoadConst32(rhs_reg, rhs_imm);
4126 }
4127 __ Sltu(dst, rhs_reg, lhs);
4128 if (cond == kCondBE) {
4129 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4130 // only the sltu instruction but no sleu.
4131 __ Xori(dst, dst, 1);
4132 }
4133 }
4134 break;
4135 }
4136}
4137
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004138bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4139 LocationSummary* input_locations,
4140 Register dst) {
4141 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4142 Location rhs_location = input_locations->InAt(1);
4143 Register rhs_reg = ZERO;
4144 int64_t rhs_imm = 0;
4145 bool use_imm = rhs_location.IsConstant();
4146 if (use_imm) {
4147 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4148 } else {
4149 rhs_reg = rhs_location.AsRegister<Register>();
4150 }
4151
4152 switch (cond) {
4153 case kCondEQ:
4154 case kCondNE:
4155 if (use_imm && IsInt<16>(-rhs_imm)) {
4156 __ Addiu(dst, lhs, -rhs_imm);
4157 } else if (use_imm && IsUint<16>(rhs_imm)) {
4158 __ Xori(dst, lhs, rhs_imm);
4159 } else {
4160 if (use_imm) {
4161 rhs_reg = TMP;
4162 __ LoadConst32(rhs_reg, rhs_imm);
4163 }
4164 __ Xor(dst, lhs, rhs_reg);
4165 }
4166 return (cond == kCondEQ);
4167
4168 case kCondLT:
4169 case kCondGE:
4170 if (use_imm && IsInt<16>(rhs_imm)) {
4171 __ Slti(dst, lhs, rhs_imm);
4172 } else {
4173 if (use_imm) {
4174 rhs_reg = TMP;
4175 __ LoadConst32(rhs_reg, rhs_imm);
4176 }
4177 __ Slt(dst, lhs, rhs_reg);
4178 }
4179 return (cond == kCondGE);
4180
4181 case kCondLE:
4182 case kCondGT:
4183 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4184 // Simulate lhs <= rhs via lhs < rhs + 1.
4185 __ Slti(dst, lhs, rhs_imm + 1);
4186 return (cond == kCondGT);
4187 } else {
4188 if (use_imm) {
4189 rhs_reg = TMP;
4190 __ LoadConst32(rhs_reg, rhs_imm);
4191 }
4192 __ Slt(dst, rhs_reg, lhs);
4193 return (cond == kCondLE);
4194 }
4195
4196 case kCondB:
4197 case kCondAE:
4198 if (use_imm && IsInt<16>(rhs_imm)) {
4199 // Sltiu sign-extends its 16-bit immediate operand before
4200 // the comparison and thus lets us compare directly with
4201 // unsigned values in the ranges [0, 0x7fff] and
4202 // [0xffff8000, 0xffffffff].
4203 __ Sltiu(dst, lhs, rhs_imm);
4204 } else {
4205 if (use_imm) {
4206 rhs_reg = TMP;
4207 __ LoadConst32(rhs_reg, rhs_imm);
4208 }
4209 __ Sltu(dst, lhs, rhs_reg);
4210 }
4211 return (cond == kCondAE);
4212
4213 case kCondBE:
4214 case kCondA:
4215 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4216 // Simulate lhs <= rhs via lhs < rhs + 1.
4217 // Note that this only works if rhs + 1 does not overflow
4218 // to 0, hence the check above.
4219 // Sltiu sign-extends its 16-bit immediate operand before
4220 // the comparison and thus lets us compare directly with
4221 // unsigned values in the ranges [0, 0x7fff] and
4222 // [0xffff8000, 0xffffffff].
4223 __ Sltiu(dst, lhs, rhs_imm + 1);
4224 return (cond == kCondA);
4225 } else {
4226 if (use_imm) {
4227 rhs_reg = TMP;
4228 __ LoadConst32(rhs_reg, rhs_imm);
4229 }
4230 __ Sltu(dst, rhs_reg, lhs);
4231 return (cond == kCondBE);
4232 }
4233 }
4234}
4235
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004236void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4237 LocationSummary* locations,
4238 MipsLabel* label) {
4239 Register lhs = locations->InAt(0).AsRegister<Register>();
4240 Location rhs_location = locations->InAt(1);
4241 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004242 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004243 bool use_imm = rhs_location.IsConstant();
4244 if (use_imm) {
4245 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4246 } else {
4247 rhs_reg = rhs_location.AsRegister<Register>();
4248 }
4249
4250 if (use_imm && rhs_imm == 0) {
4251 switch (cond) {
4252 case kCondEQ:
4253 case kCondBE: // <= 0 if zero
4254 __ Beqz(lhs, label);
4255 break;
4256 case kCondNE:
4257 case kCondA: // > 0 if non-zero
4258 __ Bnez(lhs, label);
4259 break;
4260 case kCondLT:
4261 __ Bltz(lhs, label);
4262 break;
4263 case kCondGE:
4264 __ Bgez(lhs, label);
4265 break;
4266 case kCondLE:
4267 __ Blez(lhs, label);
4268 break;
4269 case kCondGT:
4270 __ Bgtz(lhs, label);
4271 break;
4272 case kCondB: // always false
4273 break;
4274 case kCondAE: // always true
4275 __ B(label);
4276 break;
4277 }
4278 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004279 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4280 if (isR6 || !use_imm) {
4281 if (use_imm) {
4282 rhs_reg = TMP;
4283 __ LoadConst32(rhs_reg, rhs_imm);
4284 }
4285 switch (cond) {
4286 case kCondEQ:
4287 __ Beq(lhs, rhs_reg, label);
4288 break;
4289 case kCondNE:
4290 __ Bne(lhs, rhs_reg, label);
4291 break;
4292 case kCondLT:
4293 __ Blt(lhs, rhs_reg, label);
4294 break;
4295 case kCondGE:
4296 __ Bge(lhs, rhs_reg, label);
4297 break;
4298 case kCondLE:
4299 __ Bge(rhs_reg, lhs, label);
4300 break;
4301 case kCondGT:
4302 __ Blt(rhs_reg, lhs, label);
4303 break;
4304 case kCondB:
4305 __ Bltu(lhs, rhs_reg, label);
4306 break;
4307 case kCondAE:
4308 __ Bgeu(lhs, rhs_reg, label);
4309 break;
4310 case kCondBE:
4311 __ Bgeu(rhs_reg, lhs, label);
4312 break;
4313 case kCondA:
4314 __ Bltu(rhs_reg, lhs, label);
4315 break;
4316 }
4317 } else {
4318 // Special cases for more efficient comparison with constants on R2.
4319 switch (cond) {
4320 case kCondEQ:
4321 __ LoadConst32(TMP, rhs_imm);
4322 __ Beq(lhs, TMP, label);
4323 break;
4324 case kCondNE:
4325 __ LoadConst32(TMP, rhs_imm);
4326 __ Bne(lhs, TMP, label);
4327 break;
4328 case kCondLT:
4329 if (IsInt<16>(rhs_imm)) {
4330 __ Slti(TMP, lhs, rhs_imm);
4331 __ Bnez(TMP, label);
4332 } else {
4333 __ LoadConst32(TMP, rhs_imm);
4334 __ Blt(lhs, TMP, label);
4335 }
4336 break;
4337 case kCondGE:
4338 if (IsInt<16>(rhs_imm)) {
4339 __ Slti(TMP, lhs, rhs_imm);
4340 __ Beqz(TMP, label);
4341 } else {
4342 __ LoadConst32(TMP, rhs_imm);
4343 __ Bge(lhs, TMP, label);
4344 }
4345 break;
4346 case kCondLE:
4347 if (IsInt<16>(rhs_imm + 1)) {
4348 // Simulate lhs <= rhs via lhs < rhs + 1.
4349 __ Slti(TMP, lhs, rhs_imm + 1);
4350 __ Bnez(TMP, label);
4351 } else {
4352 __ LoadConst32(TMP, rhs_imm);
4353 __ Bge(TMP, lhs, label);
4354 }
4355 break;
4356 case kCondGT:
4357 if (IsInt<16>(rhs_imm + 1)) {
4358 // Simulate lhs > rhs via !(lhs < rhs + 1).
4359 __ Slti(TMP, lhs, rhs_imm + 1);
4360 __ Beqz(TMP, label);
4361 } else {
4362 __ LoadConst32(TMP, rhs_imm);
4363 __ Blt(TMP, lhs, label);
4364 }
4365 break;
4366 case kCondB:
4367 if (IsInt<16>(rhs_imm)) {
4368 __ Sltiu(TMP, lhs, rhs_imm);
4369 __ Bnez(TMP, label);
4370 } else {
4371 __ LoadConst32(TMP, rhs_imm);
4372 __ Bltu(lhs, TMP, label);
4373 }
4374 break;
4375 case kCondAE:
4376 if (IsInt<16>(rhs_imm)) {
4377 __ Sltiu(TMP, lhs, rhs_imm);
4378 __ Beqz(TMP, label);
4379 } else {
4380 __ LoadConst32(TMP, rhs_imm);
4381 __ Bgeu(lhs, TMP, label);
4382 }
4383 break;
4384 case kCondBE:
4385 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4386 // Simulate lhs <= rhs via lhs < rhs + 1.
4387 // Note that this only works if rhs + 1 does not overflow
4388 // to 0, hence the check above.
4389 __ Sltiu(TMP, lhs, rhs_imm + 1);
4390 __ Bnez(TMP, label);
4391 } else {
4392 __ LoadConst32(TMP, rhs_imm);
4393 __ Bgeu(TMP, lhs, label);
4394 }
4395 break;
4396 case kCondA:
4397 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4398 // Simulate lhs > rhs via !(lhs < rhs + 1).
4399 // Note that this only works if rhs + 1 does not overflow
4400 // to 0, hence the check above.
4401 __ Sltiu(TMP, lhs, rhs_imm + 1);
4402 __ Beqz(TMP, label);
4403 } else {
4404 __ LoadConst32(TMP, rhs_imm);
4405 __ Bltu(TMP, lhs, label);
4406 }
4407 break;
4408 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004409 }
4410 }
4411}
4412
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004413void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4414 LocationSummary* locations) {
4415 Register dst = locations->Out().AsRegister<Register>();
4416 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4417 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4418 Location rhs_location = locations->InAt(1);
4419 Register rhs_high = ZERO;
4420 Register rhs_low = ZERO;
4421 int64_t imm = 0;
4422 uint32_t imm_high = 0;
4423 uint32_t imm_low = 0;
4424 bool use_imm = rhs_location.IsConstant();
4425 if (use_imm) {
4426 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4427 imm_high = High32Bits(imm);
4428 imm_low = Low32Bits(imm);
4429 } else {
4430 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4431 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4432 }
4433 if (use_imm && imm == 0) {
4434 switch (cond) {
4435 case kCondEQ:
4436 case kCondBE: // <= 0 if zero
4437 __ Or(dst, lhs_high, lhs_low);
4438 __ Sltiu(dst, dst, 1);
4439 break;
4440 case kCondNE:
4441 case kCondA: // > 0 if non-zero
4442 __ Or(dst, lhs_high, lhs_low);
4443 __ Sltu(dst, ZERO, dst);
4444 break;
4445 case kCondLT:
4446 __ Slt(dst, lhs_high, ZERO);
4447 break;
4448 case kCondGE:
4449 __ Slt(dst, lhs_high, ZERO);
4450 __ Xori(dst, dst, 1);
4451 break;
4452 case kCondLE:
4453 __ Or(TMP, lhs_high, lhs_low);
4454 __ Sra(AT, lhs_high, 31);
4455 __ Sltu(dst, AT, TMP);
4456 __ Xori(dst, dst, 1);
4457 break;
4458 case kCondGT:
4459 __ Or(TMP, lhs_high, lhs_low);
4460 __ Sra(AT, lhs_high, 31);
4461 __ Sltu(dst, AT, TMP);
4462 break;
4463 case kCondB: // always false
4464 __ Andi(dst, dst, 0);
4465 break;
4466 case kCondAE: // always true
4467 __ Ori(dst, ZERO, 1);
4468 break;
4469 }
4470 } else if (use_imm) {
4471 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4472 switch (cond) {
4473 case kCondEQ:
4474 __ LoadConst32(TMP, imm_high);
4475 __ Xor(TMP, TMP, lhs_high);
4476 __ LoadConst32(AT, imm_low);
4477 __ Xor(AT, AT, lhs_low);
4478 __ Or(dst, TMP, AT);
4479 __ Sltiu(dst, dst, 1);
4480 break;
4481 case kCondNE:
4482 __ LoadConst32(TMP, imm_high);
4483 __ Xor(TMP, TMP, lhs_high);
4484 __ LoadConst32(AT, imm_low);
4485 __ Xor(AT, AT, lhs_low);
4486 __ Or(dst, TMP, AT);
4487 __ Sltu(dst, ZERO, dst);
4488 break;
4489 case kCondLT:
4490 case kCondGE:
4491 if (dst == lhs_low) {
4492 __ LoadConst32(TMP, imm_low);
4493 __ Sltu(dst, lhs_low, TMP);
4494 }
4495 __ LoadConst32(TMP, imm_high);
4496 __ Slt(AT, lhs_high, TMP);
4497 __ Slt(TMP, TMP, lhs_high);
4498 if (dst != lhs_low) {
4499 __ LoadConst32(dst, imm_low);
4500 __ Sltu(dst, lhs_low, dst);
4501 }
4502 __ Slt(dst, TMP, dst);
4503 __ Or(dst, dst, AT);
4504 if (cond == kCondGE) {
4505 __ Xori(dst, dst, 1);
4506 }
4507 break;
4508 case kCondGT:
4509 case kCondLE:
4510 if (dst == lhs_low) {
4511 __ LoadConst32(TMP, imm_low);
4512 __ Sltu(dst, TMP, lhs_low);
4513 }
4514 __ LoadConst32(TMP, imm_high);
4515 __ Slt(AT, TMP, lhs_high);
4516 __ Slt(TMP, lhs_high, TMP);
4517 if (dst != lhs_low) {
4518 __ LoadConst32(dst, imm_low);
4519 __ Sltu(dst, dst, lhs_low);
4520 }
4521 __ Slt(dst, TMP, dst);
4522 __ Or(dst, dst, AT);
4523 if (cond == kCondLE) {
4524 __ Xori(dst, dst, 1);
4525 }
4526 break;
4527 case kCondB:
4528 case kCondAE:
4529 if (dst == lhs_low) {
4530 __ LoadConst32(TMP, imm_low);
4531 __ Sltu(dst, lhs_low, TMP);
4532 }
4533 __ LoadConst32(TMP, imm_high);
4534 __ Sltu(AT, lhs_high, TMP);
4535 __ Sltu(TMP, TMP, lhs_high);
4536 if (dst != lhs_low) {
4537 __ LoadConst32(dst, imm_low);
4538 __ Sltu(dst, lhs_low, dst);
4539 }
4540 __ Slt(dst, TMP, dst);
4541 __ Or(dst, dst, AT);
4542 if (cond == kCondAE) {
4543 __ Xori(dst, dst, 1);
4544 }
4545 break;
4546 case kCondA:
4547 case kCondBE:
4548 if (dst == lhs_low) {
4549 __ LoadConst32(TMP, imm_low);
4550 __ Sltu(dst, TMP, lhs_low);
4551 }
4552 __ LoadConst32(TMP, imm_high);
4553 __ Sltu(AT, TMP, lhs_high);
4554 __ Sltu(TMP, lhs_high, TMP);
4555 if (dst != lhs_low) {
4556 __ LoadConst32(dst, imm_low);
4557 __ Sltu(dst, dst, lhs_low);
4558 }
4559 __ Slt(dst, TMP, dst);
4560 __ Or(dst, dst, AT);
4561 if (cond == kCondBE) {
4562 __ Xori(dst, dst, 1);
4563 }
4564 break;
4565 }
4566 } else {
4567 switch (cond) {
4568 case kCondEQ:
4569 __ Xor(TMP, lhs_high, rhs_high);
4570 __ Xor(AT, lhs_low, rhs_low);
4571 __ Or(dst, TMP, AT);
4572 __ Sltiu(dst, dst, 1);
4573 break;
4574 case kCondNE:
4575 __ Xor(TMP, lhs_high, rhs_high);
4576 __ Xor(AT, lhs_low, rhs_low);
4577 __ Or(dst, TMP, AT);
4578 __ Sltu(dst, ZERO, dst);
4579 break;
4580 case kCondLT:
4581 case kCondGE:
4582 __ Slt(TMP, rhs_high, lhs_high);
4583 __ Sltu(AT, lhs_low, rhs_low);
4584 __ Slt(TMP, TMP, AT);
4585 __ Slt(AT, lhs_high, rhs_high);
4586 __ Or(dst, AT, TMP);
4587 if (cond == kCondGE) {
4588 __ Xori(dst, dst, 1);
4589 }
4590 break;
4591 case kCondGT:
4592 case kCondLE:
4593 __ Slt(TMP, lhs_high, rhs_high);
4594 __ Sltu(AT, rhs_low, lhs_low);
4595 __ Slt(TMP, TMP, AT);
4596 __ Slt(AT, rhs_high, lhs_high);
4597 __ Or(dst, AT, TMP);
4598 if (cond == kCondLE) {
4599 __ Xori(dst, dst, 1);
4600 }
4601 break;
4602 case kCondB:
4603 case kCondAE:
4604 __ Sltu(TMP, rhs_high, lhs_high);
4605 __ Sltu(AT, lhs_low, rhs_low);
4606 __ Slt(TMP, TMP, AT);
4607 __ Sltu(AT, lhs_high, rhs_high);
4608 __ Or(dst, AT, TMP);
4609 if (cond == kCondAE) {
4610 __ Xori(dst, dst, 1);
4611 }
4612 break;
4613 case kCondA:
4614 case kCondBE:
4615 __ Sltu(TMP, lhs_high, rhs_high);
4616 __ Sltu(AT, rhs_low, lhs_low);
4617 __ Slt(TMP, TMP, AT);
4618 __ Sltu(AT, rhs_high, lhs_high);
4619 __ Or(dst, AT, TMP);
4620 if (cond == kCondBE) {
4621 __ Xori(dst, dst, 1);
4622 }
4623 break;
4624 }
4625 }
4626}
4627
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004628void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4629 LocationSummary* locations,
4630 MipsLabel* label) {
4631 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4632 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4633 Location rhs_location = locations->InAt(1);
4634 Register rhs_high = ZERO;
4635 Register rhs_low = ZERO;
4636 int64_t imm = 0;
4637 uint32_t imm_high = 0;
4638 uint32_t imm_low = 0;
4639 bool use_imm = rhs_location.IsConstant();
4640 if (use_imm) {
4641 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4642 imm_high = High32Bits(imm);
4643 imm_low = Low32Bits(imm);
4644 } else {
4645 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4646 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4647 }
4648
4649 if (use_imm && imm == 0) {
4650 switch (cond) {
4651 case kCondEQ:
4652 case kCondBE: // <= 0 if zero
4653 __ Or(TMP, lhs_high, lhs_low);
4654 __ Beqz(TMP, label);
4655 break;
4656 case kCondNE:
4657 case kCondA: // > 0 if non-zero
4658 __ Or(TMP, lhs_high, lhs_low);
4659 __ Bnez(TMP, label);
4660 break;
4661 case kCondLT:
4662 __ Bltz(lhs_high, label);
4663 break;
4664 case kCondGE:
4665 __ Bgez(lhs_high, label);
4666 break;
4667 case kCondLE:
4668 __ Or(TMP, lhs_high, lhs_low);
4669 __ Sra(AT, lhs_high, 31);
4670 __ Bgeu(AT, TMP, label);
4671 break;
4672 case kCondGT:
4673 __ Or(TMP, lhs_high, lhs_low);
4674 __ Sra(AT, lhs_high, 31);
4675 __ Bltu(AT, TMP, label);
4676 break;
4677 case kCondB: // always false
4678 break;
4679 case kCondAE: // always true
4680 __ B(label);
4681 break;
4682 }
4683 } else if (use_imm) {
4684 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4685 switch (cond) {
4686 case kCondEQ:
4687 __ LoadConst32(TMP, imm_high);
4688 __ Xor(TMP, TMP, lhs_high);
4689 __ LoadConst32(AT, imm_low);
4690 __ Xor(AT, AT, lhs_low);
4691 __ Or(TMP, TMP, AT);
4692 __ Beqz(TMP, label);
4693 break;
4694 case kCondNE:
4695 __ LoadConst32(TMP, imm_high);
4696 __ Xor(TMP, TMP, lhs_high);
4697 __ LoadConst32(AT, imm_low);
4698 __ Xor(AT, AT, lhs_low);
4699 __ Or(TMP, TMP, AT);
4700 __ Bnez(TMP, label);
4701 break;
4702 case kCondLT:
4703 __ LoadConst32(TMP, imm_high);
4704 __ Blt(lhs_high, TMP, label);
4705 __ Slt(TMP, TMP, lhs_high);
4706 __ LoadConst32(AT, imm_low);
4707 __ Sltu(AT, lhs_low, AT);
4708 __ Blt(TMP, AT, label);
4709 break;
4710 case kCondGE:
4711 __ LoadConst32(TMP, imm_high);
4712 __ Blt(TMP, lhs_high, label);
4713 __ Slt(TMP, lhs_high, TMP);
4714 __ LoadConst32(AT, imm_low);
4715 __ Sltu(AT, lhs_low, AT);
4716 __ Or(TMP, TMP, AT);
4717 __ Beqz(TMP, label);
4718 break;
4719 case kCondLE:
4720 __ LoadConst32(TMP, imm_high);
4721 __ Blt(lhs_high, TMP, label);
4722 __ Slt(TMP, TMP, lhs_high);
4723 __ LoadConst32(AT, imm_low);
4724 __ Sltu(AT, AT, lhs_low);
4725 __ Or(TMP, TMP, AT);
4726 __ Beqz(TMP, label);
4727 break;
4728 case kCondGT:
4729 __ LoadConst32(TMP, imm_high);
4730 __ Blt(TMP, lhs_high, label);
4731 __ Slt(TMP, lhs_high, TMP);
4732 __ LoadConst32(AT, imm_low);
4733 __ Sltu(AT, AT, lhs_low);
4734 __ Blt(TMP, AT, label);
4735 break;
4736 case kCondB:
4737 __ LoadConst32(TMP, imm_high);
4738 __ Bltu(lhs_high, TMP, label);
4739 __ Sltu(TMP, TMP, lhs_high);
4740 __ LoadConst32(AT, imm_low);
4741 __ Sltu(AT, lhs_low, AT);
4742 __ Blt(TMP, AT, label);
4743 break;
4744 case kCondAE:
4745 __ LoadConst32(TMP, imm_high);
4746 __ Bltu(TMP, lhs_high, label);
4747 __ Sltu(TMP, lhs_high, TMP);
4748 __ LoadConst32(AT, imm_low);
4749 __ Sltu(AT, lhs_low, AT);
4750 __ Or(TMP, TMP, AT);
4751 __ Beqz(TMP, label);
4752 break;
4753 case kCondBE:
4754 __ LoadConst32(TMP, imm_high);
4755 __ Bltu(lhs_high, TMP, label);
4756 __ Sltu(TMP, TMP, lhs_high);
4757 __ LoadConst32(AT, imm_low);
4758 __ Sltu(AT, AT, lhs_low);
4759 __ Or(TMP, TMP, AT);
4760 __ Beqz(TMP, label);
4761 break;
4762 case kCondA:
4763 __ LoadConst32(TMP, imm_high);
4764 __ Bltu(TMP, lhs_high, label);
4765 __ Sltu(TMP, lhs_high, TMP);
4766 __ LoadConst32(AT, imm_low);
4767 __ Sltu(AT, AT, lhs_low);
4768 __ Blt(TMP, AT, label);
4769 break;
4770 }
4771 } else {
4772 switch (cond) {
4773 case kCondEQ:
4774 __ Xor(TMP, lhs_high, rhs_high);
4775 __ Xor(AT, lhs_low, rhs_low);
4776 __ Or(TMP, TMP, AT);
4777 __ Beqz(TMP, label);
4778 break;
4779 case kCondNE:
4780 __ Xor(TMP, lhs_high, rhs_high);
4781 __ Xor(AT, lhs_low, rhs_low);
4782 __ Or(TMP, TMP, AT);
4783 __ Bnez(TMP, label);
4784 break;
4785 case kCondLT:
4786 __ Blt(lhs_high, rhs_high, label);
4787 __ Slt(TMP, rhs_high, lhs_high);
4788 __ Sltu(AT, lhs_low, rhs_low);
4789 __ Blt(TMP, AT, label);
4790 break;
4791 case kCondGE:
4792 __ Blt(rhs_high, lhs_high, label);
4793 __ Slt(TMP, lhs_high, rhs_high);
4794 __ Sltu(AT, lhs_low, rhs_low);
4795 __ Or(TMP, TMP, AT);
4796 __ Beqz(TMP, label);
4797 break;
4798 case kCondLE:
4799 __ Blt(lhs_high, rhs_high, label);
4800 __ Slt(TMP, rhs_high, lhs_high);
4801 __ Sltu(AT, rhs_low, lhs_low);
4802 __ Or(TMP, TMP, AT);
4803 __ Beqz(TMP, label);
4804 break;
4805 case kCondGT:
4806 __ Blt(rhs_high, lhs_high, label);
4807 __ Slt(TMP, lhs_high, rhs_high);
4808 __ Sltu(AT, rhs_low, lhs_low);
4809 __ Blt(TMP, AT, label);
4810 break;
4811 case kCondB:
4812 __ Bltu(lhs_high, rhs_high, label);
4813 __ Sltu(TMP, rhs_high, lhs_high);
4814 __ Sltu(AT, lhs_low, rhs_low);
4815 __ Blt(TMP, AT, label);
4816 break;
4817 case kCondAE:
4818 __ Bltu(rhs_high, lhs_high, label);
4819 __ Sltu(TMP, lhs_high, rhs_high);
4820 __ Sltu(AT, lhs_low, rhs_low);
4821 __ Or(TMP, TMP, AT);
4822 __ Beqz(TMP, label);
4823 break;
4824 case kCondBE:
4825 __ Bltu(lhs_high, rhs_high, label);
4826 __ Sltu(TMP, rhs_high, lhs_high);
4827 __ Sltu(AT, rhs_low, lhs_low);
4828 __ Or(TMP, TMP, AT);
4829 __ Beqz(TMP, label);
4830 break;
4831 case kCondA:
4832 __ Bltu(rhs_high, lhs_high, label);
4833 __ Sltu(TMP, lhs_high, rhs_high);
4834 __ Sltu(AT, rhs_low, lhs_low);
4835 __ Blt(TMP, AT, label);
4836 break;
4837 }
4838 }
4839}
4840
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004841void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4842 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004844 LocationSummary* locations) {
4845 Register dst = locations->Out().AsRegister<Register>();
4846 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4847 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4848 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004849 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004850 if (isR6) {
4851 switch (cond) {
4852 case kCondEQ:
4853 __ CmpEqS(FTMP, lhs, rhs);
4854 __ Mfc1(dst, FTMP);
4855 __ Andi(dst, dst, 1);
4856 break;
4857 case kCondNE:
4858 __ CmpEqS(FTMP, lhs, rhs);
4859 __ Mfc1(dst, FTMP);
4860 __ Addiu(dst, dst, 1);
4861 break;
4862 case kCondLT:
4863 if (gt_bias) {
4864 __ CmpLtS(FTMP, lhs, rhs);
4865 } else {
4866 __ CmpUltS(FTMP, lhs, rhs);
4867 }
4868 __ Mfc1(dst, FTMP);
4869 __ Andi(dst, dst, 1);
4870 break;
4871 case kCondLE:
4872 if (gt_bias) {
4873 __ CmpLeS(FTMP, lhs, rhs);
4874 } else {
4875 __ CmpUleS(FTMP, lhs, rhs);
4876 }
4877 __ Mfc1(dst, FTMP);
4878 __ Andi(dst, dst, 1);
4879 break;
4880 case kCondGT:
4881 if (gt_bias) {
4882 __ CmpUltS(FTMP, rhs, lhs);
4883 } else {
4884 __ CmpLtS(FTMP, rhs, lhs);
4885 }
4886 __ Mfc1(dst, FTMP);
4887 __ Andi(dst, dst, 1);
4888 break;
4889 case kCondGE:
4890 if (gt_bias) {
4891 __ CmpUleS(FTMP, rhs, lhs);
4892 } else {
4893 __ CmpLeS(FTMP, rhs, lhs);
4894 }
4895 __ Mfc1(dst, FTMP);
4896 __ Andi(dst, dst, 1);
4897 break;
4898 default:
4899 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4900 UNREACHABLE();
4901 }
4902 } else {
4903 switch (cond) {
4904 case kCondEQ:
4905 __ CeqS(0, lhs, rhs);
4906 __ LoadConst32(dst, 1);
4907 __ Movf(dst, ZERO, 0);
4908 break;
4909 case kCondNE:
4910 __ CeqS(0, lhs, rhs);
4911 __ LoadConst32(dst, 1);
4912 __ Movt(dst, ZERO, 0);
4913 break;
4914 case kCondLT:
4915 if (gt_bias) {
4916 __ ColtS(0, lhs, rhs);
4917 } else {
4918 __ CultS(0, lhs, rhs);
4919 }
4920 __ LoadConst32(dst, 1);
4921 __ Movf(dst, ZERO, 0);
4922 break;
4923 case kCondLE:
4924 if (gt_bias) {
4925 __ ColeS(0, lhs, rhs);
4926 } else {
4927 __ CuleS(0, lhs, rhs);
4928 }
4929 __ LoadConst32(dst, 1);
4930 __ Movf(dst, ZERO, 0);
4931 break;
4932 case kCondGT:
4933 if (gt_bias) {
4934 __ CultS(0, rhs, lhs);
4935 } else {
4936 __ ColtS(0, rhs, lhs);
4937 }
4938 __ LoadConst32(dst, 1);
4939 __ Movf(dst, ZERO, 0);
4940 break;
4941 case kCondGE:
4942 if (gt_bias) {
4943 __ CuleS(0, rhs, lhs);
4944 } else {
4945 __ ColeS(0, rhs, lhs);
4946 }
4947 __ LoadConst32(dst, 1);
4948 __ Movf(dst, ZERO, 0);
4949 break;
4950 default:
4951 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4952 UNREACHABLE();
4953 }
4954 }
4955 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004956 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004957 if (isR6) {
4958 switch (cond) {
4959 case kCondEQ:
4960 __ CmpEqD(FTMP, lhs, rhs);
4961 __ Mfc1(dst, FTMP);
4962 __ Andi(dst, dst, 1);
4963 break;
4964 case kCondNE:
4965 __ CmpEqD(FTMP, lhs, rhs);
4966 __ Mfc1(dst, FTMP);
4967 __ Addiu(dst, dst, 1);
4968 break;
4969 case kCondLT:
4970 if (gt_bias) {
4971 __ CmpLtD(FTMP, lhs, rhs);
4972 } else {
4973 __ CmpUltD(FTMP, lhs, rhs);
4974 }
4975 __ Mfc1(dst, FTMP);
4976 __ Andi(dst, dst, 1);
4977 break;
4978 case kCondLE:
4979 if (gt_bias) {
4980 __ CmpLeD(FTMP, lhs, rhs);
4981 } else {
4982 __ CmpUleD(FTMP, lhs, rhs);
4983 }
4984 __ Mfc1(dst, FTMP);
4985 __ Andi(dst, dst, 1);
4986 break;
4987 case kCondGT:
4988 if (gt_bias) {
4989 __ CmpUltD(FTMP, rhs, lhs);
4990 } else {
4991 __ CmpLtD(FTMP, rhs, lhs);
4992 }
4993 __ Mfc1(dst, FTMP);
4994 __ Andi(dst, dst, 1);
4995 break;
4996 case kCondGE:
4997 if (gt_bias) {
4998 __ CmpUleD(FTMP, rhs, lhs);
4999 } else {
5000 __ CmpLeD(FTMP, rhs, lhs);
5001 }
5002 __ Mfc1(dst, FTMP);
5003 __ Andi(dst, dst, 1);
5004 break;
5005 default:
5006 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5007 UNREACHABLE();
5008 }
5009 } else {
5010 switch (cond) {
5011 case kCondEQ:
5012 __ CeqD(0, lhs, rhs);
5013 __ LoadConst32(dst, 1);
5014 __ Movf(dst, ZERO, 0);
5015 break;
5016 case kCondNE:
5017 __ CeqD(0, lhs, rhs);
5018 __ LoadConst32(dst, 1);
5019 __ Movt(dst, ZERO, 0);
5020 break;
5021 case kCondLT:
5022 if (gt_bias) {
5023 __ ColtD(0, lhs, rhs);
5024 } else {
5025 __ CultD(0, lhs, rhs);
5026 }
5027 __ LoadConst32(dst, 1);
5028 __ Movf(dst, ZERO, 0);
5029 break;
5030 case kCondLE:
5031 if (gt_bias) {
5032 __ ColeD(0, lhs, rhs);
5033 } else {
5034 __ CuleD(0, lhs, rhs);
5035 }
5036 __ LoadConst32(dst, 1);
5037 __ Movf(dst, ZERO, 0);
5038 break;
5039 case kCondGT:
5040 if (gt_bias) {
5041 __ CultD(0, rhs, lhs);
5042 } else {
5043 __ ColtD(0, rhs, lhs);
5044 }
5045 __ LoadConst32(dst, 1);
5046 __ Movf(dst, ZERO, 0);
5047 break;
5048 case kCondGE:
5049 if (gt_bias) {
5050 __ CuleD(0, rhs, lhs);
5051 } else {
5052 __ ColeD(0, rhs, lhs);
5053 }
5054 __ LoadConst32(dst, 1);
5055 __ Movf(dst, ZERO, 0);
5056 break;
5057 default:
5058 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5059 UNREACHABLE();
5060 }
5061 }
5062 }
5063}
5064
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005065bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5066 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005067 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005068 LocationSummary* input_locations,
5069 int cc) {
5070 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5071 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5072 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005073 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005074 switch (cond) {
5075 case kCondEQ:
5076 __ CeqS(cc, lhs, rhs);
5077 return false;
5078 case kCondNE:
5079 __ CeqS(cc, lhs, rhs);
5080 return true;
5081 case kCondLT:
5082 if (gt_bias) {
5083 __ ColtS(cc, lhs, rhs);
5084 } else {
5085 __ CultS(cc, lhs, rhs);
5086 }
5087 return false;
5088 case kCondLE:
5089 if (gt_bias) {
5090 __ ColeS(cc, lhs, rhs);
5091 } else {
5092 __ CuleS(cc, lhs, rhs);
5093 }
5094 return false;
5095 case kCondGT:
5096 if (gt_bias) {
5097 __ CultS(cc, rhs, lhs);
5098 } else {
5099 __ ColtS(cc, rhs, lhs);
5100 }
5101 return false;
5102 case kCondGE:
5103 if (gt_bias) {
5104 __ CuleS(cc, rhs, lhs);
5105 } else {
5106 __ ColeS(cc, rhs, lhs);
5107 }
5108 return false;
5109 default:
5110 LOG(FATAL) << "Unexpected non-floating-point condition";
5111 UNREACHABLE();
5112 }
5113 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005114 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005115 switch (cond) {
5116 case kCondEQ:
5117 __ CeqD(cc, lhs, rhs);
5118 return false;
5119 case kCondNE:
5120 __ CeqD(cc, lhs, rhs);
5121 return true;
5122 case kCondLT:
5123 if (gt_bias) {
5124 __ ColtD(cc, lhs, rhs);
5125 } else {
5126 __ CultD(cc, lhs, rhs);
5127 }
5128 return false;
5129 case kCondLE:
5130 if (gt_bias) {
5131 __ ColeD(cc, lhs, rhs);
5132 } else {
5133 __ CuleD(cc, lhs, rhs);
5134 }
5135 return false;
5136 case kCondGT:
5137 if (gt_bias) {
5138 __ CultD(cc, rhs, lhs);
5139 } else {
5140 __ ColtD(cc, rhs, lhs);
5141 }
5142 return false;
5143 case kCondGE:
5144 if (gt_bias) {
5145 __ CuleD(cc, rhs, lhs);
5146 } else {
5147 __ ColeD(cc, rhs, lhs);
5148 }
5149 return false;
5150 default:
5151 LOG(FATAL) << "Unexpected non-floating-point condition";
5152 UNREACHABLE();
5153 }
5154 }
5155}
5156
5157bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5158 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005159 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005160 LocationSummary* input_locations,
5161 FRegister dst) {
5162 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5163 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5164 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005165 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005166 switch (cond) {
5167 case kCondEQ:
5168 __ CmpEqS(dst, lhs, rhs);
5169 return false;
5170 case kCondNE:
5171 __ CmpEqS(dst, lhs, rhs);
5172 return true;
5173 case kCondLT:
5174 if (gt_bias) {
5175 __ CmpLtS(dst, lhs, rhs);
5176 } else {
5177 __ CmpUltS(dst, lhs, rhs);
5178 }
5179 return false;
5180 case kCondLE:
5181 if (gt_bias) {
5182 __ CmpLeS(dst, lhs, rhs);
5183 } else {
5184 __ CmpUleS(dst, lhs, rhs);
5185 }
5186 return false;
5187 case kCondGT:
5188 if (gt_bias) {
5189 __ CmpUltS(dst, rhs, lhs);
5190 } else {
5191 __ CmpLtS(dst, rhs, lhs);
5192 }
5193 return false;
5194 case kCondGE:
5195 if (gt_bias) {
5196 __ CmpUleS(dst, rhs, lhs);
5197 } else {
5198 __ CmpLeS(dst, rhs, lhs);
5199 }
5200 return false;
5201 default:
5202 LOG(FATAL) << "Unexpected non-floating-point condition";
5203 UNREACHABLE();
5204 }
5205 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005206 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005207 switch (cond) {
5208 case kCondEQ:
5209 __ CmpEqD(dst, lhs, rhs);
5210 return false;
5211 case kCondNE:
5212 __ CmpEqD(dst, lhs, rhs);
5213 return true;
5214 case kCondLT:
5215 if (gt_bias) {
5216 __ CmpLtD(dst, lhs, rhs);
5217 } else {
5218 __ CmpUltD(dst, lhs, rhs);
5219 }
5220 return false;
5221 case kCondLE:
5222 if (gt_bias) {
5223 __ CmpLeD(dst, lhs, rhs);
5224 } else {
5225 __ CmpUleD(dst, lhs, rhs);
5226 }
5227 return false;
5228 case kCondGT:
5229 if (gt_bias) {
5230 __ CmpUltD(dst, rhs, lhs);
5231 } else {
5232 __ CmpLtD(dst, rhs, lhs);
5233 }
5234 return false;
5235 case kCondGE:
5236 if (gt_bias) {
5237 __ CmpUleD(dst, rhs, lhs);
5238 } else {
5239 __ CmpLeD(dst, rhs, lhs);
5240 }
5241 return false;
5242 default:
5243 LOG(FATAL) << "Unexpected non-floating-point condition";
5244 UNREACHABLE();
5245 }
5246 }
5247}
5248
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005249void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5250 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005251 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005252 LocationSummary* locations,
5253 MipsLabel* label) {
5254 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5255 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5256 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005257 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005258 if (isR6) {
5259 switch (cond) {
5260 case kCondEQ:
5261 __ CmpEqS(FTMP, lhs, rhs);
5262 __ Bc1nez(FTMP, label);
5263 break;
5264 case kCondNE:
5265 __ CmpEqS(FTMP, lhs, rhs);
5266 __ Bc1eqz(FTMP, label);
5267 break;
5268 case kCondLT:
5269 if (gt_bias) {
5270 __ CmpLtS(FTMP, lhs, rhs);
5271 } else {
5272 __ CmpUltS(FTMP, lhs, rhs);
5273 }
5274 __ Bc1nez(FTMP, label);
5275 break;
5276 case kCondLE:
5277 if (gt_bias) {
5278 __ CmpLeS(FTMP, lhs, rhs);
5279 } else {
5280 __ CmpUleS(FTMP, lhs, rhs);
5281 }
5282 __ Bc1nez(FTMP, label);
5283 break;
5284 case kCondGT:
5285 if (gt_bias) {
5286 __ CmpUltS(FTMP, rhs, lhs);
5287 } else {
5288 __ CmpLtS(FTMP, rhs, lhs);
5289 }
5290 __ Bc1nez(FTMP, label);
5291 break;
5292 case kCondGE:
5293 if (gt_bias) {
5294 __ CmpUleS(FTMP, rhs, lhs);
5295 } else {
5296 __ CmpLeS(FTMP, rhs, lhs);
5297 }
5298 __ Bc1nez(FTMP, label);
5299 break;
5300 default:
5301 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005302 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005303 }
5304 } else {
5305 switch (cond) {
5306 case kCondEQ:
5307 __ CeqS(0, lhs, rhs);
5308 __ Bc1t(0, label);
5309 break;
5310 case kCondNE:
5311 __ CeqS(0, lhs, rhs);
5312 __ Bc1f(0, label);
5313 break;
5314 case kCondLT:
5315 if (gt_bias) {
5316 __ ColtS(0, lhs, rhs);
5317 } else {
5318 __ CultS(0, lhs, rhs);
5319 }
5320 __ Bc1t(0, label);
5321 break;
5322 case kCondLE:
5323 if (gt_bias) {
5324 __ ColeS(0, lhs, rhs);
5325 } else {
5326 __ CuleS(0, lhs, rhs);
5327 }
5328 __ Bc1t(0, label);
5329 break;
5330 case kCondGT:
5331 if (gt_bias) {
5332 __ CultS(0, rhs, lhs);
5333 } else {
5334 __ ColtS(0, rhs, lhs);
5335 }
5336 __ Bc1t(0, label);
5337 break;
5338 case kCondGE:
5339 if (gt_bias) {
5340 __ CuleS(0, rhs, lhs);
5341 } else {
5342 __ ColeS(0, rhs, lhs);
5343 }
5344 __ Bc1t(0, label);
5345 break;
5346 default:
5347 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005348 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005349 }
5350 }
5351 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005352 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005353 if (isR6) {
5354 switch (cond) {
5355 case kCondEQ:
5356 __ CmpEqD(FTMP, lhs, rhs);
5357 __ Bc1nez(FTMP, label);
5358 break;
5359 case kCondNE:
5360 __ CmpEqD(FTMP, lhs, rhs);
5361 __ Bc1eqz(FTMP, label);
5362 break;
5363 case kCondLT:
5364 if (gt_bias) {
5365 __ CmpLtD(FTMP, lhs, rhs);
5366 } else {
5367 __ CmpUltD(FTMP, lhs, rhs);
5368 }
5369 __ Bc1nez(FTMP, label);
5370 break;
5371 case kCondLE:
5372 if (gt_bias) {
5373 __ CmpLeD(FTMP, lhs, rhs);
5374 } else {
5375 __ CmpUleD(FTMP, lhs, rhs);
5376 }
5377 __ Bc1nez(FTMP, label);
5378 break;
5379 case kCondGT:
5380 if (gt_bias) {
5381 __ CmpUltD(FTMP, rhs, lhs);
5382 } else {
5383 __ CmpLtD(FTMP, rhs, lhs);
5384 }
5385 __ Bc1nez(FTMP, label);
5386 break;
5387 case kCondGE:
5388 if (gt_bias) {
5389 __ CmpUleD(FTMP, rhs, lhs);
5390 } else {
5391 __ CmpLeD(FTMP, rhs, lhs);
5392 }
5393 __ Bc1nez(FTMP, label);
5394 break;
5395 default:
5396 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005397 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005398 }
5399 } else {
5400 switch (cond) {
5401 case kCondEQ:
5402 __ CeqD(0, lhs, rhs);
5403 __ Bc1t(0, label);
5404 break;
5405 case kCondNE:
5406 __ CeqD(0, lhs, rhs);
5407 __ Bc1f(0, label);
5408 break;
5409 case kCondLT:
5410 if (gt_bias) {
5411 __ ColtD(0, lhs, rhs);
5412 } else {
5413 __ CultD(0, lhs, rhs);
5414 }
5415 __ Bc1t(0, label);
5416 break;
5417 case kCondLE:
5418 if (gt_bias) {
5419 __ ColeD(0, lhs, rhs);
5420 } else {
5421 __ CuleD(0, lhs, rhs);
5422 }
5423 __ Bc1t(0, label);
5424 break;
5425 case kCondGT:
5426 if (gt_bias) {
5427 __ CultD(0, rhs, lhs);
5428 } else {
5429 __ ColtD(0, rhs, lhs);
5430 }
5431 __ Bc1t(0, label);
5432 break;
5433 case kCondGE:
5434 if (gt_bias) {
5435 __ CuleD(0, rhs, lhs);
5436 } else {
5437 __ ColeD(0, rhs, lhs);
5438 }
5439 __ Bc1t(0, label);
5440 break;
5441 default:
5442 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005443 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005444 }
5445 }
5446 }
5447}
5448
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005449void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005450 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005451 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005452 MipsLabel* false_target) {
5453 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454
David Brazdil0debae72015-11-12 18:37:00 +00005455 if (true_target == nullptr && false_target == nullptr) {
5456 // Nothing to do. The code always falls through.
5457 return;
5458 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005459 // Constant condition, statically compared against "true" (integer value 1).
5460 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005461 if (true_target != nullptr) {
5462 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005463 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005464 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005465 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005466 if (false_target != nullptr) {
5467 __ B(false_target);
5468 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005469 }
David Brazdil0debae72015-11-12 18:37:00 +00005470 return;
5471 }
5472
5473 // The following code generates these patterns:
5474 // (1) true_target == nullptr && false_target != nullptr
5475 // - opposite condition true => branch to false_target
5476 // (2) true_target != nullptr && false_target == nullptr
5477 // - condition true => branch to true_target
5478 // (3) true_target != nullptr && false_target != nullptr
5479 // - condition true => branch to true_target
5480 // - branch to false_target
5481 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005482 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005483 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005484 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005485 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005486 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5487 } else {
5488 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005490 } else {
5491 // The condition instruction has not been materialized, use its inputs as
5492 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005493 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005494 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005495 LocationSummary* locations = cond->GetLocations();
5496 IfCondition if_cond = condition->GetCondition();
5497 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005498
David Brazdil0debae72015-11-12 18:37:00 +00005499 if (true_target == nullptr) {
5500 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005501 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005502 }
5503
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005504 switch (type) {
5505 default:
5506 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5507 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005508 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005509 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5510 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 case DataType::Type::kFloat32:
5512 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005513 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5514 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005515 }
5516 }
David Brazdil0debae72015-11-12 18:37:00 +00005517
5518 // If neither branch falls through (case 3), the conditional branch to `true_target`
5519 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5520 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005521 __ B(false_target);
5522 }
5523}
5524
5525void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005526 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005527 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005528 locations->SetInAt(0, Location::RequiresRegister());
5529 }
5530}
5531
5532void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005533 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5534 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5535 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5536 nullptr : codegen_->GetLabelOf(true_successor);
5537 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5538 nullptr : codegen_->GetLabelOf(false_successor);
5539 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005540}
5541
5542void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005543 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005544 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005545 InvokeRuntimeCallingConvention calling_convention;
5546 RegisterSet caller_saves = RegisterSet::Empty();
5547 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5548 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005549 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005550 locations->SetInAt(0, Location::RequiresRegister());
5551 }
5552}
5553
5554void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005555 SlowPathCodeMIPS* slow_path =
5556 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005557 GenerateTestAndBranch(deoptimize,
5558 /* condition_input_index */ 0,
5559 slow_path->GetEntryLabel(),
5560 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561}
5562
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005563// This function returns true if a conditional move can be generated for HSelect.
5564// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5565// branches and regular moves.
5566//
5567// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5568//
5569// While determining feasibility of a conditional move and setting inputs/outputs
5570// are two distinct tasks, this function does both because they share quite a bit
5571// of common logic.
5572static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5573 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5574 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5575 HCondition* condition = cond->AsCondition();
5576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005577 DataType::Type cond_type =
5578 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5579 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005580
5581 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5582 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5583 bool is_true_value_zero_constant =
5584 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5585 bool is_false_value_zero_constant =
5586 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5587
5588 bool can_move_conditionally = false;
5589 bool use_const_for_false_in = false;
5590 bool use_const_for_true_in = false;
5591
5592 if (!cond->IsConstant()) {
5593 switch (cond_type) {
5594 default:
5595 switch (dst_type) {
5596 default:
5597 // Moving int on int condition.
5598 if (is_r6) {
5599 if (is_true_value_zero_constant) {
5600 // seleqz out_reg, false_reg, cond_reg
5601 can_move_conditionally = true;
5602 use_const_for_true_in = true;
5603 } else if (is_false_value_zero_constant) {
5604 // selnez out_reg, true_reg, cond_reg
5605 can_move_conditionally = true;
5606 use_const_for_false_in = true;
5607 } else if (materialized) {
5608 // Not materializing unmaterialized int conditions
5609 // to keep the instruction count low.
5610 // selnez AT, true_reg, cond_reg
5611 // seleqz TMP, false_reg, cond_reg
5612 // or out_reg, AT, TMP
5613 can_move_conditionally = true;
5614 }
5615 } else {
5616 // movn out_reg, true_reg/ZERO, cond_reg
5617 can_move_conditionally = true;
5618 use_const_for_true_in = is_true_value_zero_constant;
5619 }
5620 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005622 // Moving long on int condition.
5623 if (is_r6) {
5624 if (is_true_value_zero_constant) {
5625 // seleqz out_reg_lo, false_reg_lo, cond_reg
5626 // seleqz out_reg_hi, false_reg_hi, cond_reg
5627 can_move_conditionally = true;
5628 use_const_for_true_in = true;
5629 } else if (is_false_value_zero_constant) {
5630 // selnez out_reg_lo, true_reg_lo, cond_reg
5631 // selnez out_reg_hi, true_reg_hi, cond_reg
5632 can_move_conditionally = true;
5633 use_const_for_false_in = true;
5634 }
5635 // Other long conditional moves would generate 6+ instructions,
5636 // which is too many.
5637 } else {
5638 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5639 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5640 can_move_conditionally = true;
5641 use_const_for_true_in = is_true_value_zero_constant;
5642 }
5643 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 case DataType::Type::kFloat32:
5645 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005646 // Moving float/double on int condition.
5647 if (is_r6) {
5648 if (materialized) {
5649 // Not materializing unmaterialized int conditions
5650 // to keep the instruction count low.
5651 can_move_conditionally = true;
5652 if (is_true_value_zero_constant) {
5653 // sltu TMP, ZERO, cond_reg
5654 // mtc1 TMP, temp_cond_reg
5655 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5656 use_const_for_true_in = true;
5657 } else if (is_false_value_zero_constant) {
5658 // sltu TMP, ZERO, cond_reg
5659 // mtc1 TMP, temp_cond_reg
5660 // selnez.fmt out_reg, true_reg, temp_cond_reg
5661 use_const_for_false_in = true;
5662 } else {
5663 // sltu TMP, ZERO, cond_reg
5664 // mtc1 TMP, temp_cond_reg
5665 // sel.fmt temp_cond_reg, false_reg, true_reg
5666 // mov.fmt out_reg, temp_cond_reg
5667 }
5668 }
5669 } else {
5670 // movn.fmt out_reg, true_reg, cond_reg
5671 can_move_conditionally = true;
5672 }
5673 break;
5674 }
5675 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005676 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005677 // We don't materialize long comparison now
5678 // and use conditional branches instead.
5679 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kFloat32:
5681 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005682 switch (dst_type) {
5683 default:
5684 // Moving int on float/double condition.
5685 if (is_r6) {
5686 if (is_true_value_zero_constant) {
5687 // mfc1 TMP, temp_cond_reg
5688 // seleqz out_reg, false_reg, TMP
5689 can_move_conditionally = true;
5690 use_const_for_true_in = true;
5691 } else if (is_false_value_zero_constant) {
5692 // mfc1 TMP, temp_cond_reg
5693 // selnez out_reg, true_reg, TMP
5694 can_move_conditionally = true;
5695 use_const_for_false_in = true;
5696 } else {
5697 // mfc1 TMP, temp_cond_reg
5698 // selnez AT, true_reg, TMP
5699 // seleqz TMP, false_reg, TMP
5700 // or out_reg, AT, TMP
5701 can_move_conditionally = true;
5702 }
5703 } else {
5704 // movt out_reg, true_reg/ZERO, cc
5705 can_move_conditionally = true;
5706 use_const_for_true_in = is_true_value_zero_constant;
5707 }
5708 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005709 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005710 // Moving long on float/double condition.
5711 if (is_r6) {
5712 if (is_true_value_zero_constant) {
5713 // mfc1 TMP, temp_cond_reg
5714 // seleqz out_reg_lo, false_reg_lo, TMP
5715 // seleqz out_reg_hi, false_reg_hi, TMP
5716 can_move_conditionally = true;
5717 use_const_for_true_in = true;
5718 } else if (is_false_value_zero_constant) {
5719 // mfc1 TMP, temp_cond_reg
5720 // selnez out_reg_lo, true_reg_lo, TMP
5721 // selnez out_reg_hi, true_reg_hi, TMP
5722 can_move_conditionally = true;
5723 use_const_for_false_in = true;
5724 }
5725 // Other long conditional moves would generate 6+ instructions,
5726 // which is too many.
5727 } else {
5728 // movt out_reg_lo, true_reg_lo/ZERO, cc
5729 // movt out_reg_hi, true_reg_hi/ZERO, cc
5730 can_move_conditionally = true;
5731 use_const_for_true_in = is_true_value_zero_constant;
5732 }
5733 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 case DataType::Type::kFloat32:
5735 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005736 // Moving float/double on float/double condition.
5737 if (is_r6) {
5738 can_move_conditionally = true;
5739 if (is_true_value_zero_constant) {
5740 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5741 use_const_for_true_in = true;
5742 } else if (is_false_value_zero_constant) {
5743 // selnez.fmt out_reg, true_reg, temp_cond_reg
5744 use_const_for_false_in = true;
5745 } else {
5746 // sel.fmt temp_cond_reg, false_reg, true_reg
5747 // mov.fmt out_reg, temp_cond_reg
5748 }
5749 } else {
5750 // movt.fmt out_reg, true_reg, cc
5751 can_move_conditionally = true;
5752 }
5753 break;
5754 }
5755 break;
5756 }
5757 }
5758
5759 if (can_move_conditionally) {
5760 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5761 } else {
5762 DCHECK(!use_const_for_false_in);
5763 DCHECK(!use_const_for_true_in);
5764 }
5765
5766 if (locations_to_set != nullptr) {
5767 if (use_const_for_false_in) {
5768 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5769 } else {
5770 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005771 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005772 ? Location::RequiresFpuRegister()
5773 : Location::RequiresRegister());
5774 }
5775 if (use_const_for_true_in) {
5776 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5777 } else {
5778 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005780 ? Location::RequiresFpuRegister()
5781 : Location::RequiresRegister());
5782 }
5783 if (materialized) {
5784 locations_to_set->SetInAt(2, Location::RequiresRegister());
5785 }
5786 // On R6 we don't require the output to be the same as the
5787 // first input for conditional moves unlike on R2.
5788 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5789 if (is_out_same_as_first_in) {
5790 locations_to_set->SetOut(Location::SameAsFirstInput());
5791 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005792 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005793 ? Location::RequiresFpuRegister()
5794 : Location::RequiresRegister());
5795 }
5796 }
5797
5798 return can_move_conditionally;
5799}
5800
5801void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5802 LocationSummary* locations = select->GetLocations();
5803 Location dst = locations->Out();
5804 Location src = locations->InAt(1);
5805 Register src_reg = ZERO;
5806 Register src_reg_high = ZERO;
5807 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5808 Register cond_reg = TMP;
5809 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005811 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005813
5814 if (IsBooleanValueOrMaterializedCondition(cond)) {
5815 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5816 } else {
5817 HCondition* condition = cond->AsCondition();
5818 LocationSummary* cond_locations = cond->GetLocations();
5819 IfCondition if_cond = condition->GetCondition();
5820 cond_type = condition->InputAt(0)->GetType();
5821 switch (cond_type) {
5822 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005823 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005824 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5825 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 case DataType::Type::kFloat32:
5827 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005828 cond_inverted = MaterializeFpCompareR2(if_cond,
5829 condition->IsGtBias(),
5830 cond_type,
5831 cond_locations,
5832 cond_cc);
5833 break;
5834 }
5835 }
5836
5837 DCHECK(dst.Equals(locations->InAt(0)));
5838 if (src.IsRegister()) {
5839 src_reg = src.AsRegister<Register>();
5840 } else if (src.IsRegisterPair()) {
5841 src_reg = src.AsRegisterPairLow<Register>();
5842 src_reg_high = src.AsRegisterPairHigh<Register>();
5843 } else if (src.IsConstant()) {
5844 DCHECK(src.GetConstant()->IsZeroBitPattern());
5845 }
5846
5847 switch (cond_type) {
5848 default:
5849 switch (dst_type) {
5850 default:
5851 if (cond_inverted) {
5852 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5853 } else {
5854 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5855 }
5856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005858 if (cond_inverted) {
5859 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5860 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5861 } else {
5862 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5863 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5864 }
5865 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005867 if (cond_inverted) {
5868 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5869 } else {
5870 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5871 }
5872 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005874 if (cond_inverted) {
5875 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5876 } else {
5877 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5878 }
5879 break;
5880 }
5881 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005883 LOG(FATAL) << "Unreachable";
5884 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 case DataType::Type::kFloat32:
5886 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005887 switch (dst_type) {
5888 default:
5889 if (cond_inverted) {
5890 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5891 } else {
5892 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5893 }
5894 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005896 if (cond_inverted) {
5897 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5898 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5899 } else {
5900 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5901 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5902 }
5903 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005904 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005905 if (cond_inverted) {
5906 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5907 } else {
5908 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5909 }
5910 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005912 if (cond_inverted) {
5913 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5914 } else {
5915 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5916 }
5917 break;
5918 }
5919 break;
5920 }
5921}
5922
5923void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5924 LocationSummary* locations = select->GetLocations();
5925 Location dst = locations->Out();
5926 Location false_src = locations->InAt(0);
5927 Location true_src = locations->InAt(1);
5928 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5929 Register cond_reg = TMP;
5930 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005932 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005933 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005934
5935 if (IsBooleanValueOrMaterializedCondition(cond)) {
5936 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5937 } else {
5938 HCondition* condition = cond->AsCondition();
5939 LocationSummary* cond_locations = cond->GetLocations();
5940 IfCondition if_cond = condition->GetCondition();
5941 cond_type = condition->InputAt(0)->GetType();
5942 switch (cond_type) {
5943 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005945 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5946 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 case DataType::Type::kFloat32:
5948 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005949 cond_inverted = MaterializeFpCompareR6(if_cond,
5950 condition->IsGtBias(),
5951 cond_type,
5952 cond_locations,
5953 fcond_reg);
5954 break;
5955 }
5956 }
5957
5958 if (true_src.IsConstant()) {
5959 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5960 }
5961 if (false_src.IsConstant()) {
5962 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5963 }
5964
5965 switch (dst_type) {
5966 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005968 __ Mfc1(cond_reg, fcond_reg);
5969 }
5970 if (true_src.IsConstant()) {
5971 if (cond_inverted) {
5972 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5973 } else {
5974 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5975 }
5976 } else if (false_src.IsConstant()) {
5977 if (cond_inverted) {
5978 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5979 } else {
5980 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5981 }
5982 } else {
5983 DCHECK_NE(cond_reg, AT);
5984 if (cond_inverted) {
5985 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5986 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5987 } else {
5988 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5989 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5990 }
5991 __ Or(dst.AsRegister<Register>(), AT, TMP);
5992 }
5993 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kInt64: {
5995 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005996 __ Mfc1(cond_reg, fcond_reg);
5997 }
5998 Register dst_lo = dst.AsRegisterPairLow<Register>();
5999 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6000 if (true_src.IsConstant()) {
6001 Register src_lo = false_src.AsRegisterPairLow<Register>();
6002 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6003 if (cond_inverted) {
6004 __ Selnez(dst_lo, src_lo, cond_reg);
6005 __ Selnez(dst_hi, src_hi, cond_reg);
6006 } else {
6007 __ Seleqz(dst_lo, src_lo, cond_reg);
6008 __ Seleqz(dst_hi, src_hi, cond_reg);
6009 }
6010 } else {
6011 DCHECK(false_src.IsConstant());
6012 Register src_lo = true_src.AsRegisterPairLow<Register>();
6013 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6014 if (cond_inverted) {
6015 __ Seleqz(dst_lo, src_lo, cond_reg);
6016 __ Seleqz(dst_hi, src_hi, cond_reg);
6017 } else {
6018 __ Selnez(dst_lo, src_lo, cond_reg);
6019 __ Selnez(dst_hi, src_hi, cond_reg);
6020 }
6021 }
6022 break;
6023 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 case DataType::Type::kFloat32: {
6025 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006026 // sel*.fmt tests bit 0 of the condition register, account for that.
6027 __ Sltu(TMP, ZERO, cond_reg);
6028 __ Mtc1(TMP, fcond_reg);
6029 }
6030 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6031 if (true_src.IsConstant()) {
6032 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6033 if (cond_inverted) {
6034 __ SelnezS(dst_reg, src_reg, fcond_reg);
6035 } else {
6036 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6037 }
6038 } else if (false_src.IsConstant()) {
6039 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6040 if (cond_inverted) {
6041 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6042 } else {
6043 __ SelnezS(dst_reg, src_reg, fcond_reg);
6044 }
6045 } else {
6046 if (cond_inverted) {
6047 __ SelS(fcond_reg,
6048 true_src.AsFpuRegister<FRegister>(),
6049 false_src.AsFpuRegister<FRegister>());
6050 } else {
6051 __ SelS(fcond_reg,
6052 false_src.AsFpuRegister<FRegister>(),
6053 true_src.AsFpuRegister<FRegister>());
6054 }
6055 __ MovS(dst_reg, fcond_reg);
6056 }
6057 break;
6058 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006059 case DataType::Type::kFloat64: {
6060 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006061 // sel*.fmt tests bit 0 of the condition register, account for that.
6062 __ Sltu(TMP, ZERO, cond_reg);
6063 __ Mtc1(TMP, fcond_reg);
6064 }
6065 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6066 if (true_src.IsConstant()) {
6067 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6068 if (cond_inverted) {
6069 __ SelnezD(dst_reg, src_reg, fcond_reg);
6070 } else {
6071 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6072 }
6073 } else if (false_src.IsConstant()) {
6074 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6075 if (cond_inverted) {
6076 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6077 } else {
6078 __ SelnezD(dst_reg, src_reg, fcond_reg);
6079 }
6080 } else {
6081 if (cond_inverted) {
6082 __ SelD(fcond_reg,
6083 true_src.AsFpuRegister<FRegister>(),
6084 false_src.AsFpuRegister<FRegister>());
6085 } else {
6086 __ SelD(fcond_reg,
6087 false_src.AsFpuRegister<FRegister>(),
6088 true_src.AsFpuRegister<FRegister>());
6089 }
6090 __ MovD(dst_reg, fcond_reg);
6091 }
6092 break;
6093 }
6094 }
6095}
6096
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006097void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006098 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006099 LocationSummary(flag, LocationSummary::kNoCall);
6100 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006101}
6102
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006103void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6104 __ LoadFromOffset(kLoadWord,
6105 flag->GetLocations()->Out().AsRegister<Register>(),
6106 SP,
6107 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006108}
6109
David Brazdil74eb1b22015-12-14 11:44:01 +00006110void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006111 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006112 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006113}
6114
6115void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006116 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6117 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6118 if (is_r6) {
6119 GenConditionalMoveR6(select);
6120 } else {
6121 GenConditionalMoveR2(select);
6122 }
6123 } else {
6124 LocationSummary* locations = select->GetLocations();
6125 MipsLabel false_target;
6126 GenerateTestAndBranch(select,
6127 /* condition_input_index */ 2,
6128 /* true_target */ nullptr,
6129 &false_target);
6130 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6131 __ Bind(&false_target);
6132 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006133}
6134
David Srbecky0cf44932015-12-09 14:09:59 +00006135void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006136 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006137}
6138
David Srbeckyd28f4a02016-03-14 17:14:24 +00006139void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6140 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006141}
6142
6143void CodeGeneratorMIPS::GenerateNop() {
6144 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006145}
6146
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006147void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006148 DataType::Type field_type = field_info.GetFieldType();
6149 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006150 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006151 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006153 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006154 instruction,
6155 generate_volatile
6156 ? LocationSummary::kCallOnMainOnly
6157 : (object_field_get_with_read_barrier
6158 ? LocationSummary::kCallOnSlowPath
6159 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006160
Alexey Frunzec61c0762017-04-10 13:54:23 -07006161 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6162 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6163 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006164 locations->SetInAt(0, Location::RequiresRegister());
6165 if (generate_volatile) {
6166 InvokeRuntimeCallingConvention calling_convention;
6167 // need A0 to hold base + offset
6168 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006169 if (field_type == DataType::Type::kInt64) {
6170 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006171 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006172 // Use Location::Any() to prevent situations when running out of available fp registers.
6173 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006174 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006176 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6177 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6178 }
6179 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006180 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006181 locations->SetOut(Location::RequiresFpuRegister());
6182 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006183 // The output overlaps in the case of an object field get with
6184 // read barriers enabled: we do not want the move to overwrite the
6185 // object's location, as we need it to emit the read barrier.
6186 locations->SetOut(Location::RequiresRegister(),
6187 object_field_get_with_read_barrier
6188 ? Location::kOutputOverlap
6189 : Location::kNoOutputOverlap);
6190 }
6191 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6192 // We need a temporary register for the read barrier marking slow
6193 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006194 if (!kBakerReadBarrierThunksEnableForFields) {
6195 locations->AddTemp(Location::RequiresRegister());
6196 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006197 }
6198 }
6199}
6200
6201void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6202 const FieldInfo& field_info,
6203 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006204 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6205 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006206 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006207 Location obj_loc = locations->InAt(0);
6208 Register obj = obj_loc.AsRegister<Register>();
6209 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006210 LoadOperandType load_type = kLoadUnsignedByte;
6211 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006212 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006213 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214
6215 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006217 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006218 load_type = kLoadUnsignedByte;
6219 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 load_type = kLoadSignedByte;
6222 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006224 load_type = kLoadUnsignedHalfword;
6225 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006226 case DataType::Type::kInt16:
6227 load_type = kLoadSignedHalfword;
6228 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006229 case DataType::Type::kInt32:
6230 case DataType::Type::kFloat32:
6231 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006232 load_type = kLoadWord;
6233 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 case DataType::Type::kInt64:
6235 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006236 load_type = kLoadDoubleword;
6237 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006239 LOG(FATAL) << "Unreachable type " << type;
6240 UNREACHABLE();
6241 }
6242
6243 if (is_volatile && load_type == kLoadDoubleword) {
6244 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006245 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006246 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006247 __ LoadFromOffset(kLoadWord,
6248 ZERO,
6249 locations->GetTemp(0).AsRegister<Register>(),
6250 0,
6251 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006252 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006254 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006255 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006256 if (dst_loc.IsFpuRegister()) {
6257 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006258 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006259 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006260 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006261 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006262 __ StoreToOffset(kStoreWord,
6263 locations->GetTemp(1).AsRegister<Register>(),
6264 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006265 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006266 __ StoreToOffset(kStoreWord,
6267 locations->GetTemp(2).AsRegister<Register>(),
6268 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006269 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006271 }
6272 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006273 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006274 // /* HeapReference<Object> */ dst = *(obj + offset)
6275 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006276 Location temp_loc =
6277 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006278 // Note that a potential implicit null check is handled in this
6279 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6280 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6281 dst_loc,
6282 obj,
6283 offset,
6284 temp_loc,
6285 /* needs_null_check */ true);
6286 if (is_volatile) {
6287 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6288 }
6289 } else {
6290 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6291 if (is_volatile) {
6292 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6293 }
6294 // If read barriers are enabled, emit read barriers other than
6295 // Baker's using a slow path (and also unpoison the loaded
6296 // reference, if heap poisoning is enabled).
6297 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6298 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006300 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006302 DCHECK(dst_loc.IsRegisterPair());
6303 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006304 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006305 DCHECK(dst_loc.IsRegister());
6306 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006307 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006308 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006309 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006310 DCHECK(dst_loc.IsFpuRegister());
6311 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006313 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006315 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006316 }
6317 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006318 }
6319
Alexey Frunze15958152017-02-09 19:08:30 -08006320 // Memory barriers, in the case of references, are handled in the
6321 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006323 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6324 }
6325}
6326
6327void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 DataType::Type field_type = field_info.GetFieldType();
6329 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006330 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006331 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006332 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006333
6334 locations->SetInAt(0, Location::RequiresRegister());
6335 if (generate_volatile) {
6336 InvokeRuntimeCallingConvention calling_convention;
6337 // need A0 to hold base + offset
6338 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006340 locations->SetInAt(1, Location::RegisterPairLocation(
6341 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6342 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006343 // Use Location::Any() to prevent situations when running out of available fp registers.
6344 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006345 // Pass FP parameters in core registers.
6346 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6347 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6348 }
6349 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006350 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006351 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006352 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006353 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006354 }
6355 }
6356}
6357
6358void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6359 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006360 uint32_t dex_pc,
6361 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006363 LocationSummary* locations = instruction->GetLocations();
6364 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006365 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006366 StoreOperandType store_type = kStoreByte;
6367 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006368 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006369 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006370 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006371
6372 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006374 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 store_type = kStoreByte;
6377 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006378 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006379 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 store_type = kStoreHalfword;
6381 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 case DataType::Type::kInt32:
6383 case DataType::Type::kFloat32:
6384 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 store_type = kStoreWord;
6386 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 case DataType::Type::kInt64:
6388 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006389 store_type = kStoreDoubleword;
6390 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 LOG(FATAL) << "Unreachable type " << type;
6393 UNREACHABLE();
6394 }
6395
6396 if (is_volatile) {
6397 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6398 }
6399
6400 if (is_volatile && store_type == kStoreDoubleword) {
6401 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006402 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006404 __ LoadFromOffset(kLoadWord,
6405 ZERO,
6406 locations->GetTemp(0).AsRegister<Register>(),
6407 0,
6408 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006410 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006411 if (value_location.IsFpuRegister()) {
6412 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6413 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006414 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006415 value_location.AsFpuRegister<FRegister>());
6416 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006417 __ LoadFromOffset(kLoadWord,
6418 locations->GetTemp(1).AsRegister<Register>(),
6419 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006420 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006421 __ LoadFromOffset(kLoadWord,
6422 locations->GetTemp(2).AsRegister<Register>(),
6423 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006424 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006425 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006426 DCHECK(value_location.IsConstant());
6427 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6428 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006429 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6430 locations->GetTemp(1).AsRegister<Register>(),
6431 value);
6432 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006433 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006434 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006435 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6436 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006437 if (value_location.IsConstant()) {
6438 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6439 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006440 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006441 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006443 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006444 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006445 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006446 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006447 if (kPoisonHeapReferences && needs_write_barrier) {
6448 // Note that in the case where `value` is a null reference,
6449 // we do not enter this block, as a null reference does not
6450 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006451 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006452 __ PoisonHeapReference(TMP, src);
6453 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6454 } else {
6455 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006457 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006458 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006460 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006461 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006462 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006463 }
6464 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006465 }
6466
Alexey Frunzec061de12017-02-14 13:27:23 -08006467 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006468 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006469 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006470 }
6471
6472 if (is_volatile) {
6473 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6474 }
6475}
6476
6477void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6478 HandleFieldGet(instruction, instruction->GetFieldInfo());
6479}
6480
6481void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6482 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6483}
6484
6485void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6486 HandleFieldSet(instruction, instruction->GetFieldInfo());
6487}
6488
6489void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006490 HandleFieldSet(instruction,
6491 instruction->GetFieldInfo(),
6492 instruction->GetDexPc(),
6493 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494}
6495
Alexey Frunze15958152017-02-09 19:08:30 -08006496void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6497 HInstruction* instruction,
6498 Location out,
6499 uint32_t offset,
6500 Location maybe_temp,
6501 ReadBarrierOption read_barrier_option) {
6502 Register out_reg = out.AsRegister<Register>();
6503 if (read_barrier_option == kWithReadBarrier) {
6504 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006505 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6506 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6507 }
Alexey Frunze15958152017-02-09 19:08:30 -08006508 if (kUseBakerReadBarrier) {
6509 // Load with fast path based Baker's read barrier.
6510 // /* HeapReference<Object> */ out = *(out + offset)
6511 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6512 out,
6513 out_reg,
6514 offset,
6515 maybe_temp,
6516 /* needs_null_check */ false);
6517 } else {
6518 // Load with slow path based read barrier.
6519 // Save the value of `out` into `maybe_temp` before overwriting it
6520 // in the following move operation, as we will need it for the
6521 // read barrier below.
6522 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6523 // /* HeapReference<Object> */ out = *(out + offset)
6524 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6525 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6526 }
6527 } else {
6528 // Plain load with no read barrier.
6529 // /* HeapReference<Object> */ out = *(out + offset)
6530 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6531 __ MaybeUnpoisonHeapReference(out_reg);
6532 }
6533}
6534
6535void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6536 HInstruction* instruction,
6537 Location out,
6538 Location obj,
6539 uint32_t offset,
6540 Location maybe_temp,
6541 ReadBarrierOption read_barrier_option) {
6542 Register out_reg = out.AsRegister<Register>();
6543 Register obj_reg = obj.AsRegister<Register>();
6544 if (read_barrier_option == kWithReadBarrier) {
6545 CHECK(kEmitCompilerReadBarrier);
6546 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006547 if (!kBakerReadBarrierThunksEnableForFields) {
6548 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6549 }
Alexey Frunze15958152017-02-09 19:08:30 -08006550 // Load with fast path based Baker's read barrier.
6551 // /* HeapReference<Object> */ out = *(obj + offset)
6552 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6553 out,
6554 obj_reg,
6555 offset,
6556 maybe_temp,
6557 /* needs_null_check */ false);
6558 } else {
6559 // Load with slow path based read barrier.
6560 // /* HeapReference<Object> */ out = *(obj + offset)
6561 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6562 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6563 }
6564 } else {
6565 // Plain load with no read barrier.
6566 // /* HeapReference<Object> */ out = *(obj + offset)
6567 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6568 __ MaybeUnpoisonHeapReference(out_reg);
6569 }
6570}
6571
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006572static inline int GetBakerMarkThunkNumber(Register reg) {
6573 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6574 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6575 return reg - V0;
6576 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6577 return 14 + (reg - S2);
6578 } else if (reg == FP) { // One more.
6579 return 20;
6580 }
6581 LOG(FATAL) << "Unexpected register " << reg;
6582 UNREACHABLE();
6583}
6584
6585static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6586 int num = GetBakerMarkThunkNumber(reg) +
6587 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6588 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6589}
6590
6591static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6592 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6593 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6594}
6595
Alexey Frunze15958152017-02-09 19:08:30 -08006596void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6597 Location root,
6598 Register obj,
6599 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006600 ReadBarrierOption read_barrier_option,
6601 MipsLabel* label_low) {
6602 bool reordering;
6603 if (label_low != nullptr) {
6604 DCHECK_EQ(offset, 0x5678u);
6605 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006606 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006607 if (read_barrier_option == kWithReadBarrier) {
6608 DCHECK(kEmitCompilerReadBarrier);
6609 if (kUseBakerReadBarrier) {
6610 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6611 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006612 if (kBakerReadBarrierThunksEnableForGcRoots) {
6613 // Note that we do not actually check the value of `GetIsGcMarking()`
6614 // to decide whether to mark the loaded GC root or not. Instead, we
6615 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6616 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6617 // vice versa.
6618 //
6619 // We use thunks for the slow path. That thunk checks the reference
6620 // and jumps to the entrypoint if needed.
6621 //
6622 // temp = Thread::Current()->pReadBarrierMarkReg00
6623 // // AKA &art_quick_read_barrier_mark_introspection.
6624 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6625 // if (temp != nullptr) {
6626 // temp = &gc_root_thunk<root_reg>
6627 // root = temp(root)
6628 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006629
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006630 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6631 const int32_t entry_point_offset =
6632 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6633 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6634 int16_t offset_low = Low16Bits(offset);
6635 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6636 // extension in lw.
6637 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6638 Register base = short_offset ? obj : TMP;
6639 // Loading the entrypoint does not require a load acquire since it is only changed when
6640 // threads are suspended or running a checkpoint.
6641 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6642 reordering = __ SetReorder(false);
6643 if (!short_offset) {
6644 DCHECK(!label_low);
6645 __ AddUpper(base, obj, offset_high);
6646 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006647 MipsLabel skip_call;
6648 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006649 if (label_low != nullptr) {
6650 DCHECK(short_offset);
6651 __ Bind(label_low);
6652 }
6653 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6654 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6655 // in delay slot.
6656 if (isR6) {
6657 __ Jialc(T9, thunk_disp);
6658 } else {
6659 __ Addiu(T9, T9, thunk_disp);
6660 __ Jalr(T9);
6661 __ Nop();
6662 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006663 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006664 __ SetReorder(reordering);
6665 } else {
6666 // Note that we do not actually check the value of `GetIsGcMarking()`
6667 // to decide whether to mark the loaded GC root or not. Instead, we
6668 // load into `temp` (T9) the read barrier mark entry point corresponding
6669 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6670 // is false, and vice versa.
6671 //
6672 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6673 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6674 // if (temp != null) {
6675 // root = temp(root)
6676 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006677
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006678 if (label_low != nullptr) {
6679 reordering = __ SetReorder(false);
6680 __ Bind(label_low);
6681 }
6682 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6683 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6684 if (label_low != nullptr) {
6685 __ SetReorder(reordering);
6686 }
6687 static_assert(
6688 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6689 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6690 "have different sizes.");
6691 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6692 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6693 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006694
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006695 // Slow path marking the GC root `root`.
6696 Location temp = Location::RegisterLocation(T9);
6697 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006698 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006699 instruction,
6700 root,
6701 /*entrypoint*/ temp);
6702 codegen_->AddSlowPath(slow_path);
6703
6704 const int32_t entry_point_offset =
6705 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6706 // Loading the entrypoint does not require a load acquire since it is only changed when
6707 // threads are suspended or running a checkpoint.
6708 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6709 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6710 __ Bind(slow_path->GetExitLabel());
6711 }
Alexey Frunze15958152017-02-09 19:08:30 -08006712 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006713 if (label_low != nullptr) {
6714 reordering = __ SetReorder(false);
6715 __ Bind(label_low);
6716 }
Alexey Frunze15958152017-02-09 19:08:30 -08006717 // GC root loaded through a slow path for read barriers other
6718 // than Baker's.
6719 // /* GcRoot<mirror::Object>* */ root = obj + offset
6720 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006721 if (label_low != nullptr) {
6722 __ SetReorder(reordering);
6723 }
Alexey Frunze15958152017-02-09 19:08:30 -08006724 // /* mirror::Object* */ root = root->Read()
6725 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6726 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006727 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006728 if (label_low != nullptr) {
6729 reordering = __ SetReorder(false);
6730 __ Bind(label_low);
6731 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006732 // Plain GC root load with no read barrier.
6733 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6734 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6735 // Note that GC roots are not affected by heap poisoning, thus we
6736 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006737 if (label_low != nullptr) {
6738 __ SetReorder(reordering);
6739 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006740 }
6741}
6742
Alexey Frunze15958152017-02-09 19:08:30 -08006743void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6744 Location ref,
6745 Register obj,
6746 uint32_t offset,
6747 Location temp,
6748 bool needs_null_check) {
6749 DCHECK(kEmitCompilerReadBarrier);
6750 DCHECK(kUseBakerReadBarrier);
6751
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006752 if (kBakerReadBarrierThunksEnableForFields) {
6753 // Note that we do not actually check the value of `GetIsGcMarking()`
6754 // to decide whether to mark the loaded reference or not. Instead, we
6755 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6756 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6757 // vice versa.
6758 //
6759 // We use thunks for the slow path. That thunk checks the reference
6760 // and jumps to the entrypoint if needed. If the holder is not gray,
6761 // it issues a load-load memory barrier and returns to the original
6762 // reference load.
6763 //
6764 // temp = Thread::Current()->pReadBarrierMarkReg00
6765 // // AKA &art_quick_read_barrier_mark_introspection.
6766 // if (temp != nullptr) {
6767 // temp = &field_array_thunk<holder_reg>
6768 // temp()
6769 // }
6770 // not_gray_return_address:
6771 // // If the offset is too large to fit into the lw instruction, we
6772 // // use an adjusted base register (TMP) here. This register
6773 // // receives bits 16 ... 31 of the offset before the thunk invocation
6774 // // and the thunk benefits from it.
6775 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6776 // gray_return_address:
6777
6778 DCHECK(temp.IsInvalid());
6779 bool isR6 = GetInstructionSetFeatures().IsR6();
6780 int16_t offset_low = Low16Bits(offset);
6781 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6782 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6783 bool reordering = __ SetReorder(false);
6784 const int32_t entry_point_offset =
6785 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6786 // There may have or may have not been a null check if the field offset is smaller than
6787 // the page size.
6788 // There must've been a null check in case it's actually a load from an array.
6789 // We will, however, perform an explicit null check in the thunk as it's easier to
6790 // do it than not.
6791 if (instruction->IsArrayGet()) {
6792 DCHECK(!needs_null_check);
6793 }
6794 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6795 // Loading the entrypoint does not require a load acquire since it is only changed when
6796 // threads are suspended or running a checkpoint.
6797 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6798 Register ref_reg = ref.AsRegister<Register>();
6799 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006800 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006801 if (short_offset) {
6802 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006803 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006804 __ Nop(); // In forbidden slot.
6805 __ Jialc(T9, thunk_disp);
6806 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006807 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006808 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6809 __ Jalr(T9);
6810 __ Nop(); // In delay slot.
6811 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006812 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006813 } else {
6814 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006815 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006816 __ Aui(base, obj, offset_high); // In delay slot.
6817 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006818 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006819 } else {
6820 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006821 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006822 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6823 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006824 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006825 __ Addu(base, base, obj); // In delay slot.
6826 }
6827 }
6828 // /* HeapReference<Object> */ ref = *(obj + offset)
6829 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6830 if (needs_null_check) {
6831 MaybeRecordImplicitNullCheck(instruction);
6832 }
6833 __ MaybeUnpoisonHeapReference(ref_reg);
6834 __ SetReorder(reordering);
6835 return;
6836 }
6837
Alexey Frunze15958152017-02-09 19:08:30 -08006838 // /* HeapReference<Object> */ ref = *(obj + offset)
6839 Location no_index = Location::NoLocation();
6840 ScaleFactor no_scale_factor = TIMES_1;
6841 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6842 ref,
6843 obj,
6844 offset,
6845 no_index,
6846 no_scale_factor,
6847 temp,
6848 needs_null_check);
6849}
6850
6851void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6852 Location ref,
6853 Register obj,
6854 uint32_t data_offset,
6855 Location index,
6856 Location temp,
6857 bool needs_null_check) {
6858 DCHECK(kEmitCompilerReadBarrier);
6859 DCHECK(kUseBakerReadBarrier);
6860
6861 static_assert(
6862 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6863 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006864 ScaleFactor scale_factor = TIMES_4;
6865
6866 if (kBakerReadBarrierThunksEnableForArrays) {
6867 // Note that we do not actually check the value of `GetIsGcMarking()`
6868 // to decide whether to mark the loaded reference or not. Instead, we
6869 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6870 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6871 // vice versa.
6872 //
6873 // We use thunks for the slow path. That thunk checks the reference
6874 // and jumps to the entrypoint if needed. If the holder is not gray,
6875 // it issues a load-load memory barrier and returns to the original
6876 // reference load.
6877 //
6878 // temp = Thread::Current()->pReadBarrierMarkReg00
6879 // // AKA &art_quick_read_barrier_mark_introspection.
6880 // if (temp != nullptr) {
6881 // temp = &field_array_thunk<holder_reg>
6882 // temp()
6883 // }
6884 // not_gray_return_address:
6885 // // The element address is pre-calculated in the TMP register before the
6886 // // thunk invocation and the thunk benefits from it.
6887 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6888 // gray_return_address:
6889
6890 DCHECK(temp.IsInvalid());
6891 DCHECK(index.IsValid());
6892 bool reordering = __ SetReorder(false);
6893 const int32_t entry_point_offset =
6894 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6895 // We will not do the explicit null check in the thunk as some form of a null check
6896 // must've been done earlier.
6897 DCHECK(!needs_null_check);
6898 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6899 // Loading the entrypoint does not require a load acquire since it is only changed when
6900 // threads are suspended or running a checkpoint.
6901 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6902 Register ref_reg = ref.AsRegister<Register>();
6903 Register index_reg = index.IsRegisterPair()
6904 ? index.AsRegisterPairLow<Register>()
6905 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006906 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006907 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006908 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006909 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6910 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006911 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006912 } else {
6913 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006914 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006915 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6916 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006917 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006918 __ Addu(TMP, TMP, obj); // In delay slot.
6919 }
6920 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6921 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6922 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6923 __ MaybeUnpoisonHeapReference(ref_reg);
6924 __ SetReorder(reordering);
6925 return;
6926 }
6927
Alexey Frunze15958152017-02-09 19:08:30 -08006928 // /* HeapReference<Object> */ ref =
6929 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006930 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6931 ref,
6932 obj,
6933 data_offset,
6934 index,
6935 scale_factor,
6936 temp,
6937 needs_null_check);
6938}
6939
6940void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6941 Location ref,
6942 Register obj,
6943 uint32_t offset,
6944 Location index,
6945 ScaleFactor scale_factor,
6946 Location temp,
6947 bool needs_null_check,
6948 bool always_update_field) {
6949 DCHECK(kEmitCompilerReadBarrier);
6950 DCHECK(kUseBakerReadBarrier);
6951
6952 // In slow path based read barriers, the read barrier call is
6953 // inserted after the original load. However, in fast path based
6954 // Baker's read barriers, we need to perform the load of
6955 // mirror::Object::monitor_ *before* the original reference load.
6956 // This load-load ordering is required by the read barrier.
6957 // The fast path/slow path (for Baker's algorithm) should look like:
6958 //
6959 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6960 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6961 // HeapReference<Object> ref = *src; // Original reference load.
6962 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6963 // if (is_gray) {
6964 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6965 // }
6966 //
6967 // Note: the original implementation in ReadBarrier::Barrier is
6968 // slightly more complex as it performs additional checks that we do
6969 // not do here for performance reasons.
6970
6971 Register ref_reg = ref.AsRegister<Register>();
6972 Register temp_reg = temp.AsRegister<Register>();
6973 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6974
6975 // /* int32_t */ monitor = obj->monitor_
6976 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6977 if (needs_null_check) {
6978 MaybeRecordImplicitNullCheck(instruction);
6979 }
6980 // /* LockWord */ lock_word = LockWord(monitor)
6981 static_assert(sizeof(LockWord) == sizeof(int32_t),
6982 "art::LockWord and int32_t have different sizes.");
6983
6984 __ Sync(0); // Barrier to prevent load-load reordering.
6985
6986 // The actual reference load.
6987 if (index.IsValid()) {
6988 // Load types involving an "index": ArrayGet,
6989 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6990 // intrinsics.
6991 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6992 if (index.IsConstant()) {
6993 size_t computed_offset =
6994 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6995 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6996 } else {
6997 // Handle the special case of the
6998 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6999 // intrinsics, which use a register pair as index ("long
7000 // offset"), of which only the low part contains data.
7001 Register index_reg = index.IsRegisterPair()
7002 ? index.AsRegisterPairLow<Register>()
7003 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007004 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007005 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7006 }
7007 } else {
7008 // /* HeapReference<Object> */ ref = *(obj + offset)
7009 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7010 }
7011
7012 // Object* ref = ref_addr->AsMirrorPtr()
7013 __ MaybeUnpoisonHeapReference(ref_reg);
7014
7015 // Slow path marking the object `ref` when it is gray.
7016 SlowPathCodeMIPS* slow_path;
7017 if (always_update_field) {
7018 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7019 // of the form `obj + field_offset`, where `obj` is a register and
7020 // `field_offset` is a register pair (of which only the lower half
7021 // is used). Thus `offset` and `scale_factor` above are expected
7022 // to be null in this code path.
7023 DCHECK_EQ(offset, 0u);
7024 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007025 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007026 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7027 ref,
7028 obj,
7029 /* field_offset */ index,
7030 temp_reg);
7031 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007032 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007033 }
7034 AddSlowPath(slow_path);
7035
7036 // if (rb_state == ReadBarrier::GrayState())
7037 // ref = ReadBarrier::Mark(ref);
7038 // Given the numeric representation, it's enough to check the low bit of the
7039 // rb_state. We do that by shifting the bit into the sign bit (31) and
7040 // performing a branch on less than zero.
7041 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7042 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7043 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7044 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7045 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7046 __ Bind(slow_path->GetExitLabel());
7047}
7048
7049void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7050 Location out,
7051 Location ref,
7052 Location obj,
7053 uint32_t offset,
7054 Location index) {
7055 DCHECK(kEmitCompilerReadBarrier);
7056
7057 // Insert a slow path based read barrier *after* the reference load.
7058 //
7059 // If heap poisoning is enabled, the unpoisoning of the loaded
7060 // reference will be carried out by the runtime within the slow
7061 // path.
7062 //
7063 // Note that `ref` currently does not get unpoisoned (when heap
7064 // poisoning is enabled), which is alright as the `ref` argument is
7065 // not used by the artReadBarrierSlow entry point.
7066 //
7067 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007068 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007069 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7070 AddSlowPath(slow_path);
7071
7072 __ B(slow_path->GetEntryLabel());
7073 __ Bind(slow_path->GetExitLabel());
7074}
7075
7076void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7077 Location out,
7078 Location ref,
7079 Location obj,
7080 uint32_t offset,
7081 Location index) {
7082 if (kEmitCompilerReadBarrier) {
7083 // Baker's read barriers shall be handled by the fast path
7084 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7085 DCHECK(!kUseBakerReadBarrier);
7086 // If heap poisoning is enabled, unpoisoning will be taken care of
7087 // by the runtime within the slow path.
7088 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7089 } else if (kPoisonHeapReferences) {
7090 __ UnpoisonHeapReference(out.AsRegister<Register>());
7091 }
7092}
7093
7094void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7095 Location out,
7096 Location root) {
7097 DCHECK(kEmitCompilerReadBarrier);
7098
7099 // Insert a slow path based read barrier *after* the GC root load.
7100 //
7101 // Note that GC roots are not affected by heap poisoning, so we do
7102 // not need to do anything special for this here.
7103 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007104 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007105 AddSlowPath(slow_path);
7106
7107 __ B(slow_path->GetEntryLabel());
7108 __ Bind(slow_path->GetExitLabel());
7109}
7110
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007111void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007112 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7113 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007114 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007115 switch (type_check_kind) {
7116 case TypeCheckKind::kExactCheck:
7117 case TypeCheckKind::kAbstractClassCheck:
7118 case TypeCheckKind::kClassHierarchyCheck:
7119 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007120 call_kind =
7121 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007122 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007123 break;
7124 case TypeCheckKind::kArrayCheck:
7125 case TypeCheckKind::kUnresolvedCheck:
7126 case TypeCheckKind::kInterfaceCheck:
7127 call_kind = LocationSummary::kCallOnSlowPath;
7128 break;
7129 }
7130
Vladimir Markoca6fff82017-10-03 14:49:14 +01007131 LocationSummary* locations =
7132 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007133 if (baker_read_barrier_slow_path) {
7134 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7135 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007136 locations->SetInAt(0, Location::RequiresRegister());
7137 locations->SetInAt(1, Location::RequiresRegister());
7138 // The output does overlap inputs.
7139 // Note that TypeCheckSlowPathMIPS uses this register too.
7140 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007141 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007142}
7143
7144void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007145 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007146 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007147 Location obj_loc = locations->InAt(0);
7148 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007149 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007150 Location out_loc = locations->Out();
7151 Register out = out_loc.AsRegister<Register>();
7152 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7153 DCHECK_LE(num_temps, 1u);
7154 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007155 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7156 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7157 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7158 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007159 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007160 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007161
7162 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007163 // Avoid this check if we know `obj` is not null.
7164 if (instruction->MustDoNullCheck()) {
7165 __ Move(out, ZERO);
7166 __ Beqz(obj, &done);
7167 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007168
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007169 switch (type_check_kind) {
7170 case TypeCheckKind::kExactCheck: {
7171 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007172 GenerateReferenceLoadTwoRegisters(instruction,
7173 out_loc,
7174 obj_loc,
7175 class_offset,
7176 maybe_temp_loc,
7177 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007178 // Classes must be equal for the instanceof to succeed.
7179 __ Xor(out, out, cls);
7180 __ Sltiu(out, out, 1);
7181 break;
7182 }
7183
7184 case TypeCheckKind::kAbstractClassCheck: {
7185 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007186 GenerateReferenceLoadTwoRegisters(instruction,
7187 out_loc,
7188 obj_loc,
7189 class_offset,
7190 maybe_temp_loc,
7191 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007192 // If the class is abstract, we eagerly fetch the super class of the
7193 // object to avoid doing a comparison we know will fail.
7194 MipsLabel loop;
7195 __ Bind(&loop);
7196 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007197 GenerateReferenceLoadOneRegister(instruction,
7198 out_loc,
7199 super_offset,
7200 maybe_temp_loc,
7201 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007202 // If `out` is null, we use it for the result, and jump to `done`.
7203 __ Beqz(out, &done);
7204 __ Bne(out, cls, &loop);
7205 __ LoadConst32(out, 1);
7206 break;
7207 }
7208
7209 case TypeCheckKind::kClassHierarchyCheck: {
7210 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007211 GenerateReferenceLoadTwoRegisters(instruction,
7212 out_loc,
7213 obj_loc,
7214 class_offset,
7215 maybe_temp_loc,
7216 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007217 // Walk over the class hierarchy to find a match.
7218 MipsLabel loop, success;
7219 __ Bind(&loop);
7220 __ Beq(out, cls, &success);
7221 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007222 GenerateReferenceLoadOneRegister(instruction,
7223 out_loc,
7224 super_offset,
7225 maybe_temp_loc,
7226 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007227 __ Bnez(out, &loop);
7228 // If `out` is null, we use it for the result, and jump to `done`.
7229 __ B(&done);
7230 __ Bind(&success);
7231 __ LoadConst32(out, 1);
7232 break;
7233 }
7234
7235 case TypeCheckKind::kArrayObjectCheck: {
7236 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007237 GenerateReferenceLoadTwoRegisters(instruction,
7238 out_loc,
7239 obj_loc,
7240 class_offset,
7241 maybe_temp_loc,
7242 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007243 // Do an exact check.
7244 MipsLabel success;
7245 __ Beq(out, cls, &success);
7246 // Otherwise, we need to check that the object's class is a non-primitive array.
7247 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007248 GenerateReferenceLoadOneRegister(instruction,
7249 out_loc,
7250 component_offset,
7251 maybe_temp_loc,
7252 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007253 // If `out` is null, we use it for the result, and jump to `done`.
7254 __ Beqz(out, &done);
7255 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7256 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7257 __ Sltiu(out, out, 1);
7258 __ B(&done);
7259 __ Bind(&success);
7260 __ LoadConst32(out, 1);
7261 break;
7262 }
7263
7264 case TypeCheckKind::kArrayCheck: {
7265 // No read barrier since the slow path will retry upon failure.
7266 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007267 GenerateReferenceLoadTwoRegisters(instruction,
7268 out_loc,
7269 obj_loc,
7270 class_offset,
7271 maybe_temp_loc,
7272 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007273 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007274 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7275 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007276 codegen_->AddSlowPath(slow_path);
7277 __ Bne(out, cls, slow_path->GetEntryLabel());
7278 __ LoadConst32(out, 1);
7279 break;
7280 }
7281
7282 case TypeCheckKind::kUnresolvedCheck:
7283 case TypeCheckKind::kInterfaceCheck: {
7284 // Note that we indeed only call on slow path, but we always go
7285 // into the slow path for the unresolved and interface check
7286 // cases.
7287 //
7288 // We cannot directly call the InstanceofNonTrivial runtime
7289 // entry point without resorting to a type checking slow path
7290 // here (i.e. by calling InvokeRuntime directly), as it would
7291 // require to assign fixed registers for the inputs of this
7292 // HInstanceOf instruction (following the runtime calling
7293 // convention), which might be cluttered by the potential first
7294 // read barrier emission at the beginning of this method.
7295 //
7296 // TODO: Introduce a new runtime entry point taking the object
7297 // to test (instead of its class) as argument, and let it deal
7298 // with the read barrier issues. This will let us refactor this
7299 // case of the `switch` code as it was previously (with a direct
7300 // call to the runtime not using a type checking slow path).
7301 // This should also be beneficial for the other cases above.
7302 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007303 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7304 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007305 codegen_->AddSlowPath(slow_path);
7306 __ B(slow_path->GetEntryLabel());
7307 break;
7308 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007309 }
7310
7311 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007312
7313 if (slow_path != nullptr) {
7314 __ Bind(slow_path->GetExitLabel());
7315 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007316}
7317
7318void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007319 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007320 locations->SetOut(Location::ConstantLocation(constant));
7321}
7322
7323void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7324 // Will be generated at use site.
7325}
7326
7327void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007328 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007329 locations->SetOut(Location::ConstantLocation(constant));
7330}
7331
7332void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7333 // Will be generated at use site.
7334}
7335
7336void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7337 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7338 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7339}
7340
7341void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7342 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007343 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007344 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007345 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007346}
7347
7348void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7349 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7350 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007351 Location receiver = invoke->GetLocations()->InAt(0);
7352 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007353 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007354
7355 // Set the hidden argument.
7356 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7357 invoke->GetDexMethodIndex());
7358
7359 // temp = object->GetClass();
7360 if (receiver.IsStackSlot()) {
7361 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7362 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7363 } else {
7364 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7365 }
7366 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007367 // Instead of simply (possibly) unpoisoning `temp` here, we should
7368 // emit a read barrier for the previous class reference load.
7369 // However this is not required in practice, as this is an
7370 // intermediate/temporary reference and because the current
7371 // concurrent copying collector keeps the from-space memory
7372 // intact/accessible until the end of the marking phase (the
7373 // concurrent copying collector may not in the future).
7374 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007375 __ LoadFromOffset(kLoadWord, temp, temp,
7376 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7377 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007378 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007379 // temp = temp->GetImtEntryAt(method_offset);
7380 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7381 // T9 = temp->GetEntryPoint();
7382 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7383 // T9();
7384 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007385 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007386 DCHECK(!codegen_->IsLeafMethod());
7387 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7388}
7389
7390void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007391 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7392 if (intrinsic.TryDispatch(invoke)) {
7393 return;
7394 }
7395
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007396 HandleInvoke(invoke);
7397}
7398
7399void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007400 // Explicit clinit checks triggered by static invokes must have been pruned by
7401 // art::PrepareForRegisterAllocation.
7402 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007403
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007404 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007405 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7406 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007407
Chris Larsen701566a2015-10-27 15:29:13 -07007408 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7409 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007410 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7411 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7412 }
Chris Larsen701566a2015-10-27 15:29:13 -07007413 return;
7414 }
7415
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007416 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007417
7418 // Add the extra input register if either the dex cache array base register
7419 // or the PC-relative base register for accessing literals is needed.
7420 if (has_extra_input) {
7421 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7422 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007423}
7424
Orion Hodsonac141392017-01-13 11:53:47 +00007425void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7426 HandleInvoke(invoke);
7427}
7428
7429void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7430 codegen_->GenerateInvokePolymorphicCall(invoke);
7431}
7432
Chris Larsen701566a2015-10-27 15:29:13 -07007433static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007434 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007435 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7436 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007437 return true;
7438 }
7439 return false;
7440}
7441
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007442HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007443 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007444 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007445 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007446 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007447 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007448 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007449 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007450 case HLoadString::LoadKind::kJitTableAddress:
7451 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007452 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007453 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007454 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007455 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007456 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007457 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007458}
7459
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007460HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7461 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007462 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007463 case HLoadClass::LoadKind::kInvalid:
7464 LOG(FATAL) << "UNREACHABLE";
7465 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007466 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007467 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007468 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007469 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007470 case HLoadClass::LoadKind::kBssEntry:
7471 DCHECK(!Runtime::Current()->UseJitCompilation());
7472 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007473 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007474 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007475 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007476 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007477 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007478 break;
7479 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007480 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007481}
7482
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007483Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7484 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007485 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007486 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007487 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7488 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7489 if (!invoke->GetLocations()->Intrinsified()) {
7490 return location.AsRegister<Register>();
7491 }
7492 // For intrinsics we allow any location, so it may be on the stack.
7493 if (!location.IsRegister()) {
7494 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7495 return temp;
7496 }
7497 // For register locations, check if the register was saved. If so, get it from the stack.
7498 // Note: There is a chance that the register was saved but not overwritten, so we could
7499 // save one load. However, since this is just an intrinsic slow path we prefer this
7500 // simple and more robust approach rather that trying to determine if that's the case.
7501 SlowPathCode* slow_path = GetCurrentSlowPath();
7502 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7503 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7504 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7505 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7506 return temp;
7507 }
7508 return location.AsRegister<Register>();
7509}
7510
Vladimir Markodc151b22015-10-15 18:02:30 +01007511HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7512 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007513 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007514 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007515}
7516
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007517void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7518 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007519 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007520 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007521 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7522 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007523 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007524 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7525 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007526 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7527 : ZERO;
7528
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007529 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007530 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007531 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007532 uint32_t offset =
7533 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007534 __ LoadFromOffset(kLoadWord,
7535 temp.AsRegister<Register>(),
7536 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007537 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007538 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007539 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007540 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007541 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007542 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007543 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7544 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007545 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7546 PcRelativePatchInfo* info_low =
7547 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007548 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007549 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7550 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007551 break;
7552 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007553 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7554 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7555 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007556 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007557 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007558 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007559 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7560 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007561 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007562 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7563 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007564 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007565 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007566 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7567 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7568 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007569 }
7570 }
7571
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007572 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007573 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007574 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007575 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007576 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7577 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007578 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007579 T9,
7580 callee_method.AsRegister<Register>(),
7581 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007582 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007583 // T9()
7584 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007585 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007586 break;
7587 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007588 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7589
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007590 DCHECK(!IsLeafMethod());
7591}
7592
7593void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007594 // Explicit clinit checks triggered by static invokes must have been pruned by
7595 // art::PrepareForRegisterAllocation.
7596 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007597
7598 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7599 return;
7600 }
7601
7602 LocationSummary* locations = invoke->GetLocations();
7603 codegen_->GenerateStaticOrDirectCall(invoke,
7604 locations->HasTemps()
7605 ? locations->GetTemp(0)
7606 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007607}
7608
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007609void CodeGeneratorMIPS::GenerateVirtualCall(
7610 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007611 // Use the calling convention instead of the location of the receiver, as
7612 // intrinsics may have put the receiver in a different register. In the intrinsics
7613 // slow path, the arguments have been moved to the right place, so here we are
7614 // guaranteed that the receiver is the first register of the calling convention.
7615 InvokeDexCallingConvention calling_convention;
7616 Register receiver = calling_convention.GetRegisterAt(0);
7617
Chris Larsen3acee732015-11-18 13:31:08 -08007618 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7620 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7621 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007622 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007623
7624 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007625 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007626 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007627 // Instead of simply (possibly) unpoisoning `temp` here, we should
7628 // emit a read barrier for the previous class reference load.
7629 // However this is not required in practice, as this is an
7630 // intermediate/temporary reference and because the current
7631 // concurrent copying collector keeps the from-space memory
7632 // intact/accessible until the end of the marking phase (the
7633 // concurrent copying collector may not in the future).
7634 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007635 // temp = temp->GetMethodAt(method_offset);
7636 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7637 // T9 = temp->GetEntryPoint();
7638 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7639 // T9();
7640 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007641 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007642 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007643}
7644
7645void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7646 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7647 return;
7648 }
7649
7650 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007651 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007652}
7653
7654void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007655 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007656 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007657 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007658 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7659 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007660 return;
7661 }
Vladimir Marko41559982017-01-06 14:04:23 +00007662 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007663 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007664 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007665 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7666 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007667 ? LocationSummary::kCallOnSlowPath
7668 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007670 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7671 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7672 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007673 switch (load_kind) {
7674 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007675 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007676 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007677 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007678 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007679 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007680 break;
7681 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007682 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007683 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7684 codegen_->ClobberRA();
7685 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007686 break;
7687 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007688 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007689 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007690 locations->SetInAt(0, Location::RequiresRegister());
7691 break;
7692 default:
7693 break;
7694 }
7695 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007696 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7697 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7698 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007699 // Request a temp to hold the BSS entry location for the slow path.
7700 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007701 RegisterSet caller_saves = RegisterSet::Empty();
7702 InvokeRuntimeCallingConvention calling_convention;
7703 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7704 locations->SetCustomSlowPathCallerSaves(caller_saves);
7705 } else {
7706 // For non-Baker read barriers we have a temp-clobbering call.
7707 }
7708 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007709}
7710
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007711// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7712// move.
7713void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007714 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007715 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007716 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007717 return;
7718 }
Vladimir Marko41559982017-01-06 14:04:23 +00007719 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007720
Vladimir Marko41559982017-01-06 14:04:23 +00007721 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007722 Location out_loc = locations->Out();
7723 Register out = out_loc.AsRegister<Register>();
7724 Register base_or_current_method_reg;
7725 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007726 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 switch (load_kind) {
7728 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007729 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007730 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007731 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007732 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007733 base_or_current_method_reg =
7734 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007735 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007736 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007737 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007738 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7739 break;
7740 default:
7741 base_or_current_method_reg = ZERO;
7742 break;
7743 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007744
Alexey Frunze15958152017-02-09 19:08:30 -08007745 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7746 ? kWithoutReadBarrier
7747 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007748 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007749 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007750 switch (load_kind) {
7751 case HLoadClass::LoadKind::kReferrersClass: {
7752 DCHECK(!cls->CanCallRuntime());
7753 DCHECK(!cls->MustGenerateClinitCheck());
7754 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7755 GenerateGcRootFieldLoad(cls,
7756 out_loc,
7757 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007758 ArtMethod::DeclaringClassOffset().Int32Value(),
7759 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 break;
7761 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007762 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007763 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007764 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007765 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007766 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007767 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7768 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007769 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7770 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007771 base_or_current_method_reg);
7772 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007773 break;
7774 }
7775 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007776 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007777 uint32_t address = dchecked_integral_cast<uint32_t>(
7778 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7779 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007780 if (isR6 || !has_irreducible_loops) {
7781 __ LoadLiteral(out,
7782 base_or_current_method_reg,
7783 codegen_->DeduplicateBootImageAddressLiteral(address));
7784 } else {
7785 __ LoadConst32(out, address);
7786 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007787 break;
7788 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007789 case HLoadClass::LoadKind::kBootImageClassTable: {
7790 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7791 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7792 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7793 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7794 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7795 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7796 out,
7797 base_or_current_method_reg);
7798 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7799 // Extract the reference from the slot data, i.e. clear the hash bits.
7800 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7801 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7802 if (masked_hash != 0) {
7803 __ Addiu(out, out, -masked_hash);
7804 }
7805 break;
7806 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007807 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007808 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7809 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7810 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007811 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007812 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007813 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7814 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007815 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007816 GenerateGcRootFieldLoad(cls,
7817 out_loc,
7818 temp,
7819 /* placeholder */ 0x5678,
7820 read_barrier_option,
7821 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007822 generate_null_check = true;
7823 break;
7824 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007825 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007826 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7827 cls->GetTypeIndex(),
7828 cls->GetClass());
7829 bool reordering = __ SetReorder(false);
7830 __ Bind(&info->high_label);
7831 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007832 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007833 GenerateGcRootFieldLoad(cls,
7834 out_loc,
7835 out,
7836 /* placeholder */ 0x5678,
7837 read_barrier_option,
7838 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007839 break;
7840 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007841 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007842 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007843 LOG(FATAL) << "UNREACHABLE";
7844 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007845 }
7846
7847 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7848 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007849 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007850 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007851 codegen_->AddSlowPath(slow_path);
7852 if (generate_null_check) {
7853 __ Beqz(out, slow_path->GetEntryLabel());
7854 }
7855 if (cls->MustGenerateClinitCheck()) {
7856 GenerateClassInitializationCheck(slow_path, out);
7857 } else {
7858 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007859 }
7860 }
7861}
7862
7863static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007864 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007865}
7866
7867void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7868 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007869 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007870 locations->SetOut(Location::RequiresRegister());
7871}
7872
7873void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7874 Register out = load->GetLocations()->Out().AsRegister<Register>();
7875 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7876}
7877
7878void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007879 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007880}
7881
7882void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7883 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7884}
7885
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007886void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007887 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007888 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007889 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007890 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007891 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007892 switch (load_kind) {
7893 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007894 case HLoadString::LoadKind::kBootImageAddress:
7895 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007896 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007897 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007898 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007899 break;
7900 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007901 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007902 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7903 codegen_->ClobberRA();
7904 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007905 break;
7906 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007907 FALLTHROUGH_INTENDED;
7908 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007909 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007910 locations->SetInAt(0, Location::RequiresRegister());
7911 break;
7912 default:
7913 break;
7914 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007915 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007916 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007917 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007918 } else {
7919 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007920 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7921 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7922 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007923 // Request a temp to hold the BSS entry location for the slow path.
7924 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007925 RegisterSet caller_saves = RegisterSet::Empty();
7926 InvokeRuntimeCallingConvention calling_convention;
7927 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7928 locations->SetCustomSlowPathCallerSaves(caller_saves);
7929 } else {
7930 // For non-Baker read barriers we have a temp-clobbering call.
7931 }
7932 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007933 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007934}
7935
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007936// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7937// move.
7938void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007939 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007940 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007941 Location out_loc = locations->Out();
7942 Register out = out_loc.AsRegister<Register>();
7943 Register base_or_current_method_reg;
7944 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007945 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007946 switch (load_kind) {
7947 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007948 case HLoadString::LoadKind::kBootImageAddress:
7949 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007950 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007951 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007952 base_or_current_method_reg =
7953 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007954 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007955 default:
7956 base_or_current_method_reg = ZERO;
7957 break;
7958 }
7959
7960 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007961 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007962 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007963 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007964 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007965 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7966 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007967 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7968 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007969 base_or_current_method_reg);
7970 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007971 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007972 }
7973 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007974 uint32_t address = dchecked_integral_cast<uint32_t>(
7975 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7976 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007977 if (isR6 || !has_irreducible_loops) {
7978 __ LoadLiteral(out,
7979 base_or_current_method_reg,
7980 codegen_->DeduplicateBootImageAddressLiteral(address));
7981 } else {
7982 __ LoadConst32(out, address);
7983 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007984 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007985 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007986 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007987 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007988 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007989 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007990 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7991 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007992 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7993 out,
7994 base_or_current_method_reg);
7995 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7996 return;
7997 }
7998 case HLoadString::LoadKind::kBssEntry: {
7999 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8000 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8001 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8002 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8003 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008004 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008005 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008006 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8007 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008008 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008009 GenerateGcRootFieldLoad(load,
8010 out_loc,
8011 temp,
8012 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008013 kCompilerReadBarrierOption,
8014 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008015 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008016 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008017 codegen_->AddSlowPath(slow_path);
8018 __ Beqz(out, slow_path->GetEntryLabel());
8019 __ Bind(slow_path->GetExitLabel());
8020 return;
8021 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008022 case HLoadString::LoadKind::kJitTableAddress: {
8023 CodeGeneratorMIPS::JitPatchInfo* info =
8024 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8025 load->GetStringIndex(),
8026 load->GetString());
8027 bool reordering = __ SetReorder(false);
8028 __ Bind(&info->high_label);
8029 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008030 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008031 GenerateGcRootFieldLoad(load,
8032 out_loc,
8033 out,
8034 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008035 kCompilerReadBarrierOption,
8036 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008037 return;
8038 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008039 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008040 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008041 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008042
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008043 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008044 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008045 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008046 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008047 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008048 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8049 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008050}
8051
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008052void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008053 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008054 locations->SetOut(Location::ConstantLocation(constant));
8055}
8056
8057void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8058 // Will be generated at use site.
8059}
8060
8061void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008062 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8063 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008064 InvokeRuntimeCallingConvention calling_convention;
8065 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8066}
8067
8068void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8069 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008070 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008071 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8072 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008073 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008074 }
8075 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8076}
8077
8078void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008080 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008081 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008082 case DataType::Type::kInt32:
8083 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008084 locations->SetInAt(0, Location::RequiresRegister());
8085 locations->SetInAt(1, Location::RequiresRegister());
8086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8087 break;
8088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008089 case DataType::Type::kFloat32:
8090 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008091 locations->SetInAt(0, Location::RequiresFpuRegister());
8092 locations->SetInAt(1, Location::RequiresFpuRegister());
8093 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8094 break;
8095
8096 default:
8097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8098 }
8099}
8100
8101void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008102 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008103 LocationSummary* locations = instruction->GetLocations();
8104 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8105
8106 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008107 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008108 Register dst = locations->Out().AsRegister<Register>();
8109 Register lhs = locations->InAt(0).AsRegister<Register>();
8110 Register rhs = locations->InAt(1).AsRegister<Register>();
8111
8112 if (isR6) {
8113 __ MulR6(dst, lhs, rhs);
8114 } else {
8115 __ MulR2(dst, lhs, rhs);
8116 }
8117 break;
8118 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008119 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008120 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8121 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8122 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8123 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8124 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8125 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8126
8127 // Extra checks to protect caused by the existance of A1_A2.
8128 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8129 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8130 DCHECK_NE(dst_high, lhs_low);
8131 DCHECK_NE(dst_high, rhs_low);
8132
8133 // A_B * C_D
8134 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8135 // dst_lo: [ low(B*D) ]
8136 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8137
8138 if (isR6) {
8139 __ MulR6(TMP, lhs_high, rhs_low);
8140 __ MulR6(dst_high, lhs_low, rhs_high);
8141 __ Addu(dst_high, dst_high, TMP);
8142 __ MuhuR6(TMP, lhs_low, rhs_low);
8143 __ Addu(dst_high, dst_high, TMP);
8144 __ MulR6(dst_low, lhs_low, rhs_low);
8145 } else {
8146 __ MulR2(TMP, lhs_high, rhs_low);
8147 __ MulR2(dst_high, lhs_low, rhs_high);
8148 __ Addu(dst_high, dst_high, TMP);
8149 __ MultuR2(lhs_low, rhs_low);
8150 __ Mfhi(TMP);
8151 __ Addu(dst_high, dst_high, TMP);
8152 __ Mflo(dst_low);
8153 }
8154 break;
8155 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008156 case DataType::Type::kFloat32:
8157 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008158 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8159 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8160 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008161 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008162 __ MulS(dst, lhs, rhs);
8163 } else {
8164 __ MulD(dst, lhs, rhs);
8165 }
8166 break;
8167 }
8168 default:
8169 LOG(FATAL) << "Unexpected mul type " << type;
8170 }
8171}
8172
8173void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8174 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008175 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008176 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008177 case DataType::Type::kInt32:
8178 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008179 locations->SetInAt(0, Location::RequiresRegister());
8180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8181 break;
8182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008183 case DataType::Type::kFloat32:
8184 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008185 locations->SetInAt(0, Location::RequiresFpuRegister());
8186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8187 break;
8188
8189 default:
8190 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8191 }
8192}
8193
8194void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008195 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008196 LocationSummary* locations = instruction->GetLocations();
8197
8198 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008199 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008200 Register dst = locations->Out().AsRegister<Register>();
8201 Register src = locations->InAt(0).AsRegister<Register>();
8202 __ Subu(dst, ZERO, src);
8203 break;
8204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008205 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008206 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8207 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8208 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8209 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8210 __ Subu(dst_low, ZERO, src_low);
8211 __ Sltu(TMP, ZERO, dst_low);
8212 __ Subu(dst_high, ZERO, src_high);
8213 __ Subu(dst_high, dst_high, TMP);
8214 break;
8215 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008216 case DataType::Type::kFloat32:
8217 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008218 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8219 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008220 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008221 __ NegS(dst, src);
8222 } else {
8223 __ NegD(dst, src);
8224 }
8225 break;
8226 }
8227 default:
8228 LOG(FATAL) << "Unexpected neg type " << type;
8229 }
8230}
8231
8232void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008233 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8234 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008236 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008237 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8238 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008239}
8240
8241void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008242 // Note: if heap poisoning is enabled, the entry point takes care
8243 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008244 QuickEntrypointEnum entrypoint =
8245 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8246 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008247 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008248 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008249}
8250
8251void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008252 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8253 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008254 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008255 if (instruction->IsStringAlloc()) {
8256 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8257 } else {
8258 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008259 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008260 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008261}
8262
8263void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008264 // Note: if heap poisoning is enabled, the entry point takes care
8265 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008266 if (instruction->IsStringAlloc()) {
8267 // String is allocated through StringFactory. Call NewEmptyString entry point.
8268 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008269 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008270 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8271 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8272 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008273 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008274 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8275 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008276 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008277 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008278 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008279}
8280
8281void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008282 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283 locations->SetInAt(0, Location::RequiresRegister());
8284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8285}
8286
8287void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008288 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008289 LocationSummary* locations = instruction->GetLocations();
8290
8291 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008292 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008293 Register dst = locations->Out().AsRegister<Register>();
8294 Register src = locations->InAt(0).AsRegister<Register>();
8295 __ Nor(dst, src, ZERO);
8296 break;
8297 }
8298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008299 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008300 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8301 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8302 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8303 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8304 __ Nor(dst_high, src_high, ZERO);
8305 __ Nor(dst_low, src_low, ZERO);
8306 break;
8307 }
8308
8309 default:
8310 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8311 }
8312}
8313
8314void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008315 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008316 locations->SetInAt(0, Location::RequiresRegister());
8317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8318}
8319
8320void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8321 LocationSummary* locations = instruction->GetLocations();
8322 __ Xori(locations->Out().AsRegister<Register>(),
8323 locations->InAt(0).AsRegister<Register>(),
8324 1);
8325}
8326
8327void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008328 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8329 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008330}
8331
Calin Juravle2ae48182016-03-16 14:05:09 +00008332void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8333 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008334 return;
8335 }
8336 Location obj = instruction->GetLocations()->InAt(0);
8337
8338 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008339 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008340}
8341
Calin Juravle2ae48182016-03-16 14:05:09 +00008342void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008343 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008344 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345
8346 Location obj = instruction->GetLocations()->InAt(0);
8347
8348 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8349}
8350
8351void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008352 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008353}
8354
8355void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8356 HandleBinaryOp(instruction);
8357}
8358
8359void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8360 HandleBinaryOp(instruction);
8361}
8362
8363void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8364 LOG(FATAL) << "Unreachable";
8365}
8366
8367void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008368 if (instruction->GetNext()->IsSuspendCheck() &&
8369 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8370 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8371 // The back edge will generate the suspend check.
8372 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8373 }
8374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008375 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8376}
8377
8378void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008379 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008380 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8381 if (location.IsStackSlot()) {
8382 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8383 } else if (location.IsDoubleStackSlot()) {
8384 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8385 }
8386 locations->SetOut(location);
8387}
8388
8389void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8390 ATTRIBUTE_UNUSED) {
8391 // Nothing to do, the parameter is already at its location.
8392}
8393
8394void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8395 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008396 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008397 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8398}
8399
8400void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8401 ATTRIBUTE_UNUSED) {
8402 // Nothing to do, the method is already at its location.
8403}
8404
8405void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008406 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008407 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008408 locations->SetInAt(i, Location::Any());
8409 }
8410 locations->SetOut(Location::Any());
8411}
8412
8413void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8414 LOG(FATAL) << "Unreachable";
8415}
8416
8417void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008418 DataType::Type type = rem->GetResultType();
8419 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8420 ? LocationSummary::kNoCall
8421 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008422 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008423
8424 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008425 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008426 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008427 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8429 break;
8430
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008431 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008432 InvokeRuntimeCallingConvention calling_convention;
8433 locations->SetInAt(0, Location::RegisterPairLocation(
8434 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8435 locations->SetInAt(1, Location::RegisterPairLocation(
8436 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8437 locations->SetOut(calling_convention.GetReturnLocation(type));
8438 break;
8439 }
8440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008441 case DataType::Type::kFloat32:
8442 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008443 InvokeRuntimeCallingConvention calling_convention;
8444 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8445 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8446 locations->SetOut(calling_convention.GetReturnLocation(type));
8447 break;
8448 }
8449
8450 default:
8451 LOG(FATAL) << "Unexpected rem type " << type;
8452 }
8453}
8454
8455void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008456 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008457
8458 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008459 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008460 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008461 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008462 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008463 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008464 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8465 break;
8466 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008467 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008468 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008469 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008470 break;
8471 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008472 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008473 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008474 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475 break;
8476 }
8477 default:
8478 LOG(FATAL) << "Unexpected rem type " << type;
8479 }
8480}
8481
Igor Murashkind01745e2017-04-05 16:40:31 -07008482void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8483 constructor_fence->SetLocations(nullptr);
8484}
8485
8486void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8487 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8488 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8489}
8490
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8492 memory_barrier->SetLocations(nullptr);
8493}
8494
8495void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8496 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8497}
8498
8499void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008500 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008501 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008502 locations->SetInAt(0, MipsReturnLocation(return_type));
8503}
8504
8505void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8506 codegen_->GenerateFrameExit();
8507}
8508
8509void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8510 ret->SetLocations(nullptr);
8511}
8512
8513void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8514 codegen_->GenerateFrameExit();
8515}
8516
Alexey Frunze92d90602015-12-18 18:16:36 -08008517void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8518 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008519}
8520
Alexey Frunze92d90602015-12-18 18:16:36 -08008521void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8522 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008523}
8524
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8526 HandleShift(shl);
8527}
8528
8529void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8530 HandleShift(shl);
8531}
8532
8533void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8534 HandleShift(shr);
8535}
8536
8537void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8538 HandleShift(shr);
8539}
8540
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008541void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8542 HandleBinaryOp(instruction);
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8546 HandleBinaryOp(instruction);
8547}
8548
8549void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8550 HandleFieldGet(instruction, instruction->GetFieldInfo());
8551}
8552
8553void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8554 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8555}
8556
8557void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8558 HandleFieldSet(instruction, instruction->GetFieldInfo());
8559}
8560
8561void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008562 HandleFieldSet(instruction,
8563 instruction->GetFieldInfo(),
8564 instruction->GetDexPc(),
8565 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008566}
8567
8568void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8569 HUnresolvedInstanceFieldGet* instruction) {
8570 FieldAccessCallingConventionMIPS calling_convention;
8571 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8572 instruction->GetFieldType(),
8573 calling_convention);
8574}
8575
8576void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8577 HUnresolvedInstanceFieldGet* instruction) {
8578 FieldAccessCallingConventionMIPS calling_convention;
8579 codegen_->GenerateUnresolvedFieldAccess(instruction,
8580 instruction->GetFieldType(),
8581 instruction->GetFieldIndex(),
8582 instruction->GetDexPc(),
8583 calling_convention);
8584}
8585
8586void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8587 HUnresolvedInstanceFieldSet* instruction) {
8588 FieldAccessCallingConventionMIPS calling_convention;
8589 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8590 instruction->GetFieldType(),
8591 calling_convention);
8592}
8593
8594void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8595 HUnresolvedInstanceFieldSet* instruction) {
8596 FieldAccessCallingConventionMIPS calling_convention;
8597 codegen_->GenerateUnresolvedFieldAccess(instruction,
8598 instruction->GetFieldType(),
8599 instruction->GetFieldIndex(),
8600 instruction->GetDexPc(),
8601 calling_convention);
8602}
8603
8604void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8605 HUnresolvedStaticFieldGet* instruction) {
8606 FieldAccessCallingConventionMIPS calling_convention;
8607 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8608 instruction->GetFieldType(),
8609 calling_convention);
8610}
8611
8612void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8613 HUnresolvedStaticFieldGet* instruction) {
8614 FieldAccessCallingConventionMIPS calling_convention;
8615 codegen_->GenerateUnresolvedFieldAccess(instruction,
8616 instruction->GetFieldType(),
8617 instruction->GetFieldIndex(),
8618 instruction->GetDexPc(),
8619 calling_convention);
8620}
8621
8622void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8623 HUnresolvedStaticFieldSet* instruction) {
8624 FieldAccessCallingConventionMIPS calling_convention;
8625 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8626 instruction->GetFieldType(),
8627 calling_convention);
8628}
8629
8630void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8631 HUnresolvedStaticFieldSet* instruction) {
8632 FieldAccessCallingConventionMIPS calling_convention;
8633 codegen_->GenerateUnresolvedFieldAccess(instruction,
8634 instruction->GetFieldType(),
8635 instruction->GetFieldIndex(),
8636 instruction->GetDexPc(),
8637 calling_convention);
8638}
8639
8640void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008641 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8642 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008643 // In suspend check slow path, usually there are no caller-save registers at all.
8644 // If SIMD instructions are present, however, we force spilling all live SIMD
8645 // registers in full width (since the runtime only saves/restores lower part).
8646 locations->SetCustomSlowPathCallerSaves(
8647 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008648}
8649
8650void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8651 HBasicBlock* block = instruction->GetBlock();
8652 if (block->GetLoopInformation() != nullptr) {
8653 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8654 // The back edge will generate the suspend check.
8655 return;
8656 }
8657 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8658 // The goto will generate the suspend check.
8659 return;
8660 }
8661 GenerateSuspendCheck(instruction, nullptr);
8662}
8663
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008664void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008665 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8666 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008667 InvokeRuntimeCallingConvention calling_convention;
8668 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8669}
8670
8671void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008672 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008673 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8674}
8675
8676void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008677 DataType::Type input_type = conversion->GetInputType();
8678 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008679 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8680 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008681 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008683 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8684 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008685 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8686 }
8687
8688 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008689 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008690 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8691 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008692 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693 }
8694
Vladimir Markoca6fff82017-10-03 14:49:14 +01008695 LocationSummary* locations =
8696 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008697
8698 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008699 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008700 locations->SetInAt(0, Location::RequiresFpuRegister());
8701 } else {
8702 locations->SetInAt(0, Location::RequiresRegister());
8703 }
8704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008705 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008706 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8707 } else {
8708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8709 }
8710 } else {
8711 InvokeRuntimeCallingConvention calling_convention;
8712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008713 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8715 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008716 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008717 locations->SetInAt(0, Location::RegisterPairLocation(
8718 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8719 }
8720
8721 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8722 }
8723}
8724
8725void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8726 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008727 DataType::Type result_type = conversion->GetResultType();
8728 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008729 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008730 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008731
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008732 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8733 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008735 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008736 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8737 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8738 Register src = locations->InAt(0).AsRegister<Register>();
8739
Alexey Frunzea871ef12016-06-27 15:20:11 -07008740 if (dst_low != src) {
8741 __ Move(dst_low, src);
8742 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008743 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008744 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008745 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008746 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008747 ? locations->InAt(0).AsRegisterPairLow<Register>()
8748 : locations->InAt(0).AsRegister<Register>();
8749
8750 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008751 case DataType::Type::kUint8:
8752 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008753 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008754 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008755 if (has_sign_extension) {
8756 __ Seb(dst, src);
8757 } else {
8758 __ Sll(dst, src, 24);
8759 __ Sra(dst, dst, 24);
8760 }
8761 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008762 case DataType::Type::kUint16:
8763 __ Andi(dst, src, 0xFFFF);
8764 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008765 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008766 if (has_sign_extension) {
8767 __ Seh(dst, src);
8768 } else {
8769 __ Sll(dst, src, 16);
8770 __ Sra(dst, dst, 16);
8771 }
8772 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008773 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008774 if (dst != src) {
8775 __ Move(dst, src);
8776 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008777 break;
8778
8779 default:
8780 LOG(FATAL) << "Unexpected type conversion from " << input_type
8781 << " to " << result_type;
8782 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008783 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8784 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008785 if (isR6) {
8786 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8787 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8788 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8789 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8790 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8791 __ Mtc1(src_low, FTMP);
8792 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008793 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008794 __ Cvtsl(dst, FTMP);
8795 } else {
8796 __ Cvtdl(dst, FTMP);
8797 }
8798 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008799 QuickEntrypointEnum entrypoint =
8800 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008801 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008802 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008803 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8804 } else {
8805 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8806 }
8807 }
8808 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008809 Register src = locations->InAt(0).AsRegister<Register>();
8810 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8811 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008812 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008813 __ Cvtsw(dst, FTMP);
8814 } else {
8815 __ Cvtdw(dst, FTMP);
8816 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008817 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008818 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8819 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008820
8821 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8822 // value of the output type if the input is outside of the range after the truncation or
8823 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8824 // results. This matches the desired float/double-to-int/long conversion exactly.
8825 //
8826 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8827 // value when the input is either a NaN or is outside of the range of the output type
8828 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8829 // the same result.
8830 //
8831 // The code takes care of the different behaviors by first comparing the input to the
8832 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8833 // If the input is greater than or equal to the minimum, it procedes to the truncate
8834 // instruction, which will handle such an input the same way irrespective of NAN2008.
8835 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8836 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008837 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008838 if (isR6) {
8839 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8840 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8841 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8842 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8843 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008845 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008846 __ TruncLS(FTMP, src);
8847 } else {
8848 __ TruncLD(FTMP, src);
8849 }
8850 __ Mfc1(dst_low, FTMP);
8851 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008852 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008853 QuickEntrypointEnum entrypoint =
8854 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008855 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008856 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008857 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8858 } else {
8859 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008861 }
8862 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008863 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8864 Register dst = locations->Out().AsRegister<Register>();
8865 MipsLabel truncate;
8866 MipsLabel done;
8867
Lena Djokicf4e23a82017-05-09 15:43:45 +02008868 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008869 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008870 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8871 __ LoadConst32(TMP, min_val);
8872 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008873 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008874 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8875 __ LoadConst32(TMP, High32Bits(min_val));
8876 __ Mtc1(ZERO, FTMP);
8877 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008878 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008880 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008881 __ ColeS(0, FTMP, src);
8882 } else {
8883 __ ColeD(0, FTMP, src);
8884 }
8885 __ Bc1t(0, &truncate);
8886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008887 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008888 __ CeqS(0, src, src);
8889 } else {
8890 __ CeqD(0, src, src);
8891 }
8892 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8893 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008894
8895 __ B(&done);
8896
8897 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008898 }
8899
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008900 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008901 __ TruncWS(FTMP, src);
8902 } else {
8903 __ TruncWD(FTMP, src);
8904 }
8905 __ Mfc1(dst, FTMP);
8906
Lena Djokicf4e23a82017-05-09 15:43:45 +02008907 if (!isR6) {
8908 __ Bind(&done);
8909 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008910 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008911 } else if (DataType::IsFloatingPointType(result_type) &&
8912 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008913 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8914 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008915 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008916 __ Cvtsd(dst, src);
8917 } else {
8918 __ Cvtds(dst, src);
8919 }
8920 } else {
8921 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8922 << " to " << result_type;
8923 }
8924}
8925
8926void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8927 HandleShift(ushr);
8928}
8929
8930void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8931 HandleShift(ushr);
8932}
8933
8934void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8935 HandleBinaryOp(instruction);
8936}
8937
8938void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8939 HandleBinaryOp(instruction);
8940}
8941
8942void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8943 // Nothing to do, this should be removed during prepare for register allocator.
8944 LOG(FATAL) << "Unreachable";
8945}
8946
8947void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8948 // Nothing to do, this should be removed during prepare for register allocator.
8949 LOG(FATAL) << "Unreachable";
8950}
8951
8952void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008953 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008954}
8955
8956void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008957 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008958}
8959
8960void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008961 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008962}
8963
8964void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008965 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008966}
8967
8968void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008969 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008970}
8971
8972void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008973 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008974}
8975
8976void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008977 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008978}
8979
8980void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008981 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008982}
8983
8984void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008985 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008986}
8987
8988void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008989 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008990}
8991
8992void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008993 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008994}
8995
8996void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008997 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008998}
8999
9000void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009001 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009002}
9003
9004void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009005 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009006}
9007
9008void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009009 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009010}
9011
9012void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009013 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009014}
9015
9016void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009017 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009018}
9019
9020void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009021 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009022}
9023
9024void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009025 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009026}
9027
9028void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009029 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009030}
9031
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009032void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9033 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009034 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009035 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009036 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9037 uint32_t num_entries = switch_instr->GetNumEntries();
9038 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9039 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9040 // instruction to simulate PC-relative addressing when accessing the jump table.
9041 // NAL clobbers RA. Make sure RA is preserved.
9042 codegen_->ClobberRA();
9043 }
9044 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009045}
9046
Alexey Frunze96b66822016-09-10 02:32:44 -07009047void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9048 int32_t lower_bound,
9049 uint32_t num_entries,
9050 HBasicBlock* switch_block,
9051 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009052 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009053 Register temp_reg = TMP;
9054 __ Addiu32(temp_reg, value_reg, -lower_bound);
9055 // Jump to default if index is negative
9056 // Note: We don't check the case that index is positive while value < lower_bound, because in
9057 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9058 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9059
Alexey Frunze96b66822016-09-10 02:32:44 -07009060 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009061 // Jump to successors[0] if value == lower_bound.
9062 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9063 int32_t last_index = 0;
9064 for (; num_entries - last_index > 2; last_index += 2) {
9065 __ Addiu(temp_reg, temp_reg, -2);
9066 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9067 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9068 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9069 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9070 }
9071 if (num_entries - last_index == 2) {
9072 // The last missing case_value.
9073 __ Addiu(temp_reg, temp_reg, -1);
9074 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009075 }
9076
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009077 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009078 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009079 __ B(codegen_->GetLabelOf(default_block));
9080 }
9081}
9082
Alexey Frunze96b66822016-09-10 02:32:44 -07009083void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9084 Register constant_area,
9085 int32_t lower_bound,
9086 uint32_t num_entries,
9087 HBasicBlock* switch_block,
9088 HBasicBlock* default_block) {
9089 // Create a jump table.
9090 std::vector<MipsLabel*> labels(num_entries);
9091 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9092 for (uint32_t i = 0; i < num_entries; i++) {
9093 labels[i] = codegen_->GetLabelOf(successors[i]);
9094 }
9095 JumpTable* table = __ CreateJumpTable(std::move(labels));
9096
9097 // Is the value in range?
9098 __ Addiu32(TMP, value_reg, -lower_bound);
9099 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9100 __ Sltiu(AT, TMP, num_entries);
9101 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9102 } else {
9103 __ LoadConst32(AT, num_entries);
9104 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9105 }
9106
9107 // We are in the range of the table.
9108 // Load the target address from the jump table, indexing by the value.
9109 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009110 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009111 __ Lw(TMP, TMP, 0);
9112 // Compute the absolute target address by adding the table start address
9113 // (the table contains offsets to targets relative to its start).
9114 __ Addu(TMP, TMP, AT);
9115 // And jump.
9116 __ Jr(TMP);
9117 __ NopIfNoReordering();
9118}
9119
9120void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9121 int32_t lower_bound = switch_instr->GetStartValue();
9122 uint32_t num_entries = switch_instr->GetNumEntries();
9123 LocationSummary* locations = switch_instr->GetLocations();
9124 Register value_reg = locations->InAt(0).AsRegister<Register>();
9125 HBasicBlock* switch_block = switch_instr->GetBlock();
9126 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9127
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009128 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009129 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009130 //
9131 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9132 // to access the jump table and it is implemented by changing HPackedSwitch to
9133 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9134 // VisitMipsPackedSwitch()).
9135 //
9136 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9137 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9138 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009139 GenTableBasedPackedSwitch(value_reg,
9140 ZERO,
9141 lower_bound,
9142 num_entries,
9143 switch_block,
9144 default_block);
9145 } else {
9146 GenPackedSwitchWithCompares(value_reg,
9147 lower_bound,
9148 num_entries,
9149 switch_block,
9150 default_block);
9151 }
9152}
9153
9154void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9155 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009156 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009157 locations->SetInAt(0, Location::RequiresRegister());
9158 // Constant area pointer (HMipsComputeBaseMethodAddress).
9159 locations->SetInAt(1, Location::RequiresRegister());
9160}
9161
9162void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9163 int32_t lower_bound = switch_instr->GetStartValue();
9164 uint32_t num_entries = switch_instr->GetNumEntries();
9165 LocationSummary* locations = switch_instr->GetLocations();
9166 Register value_reg = locations->InAt(0).AsRegister<Register>();
9167 Register constant_area = locations->InAt(1).AsRegister<Register>();
9168 HBasicBlock* switch_block = switch_instr->GetBlock();
9169 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9170
9171 // This is an R2-only path. HPackedSwitch has been changed to
9172 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9173 // required to address the jump table relative to PC.
9174 GenTableBasedPackedSwitch(value_reg,
9175 constant_area,
9176 lower_bound,
9177 num_entries,
9178 switch_block,
9179 default_block);
9180}
9181
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009182void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9183 HMipsComputeBaseMethodAddress* insn) {
9184 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009185 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009186 locations->SetOut(Location::RequiresRegister());
9187}
9188
9189void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9190 HMipsComputeBaseMethodAddress* insn) {
9191 LocationSummary* locations = insn->GetLocations();
9192 Register reg = locations->Out().AsRegister<Register>();
9193
9194 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9195
9196 // Generate a dummy PC-relative call to obtain PC.
9197 __ Nal();
9198 // Grab the return address off RA.
9199 __ Move(reg, RA);
9200
9201 // Remember this offset (the obtained PC value) for later use with constant area.
9202 __ BindPcRelBaseLabel();
9203}
9204
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009205void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9206 // The trampoline uses the same calling convention as dex calling conventions,
9207 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9208 // the method_idx.
9209 HandleInvoke(invoke);
9210}
9211
9212void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9213 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9214}
9215
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009216void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009218 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009219 locations->SetInAt(0, Location::RequiresRegister());
9220 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009221}
9222
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009223void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9224 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009225 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009226 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009227 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009228 __ LoadFromOffset(kLoadWord,
9229 locations->Out().AsRegister<Register>(),
9230 locations->InAt(0).AsRegister<Register>(),
9231 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009232 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009233 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009234 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009235 __ LoadFromOffset(kLoadWord,
9236 locations->Out().AsRegister<Register>(),
9237 locations->InAt(0).AsRegister<Register>(),
9238 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009239 __ LoadFromOffset(kLoadWord,
9240 locations->Out().AsRegister<Register>(),
9241 locations->Out().AsRegister<Register>(),
9242 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009243 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009244}
9245
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009246#undef __
9247#undef QUICK_ENTRY_POINT
9248
9249} // namespace mips
9250} // namespace art