blob: ddec0cc4536077f5ca3c0cd427e695ad0b0d48a1 [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
Chris Larsena2045912017-11-02 12:39:54 -0700464 HBasicBlock* GetSuccessor() const {
465 return successor_;
466 }
467
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200468 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200469 // If not null, the block to branch to after the suspend check.
470 HBasicBlock* const successor_;
471
472 // If `successor_` is null, the label to branch to after the suspend check.
473 MipsLabel return_label_;
474
475 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
476};
477
478class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
479 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800480 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
481 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482
483 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
484 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485 uint32_t dex_pc = instruction_->GetDexPc();
486 DCHECK(instruction_->IsCheckCast()
487 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
488 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
489
490 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800491 if (!is_fatal_) {
492 SaveLiveRegisters(codegen, locations);
493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494
495 // We're moving two locations to locations that could overlap, so we need a parallel
496 // move resolver.
497 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800498 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800501 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200502 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100503 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200504 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100505 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800506 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100507 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
509 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200510 } else {
511 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800512 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
513 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200514 }
515
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800516 if (!is_fatal_) {
517 RestoreLiveRegisters(codegen, locations);
518 __ B(GetExitLabel());
519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200520 }
521
522 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
523
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800524 bool IsFatal() const OVERRIDE { return is_fatal_; }
525
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200526 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800527 const bool is_fatal_;
528
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200529 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
530};
531
532class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
533 public:
Aart Bik42249c32016-01-07 15:33:50 -0800534 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000535 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200536
537 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800538 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200539 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100540 LocationSummary* locations = instruction_->GetLocations();
541 SaveLiveRegisters(codegen, locations);
542 InvokeRuntimeCallingConvention calling_convention;
543 __ LoadConst32(calling_convention.GetRegisterAt(0),
544 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100545 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100546 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200547 }
548
549 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
550
551 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200552 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
553};
554
Alexey Frunze15958152017-02-09 19:08:30 -0800555class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
556 public:
557 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
558
559 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
560 LocationSummary* locations = instruction_->GetLocations();
561 __ Bind(GetEntryLabel());
562 SaveLiveRegisters(codegen, locations);
563
564 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100565 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800566 parallel_move.AddMove(
567 locations->InAt(0),
568 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800570 nullptr);
571 parallel_move.AddMove(
572 locations->InAt(1),
573 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800575 nullptr);
576 parallel_move.AddMove(
577 locations->InAt(2),
578 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100579 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800580 nullptr);
581 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
582
583 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
584 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
585 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
586 RestoreLiveRegisters(codegen, locations);
587 __ B(GetExitLabel());
588 }
589
590 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
591
592 private:
593 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
594};
595
596// Slow path marking an object reference `ref` during a read
597// barrier. The field `obj.field` in the object `obj` holding this
598// reference does not get updated by this slow path after marking (see
599// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
600//
601// This means that after the execution of this slow path, `ref` will
602// always be up-to-date, but `obj.field` may not; i.e., after the
603// flip, `ref` will be a to-space reference, but `obj.field` will
604// probably still be a from-space reference (unless it gets updated by
605// another thread, or if another thread installed another object
606// reference (different from `ref`) in `obj.field`).
607//
608// If `entrypoint` is a valid location it is assumed to already be
609// holding the entrypoint. The case where the entrypoint is passed in
610// is for the GcRoot read barrier.
611class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
612 public:
613 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
614 Location ref,
615 Location entrypoint = Location::NoLocation())
616 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
617 DCHECK(kEmitCompilerReadBarrier);
618 }
619
620 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
621
622 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
623 LocationSummary* locations = instruction_->GetLocations();
624 Register ref_reg = ref_.AsRegister<Register>();
625 DCHECK(locations->CanCall());
626 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
627 DCHECK(instruction_->IsInstanceFieldGet() ||
628 instruction_->IsStaticFieldGet() ||
629 instruction_->IsArrayGet() ||
630 instruction_->IsArraySet() ||
631 instruction_->IsLoadClass() ||
632 instruction_->IsLoadString() ||
633 instruction_->IsInstanceOf() ||
634 instruction_->IsCheckCast() ||
635 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
636 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
637 << "Unexpected instruction in read barrier marking slow path: "
638 << instruction_->DebugName();
639
640 __ Bind(GetEntryLabel());
641 // No need to save live registers; it's taken care of by the
642 // entrypoint. Also, there is no need to update the stack mask,
643 // as this runtime call will not trigger a garbage collection.
644 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
645 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
646 (S2 <= ref_reg && ref_reg <= S7) ||
647 (ref_reg == FP)) << ref_reg;
648 // "Compact" slow path, saving two moves.
649 //
650 // Instead of using the standard runtime calling convention (input
651 // and output in A0 and V0 respectively):
652 //
653 // A0 <- ref
654 // V0 <- ReadBarrierMark(A0)
655 // ref <- V0
656 //
657 // we just use rX (the register containing `ref`) as input and output
658 // of a dedicated entrypoint:
659 //
660 // rX <- ReadBarrierMarkRegX(rX)
661 //
662 if (entrypoint_.IsValid()) {
663 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
664 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
665 __ Jalr(entrypoint_.AsRegister<Register>());
666 __ NopIfNoReordering();
667 } else {
668 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100669 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800670 // This runtime call does not require a stack map.
671 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
672 instruction_,
673 this,
674 /* direct */ false);
675 }
676 __ B(GetExitLabel());
677 }
678
679 private:
680 // The location (register) of the marked object reference.
681 const Location ref_;
682
683 // The location of the entrypoint if already loaded.
684 const Location entrypoint_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
687};
688
689// Slow path marking an object reference `ref` during a read barrier,
690// and if needed, atomically updating the field `obj.field` in the
691// object `obj` holding this reference after marking (contrary to
692// ReadBarrierMarkSlowPathMIPS above, which never tries to update
693// `obj.field`).
694//
695// This means that after the execution of this slow path, both `ref`
696// and `obj.field` will be up-to-date; i.e., after the flip, both will
697// hold the same to-space reference (unless another thread installed
698// another object reference (different from `ref`) in `obj.field`).
699class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
700 public:
701 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
702 Location ref,
703 Register obj,
704 Location field_offset,
705 Register temp1)
706 : SlowPathCodeMIPS(instruction),
707 ref_(ref),
708 obj_(obj),
709 field_offset_(field_offset),
710 temp1_(temp1) {
711 DCHECK(kEmitCompilerReadBarrier);
712 }
713
714 const char* GetDescription() const OVERRIDE {
715 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 LocationSummary* locations = instruction_->GetLocations();
720 Register ref_reg = ref_.AsRegister<Register>();
721 DCHECK(locations->CanCall());
722 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
723 // This slow path is only used by the UnsafeCASObject intrinsic.
724 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
725 << "Unexpected instruction in read barrier marking and field updating slow path: "
726 << instruction_->DebugName();
727 DCHECK(instruction_->GetLocations()->Intrinsified());
728 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
729 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
730
731 __ Bind(GetEntryLabel());
732
733 // Save the old reference.
734 // Note that we cannot use AT or TMP to save the old reference, as those
735 // are used by the code that follows, but we need the old reference after
736 // the call to the ReadBarrierMarkRegX entry point.
737 DCHECK_NE(temp1_, AT);
738 DCHECK_NE(temp1_, TMP);
739 __ Move(temp1_, ref_reg);
740
741 // No need to save live registers; it's taken care of by the
742 // entrypoint. Also, there is no need to update the stack mask,
743 // as this runtime call will not trigger a garbage collection.
744 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
745 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
746 (S2 <= ref_reg && ref_reg <= S7) ||
747 (ref_reg == FP)) << ref_reg;
748 // "Compact" slow path, saving two moves.
749 //
750 // Instead of using the standard runtime calling convention (input
751 // and output in A0 and V0 respectively):
752 //
753 // A0 <- ref
754 // V0 <- ReadBarrierMark(A0)
755 // ref <- V0
756 //
757 // we just use rX (the register containing `ref`) as input and output
758 // of a dedicated entrypoint:
759 //
760 // rX <- ReadBarrierMarkRegX(rX)
761 //
762 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100763 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800764 // This runtime call does not require a stack map.
765 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
766 instruction_,
767 this,
768 /* direct */ false);
769
770 // If the new reference is different from the old reference,
771 // update the field in the holder (`*(obj_ + field_offset_)`).
772 //
773 // Note that this field could also hold a different object, if
774 // another thread had concurrently changed it. In that case, the
775 // the compare-and-set (CAS) loop below would abort, leaving the
776 // field as-is.
777 MipsLabel done;
778 __ Beq(temp1_, ref_reg, &done);
779
780 // Update the the holder's field atomically. This may fail if
781 // mutator updates before us, but it's OK. This is achieved
782 // using a strong compare-and-set (CAS) operation with relaxed
783 // memory synchronization ordering, where the expected value is
784 // the old reference and the desired value is the new reference.
785
786 // Convenience aliases.
787 Register base = obj_;
788 // The UnsafeCASObject intrinsic uses a register pair as field
789 // offset ("long offset"), of which only the low part contains
790 // data.
791 Register offset = field_offset_.AsRegisterPairLow<Register>();
792 Register expected = temp1_;
793 Register value = ref_reg;
794 Register tmp_ptr = TMP; // Pointer to actual memory.
795 Register tmp = AT; // Value in memory.
796
797 __ Addu(tmp_ptr, base, offset);
798
799 if (kPoisonHeapReferences) {
800 __ PoisonHeapReference(expected);
801 // Do not poison `value` if it is the same register as
802 // `expected`, which has just been poisoned.
803 if (value != expected) {
804 __ PoisonHeapReference(value);
805 }
806 }
807
808 // do {
809 // tmp = [r_ptr] - expected;
810 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
811
812 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
813 MipsLabel loop_head, exit_loop;
814 __ Bind(&loop_head);
815 if (is_r6) {
816 __ LlR6(tmp, tmp_ptr);
817 } else {
818 __ LlR2(tmp, tmp_ptr);
819 }
820 __ Bne(tmp, expected, &exit_loop);
821 __ Move(tmp, value);
822 if (is_r6) {
823 __ ScR6(tmp, tmp_ptr);
824 } else {
825 __ ScR2(tmp, tmp_ptr);
826 }
827 __ Beqz(tmp, &loop_head);
828 __ Bind(&exit_loop);
829
830 if (kPoisonHeapReferences) {
831 __ UnpoisonHeapReference(expected);
832 // Do not unpoison `value` if it is the same register as
833 // `expected`, which has just been unpoisoned.
834 if (value != expected) {
835 __ UnpoisonHeapReference(value);
836 }
837 }
838
839 __ Bind(&done);
840 __ B(GetExitLabel());
841 }
842
843 private:
844 // The location (register) of the marked object reference.
845 const Location ref_;
846 // The register containing the object holding the marked object reference field.
847 const Register obj_;
848 // The location of the offset of the marked reference field within `obj_`.
849 Location field_offset_;
850
851 const Register temp1_;
852
853 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
854};
855
856// Slow path generating a read barrier for a heap reference.
857class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
858 public:
859 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
860 Location out,
861 Location ref,
862 Location obj,
863 uint32_t offset,
864 Location index)
865 : SlowPathCodeMIPS(instruction),
866 out_(out),
867 ref_(ref),
868 obj_(obj),
869 offset_(offset),
870 index_(index) {
871 DCHECK(kEmitCompilerReadBarrier);
872 // If `obj` is equal to `out` or `ref`, it means the initial object
873 // has been overwritten by (or after) the heap object reference load
874 // to be instrumented, e.g.:
875 //
876 // __ LoadFromOffset(kLoadWord, out, out, offset);
877 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
878 //
879 // In that case, we have lost the information about the original
880 // object, and the emitted read barrier cannot work properly.
881 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
882 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
883 }
884
885 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
886 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
887 LocationSummary* locations = instruction_->GetLocations();
888 Register reg_out = out_.AsRegister<Register>();
889 DCHECK(locations->CanCall());
890 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
891 DCHECK(instruction_->IsInstanceFieldGet() ||
892 instruction_->IsStaticFieldGet() ||
893 instruction_->IsArrayGet() ||
894 instruction_->IsInstanceOf() ||
895 instruction_->IsCheckCast() ||
896 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
897 << "Unexpected instruction in read barrier for heap reference slow path: "
898 << instruction_->DebugName();
899
900 __ Bind(GetEntryLabel());
901 SaveLiveRegisters(codegen, locations);
902
903 // We may have to change the index's value, but as `index_` is a
904 // constant member (like other "inputs" of this slow path),
905 // introduce a copy of it, `index`.
906 Location index = index_;
907 if (index_.IsValid()) {
908 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
909 if (instruction_->IsArrayGet()) {
910 // Compute the actual memory offset and store it in `index`.
911 Register index_reg = index_.AsRegister<Register>();
912 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
913 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
914 // We are about to change the value of `index_reg` (see the
915 // calls to art::mips::MipsAssembler::Sll and
916 // art::mips::MipsAssembler::Addiu32 below), but it has
917 // not been saved by the previous call to
918 // art::SlowPathCode::SaveLiveRegisters, as it is a
919 // callee-save register --
920 // art::SlowPathCode::SaveLiveRegisters does not consider
921 // callee-save registers, as it has been designed with the
922 // assumption that callee-save registers are supposed to be
923 // handled by the called function. So, as a callee-save
924 // register, `index_reg` _would_ eventually be saved onto
925 // the stack, but it would be too late: we would have
926 // changed its value earlier. Therefore, we manually save
927 // it here into another freely available register,
928 // `free_reg`, chosen of course among the caller-save
929 // registers (as a callee-save `free_reg` register would
930 // exhibit the same problem).
931 //
932 // Note we could have requested a temporary register from
933 // the register allocator instead; but we prefer not to, as
934 // this is a slow path, and we know we can find a
935 // caller-save register that is available.
936 Register free_reg = FindAvailableCallerSaveRegister(codegen);
937 __ Move(free_reg, index_reg);
938 index_reg = free_reg;
939 index = Location::RegisterLocation(index_reg);
940 } else {
941 // The initial register stored in `index_` has already been
942 // saved in the call to art::SlowPathCode::SaveLiveRegisters
943 // (as it is not a callee-save register), so we can freely
944 // use it.
945 }
946 // Shifting the index value contained in `index_reg` by the scale
947 // factor (2) cannot overflow in practice, as the runtime is
948 // unable to allocate object arrays with a size larger than
949 // 2^26 - 1 (that is, 2^28 - 4 bytes).
950 __ Sll(index_reg, index_reg, TIMES_4);
951 static_assert(
952 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
953 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
954 __ Addiu32(index_reg, index_reg, offset_);
955 } else {
956 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
957 // intrinsics, `index_` is not shifted by a scale factor of 2
958 // (as in the case of ArrayGet), as it is actually an offset
959 // to an object field within an object.
960 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
961 DCHECK(instruction_->GetLocations()->Intrinsified());
962 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
963 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
964 << instruction_->AsInvoke()->GetIntrinsic();
965 DCHECK_EQ(offset_, 0U);
966 DCHECK(index_.IsRegisterPair());
967 // UnsafeGet's offset location is a register pair, the low
968 // part contains the correct offset.
969 index = index_.ToLow();
970 }
971 }
972
973 // We're moving two or three locations to locations that could
974 // overlap, so we need a parallel move resolver.
975 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100976 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800977 parallel_move.AddMove(ref_,
978 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100979 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800980 nullptr);
981 parallel_move.AddMove(obj_,
982 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800984 nullptr);
985 if (index.IsValid()) {
986 parallel_move.AddMove(index,
987 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100988 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800989 nullptr);
990 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
991 } else {
992 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
993 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
994 }
995 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
996 instruction_,
997 instruction_->GetDexPc(),
998 this);
999 CheckEntrypointTypes<
1000 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +02001001 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001002 calling_convention.GetReturnLocation(DataType::Type::kReference),
1003 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001004
1005 RestoreLiveRegisters(codegen, locations);
1006 __ B(GetExitLabel());
1007 }
1008
1009 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1010
1011 private:
1012 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1013 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1014 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1015 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1016 if (i != ref &&
1017 i != obj &&
1018 !codegen->IsCoreCalleeSaveRegister(i) &&
1019 !codegen->IsBlockedCoreRegister(i)) {
1020 return static_cast<Register>(i);
1021 }
1022 }
1023 // We shall never fail to find a free caller-save register, as
1024 // there are more than two core caller-save registers on MIPS
1025 // (meaning it is possible to find one which is different from
1026 // `ref` and `obj`).
1027 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1028 LOG(FATAL) << "Could not find a free caller-save register";
1029 UNREACHABLE();
1030 }
1031
1032 const Location out_;
1033 const Location ref_;
1034 const Location obj_;
1035 const uint32_t offset_;
1036 // An additional location containing an index to an array.
1037 // Only used for HArrayGet and the UnsafeGetObject &
1038 // UnsafeGetObjectVolatile intrinsics.
1039 const Location index_;
1040
1041 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1042};
1043
1044// Slow path generating a read barrier for a GC root.
1045class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1046 public:
1047 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1048 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1049 DCHECK(kEmitCompilerReadBarrier);
1050 }
1051
1052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1053 LocationSummary* locations = instruction_->GetLocations();
1054 Register reg_out = out_.AsRegister<Register>();
1055 DCHECK(locations->CanCall());
1056 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1057 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1058 << "Unexpected instruction in read barrier for GC root slow path: "
1059 << instruction_->DebugName();
1060
1061 __ Bind(GetEntryLabel());
1062 SaveLiveRegisters(codegen, locations);
1063
1064 InvokeRuntimeCallingConvention calling_convention;
1065 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001066 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1067 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001068 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001069 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1070 instruction_,
1071 instruction_->GetDexPc(),
1072 this);
1073 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001074 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001075 calling_convention.GetReturnLocation(DataType::Type::kReference),
1076 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001077
1078 RestoreLiveRegisters(codegen, locations);
1079 __ B(GetExitLabel());
1080 }
1081
1082 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1083
1084 private:
1085 const Location out_;
1086 const Location root_;
1087
1088 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1089};
1090
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001091CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1092 const MipsInstructionSetFeatures& isa_features,
1093 const CompilerOptions& compiler_options,
1094 OptimizingCompilerStats* stats)
1095 : CodeGenerator(graph,
1096 kNumberOfCoreRegisters,
1097 kNumberOfFRegisters,
1098 kNumberOfRegisterPairs,
1099 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1100 arraysize(kCoreCalleeSaves)),
1101 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1102 arraysize(kFpuCalleeSaves)),
1103 compiler_options,
1104 stats),
1105 block_labels_(nullptr),
1106 location_builder_(graph, this),
1107 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 move_resolver_(graph->GetAllocator(), this),
1109 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001110 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001111 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001112 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1113 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1114 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1115 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1116 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1117 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1118 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1119 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1120 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001121 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001122 // Save RA (containing the return address) to mimic Quick.
1123 AddAllocatedRegister(Location::RegisterLocation(RA));
1124}
1125
1126#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001127// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1128#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001129#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001130
1131void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1132 // Ensure that we fix up branches.
1133 __ FinalizeCode();
1134
1135 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001136 StackMapStream* stack_map_stream = GetStackMapStream();
1137 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001138 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001139 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001140 uint32_t new_position = __ GetAdjustedPosition(old_position);
1141 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001142 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001143 }
1144
1145 // Adjust pc offsets for the disassembly information.
1146 if (disasm_info_ != nullptr) {
1147 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1148 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1149 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1150 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1151 it.second.start = __ GetAdjustedPosition(it.second.start);
1152 it.second.end = __ GetAdjustedPosition(it.second.end);
1153 }
1154 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1155 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1156 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1157 }
1158 }
1159
1160 CodeGenerator::Finalize(allocator);
1161}
1162
1163MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1164 return codegen_->GetAssembler();
1165}
1166
1167void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1168 DCHECK_LT(index, moves_.size());
1169 MoveOperands* move = moves_[index];
1170 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1171}
1172
1173void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1174 DCHECK_LT(index, moves_.size());
1175 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001176 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001177 Location loc1 = move->GetDestination();
1178 Location loc2 = move->GetSource();
1179
1180 DCHECK(!loc1.IsConstant());
1181 DCHECK(!loc2.IsConstant());
1182
1183 if (loc1.Equals(loc2)) {
1184 return;
1185 }
1186
1187 if (loc1.IsRegister() && loc2.IsRegister()) {
1188 // Swap 2 GPRs.
1189 Register r1 = loc1.AsRegister<Register>();
1190 Register r2 = loc2.AsRegister<Register>();
1191 __ Move(TMP, r2);
1192 __ Move(r2, r1);
1193 __ Move(r1, TMP);
1194 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1195 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1196 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001198 __ MovS(FTMP, f2);
1199 __ MovS(f2, f1);
1200 __ MovS(f1, FTMP);
1201 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001202 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001203 __ MovD(FTMP, f2);
1204 __ MovD(f2, f1);
1205 __ MovD(f1, FTMP);
1206 }
1207 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1208 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1209 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001210 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001211 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1212 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001213 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001214 __ Move(TMP, r2);
1215 __ Mfc1(r2, f1);
1216 __ Mtc1(TMP, f1);
1217 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1218 // Swap 2 GPR register pairs.
1219 Register r1 = loc1.AsRegisterPairLow<Register>();
1220 Register r2 = loc2.AsRegisterPairLow<Register>();
1221 __ Move(TMP, r2);
1222 __ Move(r2, r1);
1223 __ Move(r1, TMP);
1224 r1 = loc1.AsRegisterPairHigh<Register>();
1225 r2 = loc2.AsRegisterPairHigh<Register>();
1226 __ Move(TMP, r2);
1227 __ Move(r2, r1);
1228 __ Move(r1, TMP);
1229 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1230 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1231 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1234 : loc2.AsFpuRegister<FRegister>();
1235 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1236 : loc2.AsRegisterPairLow<Register>();
1237 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1238 : loc2.AsRegisterPairHigh<Register>();
1239 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1240 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1241 // unpredictable and the following mfch1 will fail.
1242 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001243 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001245 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246 __ Move(r2_l, TMP);
1247 __ Move(r2_h, AT);
1248 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1249 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1250 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1251 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001252 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1253 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001254 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1255 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001256 __ Move(TMP, reg);
1257 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1258 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1259 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1260 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1261 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1262 : loc2.AsRegisterPairLow<Register>();
1263 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1264 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001265 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001266 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1267 : loc2.GetHighStackIndex(kMipsWordSize);
1268 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001269 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001270 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001271 __ Move(TMP, reg_h);
1272 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1273 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001274 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1275 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1276 : loc2.AsFpuRegister<FRegister>();
1277 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001278 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001279 __ MovS(FTMP, reg);
1280 __ LoadSFromOffset(reg, SP, offset);
1281 __ StoreSToOffset(FTMP, SP, offset);
1282 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001284 __ MovD(FTMP, reg);
1285 __ LoadDFromOffset(reg, SP, offset);
1286 __ StoreDToOffset(FTMP, SP, offset);
1287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001288 } else {
1289 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1290 }
1291}
1292
1293void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1294 __ Pop(static_cast<Register>(reg));
1295}
1296
1297void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1298 __ Push(static_cast<Register>(reg));
1299}
1300
1301void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1302 // Allocate a scratch register other than TMP, if available.
1303 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1304 // automatically unspilled when the scratch scope object is destroyed).
1305 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1306 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001307 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1309 __ LoadFromOffset(kLoadWord,
1310 Register(ensure_scratch.GetRegister()),
1311 SP,
1312 index1 + stack_offset);
1313 __ LoadFromOffset(kLoadWord,
1314 TMP,
1315 SP,
1316 index2 + stack_offset);
1317 __ StoreToOffset(kStoreWord,
1318 Register(ensure_scratch.GetRegister()),
1319 SP,
1320 index2 + stack_offset);
1321 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1322 }
1323}
1324
Alexey Frunze73296a72016-06-03 22:51:46 -07001325void CodeGeneratorMIPS::ComputeSpillMask() {
1326 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1327 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1328 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1329 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1330 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1331 // within the stack frame.
1332 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1333 core_spill_mask_ |= (1 << ZERO);
1334 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001335}
1336
1337bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001338 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001339 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1340 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1341 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001342 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001343}
1344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345static dwarf::Reg DWARFReg(Register reg) {
1346 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1347}
1348
1349// TODO: mapping of floating-point registers to DWARF.
1350
1351void CodeGeneratorMIPS::GenerateFrameEntry() {
1352 __ Bind(&frame_entry_label_);
1353
Vladimir Marko33bff252017-11-01 14:35:42 +00001354 bool do_overflow_check =
1355 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001356
1357 if (do_overflow_check) {
1358 __ LoadFromOffset(kLoadWord,
1359 ZERO,
1360 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001361 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362 RecordPcInfo(nullptr, 0);
1363 }
1364
1365 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001366 CHECK_EQ(fpu_spill_mask_, 0u);
1367 CHECK_EQ(core_spill_mask_, 1u << RA);
1368 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001369 return;
1370 }
1371
1372 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001373 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1374 LOG(FATAL) << "Stack frame larger than "
1375 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001376 }
1377
1378 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379
Alexey Frunze73296a72016-06-03 22:51:46 -07001380 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001381 __ IncreaseFrameSize(ofs);
1382
Alexey Frunze73296a72016-06-03 22:51:46 -07001383 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1384 Register reg = static_cast<Register>(MostSignificantBit(mask));
1385 mask ^= 1u << reg;
1386 ofs -= kMipsWordSize;
1387 // The ZERO register is only included for alignment.
1388 if (reg != ZERO) {
1389 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001390 __ cfi().RelOffset(DWARFReg(reg), ofs);
1391 }
1392 }
1393
Alexey Frunze73296a72016-06-03 22:51:46 -07001394 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1395 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1396 mask ^= 1u << reg;
1397 ofs -= kMipsDoublewordSize;
1398 __ StoreDToOffset(reg, SP, ofs);
1399 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001400 }
1401
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001402 // Save the current method if we need it. Note that we do not
1403 // do this in HCurrentMethod, as the instruction might have been removed
1404 // in the SSA graph.
1405 if (RequiresCurrentMethod()) {
1406 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1407 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001408
1409 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1410 // Initialize should deoptimize flag to 0.
1411 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413}
1414
1415void CodeGeneratorMIPS::GenerateFrameExit() {
1416 __ cfi().RememberState();
1417
1418 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001420
Alexey Frunze73296a72016-06-03 22:51:46 -07001421 // For better instruction scheduling restore RA before other registers.
1422 uint32_t ofs = GetFrameSize();
1423 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1424 Register reg = static_cast<Register>(MostSignificantBit(mask));
1425 mask ^= 1u << reg;
1426 ofs -= kMipsWordSize;
1427 // The ZERO register is only included for alignment.
1428 if (reg != ZERO) {
1429 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001430 __ cfi().Restore(DWARFReg(reg));
1431 }
1432 }
1433
Alexey Frunze73296a72016-06-03 22:51:46 -07001434 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1435 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1436 mask ^= 1u << reg;
1437 ofs -= kMipsDoublewordSize;
1438 __ LoadDFromOffset(reg, SP, ofs);
1439 // TODO: __ cfi().Restore(DWARFReg(reg));
1440 }
1441
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001442 size_t frame_size = GetFrameSize();
1443 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1444 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1445 bool reordering = __ SetReorder(false);
1446 if (exchange) {
1447 __ Jr(RA);
1448 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1449 } else {
1450 __ DecreaseFrameSize(frame_size);
1451 __ Jr(RA);
1452 __ Nop(); // In delay slot.
1453 }
1454 __ SetReorder(reordering);
1455 } else {
1456 __ Jr(RA);
1457 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001458 }
1459
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001460 __ cfi().RestoreState();
1461 __ cfi().DefCFAOffset(GetFrameSize());
1462}
1463
1464void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1465 __ Bind(GetLabelOf(block));
1466}
1467
Lena Djokicca8c2952017-05-29 11:31:46 +02001468VectorRegister VectorRegisterFrom(Location location) {
1469 DCHECK(location.IsFpuRegister());
1470 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1471}
1472
Lena Djokic8098da92017-06-28 12:07:50 +02001473void CodeGeneratorMIPS::MoveLocation(Location destination,
1474 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001475 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001476 if (source.Equals(destination)) {
1477 return;
1478 }
1479
Lena Djokic8098da92017-06-28 12:07:50 +02001480 if (source.IsConstant()) {
1481 MoveConstant(destination, source.GetConstant());
1482 } else {
1483 if (destination.IsRegister()) {
1484 if (source.IsRegister()) {
1485 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1486 } else if (source.IsFpuRegister()) {
1487 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1488 } else {
1489 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001490 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001491 }
1492 } else if (destination.IsRegisterPair()) {
1493 if (source.IsRegisterPair()) {
1494 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1495 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1496 } else if (source.IsFpuRegister()) {
1497 Register dst_high = destination.AsRegisterPairHigh<Register>();
1498 Register dst_low = destination.AsRegisterPairLow<Register>();
1499 FRegister src = source.AsFpuRegister<FRegister>();
1500 __ Mfc1(dst_low, src);
1501 __ MoveFromFpuHigh(dst_high, src);
1502 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001503 DCHECK(source.IsDoubleStackSlot())
1504 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001505 int32_t off = source.GetStackIndex();
1506 Register r = destination.AsRegisterPairLow<Register>();
1507 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1508 }
1509 } else if (destination.IsFpuRegister()) {
1510 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001511 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001512 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1513 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001515 FRegister dst = destination.AsFpuRegister<FRegister>();
1516 Register src_high = source.AsRegisterPairHigh<Register>();
1517 Register src_low = source.AsRegisterPairLow<Register>();
1518 __ Mtc1(src_low, dst);
1519 __ MoveToFpuHigh(src_high, dst);
1520 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001521 if (GetGraph()->HasSIMD()) {
1522 __ MoveV(VectorRegisterFrom(destination),
1523 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001524 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001526 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1527 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001528 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001529 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1530 }
Lena Djokic8098da92017-06-28 12:07:50 +02001531 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001532 } else if (source.IsSIMDStackSlot()) {
1533 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001534 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001535 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001536 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1537 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001539 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1540 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1541 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001542 } else if (destination.IsSIMDStackSlot()) {
1543 if (source.IsFpuRegister()) {
1544 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1545 } else {
1546 DCHECK(source.IsSIMDStackSlot());
1547 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1548 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1549 }
Lena Djokic8098da92017-06-28 12:07:50 +02001550 } else if (destination.IsDoubleStackSlot()) {
1551 int32_t dst_offset = destination.GetStackIndex();
1552 if (source.IsRegisterPair()) {
1553 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1554 } else if (source.IsFpuRegister()) {
1555 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1556 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001557 DCHECK(source.IsDoubleStackSlot())
1558 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001559 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1560 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1561 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1562 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1563 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001564 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001565 DCHECK(destination.IsStackSlot()) << destination;
1566 int32_t dst_offset = destination.GetStackIndex();
1567 if (source.IsRegister()) {
1568 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1569 } else if (source.IsFpuRegister()) {
1570 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1571 } else {
1572 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1573 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1574 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1575 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 }
1577 }
1578}
1579
1580void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1581 if (c->IsIntConstant() || c->IsNullConstant()) {
1582 // Move 32 bit constant.
1583 int32_t value = GetInt32ValueOf(c);
1584 if (destination.IsRegister()) {
1585 Register dst = destination.AsRegister<Register>();
1586 __ LoadConst32(dst, value);
1587 } else {
1588 DCHECK(destination.IsStackSlot())
1589 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001590 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001591 }
1592 } else if (c->IsLongConstant()) {
1593 // Move 64 bit constant.
1594 int64_t value = GetInt64ValueOf(c);
1595 if (destination.IsRegisterPair()) {
1596 Register r_h = destination.AsRegisterPairHigh<Register>();
1597 Register r_l = destination.AsRegisterPairLow<Register>();
1598 __ LoadConst64(r_h, r_l, value);
1599 } else {
1600 DCHECK(destination.IsDoubleStackSlot())
1601 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001602 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 }
1604 } else if (c->IsFloatConstant()) {
1605 // Move 32 bit float constant.
1606 int32_t value = GetInt32ValueOf(c);
1607 if (destination.IsFpuRegister()) {
1608 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1609 } else {
1610 DCHECK(destination.IsStackSlot())
1611 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001612 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001613 }
1614 } else {
1615 // Move 64 bit double constant.
1616 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1617 int64_t value = GetInt64ValueOf(c);
1618 if (destination.IsFpuRegister()) {
1619 FRegister fd = destination.AsFpuRegister<FRegister>();
1620 __ LoadDConst64(fd, value, TMP);
1621 } else {
1622 DCHECK(destination.IsDoubleStackSlot())
1623 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001624 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 }
1626 }
1627}
1628
1629void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1630 DCHECK(destination.IsRegister());
1631 Register dst = destination.AsRegister<Register>();
1632 __ LoadConst32(dst, value);
1633}
1634
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001635void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1636 if (location.IsRegister()) {
1637 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001638 } else if (location.IsRegisterPair()) {
1639 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1640 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641 } else {
1642 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1643 }
1644}
1645
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001646template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001647inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1648 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001649 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001650 for (const PcRelativePatchInfo& info : infos) {
1651 const DexFile& dex_file = info.target_dex_file;
1652 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001653 DCHECK(info.label.IsBound());
1654 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001655 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1656 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001657 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1658 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1659 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001660 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001661 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001662 }
1663}
1664
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001665void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001666 DCHECK(linker_patches->empty());
1667 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001668 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001669 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001670 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001671 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001672 pc_relative_string_patches_.size() +
1673 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001674 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001675 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1677 pc_relative_method_patches_, linker_patches);
1678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1679 pc_relative_type_patches_, linker_patches);
1680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1681 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001682 } else {
1683 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001684 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1685 pc_relative_type_patches_, linker_patches);
1686 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1687 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001688 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001689 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1690 method_bss_entry_patches_, linker_patches);
1691 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1692 type_bss_entry_patches_, linker_patches);
1693 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1694 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001695 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001696}
1697
Vladimir Marko65979462017-05-19 17:25:12 +01001698CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001699 MethodReference target_method,
1700 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001701 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001702 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001703 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001704 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705}
1706
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001707CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001708 MethodReference target_method,
1709 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001710 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001711 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001713 &method_bss_entry_patches_);
1714}
1715
Alexey Frunze06a46c42016-07-19 15:00:40 -07001716CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001717 const DexFile& dex_file,
1718 dex::TypeIndex type_index,
1719 const PcRelativePatchInfo* info_high) {
1720 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001721}
1722
Vladimir Marko1998cd02017-01-13 13:02:58 +00001723CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001724 const DexFile& dex_file,
1725 dex::TypeIndex type_index,
1726 const PcRelativePatchInfo* info_high) {
1727 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001728}
1729
Vladimir Marko65979462017-05-19 17:25:12 +01001730CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001731 const DexFile& dex_file,
1732 dex::StringIndex string_index,
1733 const PcRelativePatchInfo* info_high) {
1734 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001735}
1736
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001737CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1738 const DexFile& dex_file,
1739 dex::StringIndex string_index,
1740 const PcRelativePatchInfo* info_high) {
1741 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1742}
1743
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001744CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001745 const DexFile& dex_file,
1746 uint32_t offset_or_index,
1747 const PcRelativePatchInfo* info_high,
1748 ArenaDeque<PcRelativePatchInfo>* patches) {
1749 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001750 return &patches->back();
1751}
1752
Alexey Frunze06a46c42016-07-19 15:00:40 -07001753Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1754 return map->GetOrCreate(
1755 value,
1756 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1757}
1758
Alexey Frunze06a46c42016-07-19 15:00:40 -07001759Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001760 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001761}
1762
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001763void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001764 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001765 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001766 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001767 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001768 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001769 if (GetInstructionSetFeatures().IsR6()) {
1770 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001771 __ Bind(&info_high->label);
1772 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001773 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001774 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001775 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001776 } else {
1777 // If base is ZERO, emit NAL to obtain the actual base.
1778 if (base == ZERO) {
1779 // Generate a dummy PC-relative call to obtain PC.
1780 __ Nal();
1781 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001782 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001783 __ Lui(out, /* placeholder */ 0x1234);
1784 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1785 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1786 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001787 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001788 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001789 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001790 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001791 __ Addu(out, out, (base == ZERO) ? RA : base);
1792 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001793 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001794 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001795}
1796
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1798 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001799 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001800 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001801 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1802 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001803 return &jit_string_patches_.back();
1804}
1805
1806CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1807 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001808 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001809 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001810 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1811 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812 return &jit_class_patches_.back();
1813}
1814
1815void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1816 const uint8_t* roots_data,
1817 const CodeGeneratorMIPS::JitPatchInfo& info,
1818 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1820 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001821 uintptr_t address =
1822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1823 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1824 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001825 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1826 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1827 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1828 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001829 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001830 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1831 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001832 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001834 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1835 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001836 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001837 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1838 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839}
1840
1841void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1842 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001843 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1844 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001845 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 }
1847 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001848 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1849 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001850 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001851 }
1852}
1853
Goran Jakovljevice114da22016-12-26 14:21:43 +01001854void CodeGeneratorMIPS::MarkGCCard(Register object,
1855 Register value,
1856 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 MipsLabel done;
1858 Register card = AT;
1859 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001860 if (value_can_be_null) {
1861 __ Beqz(value, &done);
1862 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 __ LoadFromOffset(kLoadWord,
1864 card,
1865 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001866 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1868 __ Addu(temp, card, temp);
1869 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001870 if (value_can_be_null) {
1871 __ Bind(&done);
1872 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873}
1874
David Brazdil58282f42016-01-14 12:45:10 +00001875void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1877 blocked_core_registers_[ZERO] = true;
1878 blocked_core_registers_[K0] = true;
1879 blocked_core_registers_[K1] = true;
1880 blocked_core_registers_[GP] = true;
1881 blocked_core_registers_[SP] = true;
1882 blocked_core_registers_[RA] = true;
1883
1884 // AT and TMP(T8) are used as temporary/scratch registers
1885 // (similar to how AT is used by MIPS assemblers).
1886 blocked_core_registers_[AT] = true;
1887 blocked_core_registers_[TMP] = true;
1888 blocked_fpu_registers_[FTMP] = true;
1889
1890 // Reserve suspend and thread registers.
1891 blocked_core_registers_[S0] = true;
1892 blocked_core_registers_[TR] = true;
1893
1894 // Reserve T9 for function calls
1895 blocked_core_registers_[T9] = true;
1896
1897 // Reserve odd-numbered FPU registers.
1898 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1899 blocked_fpu_registers_[i] = true;
1900 }
1901
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001902 if (GetGraph()->IsDebuggable()) {
1903 // Stubs do not save callee-save floating point registers. If the graph
1904 // is debuggable, we need to deal with these registers differently. For
1905 // now, just block them.
1906 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1907 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1908 }
1909 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910}
1911
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1913 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1914 return kMipsWordSize;
1915}
1916
1917size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1918 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1919 return kMipsWordSize;
1920}
1921
1922size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001923 if (GetGraph()->HasSIMD()) {
1924 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1925 } else {
1926 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1927 }
1928 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929}
1930
1931size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001932 if (GetGraph()->HasSIMD()) {
1933 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1934 } else {
1935 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1936 }
1937 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938}
1939
1940void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001941 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942}
1943
1944void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001945 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946}
1947
Serban Constantinescufca16662016-07-14 09:21:59 +01001948constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1949
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001950void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1951 HInstruction* instruction,
1952 uint32_t dex_pc,
1953 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001954 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001955 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1956 IsDirectEntrypoint(entrypoint));
1957 if (EntrypointRequiresStackMap(entrypoint)) {
1958 RecordPcInfo(instruction, dex_pc, slow_path);
1959 }
1960}
1961
1962void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1963 HInstruction* instruction,
1964 SlowPathCode* slow_path,
1965 bool direct) {
1966 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1967 GenerateInvokeRuntime(entry_point_offset, direct);
1968}
1969
1970void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001971 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001972 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001973 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001974 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975 // Reserve argument space on stack (for $a0-$a3) for
1976 // entrypoints that directly reference native implementations.
1977 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001978 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001980 } else {
1981 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001983 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984}
1985
1986void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1987 Register class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07001988 __ LoadFromOffset(kLoadSignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001989 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1990 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1991 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1992 __ Sync(0);
1993 __ Bind(slow_path->GetExitLabel());
1994}
1995
1996void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1997 __ Sync(0); // Only stype 0 is supported.
1998}
1999
2000void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2001 HBasicBlock* successor) {
2002 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002003 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2004
2005 if (slow_path == nullptr) {
2006 slow_path =
2007 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2008 instruction->SetSlowPath(slow_path);
2009 codegen_->AddSlowPath(slow_path);
2010 if (successor != nullptr) {
2011 DCHECK(successor->IsLoopHeader());
2012 }
2013 } else {
2014 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2015 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016
2017 __ LoadFromOffset(kLoadUnsignedHalfword,
2018 TMP,
2019 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002020 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 if (successor == nullptr) {
2022 __ Bnez(TMP, slow_path->GetEntryLabel());
2023 __ Bind(slow_path->GetReturnLabel());
2024 } else {
2025 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2026 __ B(slow_path->GetEntryLabel());
2027 // slow_path will return to GetLabelOf(successor).
2028 }
2029}
2030
2031InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2032 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002033 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034 assembler_(codegen->GetAssembler()),
2035 codegen_(codegen) {}
2036
2037void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2038 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002039 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043 locations->SetInAt(0, Location::RequiresRegister());
2044 HInstruction* right = instruction->InputAt(1);
2045 bool can_use_imm = false;
2046 if (right->IsConstant()) {
2047 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2048 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2049 can_use_imm = IsUint<16>(imm);
2050 } else if (instruction->IsAdd()) {
2051 can_use_imm = IsInt<16>(imm);
2052 } else {
2053 DCHECK(instruction->IsSub());
2054 can_use_imm = IsInt<16>(-imm);
2055 }
2056 }
2057 if (can_use_imm)
2058 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2059 else
2060 locations->SetInAt(1, Location::RequiresRegister());
2061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2062 break;
2063 }
2064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002067 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2068 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002069 break;
2070 }
2071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 case DataType::Type::kFloat32:
2073 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 DCHECK(instruction->IsAdd() || instruction->IsSub());
2075 locations->SetInAt(0, Location::RequiresFpuRegister());
2076 locations->SetInAt(1, Location::RequiresFpuRegister());
2077 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2078 break;
2079
2080 default:
2081 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2082 }
2083}
2084
2085void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 LocationSummary* locations = instruction->GetLocations();
2088
2089 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002091 Register dst = locations->Out().AsRegister<Register>();
2092 Register lhs = locations->InAt(0).AsRegister<Register>();
2093 Location rhs_location = locations->InAt(1);
2094
2095 Register rhs_reg = ZERO;
2096 int32_t rhs_imm = 0;
2097 bool use_imm = rhs_location.IsConstant();
2098 if (use_imm) {
2099 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2100 } else {
2101 rhs_reg = rhs_location.AsRegister<Register>();
2102 }
2103
2104 if (instruction->IsAnd()) {
2105 if (use_imm)
2106 __ Andi(dst, lhs, rhs_imm);
2107 else
2108 __ And(dst, lhs, rhs_reg);
2109 } else if (instruction->IsOr()) {
2110 if (use_imm)
2111 __ Ori(dst, lhs, rhs_imm);
2112 else
2113 __ Or(dst, lhs, rhs_reg);
2114 } else if (instruction->IsXor()) {
2115 if (use_imm)
2116 __ Xori(dst, lhs, rhs_imm);
2117 else
2118 __ Xor(dst, lhs, rhs_reg);
2119 } else if (instruction->IsAdd()) {
2120 if (use_imm)
2121 __ Addiu(dst, lhs, rhs_imm);
2122 else
2123 __ Addu(dst, lhs, rhs_reg);
2124 } else {
2125 DCHECK(instruction->IsSub());
2126 if (use_imm)
2127 __ Addiu(dst, lhs, -rhs_imm);
2128 else
2129 __ Subu(dst, lhs, rhs_reg);
2130 }
2131 break;
2132 }
2133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002134 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002135 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2136 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2137 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2138 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002139 Location rhs_location = locations->InAt(1);
2140 bool use_imm = rhs_location.IsConstant();
2141 if (!use_imm) {
2142 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2143 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2144 if (instruction->IsAnd()) {
2145 __ And(dst_low, lhs_low, rhs_low);
2146 __ And(dst_high, lhs_high, rhs_high);
2147 } else if (instruction->IsOr()) {
2148 __ Or(dst_low, lhs_low, rhs_low);
2149 __ Or(dst_high, lhs_high, rhs_high);
2150 } else if (instruction->IsXor()) {
2151 __ Xor(dst_low, lhs_low, rhs_low);
2152 __ Xor(dst_high, lhs_high, rhs_high);
2153 } else if (instruction->IsAdd()) {
2154 if (lhs_low == rhs_low) {
2155 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2156 __ Slt(TMP, lhs_low, ZERO);
2157 __ Addu(dst_low, lhs_low, rhs_low);
2158 } else {
2159 __ Addu(dst_low, lhs_low, rhs_low);
2160 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2161 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2162 }
2163 __ Addu(dst_high, lhs_high, rhs_high);
2164 __ Addu(dst_high, dst_high, TMP);
2165 } else {
2166 DCHECK(instruction->IsSub());
2167 __ Sltu(TMP, lhs_low, rhs_low);
2168 __ Subu(dst_low, lhs_low, rhs_low);
2169 __ Subu(dst_high, lhs_high, rhs_high);
2170 __ Subu(dst_high, dst_high, TMP);
2171 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002172 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002173 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2174 if (instruction->IsOr()) {
2175 uint32_t low = Low32Bits(value);
2176 uint32_t high = High32Bits(value);
2177 if (IsUint<16>(low)) {
2178 if (dst_low != lhs_low || low != 0) {
2179 __ Ori(dst_low, lhs_low, low);
2180 }
2181 } else {
2182 __ LoadConst32(TMP, low);
2183 __ Or(dst_low, lhs_low, TMP);
2184 }
2185 if (IsUint<16>(high)) {
2186 if (dst_high != lhs_high || high != 0) {
2187 __ Ori(dst_high, lhs_high, high);
2188 }
2189 } else {
2190 if (high != low) {
2191 __ LoadConst32(TMP, high);
2192 }
2193 __ Or(dst_high, lhs_high, TMP);
2194 }
2195 } else if (instruction->IsXor()) {
2196 uint32_t low = Low32Bits(value);
2197 uint32_t high = High32Bits(value);
2198 if (IsUint<16>(low)) {
2199 if (dst_low != lhs_low || low != 0) {
2200 __ Xori(dst_low, lhs_low, low);
2201 }
2202 } else {
2203 __ LoadConst32(TMP, low);
2204 __ Xor(dst_low, lhs_low, TMP);
2205 }
2206 if (IsUint<16>(high)) {
2207 if (dst_high != lhs_high || high != 0) {
2208 __ Xori(dst_high, lhs_high, high);
2209 }
2210 } else {
2211 if (high != low) {
2212 __ LoadConst32(TMP, high);
2213 }
2214 __ Xor(dst_high, lhs_high, TMP);
2215 }
2216 } else if (instruction->IsAnd()) {
2217 uint32_t low = Low32Bits(value);
2218 uint32_t high = High32Bits(value);
2219 if (IsUint<16>(low)) {
2220 __ Andi(dst_low, lhs_low, low);
2221 } else if (low != 0xFFFFFFFF) {
2222 __ LoadConst32(TMP, low);
2223 __ And(dst_low, lhs_low, TMP);
2224 } else if (dst_low != lhs_low) {
2225 __ Move(dst_low, lhs_low);
2226 }
2227 if (IsUint<16>(high)) {
2228 __ Andi(dst_high, lhs_high, high);
2229 } else if (high != 0xFFFFFFFF) {
2230 if (high != low) {
2231 __ LoadConst32(TMP, high);
2232 }
2233 __ And(dst_high, lhs_high, TMP);
2234 } else if (dst_high != lhs_high) {
2235 __ Move(dst_high, lhs_high);
2236 }
2237 } else {
2238 if (instruction->IsSub()) {
2239 value = -value;
2240 } else {
2241 DCHECK(instruction->IsAdd());
2242 }
2243 int32_t low = Low32Bits(value);
2244 int32_t high = High32Bits(value);
2245 if (IsInt<16>(low)) {
2246 if (dst_low != lhs_low || low != 0) {
2247 __ Addiu(dst_low, lhs_low, low);
2248 }
2249 if (low != 0) {
2250 __ Sltiu(AT, dst_low, low);
2251 }
2252 } else {
2253 __ LoadConst32(TMP, low);
2254 __ Addu(dst_low, lhs_low, TMP);
2255 __ Sltu(AT, dst_low, TMP);
2256 }
2257 if (IsInt<16>(high)) {
2258 if (dst_high != lhs_high || high != 0) {
2259 __ Addiu(dst_high, lhs_high, high);
2260 }
2261 } else {
2262 if (high != low) {
2263 __ LoadConst32(TMP, high);
2264 }
2265 __ Addu(dst_high, lhs_high, TMP);
2266 }
2267 if (low != 0) {
2268 __ Addu(dst_high, dst_high, AT);
2269 }
2270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 }
2272 break;
2273 }
2274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 case DataType::Type::kFloat32:
2276 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2278 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2279 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2280 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 __ AddS(dst, lhs, rhs);
2283 } else {
2284 __ AddD(dst, lhs, rhs);
2285 }
2286 } else {
2287 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289 __ SubS(dst, lhs, rhs);
2290 } else {
2291 __ SubD(dst, lhs, rhs);
2292 }
2293 }
2294 break;
2295 }
2296
2297 default:
2298 LOG(FATAL) << "Unexpected binary operation type " << type;
2299 }
2300}
2301
2302void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002303 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002304
Vladimir Markoca6fff82017-10-03 14:49:14 +01002305 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002307 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002309 locations->SetInAt(0, Location::RequiresRegister());
2310 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2312 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 locations->SetInAt(0, Location::RequiresRegister());
2315 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2316 locations->SetOut(Location::RequiresRegister());
2317 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002318 default:
2319 LOG(FATAL) << "Unexpected shift type " << type;
2320 }
2321}
2322
2323static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2324
2325void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002326 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329
2330 Location rhs_location = locations->InAt(1);
2331 bool use_imm = rhs_location.IsConstant();
2332 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2333 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002334 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002336 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2338 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002339
2340 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002342 Register dst = locations->Out().AsRegister<Register>();
2343 Register lhs = locations->InAt(0).AsRegister<Register>();
2344 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 if (shift_value == 0) {
2346 if (dst != lhs) {
2347 __ Move(dst, lhs);
2348 }
2349 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 __ Sll(dst, lhs, shift_value);
2351 } else if (instr->IsShr()) {
2352 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002353 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002355 } else {
2356 if (has_ins_rotr) {
2357 __ Rotr(dst, lhs, shift_value);
2358 } else {
2359 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2360 __ Srl(dst, lhs, shift_value);
2361 __ Or(dst, dst, TMP);
2362 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002363 }
2364 } else {
2365 if (instr->IsShl()) {
2366 __ Sllv(dst, lhs, rhs_reg);
2367 } else if (instr->IsShr()) {
2368 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002369 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002371 } else {
2372 if (has_ins_rotr) {
2373 __ Rotrv(dst, lhs, rhs_reg);
2374 } else {
2375 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002376 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2377 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2378 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2379 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2380 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002381 __ Sllv(TMP, lhs, TMP);
2382 __ Srlv(dst, lhs, rhs_reg);
2383 __ Or(dst, dst, TMP);
2384 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 }
2386 }
2387 break;
2388 }
2389
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002390 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002391 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2392 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2393 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2394 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2395 if (use_imm) {
2396 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002397 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002399 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002400 if (instr->IsShl()) {
2401 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2402 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2403 __ Sll(dst_low, lhs_low, shift_value);
2404 } else if (instr->IsShr()) {
2405 __ Srl(dst_low, lhs_low, shift_value);
2406 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2407 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002408 } else if (instr->IsUShr()) {
2409 __ Srl(dst_low, lhs_low, shift_value);
2410 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2411 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002412 } else {
2413 __ Srl(dst_low, lhs_low, shift_value);
2414 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2415 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002417 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002419 if (instr->IsShl()) {
2420 __ Sll(dst_low, lhs_low, shift_value);
2421 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2422 __ Sll(dst_high, lhs_high, shift_value);
2423 __ Or(dst_high, dst_high, TMP);
2424 } else if (instr->IsShr()) {
2425 __ Sra(dst_high, lhs_high, shift_value);
2426 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2427 __ Srl(dst_low, lhs_low, shift_value);
2428 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002430 __ Srl(dst_high, lhs_high, shift_value);
2431 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2432 __ Srl(dst_low, lhs_low, shift_value);
2433 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else {
2435 __ Srl(TMP, lhs_low, shift_value);
2436 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2437 __ Or(dst_low, dst_low, TMP);
2438 __ Srl(TMP, lhs_high, shift_value);
2439 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2440 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002441 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 }
2443 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002444 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002446 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002447 __ Move(dst_low, ZERO);
2448 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002449 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002451 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002452 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002453 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002454 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002455 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002456 // 64-bit rotation by 32 is just a swap.
2457 __ Move(dst_low, lhs_high);
2458 __ Move(dst_high, lhs_low);
2459 } else {
2460 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002461 __ Srl(dst_low, lhs_high, shift_value_high);
2462 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2463 __ Srl(dst_high, lhs_low, shift_value_high);
2464 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002465 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002466 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2467 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002468 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002469 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2470 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002471 __ Or(dst_high, dst_high, TMP);
2472 }
2473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002474 }
2475 }
2476 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002477 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002478 MipsLabel done;
2479 if (instr->IsShl()) {
2480 __ Sllv(dst_low, lhs_low, rhs_reg);
2481 __ Nor(AT, ZERO, rhs_reg);
2482 __ Srl(TMP, lhs_low, 1);
2483 __ Srlv(TMP, TMP, AT);
2484 __ Sllv(dst_high, lhs_high, rhs_reg);
2485 __ Or(dst_high, dst_high, TMP);
2486 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002487 if (isR6) {
2488 __ Beqzc(TMP, &done, /* is_bare */ true);
2489 __ Move(dst_high, dst_low);
2490 __ Move(dst_low, ZERO);
2491 } else {
2492 __ Movn(dst_high, dst_low, TMP);
2493 __ Movn(dst_low, ZERO, TMP);
2494 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002495 } else if (instr->IsShr()) {
2496 __ Srav(dst_high, lhs_high, rhs_reg);
2497 __ Nor(AT, ZERO, rhs_reg);
2498 __ Sll(TMP, lhs_high, 1);
2499 __ Sllv(TMP, TMP, AT);
2500 __ Srlv(dst_low, lhs_low, rhs_reg);
2501 __ Or(dst_low, dst_low, TMP);
2502 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002503 if (isR6) {
2504 __ Beqzc(TMP, &done, /* is_bare */ true);
2505 __ Move(dst_low, dst_high);
2506 __ Sra(dst_high, dst_high, 31);
2507 } else {
2508 __ Sra(AT, dst_high, 31);
2509 __ Movn(dst_low, dst_high, TMP);
2510 __ Movn(dst_high, AT, TMP);
2511 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002512 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 __ Srlv(dst_high, lhs_high, rhs_reg);
2514 __ Nor(AT, ZERO, rhs_reg);
2515 __ Sll(TMP, lhs_high, 1);
2516 __ Sllv(TMP, TMP, AT);
2517 __ Srlv(dst_low, lhs_low, rhs_reg);
2518 __ Or(dst_low, dst_low, TMP);
2519 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002520 if (isR6) {
2521 __ Beqzc(TMP, &done, /* is_bare */ true);
2522 __ Move(dst_low, dst_high);
2523 __ Move(dst_high, ZERO);
2524 } else {
2525 __ Movn(dst_low, dst_high, TMP);
2526 __ Movn(dst_high, ZERO, TMP);
2527 }
2528 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002529 __ Nor(AT, ZERO, rhs_reg);
2530 __ Srlv(TMP, lhs_low, rhs_reg);
2531 __ Sll(dst_low, lhs_high, 1);
2532 __ Sllv(dst_low, dst_low, AT);
2533 __ Or(dst_low, dst_low, TMP);
2534 __ Srlv(TMP, lhs_high, rhs_reg);
2535 __ Sll(dst_high, lhs_low, 1);
2536 __ Sllv(dst_high, dst_high, AT);
2537 __ Or(dst_high, dst_high, TMP);
2538 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002539 if (isR6) {
2540 __ Beqzc(TMP, &done, /* is_bare */ true);
2541 __ Move(TMP, dst_high);
2542 __ Move(dst_high, dst_low);
2543 __ Move(dst_low, TMP);
2544 } else {
2545 __ Movn(AT, dst_high, TMP);
2546 __ Movn(dst_high, dst_low, TMP);
2547 __ Movn(dst_low, AT, TMP);
2548 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 }
2550 __ Bind(&done);
2551 }
2552 break;
2553 }
2554
2555 default:
2556 LOG(FATAL) << "Unexpected shift operation type " << type;
2557 }
2558}
2559
2560void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2561 HandleBinaryOp(instruction);
2562}
2563
2564void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2565 HandleBinaryOp(instruction);
2566}
2567
2568void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2569 HandleBinaryOp(instruction);
2570}
2571
2572void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2573 HandleBinaryOp(instruction);
2574}
2575
2576void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002578 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002581 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2582 object_array_get_with_read_barrier
2583 ? LocationSummary::kCallOnSlowPath
2584 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002585 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2586 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2587 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002588 locations->SetInAt(0, Location::RequiresRegister());
2589 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002591 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2592 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002593 // The output overlaps in the case of an object array get with
2594 // read barriers enabled: we do not want the move to overwrite the
2595 // array's location, as we need it to emit the read barrier.
2596 locations->SetOut(Location::RequiresRegister(),
2597 object_array_get_with_read_barrier
2598 ? Location::kOutputOverlap
2599 : Location::kNoOutputOverlap);
2600 }
2601 // We need a temporary register for the read barrier marking slow
2602 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2603 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002604 bool temp_needed = instruction->GetIndex()->IsConstant()
2605 ? !kBakerReadBarrierThunksEnableForFields
2606 : !kBakerReadBarrierThunksEnableForArrays;
2607 if (temp_needed) {
2608 locations->AddTemp(Location::RequiresRegister());
2609 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 }
2611}
2612
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002613static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2614 auto null_checker = [codegen, instruction]() {
2615 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002616 };
2617 return null_checker;
2618}
2619
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2621 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002622 Location obj_loc = locations->InAt(0);
2623 Register obj = obj_loc.AsRegister<Register>();
2624 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002625 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002626 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002627 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002630 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2631 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002632 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002633 case DataType::Type::kBool:
2634 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002635 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 if (index.IsConstant()) {
2637 size_t offset =
2638 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002639 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002640 } else {
2641 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002642 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 }
2644 break;
2645 }
2646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002648 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002649 if (index.IsConstant()) {
2650 size_t offset =
2651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002652 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002653 } else {
2654 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002655 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 }
2657 break;
2658 }
2659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002661 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002662 if (maybe_compressed_char_at) {
2663 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2664 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2665 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2666 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2667 "Expecting 0=compressed, 1=uncompressed");
2668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002670 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2671 if (maybe_compressed_char_at) {
2672 MipsLabel uncompressed_load, done;
2673 __ Bnez(TMP, &uncompressed_load);
2674 __ LoadFromOffset(kLoadUnsignedByte,
2675 out,
2676 obj,
2677 data_offset + (const_index << TIMES_1));
2678 __ B(&done);
2679 __ Bind(&uncompressed_load);
2680 __ LoadFromOffset(kLoadUnsignedHalfword,
2681 out,
2682 obj,
2683 data_offset + (const_index << TIMES_2));
2684 __ Bind(&done);
2685 } else {
2686 __ LoadFromOffset(kLoadUnsignedHalfword,
2687 out,
2688 obj,
2689 data_offset + (const_index << TIMES_2),
2690 null_checker);
2691 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002693 Register index_reg = index.AsRegister<Register>();
2694 if (maybe_compressed_char_at) {
2695 MipsLabel uncompressed_load, done;
2696 __ Bnez(TMP, &uncompressed_load);
2697 __ Addu(TMP, obj, index_reg);
2698 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2699 __ B(&done);
2700 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002701 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002702 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2703 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002704 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2705 __ Addu(TMP, index_reg, obj);
2706 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002707 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002708 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002709 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2710 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 }
2712 break;
2713 }
2714
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002715 case DataType::Type::kInt16: {
2716 Register out = out_loc.AsRegister<Register>();
2717 if (index.IsConstant()) {
2718 size_t offset =
2719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2720 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002721 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2722 __ Addu(TMP, index.AsRegister<Register>(), obj);
2723 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002724 } else {
2725 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2726 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2727 }
2728 break;
2729 }
2730
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002732 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002733 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002734 if (index.IsConstant()) {
2735 size_t offset =
2736 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002737 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002738 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2739 __ Addu(TMP, index.AsRegister<Register>(), obj);
2740 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002741 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002742 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002743 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002744 }
2745 break;
2746 }
2747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002749 static_assert(
2750 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2751 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2752 // /* HeapReference<Object> */ out =
2753 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2754 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002755 bool temp_needed = index.IsConstant()
2756 ? !kBakerReadBarrierThunksEnableForFields
2757 : !kBakerReadBarrierThunksEnableForArrays;
2758 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002759 // Note that a potential implicit null check is handled in this
2760 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002761 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2762 if (index.IsConstant()) {
2763 // Array load with a constant index can be treated as a field load.
2764 size_t offset =
2765 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2766 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2767 out_loc,
2768 obj,
2769 offset,
2770 temp,
2771 /* needs_null_check */ false);
2772 } else {
2773 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2774 out_loc,
2775 obj,
2776 data_offset,
2777 index,
2778 temp,
2779 /* needs_null_check */ false);
2780 }
Alexey Frunze15958152017-02-09 19:08:30 -08002781 } else {
2782 Register out = out_loc.AsRegister<Register>();
2783 if (index.IsConstant()) {
2784 size_t offset =
2785 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2786 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2787 // If read barriers are enabled, emit read barriers other than
2788 // Baker's using a slow path (and also unpoison the loaded
2789 // reference, if heap poisoning is enabled).
2790 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2791 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002792 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002793 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2794 // If read barriers are enabled, emit read barriers other than
2795 // Baker's using a slow path (and also unpoison the loaded
2796 // reference, if heap poisoning is enabled).
2797 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2798 out_loc,
2799 out_loc,
2800 obj_loc,
2801 data_offset,
2802 index);
2803 }
2804 }
2805 break;
2806 }
2807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002809 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810 if (index.IsConstant()) {
2811 size_t offset =
2812 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002813 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002814 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2815 __ Addu(TMP, index.AsRegister<Register>(), obj);
2816 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002818 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002819 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 }
2821 break;
2822 }
2823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002825 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 if (index.IsConstant()) {
2827 size_t offset =
2828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002829 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002830 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2831 __ Addu(TMP, index.AsRegister<Register>(), obj);
2832 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002834 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002835 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002836 }
2837 break;
2838 }
2839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002841 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002842 if (index.IsConstant()) {
2843 size_t offset =
2844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002845 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002846 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2847 __ Addu(TMP, index.AsRegister<Register>(), obj);
2848 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002849 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002850 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002851 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002852 }
2853 break;
2854 }
2855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002856 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002857 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2858 UNREACHABLE();
2859 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860}
2861
2862void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002863 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864 locations->SetInAt(0, Location::RequiresRegister());
2865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2866}
2867
2868void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2869 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002870 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002871 Register obj = locations->InAt(0).AsRegister<Register>();
2872 Register out = locations->Out().AsRegister<Register>();
2873 __ LoadFromOffset(kLoadWord, out, obj, offset);
2874 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002875 // Mask out compression flag from String's array length.
2876 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2877 __ Srl(out, out, 1u);
2878 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002879}
2880
Alexey Frunzef58b2482016-09-02 22:14:06 -07002881Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2882 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2883 ? Location::ConstantLocation(instruction->AsConstant())
2884 : Location::RequiresRegister();
2885}
2886
2887Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2888 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2889 // We can store a non-zero float or double constant without first loading it into the FPU,
2890 // but we should only prefer this if the constant has a single use.
2891 if (instruction->IsConstant() &&
2892 (instruction->AsConstant()->IsZeroBitPattern() ||
2893 instruction->GetUses().HasExactlyOneElement())) {
2894 return Location::ConstantLocation(instruction->AsConstant());
2895 // Otherwise fall through and require an FPU register for the constant.
2896 }
2897 return Location::RequiresFpuRegister();
2898}
2899
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002902
2903 bool needs_write_barrier =
2904 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2905 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2906
Vladimir Markoca6fff82017-10-03 14:49:14 +01002907 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002908 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002909 may_need_runtime_call_for_type_check ?
2910 LocationSummary::kCallOnSlowPath :
2911 LocationSummary::kNoCall);
2912
2913 locations->SetInAt(0, Location::RequiresRegister());
2914 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002915 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002916 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002917 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002918 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2919 }
2920 if (needs_write_barrier) {
2921 // Temporary register for the write barrier.
2922 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 }
2924}
2925
2926void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2927 LocationSummary* locations = instruction->GetLocations();
2928 Register obj = locations->InAt(0).AsRegister<Register>();
2929 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002930 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002931 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002932 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 bool needs_write_barrier =
2934 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002935 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002936 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937
2938 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002940 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002942 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002943 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002944 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002945 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002946 __ Addu(base_reg, obj, index.AsRegister<Register>());
2947 }
2948 if (value_location.IsConstant()) {
2949 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2950 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2951 } else {
2952 Register value = value_location.AsRegister<Register>();
2953 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002954 }
2955 break;
2956 }
2957
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002958 case DataType::Type::kUint16:
2959 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002960 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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_2;
Lena Djokica2901602017-09-21 13:50:52 +02002963 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2964 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002966 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002967 }
2968 if (value_location.IsConstant()) {
2969 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2970 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2971 } else {
2972 Register value = value_location.AsRegister<Register>();
2973 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002974 }
2975 break;
2976 }
2977
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002979 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2980 if (index.IsConstant()) {
2981 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002982 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2983 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002984 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002985 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002986 }
2987 if (value_location.IsConstant()) {
2988 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2989 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2990 } else {
2991 Register value = value_location.AsRegister<Register>();
2992 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2993 }
2994 break;
2995 }
2996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002998 if (value_location.IsConstant()) {
2999 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003001 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003002 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003003 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003004 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003005 }
Alexey Frunze15958152017-02-09 19:08:30 -08003006 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3007 DCHECK_EQ(value, 0);
3008 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3009 DCHECK(!needs_write_barrier);
3010 DCHECK(!may_need_runtime_call_for_type_check);
3011 break;
3012 }
3013
3014 DCHECK(needs_write_barrier);
3015 Register value = value_location.AsRegister<Register>();
3016 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3017 Register temp2 = TMP; // Doesn't need to survive slow path.
3018 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3019 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3020 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3021 MipsLabel done;
3022 SlowPathCodeMIPS* slow_path = nullptr;
3023
3024 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003025 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003026 codegen_->AddSlowPath(slow_path);
3027 if (instruction->GetValueCanBeNull()) {
3028 MipsLabel non_zero;
3029 __ Bnez(value, &non_zero);
3030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3031 if (index.IsConstant()) {
3032 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003033 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3034 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003035 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003036 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003037 }
Alexey Frunze15958152017-02-09 19:08:30 -08003038 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3039 __ B(&done);
3040 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003041 }
Alexey Frunze15958152017-02-09 19:08:30 -08003042
3043 // Note that when read barriers are enabled, the type checks
3044 // are performed without read barriers. This is fine, even in
3045 // the case where a class object is in the from-space after
3046 // the flip, as a comparison involving such a type would not
3047 // produce a false positive; it may of course produce a false
3048 // negative, in which case we would take the ArraySet slow
3049 // path.
3050
3051 // /* HeapReference<Class> */ temp1 = obj->klass_
3052 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3053 __ MaybeUnpoisonHeapReference(temp1);
3054
3055 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3056 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3057 // /* HeapReference<Class> */ temp2 = value->klass_
3058 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3059 // If heap poisoning is enabled, no need to unpoison `temp1`
3060 // nor `temp2`, as we are comparing two poisoned references.
3061
3062 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3063 MipsLabel do_put;
3064 __ Beq(temp1, temp2, &do_put);
3065 // If heap poisoning is enabled, the `temp1` reference has
3066 // not been unpoisoned yet; unpoison it now.
3067 __ MaybeUnpoisonHeapReference(temp1);
3068
3069 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3070 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3071 // If heap poisoning is enabled, no need to unpoison
3072 // `temp1`, as we are comparing against null below.
3073 __ Bnez(temp1, slow_path->GetEntryLabel());
3074 __ Bind(&do_put);
3075 } else {
3076 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3077 }
3078 }
3079
3080 Register source = value;
3081 if (kPoisonHeapReferences) {
3082 // Note that in the case where `value` is a null reference,
3083 // we do not enter this block, as a null reference does not
3084 // need poisoning.
3085 __ Move(temp1, value);
3086 __ PoisonHeapReference(temp1);
3087 source = temp1;
3088 }
3089
3090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3091 if (index.IsConstant()) {
3092 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003094 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003095 }
3096 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3097
3098 if (!may_need_runtime_call_for_type_check) {
3099 codegen_->MaybeRecordImplicitNullCheck(instruction);
3100 }
3101
3102 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3103
3104 if (done.IsLinked()) {
3105 __ Bind(&done);
3106 }
3107
3108 if (slow_path != nullptr) {
3109 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003110 }
3111 break;
3112 }
3113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003114 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003116 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003117 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003118 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3119 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003120 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003121 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003122 }
3123 if (value_location.IsConstant()) {
3124 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3125 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3126 } else {
3127 Register value = value_location.AsRegisterPairLow<Register>();
3128 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003129 }
3130 break;
3131 }
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003136 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003137 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3138 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003139 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003140 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003141 }
3142 if (value_location.IsConstant()) {
3143 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3144 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3145 } else {
3146 FRegister value = value_location.AsFpuRegister<FRegister>();
3147 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003148 }
3149 break;
3150 }
3151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003152 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003153 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003154 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003155 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003156 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3157 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003158 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003159 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003160 }
3161 if (value_location.IsConstant()) {
3162 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3163 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3164 } else {
3165 FRegister value = value_location.AsFpuRegister<FRegister>();
3166 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003167 }
3168 break;
3169 }
3170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003172 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3173 UNREACHABLE();
3174 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003175}
3176
Lena Djokica2901602017-09-21 13:50:52 +02003177void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3178 HIntermediateArrayAddressIndex* instruction) {
3179 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003180 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003181
3182 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3183
3184 locations->SetInAt(0, Location::RequiresRegister());
3185 locations->SetInAt(1, Location::ConstantLocation(shift));
3186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3187}
3188
3189void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3190 HIntermediateArrayAddressIndex* instruction) {
3191 LocationSummary* locations = instruction->GetLocations();
3192 Register index_reg = locations->InAt(0).AsRegister<Register>();
3193 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3194 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3195}
3196
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003197void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003198 RegisterSet caller_saves = RegisterSet::Empty();
3199 InvokeRuntimeCallingConvention calling_convention;
3200 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3201 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3202 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003203 locations->SetInAt(0, Location::RequiresRegister());
3204 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003205}
3206
3207void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3208 LocationSummary* locations = instruction->GetLocations();
3209 BoundsCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003210 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003211 codegen_->AddSlowPath(slow_path);
3212
3213 Register index = locations->InAt(0).AsRegister<Register>();
3214 Register length = locations->InAt(1).AsRegister<Register>();
3215
3216 // length is limited by the maximum positive signed 32-bit integer.
3217 // Unsigned comparison of length and index checks for index < 0
3218 // and for length <= index simultaneously.
3219 __ Bgeu(index, length, slow_path->GetEntryLabel());
3220}
3221
Alexey Frunze15958152017-02-09 19:08:30 -08003222// Temp is used for read barrier.
3223static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3224 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003225 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003226 (kUseBakerReadBarrier ||
3227 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3228 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3229 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3230 return 1;
3231 }
3232 return 0;
3233}
3234
3235// Extra temp is used for read barrier.
3236static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3237 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3238}
3239
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003241 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3242 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3243
3244 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3245 switch (type_check_kind) {
3246 case TypeCheckKind::kExactCheck:
3247 case TypeCheckKind::kAbstractClassCheck:
3248 case TypeCheckKind::kClassHierarchyCheck:
3249 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003250 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003251 ? LocationSummary::kCallOnSlowPath
3252 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3253 break;
3254 case TypeCheckKind::kArrayCheck:
3255 case TypeCheckKind::kUnresolvedCheck:
3256 case TypeCheckKind::kInterfaceCheck:
3257 call_kind = LocationSummary::kCallOnSlowPath;
3258 break;
3259 }
3260
Vladimir Markoca6fff82017-10-03 14:49:14 +01003261 LocationSummary* locations =
3262 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003263 locations->SetInAt(0, Location::RequiresRegister());
3264 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003265 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003266}
3267
3268void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003269 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003270 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003271 Location obj_loc = locations->InAt(0);
3272 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003273 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003274 Location temp_loc = locations->GetTemp(0);
3275 Register temp = temp_loc.AsRegister<Register>();
3276 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3277 DCHECK_LE(num_temps, 2u);
3278 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003279 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3280 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3281 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3282 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3283 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3284 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3285 const uint32_t object_array_data_offset =
3286 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3287 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003288
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003289 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3290 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3291 // read barriers is done for performance and code size reasons.
3292 bool is_type_check_slow_path_fatal = false;
3293 if (!kEmitCompilerReadBarrier) {
3294 is_type_check_slow_path_fatal =
3295 (type_check_kind == TypeCheckKind::kExactCheck ||
3296 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3297 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3298 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3299 !instruction->CanThrowIntoCatchBlock();
3300 }
3301 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003302 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3303 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003304 codegen_->AddSlowPath(slow_path);
3305
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003306 // Avoid this check if we know `obj` is not null.
3307 if (instruction->MustDoNullCheck()) {
3308 __ Beqz(obj, &done);
3309 }
3310
3311 switch (type_check_kind) {
3312 case TypeCheckKind::kExactCheck:
3313 case TypeCheckKind::kArrayCheck: {
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 // Jump to slow path for throwing the exception or doing a
3322 // more involved array check.
3323 __ Bne(temp, cls, slow_path->GetEntryLabel());
3324 break;
3325 }
3326
3327 case TypeCheckKind::kAbstractClassCheck: {
3328 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003329 GenerateReferenceLoadTwoRegisters(instruction,
3330 temp_loc,
3331 obj_loc,
3332 class_offset,
3333 maybe_temp2_loc,
3334 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003335 // If the class is abstract, we eagerly fetch the super class of the
3336 // object to avoid doing a comparison we know will fail.
3337 MipsLabel loop;
3338 __ Bind(&loop);
3339 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003340 GenerateReferenceLoadOneRegister(instruction,
3341 temp_loc,
3342 super_offset,
3343 maybe_temp2_loc,
3344 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003345 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3346 // exception.
3347 __ Beqz(temp, slow_path->GetEntryLabel());
3348 // Otherwise, compare the classes.
3349 __ Bne(temp, cls, &loop);
3350 break;
3351 }
3352
3353 case TypeCheckKind::kClassHierarchyCheck: {
3354 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003355 GenerateReferenceLoadTwoRegisters(instruction,
3356 temp_loc,
3357 obj_loc,
3358 class_offset,
3359 maybe_temp2_loc,
3360 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003361 // Walk over the class hierarchy to find a match.
3362 MipsLabel loop;
3363 __ Bind(&loop);
3364 __ Beq(temp, cls, &done);
3365 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003366 GenerateReferenceLoadOneRegister(instruction,
3367 temp_loc,
3368 super_offset,
3369 maybe_temp2_loc,
3370 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003371 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3372 // exception. Otherwise, jump to the beginning of the loop.
3373 __ Bnez(temp, &loop);
3374 __ B(slow_path->GetEntryLabel());
3375 break;
3376 }
3377
3378 case TypeCheckKind::kArrayObjectCheck: {
3379 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003380 GenerateReferenceLoadTwoRegisters(instruction,
3381 temp_loc,
3382 obj_loc,
3383 class_offset,
3384 maybe_temp2_loc,
3385 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003386 // Do an exact check.
3387 __ Beq(temp, cls, &done);
3388 // Otherwise, we need to check that the object's class is a non-primitive array.
3389 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003390 GenerateReferenceLoadOneRegister(instruction,
3391 temp_loc,
3392 component_offset,
3393 maybe_temp2_loc,
3394 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003395 // If the component type is null, jump to the slow path to throw the exception.
3396 __ Beqz(temp, slow_path->GetEntryLabel());
3397 // Otherwise, the object is indeed an array, further check that this component
3398 // type is not a primitive type.
3399 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3400 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3401 __ Bnez(temp, slow_path->GetEntryLabel());
3402 break;
3403 }
3404
3405 case TypeCheckKind::kUnresolvedCheck:
3406 // We always go into the type check slow path for the unresolved check case.
3407 // We cannot directly call the CheckCast runtime entry point
3408 // without resorting to a type checking slow path here (i.e. by
3409 // calling InvokeRuntime directly), as it would require to
3410 // assign fixed registers for the inputs of this HInstanceOf
3411 // instruction (following the runtime calling convention), which
3412 // might be cluttered by the potential first read barrier
3413 // emission at the beginning of this method.
3414 __ B(slow_path->GetEntryLabel());
3415 break;
3416
3417 case TypeCheckKind::kInterfaceCheck: {
3418 // Avoid read barriers to improve performance of the fast path. We can not get false
3419 // positives by doing this.
3420 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003421 GenerateReferenceLoadTwoRegisters(instruction,
3422 temp_loc,
3423 obj_loc,
3424 class_offset,
3425 maybe_temp2_loc,
3426 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003427 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003428 GenerateReferenceLoadTwoRegisters(instruction,
3429 temp_loc,
3430 temp_loc,
3431 iftable_offset,
3432 maybe_temp2_loc,
3433 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003434 // Iftable is never null.
3435 __ Lw(TMP, temp, array_length_offset);
3436 // Loop through the iftable and check if any class matches.
3437 MipsLabel loop;
3438 __ Bind(&loop);
3439 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3440 __ Beqz(TMP, slow_path->GetEntryLabel());
3441 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3442 __ MaybeUnpoisonHeapReference(AT);
3443 // Go to next interface.
3444 __ Addiu(TMP, TMP, -2);
3445 // Compare the classes and continue the loop if they do not match.
3446 __ Bne(AT, cls, &loop);
3447 break;
3448 }
3449 }
3450
3451 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003452 __ Bind(slow_path->GetExitLabel());
3453}
3454
3455void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3456 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003457 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003458 locations->SetInAt(0, Location::RequiresRegister());
3459 if (check->HasUses()) {
3460 locations->SetOut(Location::SameAsFirstInput());
3461 }
3462}
3463
3464void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3465 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003466 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467 check->GetLoadClass(),
3468 check,
3469 check->GetDexPc(),
3470 true);
3471 codegen_->AddSlowPath(slow_path);
3472 GenerateClassInitializationCheck(slow_path,
3473 check->GetLocations()->InAt(0).AsRegister<Register>());
3474}
3475
3476void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003477 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003478
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003479 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003480 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003481
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:
Alexey Frunzee7697712016-09-15 21:37:49 -07003489 locations->SetInAt(0, Location::RequiresRegister());
3490 locations->SetInAt(1, Location::RequiresRegister());
3491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3492 break;
3493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003494 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003495 locations->SetInAt(0, Location::RequiresRegister());
3496 locations->SetInAt(1, Location::RequiresRegister());
3497 // Output overlaps because it is written before doing the low comparison.
3498 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3499 break;
3500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kFloat32:
3502 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003503 locations->SetInAt(0, Location::RequiresFpuRegister());
3504 locations->SetInAt(1, Location::RequiresFpuRegister());
3505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003506 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507
3508 default:
3509 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3510 }
3511}
3512
3513void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3514 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003515 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003517 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003518
3519 // 0 if: left == right
3520 // 1 if: left > right
3521 // -1 if: left < right
3522 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003524 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003526 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003527 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003529 Register lhs = locations->InAt(0).AsRegister<Register>();
3530 Register rhs = locations->InAt(1).AsRegister<Register>();
3531 __ Slt(TMP, lhs, rhs);
3532 __ Slt(res, rhs, lhs);
3533 __ Subu(res, res, TMP);
3534 break;
3535 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003536 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003537 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003538 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3539 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3540 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3541 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3542 // TODO: more efficient (direct) comparison with a constant.
3543 __ Slt(TMP, lhs_high, rhs_high);
3544 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3545 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3546 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3547 __ Sltu(TMP, lhs_low, rhs_low);
3548 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3549 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3550 __ Bind(&done);
3551 break;
3552 }
3553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kFloat32: {
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 __ CmpEqS(FTMP, lhs, rhs);
3561 __ LoadConst32(res, 0);
3562 __ Bc1nez(FTMP, &done);
3563 if (gt_bias) {
3564 __ CmpLtS(FTMP, lhs, rhs);
3565 __ LoadConst32(res, -1);
3566 __ Bc1nez(FTMP, &done);
3567 __ LoadConst32(res, 1);
3568 } else {
3569 __ CmpLtS(FTMP, rhs, lhs);
3570 __ LoadConst32(res, 1);
3571 __ Bc1nez(FTMP, &done);
3572 __ LoadConst32(res, -1);
3573 }
3574 } else {
3575 if (gt_bias) {
3576 __ ColtS(0, lhs, rhs);
3577 __ LoadConst32(res, -1);
3578 __ Bc1t(0, &done);
3579 __ CeqS(0, lhs, rhs);
3580 __ LoadConst32(res, 1);
3581 __ Movt(res, ZERO, 0);
3582 } else {
3583 __ ColtS(0, rhs, lhs);
3584 __ LoadConst32(res, 1);
3585 __ Bc1t(0, &done);
3586 __ CeqS(0, lhs, rhs);
3587 __ LoadConst32(res, -1);
3588 __ Movt(res, ZERO, 0);
3589 }
3590 }
3591 __ Bind(&done);
3592 break;
3593 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003595 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003596 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3597 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3598 MipsLabel done;
3599 if (isR6) {
3600 __ CmpEqD(FTMP, lhs, rhs);
3601 __ LoadConst32(res, 0);
3602 __ Bc1nez(FTMP, &done);
3603 if (gt_bias) {
3604 __ CmpLtD(FTMP, lhs, rhs);
3605 __ LoadConst32(res, -1);
3606 __ Bc1nez(FTMP, &done);
3607 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003608 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003609 __ CmpLtD(FTMP, rhs, lhs);
3610 __ LoadConst32(res, 1);
3611 __ Bc1nez(FTMP, &done);
3612 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613 }
3614 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003615 if (gt_bias) {
3616 __ ColtD(0, lhs, rhs);
3617 __ LoadConst32(res, -1);
3618 __ Bc1t(0, &done);
3619 __ CeqD(0, lhs, rhs);
3620 __ LoadConst32(res, 1);
3621 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003622 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003623 __ ColtD(0, rhs, lhs);
3624 __ LoadConst32(res, 1);
3625 __ Bc1t(0, &done);
3626 __ CeqD(0, lhs, rhs);
3627 __ LoadConst32(res, -1);
3628 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003629 }
3630 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003631 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003632 break;
3633 }
3634
3635 default:
3636 LOG(FATAL) << "Unimplemented compare type " << in_type;
3637 }
3638}
3639
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003640void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003641 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003642 switch (instruction->InputAt(0)->GetType()) {
3643 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003644 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003645 locations->SetInAt(0, Location::RequiresRegister());
3646 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3647 break;
3648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kFloat32:
3650 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003651 locations->SetInAt(0, Location::RequiresFpuRegister());
3652 locations->SetInAt(1, Location::RequiresFpuRegister());
3653 break;
3654 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003655 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3657 }
3658}
3659
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003660void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003661 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003662 return;
3663 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003665 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003667
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003668 switch (type) {
3669 default:
3670 // Integer case.
3671 GenerateIntCompare(instruction->GetCondition(), locations);
3672 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003675 GenerateLongCompare(instruction->GetCondition(), locations);
3676 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kFloat32:
3679 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003680 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3681 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682 }
3683}
3684
Alexey Frunze7e99e052015-11-24 19:28:01 -08003685void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3686 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003688
3689 LocationSummary* locations = instruction->GetLocations();
3690 Location second = locations->InAt(1);
3691 DCHECK(second.IsConstant());
3692
3693 Register out = locations->Out().AsRegister<Register>();
3694 Register dividend = locations->InAt(0).AsRegister<Register>();
3695 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3696 DCHECK(imm == 1 || imm == -1);
3697
3698 if (instruction->IsRem()) {
3699 __ Move(out, ZERO);
3700 } else {
3701 if (imm == -1) {
3702 __ Subu(out, ZERO, dividend);
3703 } else if (out != dividend) {
3704 __ Move(out, dividend);
3705 }
3706 }
3707}
3708
3709void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3710 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003712
3713 LocationSummary* locations = instruction->GetLocations();
3714 Location second = locations->InAt(1);
3715 DCHECK(second.IsConstant());
3716
3717 Register out = locations->Out().AsRegister<Register>();
3718 Register dividend = locations->InAt(0).AsRegister<Register>();
3719 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003720 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003721 int ctz_imm = CTZ(abs_imm);
3722
3723 if (instruction->IsDiv()) {
3724 if (ctz_imm == 1) {
3725 // Fast path for division by +/-2, which is very common.
3726 __ Srl(TMP, dividend, 31);
3727 } else {
3728 __ Sra(TMP, dividend, 31);
3729 __ Srl(TMP, TMP, 32 - ctz_imm);
3730 }
3731 __ Addu(out, dividend, TMP);
3732 __ Sra(out, out, ctz_imm);
3733 if (imm < 0) {
3734 __ Subu(out, ZERO, out);
3735 }
3736 } else {
3737 if (ctz_imm == 1) {
3738 // Fast path for modulo +/-2, which is very common.
3739 __ Sra(TMP, dividend, 31);
3740 __ Subu(out, dividend, TMP);
3741 __ Andi(out, out, 1);
3742 __ Addu(out, out, TMP);
3743 } else {
3744 __ Sra(TMP, dividend, 31);
3745 __ Srl(TMP, TMP, 32 - ctz_imm);
3746 __ Addu(out, dividend, TMP);
3747 if (IsUint<16>(abs_imm - 1)) {
3748 __ Andi(out, out, abs_imm - 1);
3749 } else {
3750 __ Sll(out, out, 32 - ctz_imm);
3751 __ Srl(out, out, 32 - ctz_imm);
3752 }
3753 __ Subu(out, out, TMP);
3754 }
3755 }
3756}
3757
3758void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3759 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003761
3762 LocationSummary* locations = instruction->GetLocations();
3763 Location second = locations->InAt(1);
3764 DCHECK(second.IsConstant());
3765
3766 Register out = locations->Out().AsRegister<Register>();
3767 Register dividend = locations->InAt(0).AsRegister<Register>();
3768 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3769
3770 int64_t magic;
3771 int shift;
3772 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3773
3774 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3775
3776 __ LoadConst32(TMP, magic);
3777 if (isR6) {
3778 __ MuhR6(TMP, dividend, TMP);
3779 } else {
3780 __ MultR2(dividend, TMP);
3781 __ Mfhi(TMP);
3782 }
3783 if (imm > 0 && magic < 0) {
3784 __ Addu(TMP, TMP, dividend);
3785 } else if (imm < 0 && magic > 0) {
3786 __ Subu(TMP, TMP, dividend);
3787 }
3788
3789 if (shift != 0) {
3790 __ Sra(TMP, TMP, shift);
3791 }
3792
3793 if (instruction->IsDiv()) {
3794 __ Sra(out, TMP, 31);
3795 __ Subu(out, TMP, out);
3796 } else {
3797 __ Sra(AT, TMP, 31);
3798 __ Subu(AT, TMP, AT);
3799 __ LoadConst32(TMP, imm);
3800 if (isR6) {
3801 __ MulR6(TMP, AT, TMP);
3802 } else {
3803 __ MulR2(TMP, AT, TMP);
3804 }
3805 __ Subu(out, dividend, TMP);
3806 }
3807}
3808
3809void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3810 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003812
3813 LocationSummary* locations = instruction->GetLocations();
3814 Register out = locations->Out().AsRegister<Register>();
3815 Location second = locations->InAt(1);
3816
3817 if (second.IsConstant()) {
3818 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3819 if (imm == 0) {
3820 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3821 } else if (imm == 1 || imm == -1) {
3822 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003823 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003824 DivRemByPowerOfTwo(instruction);
3825 } else {
3826 DCHECK(imm <= -2 || imm >= 2);
3827 GenerateDivRemWithAnyConstant(instruction);
3828 }
3829 } else {
3830 Register dividend = locations->InAt(0).AsRegister<Register>();
3831 Register divisor = second.AsRegister<Register>();
3832 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3833 if (instruction->IsDiv()) {
3834 if (isR6) {
3835 __ DivR6(out, dividend, divisor);
3836 } else {
3837 __ DivR2(out, dividend, divisor);
3838 }
3839 } else {
3840 if (isR6) {
3841 __ ModR6(out, dividend, divisor);
3842 } else {
3843 __ ModR2(out, dividend, divisor);
3844 }
3845 }
3846 }
3847}
3848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 DataType::Type type = div->GetResultType();
3851 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003852 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 : LocationSummary::kNoCall;
3854
Vladimir Markoca6fff82017-10-03 14:49:14 +01003855 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856
3857 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003859 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003860 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3862 break;
3863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 InvokeRuntimeCallingConvention calling_convention;
3866 locations->SetInAt(0, Location::RegisterPairLocation(
3867 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3868 locations->SetInAt(1, Location::RegisterPairLocation(
3869 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3870 locations->SetOut(calling_convention.GetReturnLocation(type));
3871 break;
3872 }
3873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kFloat32:
3875 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876 locations->SetInAt(0, Location::RequiresFpuRegister());
3877 locations->SetInAt(1, Location::RequiresFpuRegister());
3878 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3879 break;
3880
3881 default:
3882 LOG(FATAL) << "Unexpected div type " << type;
3883 }
3884}
3885
3886void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003887 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003888 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003889
3890 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003892 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003893 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003894 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003895 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3897 break;
3898 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kFloat32:
3900 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003901 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3902 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3903 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003905 __ DivS(dst, lhs, rhs);
3906 } else {
3907 __ DivD(dst, lhs, rhs);
3908 }
3909 break;
3910 }
3911 default:
3912 LOG(FATAL) << "Unexpected div type " << type;
3913 }
3914}
3915
3916void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003917 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003918 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003919}
3920
3921void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003922 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003923 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003924 codegen_->AddSlowPath(slow_path);
3925 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003926 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003927
3928 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003930 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003931 case DataType::Type::kInt8:
3932 case DataType::Type::kUint16:
3933 case DataType::Type::kInt16:
3934 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003935 if (value.IsConstant()) {
3936 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3937 __ B(slow_path->GetEntryLabel());
3938 } else {
3939 // A division by a non-null constant is valid. We don't need to perform
3940 // any check, so simply fall through.
3941 }
3942 } else {
3943 DCHECK(value.IsRegister()) << value;
3944 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3945 }
3946 break;
3947 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 if (value.IsConstant()) {
3950 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3951 __ B(slow_path->GetEntryLabel());
3952 } else {
3953 // A division by a non-null constant is valid. We don't need to perform
3954 // any check, so simply fall through.
3955 }
3956 } else {
3957 DCHECK(value.IsRegisterPair()) << value;
3958 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3959 __ Beqz(TMP, slow_path->GetEntryLabel());
3960 }
3961 break;
3962 }
3963 default:
3964 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3965 }
3966}
3967
3968void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3969 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003970 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003971 locations->SetOut(Location::ConstantLocation(constant));
3972}
3973
3974void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3975 // Will be generated at use site.
3976}
3977
3978void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3979 exit->SetLocations(nullptr);
3980}
3981
3982void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3983}
3984
3985void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3986 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003987 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003988 locations->SetOut(Location::ConstantLocation(constant));
3989}
3990
3991void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3992 // Will be generated at use site.
3993}
3994
3995void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3996 got->SetLocations(nullptr);
3997}
3998
3999void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
4000 DCHECK(!successor->IsExitBlock());
4001 HBasicBlock* block = got->GetBlock();
4002 HInstruction* previous = got->GetPrevious();
4003 HLoopInformation* info = block->GetLoopInformation();
4004
4005 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004006 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4007 return;
4008 }
4009 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4010 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4011 }
4012 if (!codegen_->GoesToNextBlock(block, successor)) {
4013 __ B(codegen_->GetLabelOf(successor));
4014 }
4015}
4016
4017void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4018 HandleGoto(got, got->GetSuccessor());
4019}
4020
4021void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4022 try_boundary->SetLocations(nullptr);
4023}
4024
4025void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4026 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4027 if (!successor->IsExitBlock()) {
4028 HandleGoto(try_boundary, successor);
4029 }
4030}
4031
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004032void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4033 LocationSummary* locations) {
4034 Register dst = locations->Out().AsRegister<Register>();
4035 Register lhs = locations->InAt(0).AsRegister<Register>();
4036 Location rhs_location = locations->InAt(1);
4037 Register rhs_reg = ZERO;
4038 int64_t rhs_imm = 0;
4039 bool use_imm = rhs_location.IsConstant();
4040 if (use_imm) {
4041 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4042 } else {
4043 rhs_reg = rhs_location.AsRegister<Register>();
4044 }
4045
4046 switch (cond) {
4047 case kCondEQ:
4048 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004049 if (use_imm && IsInt<16>(-rhs_imm)) {
4050 if (rhs_imm == 0) {
4051 if (cond == kCondEQ) {
4052 __ Sltiu(dst, lhs, 1);
4053 } else {
4054 __ Sltu(dst, ZERO, lhs);
4055 }
4056 } else {
4057 __ Addiu(dst, lhs, -rhs_imm);
4058 if (cond == kCondEQ) {
4059 __ Sltiu(dst, dst, 1);
4060 } else {
4061 __ Sltu(dst, ZERO, dst);
4062 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004063 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004064 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004065 if (use_imm && IsUint<16>(rhs_imm)) {
4066 __ Xori(dst, lhs, rhs_imm);
4067 } else {
4068 if (use_imm) {
4069 rhs_reg = TMP;
4070 __ LoadConst32(rhs_reg, rhs_imm);
4071 }
4072 __ Xor(dst, lhs, rhs_reg);
4073 }
4074 if (cond == kCondEQ) {
4075 __ Sltiu(dst, dst, 1);
4076 } else {
4077 __ Sltu(dst, ZERO, dst);
4078 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004079 }
4080 break;
4081
4082 case kCondLT:
4083 case kCondGE:
4084 if (use_imm && IsInt<16>(rhs_imm)) {
4085 __ Slti(dst, lhs, rhs_imm);
4086 } else {
4087 if (use_imm) {
4088 rhs_reg = TMP;
4089 __ LoadConst32(rhs_reg, rhs_imm);
4090 }
4091 __ Slt(dst, lhs, rhs_reg);
4092 }
4093 if (cond == kCondGE) {
4094 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4095 // only the slt instruction but no sge.
4096 __ Xori(dst, dst, 1);
4097 }
4098 break;
4099
4100 case kCondLE:
4101 case kCondGT:
4102 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4103 // Simulate lhs <= rhs via lhs < rhs + 1.
4104 __ Slti(dst, lhs, rhs_imm + 1);
4105 if (cond == kCondGT) {
4106 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4107 // only the slti instruction but no sgti.
4108 __ Xori(dst, dst, 1);
4109 }
4110 } else {
4111 if (use_imm) {
4112 rhs_reg = TMP;
4113 __ LoadConst32(rhs_reg, rhs_imm);
4114 }
4115 __ Slt(dst, rhs_reg, lhs);
4116 if (cond == kCondLE) {
4117 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4118 // only the slt instruction but no sle.
4119 __ Xori(dst, dst, 1);
4120 }
4121 }
4122 break;
4123
4124 case kCondB:
4125 case kCondAE:
4126 if (use_imm && IsInt<16>(rhs_imm)) {
4127 // Sltiu sign-extends its 16-bit immediate operand before
4128 // the comparison and thus lets us compare directly with
4129 // unsigned values in the ranges [0, 0x7fff] and
4130 // [0xffff8000, 0xffffffff].
4131 __ Sltiu(dst, lhs, rhs_imm);
4132 } else {
4133 if (use_imm) {
4134 rhs_reg = TMP;
4135 __ LoadConst32(rhs_reg, rhs_imm);
4136 }
4137 __ Sltu(dst, lhs, rhs_reg);
4138 }
4139 if (cond == kCondAE) {
4140 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4141 // only the sltu instruction but no sgeu.
4142 __ Xori(dst, dst, 1);
4143 }
4144 break;
4145
4146 case kCondBE:
4147 case kCondA:
4148 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4149 // Simulate lhs <= rhs via lhs < rhs + 1.
4150 // Note that this only works if rhs + 1 does not overflow
4151 // to 0, hence the check above.
4152 // Sltiu sign-extends its 16-bit immediate operand before
4153 // the comparison and thus lets us compare directly with
4154 // unsigned values in the ranges [0, 0x7fff] and
4155 // [0xffff8000, 0xffffffff].
4156 __ Sltiu(dst, lhs, rhs_imm + 1);
4157 if (cond == kCondA) {
4158 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4159 // only the sltiu instruction but no sgtiu.
4160 __ Xori(dst, dst, 1);
4161 }
4162 } else {
4163 if (use_imm) {
4164 rhs_reg = TMP;
4165 __ LoadConst32(rhs_reg, rhs_imm);
4166 }
4167 __ Sltu(dst, rhs_reg, lhs);
4168 if (cond == kCondBE) {
4169 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4170 // only the sltu instruction but no sleu.
4171 __ Xori(dst, dst, 1);
4172 }
4173 }
4174 break;
4175 }
4176}
4177
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004178bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4179 LocationSummary* input_locations,
4180 Register dst) {
4181 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4182 Location rhs_location = input_locations->InAt(1);
4183 Register rhs_reg = ZERO;
4184 int64_t rhs_imm = 0;
4185 bool use_imm = rhs_location.IsConstant();
4186 if (use_imm) {
4187 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4188 } else {
4189 rhs_reg = rhs_location.AsRegister<Register>();
4190 }
4191
4192 switch (cond) {
4193 case kCondEQ:
4194 case kCondNE:
4195 if (use_imm && IsInt<16>(-rhs_imm)) {
4196 __ Addiu(dst, lhs, -rhs_imm);
4197 } else if (use_imm && IsUint<16>(rhs_imm)) {
4198 __ Xori(dst, lhs, rhs_imm);
4199 } else {
4200 if (use_imm) {
4201 rhs_reg = TMP;
4202 __ LoadConst32(rhs_reg, rhs_imm);
4203 }
4204 __ Xor(dst, lhs, rhs_reg);
4205 }
4206 return (cond == kCondEQ);
4207
4208 case kCondLT:
4209 case kCondGE:
4210 if (use_imm && IsInt<16>(rhs_imm)) {
4211 __ Slti(dst, lhs, rhs_imm);
4212 } else {
4213 if (use_imm) {
4214 rhs_reg = TMP;
4215 __ LoadConst32(rhs_reg, rhs_imm);
4216 }
4217 __ Slt(dst, lhs, rhs_reg);
4218 }
4219 return (cond == kCondGE);
4220
4221 case kCondLE:
4222 case kCondGT:
4223 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4224 // Simulate lhs <= rhs via lhs < rhs + 1.
4225 __ Slti(dst, lhs, rhs_imm + 1);
4226 return (cond == kCondGT);
4227 } else {
4228 if (use_imm) {
4229 rhs_reg = TMP;
4230 __ LoadConst32(rhs_reg, rhs_imm);
4231 }
4232 __ Slt(dst, rhs_reg, lhs);
4233 return (cond == kCondLE);
4234 }
4235
4236 case kCondB:
4237 case kCondAE:
4238 if (use_imm && IsInt<16>(rhs_imm)) {
4239 // Sltiu sign-extends its 16-bit immediate operand before
4240 // the comparison and thus lets us compare directly with
4241 // unsigned values in the ranges [0, 0x7fff] and
4242 // [0xffff8000, 0xffffffff].
4243 __ Sltiu(dst, lhs, rhs_imm);
4244 } else {
4245 if (use_imm) {
4246 rhs_reg = TMP;
4247 __ LoadConst32(rhs_reg, rhs_imm);
4248 }
4249 __ Sltu(dst, lhs, rhs_reg);
4250 }
4251 return (cond == kCondAE);
4252
4253 case kCondBE:
4254 case kCondA:
4255 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4256 // Simulate lhs <= rhs via lhs < rhs + 1.
4257 // Note that this only works if rhs + 1 does not overflow
4258 // to 0, hence the check above.
4259 // Sltiu sign-extends its 16-bit immediate operand before
4260 // the comparison and thus lets us compare directly with
4261 // unsigned values in the ranges [0, 0x7fff] and
4262 // [0xffff8000, 0xffffffff].
4263 __ Sltiu(dst, lhs, rhs_imm + 1);
4264 return (cond == kCondA);
4265 } else {
4266 if (use_imm) {
4267 rhs_reg = TMP;
4268 __ LoadConst32(rhs_reg, rhs_imm);
4269 }
4270 __ Sltu(dst, rhs_reg, lhs);
4271 return (cond == kCondBE);
4272 }
4273 }
4274}
4275
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004276void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4277 LocationSummary* locations,
4278 MipsLabel* label) {
4279 Register lhs = locations->InAt(0).AsRegister<Register>();
4280 Location rhs_location = locations->InAt(1);
4281 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004282 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004283 bool use_imm = rhs_location.IsConstant();
4284 if (use_imm) {
4285 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4286 } else {
4287 rhs_reg = rhs_location.AsRegister<Register>();
4288 }
4289
4290 if (use_imm && rhs_imm == 0) {
4291 switch (cond) {
4292 case kCondEQ:
4293 case kCondBE: // <= 0 if zero
4294 __ Beqz(lhs, label);
4295 break;
4296 case kCondNE:
4297 case kCondA: // > 0 if non-zero
4298 __ Bnez(lhs, label);
4299 break;
4300 case kCondLT:
4301 __ Bltz(lhs, label);
4302 break;
4303 case kCondGE:
4304 __ Bgez(lhs, label);
4305 break;
4306 case kCondLE:
4307 __ Blez(lhs, label);
4308 break;
4309 case kCondGT:
4310 __ Bgtz(lhs, label);
4311 break;
4312 case kCondB: // always false
4313 break;
4314 case kCondAE: // always true
4315 __ B(label);
4316 break;
4317 }
4318 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004319 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4320 if (isR6 || !use_imm) {
4321 if (use_imm) {
4322 rhs_reg = TMP;
4323 __ LoadConst32(rhs_reg, rhs_imm);
4324 }
4325 switch (cond) {
4326 case kCondEQ:
4327 __ Beq(lhs, rhs_reg, label);
4328 break;
4329 case kCondNE:
4330 __ Bne(lhs, rhs_reg, label);
4331 break;
4332 case kCondLT:
4333 __ Blt(lhs, rhs_reg, label);
4334 break;
4335 case kCondGE:
4336 __ Bge(lhs, rhs_reg, label);
4337 break;
4338 case kCondLE:
4339 __ Bge(rhs_reg, lhs, label);
4340 break;
4341 case kCondGT:
4342 __ Blt(rhs_reg, lhs, label);
4343 break;
4344 case kCondB:
4345 __ Bltu(lhs, rhs_reg, label);
4346 break;
4347 case kCondAE:
4348 __ Bgeu(lhs, rhs_reg, label);
4349 break;
4350 case kCondBE:
4351 __ Bgeu(rhs_reg, lhs, label);
4352 break;
4353 case kCondA:
4354 __ Bltu(rhs_reg, lhs, label);
4355 break;
4356 }
4357 } else {
4358 // Special cases for more efficient comparison with constants on R2.
4359 switch (cond) {
4360 case kCondEQ:
4361 __ LoadConst32(TMP, rhs_imm);
4362 __ Beq(lhs, TMP, label);
4363 break;
4364 case kCondNE:
4365 __ LoadConst32(TMP, rhs_imm);
4366 __ Bne(lhs, TMP, label);
4367 break;
4368 case kCondLT:
4369 if (IsInt<16>(rhs_imm)) {
4370 __ Slti(TMP, lhs, rhs_imm);
4371 __ Bnez(TMP, label);
4372 } else {
4373 __ LoadConst32(TMP, rhs_imm);
4374 __ Blt(lhs, TMP, label);
4375 }
4376 break;
4377 case kCondGE:
4378 if (IsInt<16>(rhs_imm)) {
4379 __ Slti(TMP, lhs, rhs_imm);
4380 __ Beqz(TMP, label);
4381 } else {
4382 __ LoadConst32(TMP, rhs_imm);
4383 __ Bge(lhs, TMP, label);
4384 }
4385 break;
4386 case kCondLE:
4387 if (IsInt<16>(rhs_imm + 1)) {
4388 // Simulate lhs <= rhs via lhs < rhs + 1.
4389 __ Slti(TMP, lhs, rhs_imm + 1);
4390 __ Bnez(TMP, label);
4391 } else {
4392 __ LoadConst32(TMP, rhs_imm);
4393 __ Bge(TMP, lhs, label);
4394 }
4395 break;
4396 case kCondGT:
4397 if (IsInt<16>(rhs_imm + 1)) {
4398 // Simulate lhs > rhs via !(lhs < rhs + 1).
4399 __ Slti(TMP, lhs, rhs_imm + 1);
4400 __ Beqz(TMP, label);
4401 } else {
4402 __ LoadConst32(TMP, rhs_imm);
4403 __ Blt(TMP, lhs, label);
4404 }
4405 break;
4406 case kCondB:
4407 if (IsInt<16>(rhs_imm)) {
4408 __ Sltiu(TMP, lhs, rhs_imm);
4409 __ Bnez(TMP, label);
4410 } else {
4411 __ LoadConst32(TMP, rhs_imm);
4412 __ Bltu(lhs, TMP, label);
4413 }
4414 break;
4415 case kCondAE:
4416 if (IsInt<16>(rhs_imm)) {
4417 __ Sltiu(TMP, lhs, rhs_imm);
4418 __ Beqz(TMP, label);
4419 } else {
4420 __ LoadConst32(TMP, rhs_imm);
4421 __ Bgeu(lhs, TMP, label);
4422 }
4423 break;
4424 case kCondBE:
4425 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4426 // Simulate lhs <= rhs via lhs < rhs + 1.
4427 // Note that this only works if rhs + 1 does not overflow
4428 // to 0, hence the check above.
4429 __ Sltiu(TMP, lhs, rhs_imm + 1);
4430 __ Bnez(TMP, label);
4431 } else {
4432 __ LoadConst32(TMP, rhs_imm);
4433 __ Bgeu(TMP, lhs, label);
4434 }
4435 break;
4436 case kCondA:
4437 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4438 // Simulate lhs > rhs via !(lhs < rhs + 1).
4439 // Note that this only works if rhs + 1 does not overflow
4440 // to 0, hence the check above.
4441 __ Sltiu(TMP, lhs, rhs_imm + 1);
4442 __ Beqz(TMP, label);
4443 } else {
4444 __ LoadConst32(TMP, rhs_imm);
4445 __ Bltu(TMP, lhs, label);
4446 }
4447 break;
4448 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004449 }
4450 }
4451}
4452
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004453void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4454 LocationSummary* locations) {
4455 Register dst = locations->Out().AsRegister<Register>();
4456 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4457 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4458 Location rhs_location = locations->InAt(1);
4459 Register rhs_high = ZERO;
4460 Register rhs_low = ZERO;
4461 int64_t imm = 0;
4462 uint32_t imm_high = 0;
4463 uint32_t imm_low = 0;
4464 bool use_imm = rhs_location.IsConstant();
4465 if (use_imm) {
4466 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4467 imm_high = High32Bits(imm);
4468 imm_low = Low32Bits(imm);
4469 } else {
4470 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4471 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4472 }
4473 if (use_imm && imm == 0) {
4474 switch (cond) {
4475 case kCondEQ:
4476 case kCondBE: // <= 0 if zero
4477 __ Or(dst, lhs_high, lhs_low);
4478 __ Sltiu(dst, dst, 1);
4479 break;
4480 case kCondNE:
4481 case kCondA: // > 0 if non-zero
4482 __ Or(dst, lhs_high, lhs_low);
4483 __ Sltu(dst, ZERO, dst);
4484 break;
4485 case kCondLT:
4486 __ Slt(dst, lhs_high, ZERO);
4487 break;
4488 case kCondGE:
4489 __ Slt(dst, lhs_high, ZERO);
4490 __ Xori(dst, dst, 1);
4491 break;
4492 case kCondLE:
4493 __ Or(TMP, lhs_high, lhs_low);
4494 __ Sra(AT, lhs_high, 31);
4495 __ Sltu(dst, AT, TMP);
4496 __ Xori(dst, dst, 1);
4497 break;
4498 case kCondGT:
4499 __ Or(TMP, lhs_high, lhs_low);
4500 __ Sra(AT, lhs_high, 31);
4501 __ Sltu(dst, AT, TMP);
4502 break;
4503 case kCondB: // always false
4504 __ Andi(dst, dst, 0);
4505 break;
4506 case kCondAE: // always true
4507 __ Ori(dst, ZERO, 1);
4508 break;
4509 }
4510 } else if (use_imm) {
4511 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4512 switch (cond) {
4513 case kCondEQ:
4514 __ LoadConst32(TMP, imm_high);
4515 __ Xor(TMP, TMP, lhs_high);
4516 __ LoadConst32(AT, imm_low);
4517 __ Xor(AT, AT, lhs_low);
4518 __ Or(dst, TMP, AT);
4519 __ Sltiu(dst, dst, 1);
4520 break;
4521 case kCondNE:
4522 __ LoadConst32(TMP, imm_high);
4523 __ Xor(TMP, TMP, lhs_high);
4524 __ LoadConst32(AT, imm_low);
4525 __ Xor(AT, AT, lhs_low);
4526 __ Or(dst, TMP, AT);
4527 __ Sltu(dst, ZERO, dst);
4528 break;
4529 case kCondLT:
4530 case kCondGE:
4531 if (dst == lhs_low) {
4532 __ LoadConst32(TMP, imm_low);
4533 __ Sltu(dst, lhs_low, TMP);
4534 }
4535 __ LoadConst32(TMP, imm_high);
4536 __ Slt(AT, lhs_high, TMP);
4537 __ Slt(TMP, TMP, lhs_high);
4538 if (dst != lhs_low) {
4539 __ LoadConst32(dst, imm_low);
4540 __ Sltu(dst, lhs_low, dst);
4541 }
4542 __ Slt(dst, TMP, dst);
4543 __ Or(dst, dst, AT);
4544 if (cond == kCondGE) {
4545 __ Xori(dst, dst, 1);
4546 }
4547 break;
4548 case kCondGT:
4549 case kCondLE:
4550 if (dst == lhs_low) {
4551 __ LoadConst32(TMP, imm_low);
4552 __ Sltu(dst, TMP, lhs_low);
4553 }
4554 __ LoadConst32(TMP, imm_high);
4555 __ Slt(AT, TMP, lhs_high);
4556 __ Slt(TMP, lhs_high, TMP);
4557 if (dst != lhs_low) {
4558 __ LoadConst32(dst, imm_low);
4559 __ Sltu(dst, dst, lhs_low);
4560 }
4561 __ Slt(dst, TMP, dst);
4562 __ Or(dst, dst, AT);
4563 if (cond == kCondLE) {
4564 __ Xori(dst, dst, 1);
4565 }
4566 break;
4567 case kCondB:
4568 case kCondAE:
4569 if (dst == lhs_low) {
4570 __ LoadConst32(TMP, imm_low);
4571 __ Sltu(dst, lhs_low, TMP);
4572 }
4573 __ LoadConst32(TMP, imm_high);
4574 __ Sltu(AT, lhs_high, TMP);
4575 __ Sltu(TMP, TMP, lhs_high);
4576 if (dst != lhs_low) {
4577 __ LoadConst32(dst, imm_low);
4578 __ Sltu(dst, lhs_low, dst);
4579 }
4580 __ Slt(dst, TMP, dst);
4581 __ Or(dst, dst, AT);
4582 if (cond == kCondAE) {
4583 __ Xori(dst, dst, 1);
4584 }
4585 break;
4586 case kCondA:
4587 case kCondBE:
4588 if (dst == lhs_low) {
4589 __ LoadConst32(TMP, imm_low);
4590 __ Sltu(dst, TMP, lhs_low);
4591 }
4592 __ LoadConst32(TMP, imm_high);
4593 __ Sltu(AT, TMP, lhs_high);
4594 __ Sltu(TMP, lhs_high, TMP);
4595 if (dst != lhs_low) {
4596 __ LoadConst32(dst, imm_low);
4597 __ Sltu(dst, dst, lhs_low);
4598 }
4599 __ Slt(dst, TMP, dst);
4600 __ Or(dst, dst, AT);
4601 if (cond == kCondBE) {
4602 __ Xori(dst, dst, 1);
4603 }
4604 break;
4605 }
4606 } else {
4607 switch (cond) {
4608 case kCondEQ:
4609 __ Xor(TMP, lhs_high, rhs_high);
4610 __ Xor(AT, lhs_low, rhs_low);
4611 __ Or(dst, TMP, AT);
4612 __ Sltiu(dst, dst, 1);
4613 break;
4614 case kCondNE:
4615 __ Xor(TMP, lhs_high, rhs_high);
4616 __ Xor(AT, lhs_low, rhs_low);
4617 __ Or(dst, TMP, AT);
4618 __ Sltu(dst, ZERO, dst);
4619 break;
4620 case kCondLT:
4621 case kCondGE:
4622 __ Slt(TMP, rhs_high, lhs_high);
4623 __ Sltu(AT, lhs_low, rhs_low);
4624 __ Slt(TMP, TMP, AT);
4625 __ Slt(AT, lhs_high, rhs_high);
4626 __ Or(dst, AT, TMP);
4627 if (cond == kCondGE) {
4628 __ Xori(dst, dst, 1);
4629 }
4630 break;
4631 case kCondGT:
4632 case kCondLE:
4633 __ Slt(TMP, lhs_high, rhs_high);
4634 __ Sltu(AT, rhs_low, lhs_low);
4635 __ Slt(TMP, TMP, AT);
4636 __ Slt(AT, rhs_high, lhs_high);
4637 __ Or(dst, AT, TMP);
4638 if (cond == kCondLE) {
4639 __ Xori(dst, dst, 1);
4640 }
4641 break;
4642 case kCondB:
4643 case kCondAE:
4644 __ Sltu(TMP, rhs_high, lhs_high);
4645 __ Sltu(AT, lhs_low, rhs_low);
4646 __ Slt(TMP, TMP, AT);
4647 __ Sltu(AT, lhs_high, rhs_high);
4648 __ Or(dst, AT, TMP);
4649 if (cond == kCondAE) {
4650 __ Xori(dst, dst, 1);
4651 }
4652 break;
4653 case kCondA:
4654 case kCondBE:
4655 __ Sltu(TMP, lhs_high, rhs_high);
4656 __ Sltu(AT, rhs_low, lhs_low);
4657 __ Slt(TMP, TMP, AT);
4658 __ Sltu(AT, rhs_high, lhs_high);
4659 __ Or(dst, AT, TMP);
4660 if (cond == kCondBE) {
4661 __ Xori(dst, dst, 1);
4662 }
4663 break;
4664 }
4665 }
4666}
4667
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004668void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4669 LocationSummary* locations,
4670 MipsLabel* label) {
4671 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4672 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4673 Location rhs_location = locations->InAt(1);
4674 Register rhs_high = ZERO;
4675 Register rhs_low = ZERO;
4676 int64_t imm = 0;
4677 uint32_t imm_high = 0;
4678 uint32_t imm_low = 0;
4679 bool use_imm = rhs_location.IsConstant();
4680 if (use_imm) {
4681 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4682 imm_high = High32Bits(imm);
4683 imm_low = Low32Bits(imm);
4684 } else {
4685 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4686 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4687 }
4688
4689 if (use_imm && imm == 0) {
4690 switch (cond) {
4691 case kCondEQ:
4692 case kCondBE: // <= 0 if zero
4693 __ Or(TMP, lhs_high, lhs_low);
4694 __ Beqz(TMP, label);
4695 break;
4696 case kCondNE:
4697 case kCondA: // > 0 if non-zero
4698 __ Or(TMP, lhs_high, lhs_low);
4699 __ Bnez(TMP, label);
4700 break;
4701 case kCondLT:
4702 __ Bltz(lhs_high, label);
4703 break;
4704 case kCondGE:
4705 __ Bgez(lhs_high, label);
4706 break;
4707 case kCondLE:
4708 __ Or(TMP, lhs_high, lhs_low);
4709 __ Sra(AT, lhs_high, 31);
4710 __ Bgeu(AT, TMP, label);
4711 break;
4712 case kCondGT:
4713 __ Or(TMP, lhs_high, lhs_low);
4714 __ Sra(AT, lhs_high, 31);
4715 __ Bltu(AT, TMP, label);
4716 break;
4717 case kCondB: // always false
4718 break;
4719 case kCondAE: // always true
4720 __ B(label);
4721 break;
4722 }
4723 } else if (use_imm) {
4724 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4725 switch (cond) {
4726 case kCondEQ:
4727 __ LoadConst32(TMP, imm_high);
4728 __ Xor(TMP, TMP, lhs_high);
4729 __ LoadConst32(AT, imm_low);
4730 __ Xor(AT, AT, lhs_low);
4731 __ Or(TMP, TMP, AT);
4732 __ Beqz(TMP, label);
4733 break;
4734 case kCondNE:
4735 __ LoadConst32(TMP, imm_high);
4736 __ Xor(TMP, TMP, lhs_high);
4737 __ LoadConst32(AT, imm_low);
4738 __ Xor(AT, AT, lhs_low);
4739 __ Or(TMP, TMP, AT);
4740 __ Bnez(TMP, label);
4741 break;
4742 case kCondLT:
4743 __ LoadConst32(TMP, imm_high);
4744 __ Blt(lhs_high, TMP, label);
4745 __ Slt(TMP, TMP, lhs_high);
4746 __ LoadConst32(AT, imm_low);
4747 __ Sltu(AT, lhs_low, AT);
4748 __ Blt(TMP, AT, label);
4749 break;
4750 case kCondGE:
4751 __ LoadConst32(TMP, imm_high);
4752 __ Blt(TMP, lhs_high, label);
4753 __ Slt(TMP, lhs_high, TMP);
4754 __ LoadConst32(AT, imm_low);
4755 __ Sltu(AT, lhs_low, AT);
4756 __ Or(TMP, TMP, AT);
4757 __ Beqz(TMP, label);
4758 break;
4759 case kCondLE:
4760 __ LoadConst32(TMP, imm_high);
4761 __ Blt(lhs_high, TMP, label);
4762 __ Slt(TMP, TMP, lhs_high);
4763 __ LoadConst32(AT, imm_low);
4764 __ Sltu(AT, AT, lhs_low);
4765 __ Or(TMP, TMP, AT);
4766 __ Beqz(TMP, label);
4767 break;
4768 case kCondGT:
4769 __ LoadConst32(TMP, imm_high);
4770 __ Blt(TMP, lhs_high, label);
4771 __ Slt(TMP, lhs_high, TMP);
4772 __ LoadConst32(AT, imm_low);
4773 __ Sltu(AT, AT, lhs_low);
4774 __ Blt(TMP, AT, label);
4775 break;
4776 case kCondB:
4777 __ LoadConst32(TMP, imm_high);
4778 __ Bltu(lhs_high, TMP, label);
4779 __ Sltu(TMP, TMP, lhs_high);
4780 __ LoadConst32(AT, imm_low);
4781 __ Sltu(AT, lhs_low, AT);
4782 __ Blt(TMP, AT, label);
4783 break;
4784 case kCondAE:
4785 __ LoadConst32(TMP, imm_high);
4786 __ Bltu(TMP, lhs_high, label);
4787 __ Sltu(TMP, lhs_high, TMP);
4788 __ LoadConst32(AT, imm_low);
4789 __ Sltu(AT, lhs_low, AT);
4790 __ Or(TMP, TMP, AT);
4791 __ Beqz(TMP, label);
4792 break;
4793 case kCondBE:
4794 __ LoadConst32(TMP, imm_high);
4795 __ Bltu(lhs_high, TMP, label);
4796 __ Sltu(TMP, TMP, lhs_high);
4797 __ LoadConst32(AT, imm_low);
4798 __ Sltu(AT, AT, lhs_low);
4799 __ Or(TMP, TMP, AT);
4800 __ Beqz(TMP, label);
4801 break;
4802 case kCondA:
4803 __ LoadConst32(TMP, imm_high);
4804 __ Bltu(TMP, lhs_high, label);
4805 __ Sltu(TMP, lhs_high, TMP);
4806 __ LoadConst32(AT, imm_low);
4807 __ Sltu(AT, AT, lhs_low);
4808 __ Blt(TMP, AT, label);
4809 break;
4810 }
4811 } else {
4812 switch (cond) {
4813 case kCondEQ:
4814 __ Xor(TMP, lhs_high, rhs_high);
4815 __ Xor(AT, lhs_low, rhs_low);
4816 __ Or(TMP, TMP, AT);
4817 __ Beqz(TMP, label);
4818 break;
4819 case kCondNE:
4820 __ Xor(TMP, lhs_high, rhs_high);
4821 __ Xor(AT, lhs_low, rhs_low);
4822 __ Or(TMP, TMP, AT);
4823 __ Bnez(TMP, label);
4824 break;
4825 case kCondLT:
4826 __ Blt(lhs_high, rhs_high, label);
4827 __ Slt(TMP, rhs_high, lhs_high);
4828 __ Sltu(AT, lhs_low, rhs_low);
4829 __ Blt(TMP, AT, label);
4830 break;
4831 case kCondGE:
4832 __ Blt(rhs_high, lhs_high, label);
4833 __ Slt(TMP, lhs_high, rhs_high);
4834 __ Sltu(AT, lhs_low, rhs_low);
4835 __ Or(TMP, TMP, AT);
4836 __ Beqz(TMP, label);
4837 break;
4838 case kCondLE:
4839 __ Blt(lhs_high, rhs_high, label);
4840 __ Slt(TMP, rhs_high, lhs_high);
4841 __ Sltu(AT, rhs_low, lhs_low);
4842 __ Or(TMP, TMP, AT);
4843 __ Beqz(TMP, label);
4844 break;
4845 case kCondGT:
4846 __ Blt(rhs_high, lhs_high, label);
4847 __ Slt(TMP, lhs_high, rhs_high);
4848 __ Sltu(AT, rhs_low, lhs_low);
4849 __ Blt(TMP, AT, label);
4850 break;
4851 case kCondB:
4852 __ Bltu(lhs_high, rhs_high, label);
4853 __ Sltu(TMP, rhs_high, lhs_high);
4854 __ Sltu(AT, lhs_low, rhs_low);
4855 __ Blt(TMP, AT, label);
4856 break;
4857 case kCondAE:
4858 __ Bltu(rhs_high, lhs_high, label);
4859 __ Sltu(TMP, lhs_high, rhs_high);
4860 __ Sltu(AT, lhs_low, rhs_low);
4861 __ Or(TMP, TMP, AT);
4862 __ Beqz(TMP, label);
4863 break;
4864 case kCondBE:
4865 __ Bltu(lhs_high, rhs_high, label);
4866 __ Sltu(TMP, rhs_high, lhs_high);
4867 __ Sltu(AT, rhs_low, lhs_low);
4868 __ Or(TMP, TMP, AT);
4869 __ Beqz(TMP, label);
4870 break;
4871 case kCondA:
4872 __ Bltu(rhs_high, lhs_high, label);
4873 __ Sltu(TMP, lhs_high, rhs_high);
4874 __ Sltu(AT, rhs_low, lhs_low);
4875 __ Blt(TMP, AT, label);
4876 break;
4877 }
4878 }
4879}
4880
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004881void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4882 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004884 LocationSummary* locations) {
4885 Register dst = locations->Out().AsRegister<Register>();
4886 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4887 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4888 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004889 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004890 if (isR6) {
4891 switch (cond) {
4892 case kCondEQ:
4893 __ CmpEqS(FTMP, lhs, rhs);
4894 __ Mfc1(dst, FTMP);
4895 __ Andi(dst, dst, 1);
4896 break;
4897 case kCondNE:
4898 __ CmpEqS(FTMP, lhs, rhs);
4899 __ Mfc1(dst, FTMP);
4900 __ Addiu(dst, dst, 1);
4901 break;
4902 case kCondLT:
4903 if (gt_bias) {
4904 __ CmpLtS(FTMP, lhs, rhs);
4905 } else {
4906 __ CmpUltS(FTMP, lhs, rhs);
4907 }
4908 __ Mfc1(dst, FTMP);
4909 __ Andi(dst, dst, 1);
4910 break;
4911 case kCondLE:
4912 if (gt_bias) {
4913 __ CmpLeS(FTMP, lhs, rhs);
4914 } else {
4915 __ CmpUleS(FTMP, lhs, rhs);
4916 }
4917 __ Mfc1(dst, FTMP);
4918 __ Andi(dst, dst, 1);
4919 break;
4920 case kCondGT:
4921 if (gt_bias) {
4922 __ CmpUltS(FTMP, rhs, lhs);
4923 } else {
4924 __ CmpLtS(FTMP, rhs, lhs);
4925 }
4926 __ Mfc1(dst, FTMP);
4927 __ Andi(dst, dst, 1);
4928 break;
4929 case kCondGE:
4930 if (gt_bias) {
4931 __ CmpUleS(FTMP, rhs, lhs);
4932 } else {
4933 __ CmpLeS(FTMP, rhs, lhs);
4934 }
4935 __ Mfc1(dst, FTMP);
4936 __ Andi(dst, dst, 1);
4937 break;
4938 default:
4939 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4940 UNREACHABLE();
4941 }
4942 } else {
4943 switch (cond) {
4944 case kCondEQ:
4945 __ CeqS(0, lhs, rhs);
4946 __ LoadConst32(dst, 1);
4947 __ Movf(dst, ZERO, 0);
4948 break;
4949 case kCondNE:
4950 __ CeqS(0, lhs, rhs);
4951 __ LoadConst32(dst, 1);
4952 __ Movt(dst, ZERO, 0);
4953 break;
4954 case kCondLT:
4955 if (gt_bias) {
4956 __ ColtS(0, lhs, rhs);
4957 } else {
4958 __ CultS(0, lhs, rhs);
4959 }
4960 __ LoadConst32(dst, 1);
4961 __ Movf(dst, ZERO, 0);
4962 break;
4963 case kCondLE:
4964 if (gt_bias) {
4965 __ ColeS(0, lhs, rhs);
4966 } else {
4967 __ CuleS(0, lhs, rhs);
4968 }
4969 __ LoadConst32(dst, 1);
4970 __ Movf(dst, ZERO, 0);
4971 break;
4972 case kCondGT:
4973 if (gt_bias) {
4974 __ CultS(0, rhs, lhs);
4975 } else {
4976 __ ColtS(0, rhs, lhs);
4977 }
4978 __ LoadConst32(dst, 1);
4979 __ Movf(dst, ZERO, 0);
4980 break;
4981 case kCondGE:
4982 if (gt_bias) {
4983 __ CuleS(0, rhs, lhs);
4984 } else {
4985 __ ColeS(0, rhs, lhs);
4986 }
4987 __ LoadConst32(dst, 1);
4988 __ Movf(dst, ZERO, 0);
4989 break;
4990 default:
4991 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4992 UNREACHABLE();
4993 }
4994 }
4995 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004996 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004997 if (isR6) {
4998 switch (cond) {
4999 case kCondEQ:
5000 __ CmpEqD(FTMP, lhs, rhs);
5001 __ Mfc1(dst, FTMP);
5002 __ Andi(dst, dst, 1);
5003 break;
5004 case kCondNE:
5005 __ CmpEqD(FTMP, lhs, rhs);
5006 __ Mfc1(dst, FTMP);
5007 __ Addiu(dst, dst, 1);
5008 break;
5009 case kCondLT:
5010 if (gt_bias) {
5011 __ CmpLtD(FTMP, lhs, rhs);
5012 } else {
5013 __ CmpUltD(FTMP, lhs, rhs);
5014 }
5015 __ Mfc1(dst, FTMP);
5016 __ Andi(dst, dst, 1);
5017 break;
5018 case kCondLE:
5019 if (gt_bias) {
5020 __ CmpLeD(FTMP, lhs, rhs);
5021 } else {
5022 __ CmpUleD(FTMP, lhs, rhs);
5023 }
5024 __ Mfc1(dst, FTMP);
5025 __ Andi(dst, dst, 1);
5026 break;
5027 case kCondGT:
5028 if (gt_bias) {
5029 __ CmpUltD(FTMP, rhs, lhs);
5030 } else {
5031 __ CmpLtD(FTMP, rhs, lhs);
5032 }
5033 __ Mfc1(dst, FTMP);
5034 __ Andi(dst, dst, 1);
5035 break;
5036 case kCondGE:
5037 if (gt_bias) {
5038 __ CmpUleD(FTMP, rhs, lhs);
5039 } else {
5040 __ CmpLeD(FTMP, rhs, lhs);
5041 }
5042 __ Mfc1(dst, FTMP);
5043 __ Andi(dst, dst, 1);
5044 break;
5045 default:
5046 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5047 UNREACHABLE();
5048 }
5049 } else {
5050 switch (cond) {
5051 case kCondEQ:
5052 __ CeqD(0, lhs, rhs);
5053 __ LoadConst32(dst, 1);
5054 __ Movf(dst, ZERO, 0);
5055 break;
5056 case kCondNE:
5057 __ CeqD(0, lhs, rhs);
5058 __ LoadConst32(dst, 1);
5059 __ Movt(dst, ZERO, 0);
5060 break;
5061 case kCondLT:
5062 if (gt_bias) {
5063 __ ColtD(0, lhs, rhs);
5064 } else {
5065 __ CultD(0, lhs, rhs);
5066 }
5067 __ LoadConst32(dst, 1);
5068 __ Movf(dst, ZERO, 0);
5069 break;
5070 case kCondLE:
5071 if (gt_bias) {
5072 __ ColeD(0, lhs, rhs);
5073 } else {
5074 __ CuleD(0, lhs, rhs);
5075 }
5076 __ LoadConst32(dst, 1);
5077 __ Movf(dst, ZERO, 0);
5078 break;
5079 case kCondGT:
5080 if (gt_bias) {
5081 __ CultD(0, rhs, lhs);
5082 } else {
5083 __ ColtD(0, rhs, lhs);
5084 }
5085 __ LoadConst32(dst, 1);
5086 __ Movf(dst, ZERO, 0);
5087 break;
5088 case kCondGE:
5089 if (gt_bias) {
5090 __ CuleD(0, rhs, lhs);
5091 } else {
5092 __ ColeD(0, rhs, lhs);
5093 }
5094 __ LoadConst32(dst, 1);
5095 __ Movf(dst, ZERO, 0);
5096 break;
5097 default:
5098 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5099 UNREACHABLE();
5100 }
5101 }
5102 }
5103}
5104
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005105bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5106 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005107 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005108 LocationSummary* input_locations,
5109 int cc) {
5110 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5111 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5112 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005113 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005114 switch (cond) {
5115 case kCondEQ:
5116 __ CeqS(cc, lhs, rhs);
5117 return false;
5118 case kCondNE:
5119 __ CeqS(cc, lhs, rhs);
5120 return true;
5121 case kCondLT:
5122 if (gt_bias) {
5123 __ ColtS(cc, lhs, rhs);
5124 } else {
5125 __ CultS(cc, lhs, rhs);
5126 }
5127 return false;
5128 case kCondLE:
5129 if (gt_bias) {
5130 __ ColeS(cc, lhs, rhs);
5131 } else {
5132 __ CuleS(cc, lhs, rhs);
5133 }
5134 return false;
5135 case kCondGT:
5136 if (gt_bias) {
5137 __ CultS(cc, rhs, lhs);
5138 } else {
5139 __ ColtS(cc, rhs, lhs);
5140 }
5141 return false;
5142 case kCondGE:
5143 if (gt_bias) {
5144 __ CuleS(cc, rhs, lhs);
5145 } else {
5146 __ ColeS(cc, rhs, lhs);
5147 }
5148 return false;
5149 default:
5150 LOG(FATAL) << "Unexpected non-floating-point condition";
5151 UNREACHABLE();
5152 }
5153 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005154 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005155 switch (cond) {
5156 case kCondEQ:
5157 __ CeqD(cc, lhs, rhs);
5158 return false;
5159 case kCondNE:
5160 __ CeqD(cc, lhs, rhs);
5161 return true;
5162 case kCondLT:
5163 if (gt_bias) {
5164 __ ColtD(cc, lhs, rhs);
5165 } else {
5166 __ CultD(cc, lhs, rhs);
5167 }
5168 return false;
5169 case kCondLE:
5170 if (gt_bias) {
5171 __ ColeD(cc, lhs, rhs);
5172 } else {
5173 __ CuleD(cc, lhs, rhs);
5174 }
5175 return false;
5176 case kCondGT:
5177 if (gt_bias) {
5178 __ CultD(cc, rhs, lhs);
5179 } else {
5180 __ ColtD(cc, rhs, lhs);
5181 }
5182 return false;
5183 case kCondGE:
5184 if (gt_bias) {
5185 __ CuleD(cc, rhs, lhs);
5186 } else {
5187 __ ColeD(cc, rhs, lhs);
5188 }
5189 return false;
5190 default:
5191 LOG(FATAL) << "Unexpected non-floating-point condition";
5192 UNREACHABLE();
5193 }
5194 }
5195}
5196
5197bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5198 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005200 LocationSummary* input_locations,
5201 FRegister dst) {
5202 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5203 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5204 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005206 switch (cond) {
5207 case kCondEQ:
5208 __ CmpEqS(dst, lhs, rhs);
5209 return false;
5210 case kCondNE:
5211 __ CmpEqS(dst, lhs, rhs);
5212 return true;
5213 case kCondLT:
5214 if (gt_bias) {
5215 __ CmpLtS(dst, lhs, rhs);
5216 } else {
5217 __ CmpUltS(dst, lhs, rhs);
5218 }
5219 return false;
5220 case kCondLE:
5221 if (gt_bias) {
5222 __ CmpLeS(dst, lhs, rhs);
5223 } else {
5224 __ CmpUleS(dst, lhs, rhs);
5225 }
5226 return false;
5227 case kCondGT:
5228 if (gt_bias) {
5229 __ CmpUltS(dst, rhs, lhs);
5230 } else {
5231 __ CmpLtS(dst, rhs, lhs);
5232 }
5233 return false;
5234 case kCondGE:
5235 if (gt_bias) {
5236 __ CmpUleS(dst, rhs, lhs);
5237 } else {
5238 __ CmpLeS(dst, rhs, lhs);
5239 }
5240 return false;
5241 default:
5242 LOG(FATAL) << "Unexpected non-floating-point condition";
5243 UNREACHABLE();
5244 }
5245 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005246 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005247 switch (cond) {
5248 case kCondEQ:
5249 __ CmpEqD(dst, lhs, rhs);
5250 return false;
5251 case kCondNE:
5252 __ CmpEqD(dst, lhs, rhs);
5253 return true;
5254 case kCondLT:
5255 if (gt_bias) {
5256 __ CmpLtD(dst, lhs, rhs);
5257 } else {
5258 __ CmpUltD(dst, lhs, rhs);
5259 }
5260 return false;
5261 case kCondLE:
5262 if (gt_bias) {
5263 __ CmpLeD(dst, lhs, rhs);
5264 } else {
5265 __ CmpUleD(dst, lhs, rhs);
5266 }
5267 return false;
5268 case kCondGT:
5269 if (gt_bias) {
5270 __ CmpUltD(dst, rhs, lhs);
5271 } else {
5272 __ CmpLtD(dst, rhs, lhs);
5273 }
5274 return false;
5275 case kCondGE:
5276 if (gt_bias) {
5277 __ CmpUleD(dst, rhs, lhs);
5278 } else {
5279 __ CmpLeD(dst, rhs, lhs);
5280 }
5281 return false;
5282 default:
5283 LOG(FATAL) << "Unexpected non-floating-point condition";
5284 UNREACHABLE();
5285 }
5286 }
5287}
5288
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005289void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5290 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005292 LocationSummary* locations,
5293 MipsLabel* label) {
5294 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5295 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5296 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005298 if (isR6) {
5299 switch (cond) {
5300 case kCondEQ:
5301 __ CmpEqS(FTMP, lhs, rhs);
5302 __ Bc1nez(FTMP, label);
5303 break;
5304 case kCondNE:
5305 __ CmpEqS(FTMP, lhs, rhs);
5306 __ Bc1eqz(FTMP, label);
5307 break;
5308 case kCondLT:
5309 if (gt_bias) {
5310 __ CmpLtS(FTMP, lhs, rhs);
5311 } else {
5312 __ CmpUltS(FTMP, lhs, rhs);
5313 }
5314 __ Bc1nez(FTMP, label);
5315 break;
5316 case kCondLE:
5317 if (gt_bias) {
5318 __ CmpLeS(FTMP, lhs, rhs);
5319 } else {
5320 __ CmpUleS(FTMP, lhs, rhs);
5321 }
5322 __ Bc1nez(FTMP, label);
5323 break;
5324 case kCondGT:
5325 if (gt_bias) {
5326 __ CmpUltS(FTMP, rhs, lhs);
5327 } else {
5328 __ CmpLtS(FTMP, rhs, lhs);
5329 }
5330 __ Bc1nez(FTMP, label);
5331 break;
5332 case kCondGE:
5333 if (gt_bias) {
5334 __ CmpUleS(FTMP, rhs, lhs);
5335 } else {
5336 __ CmpLeS(FTMP, rhs, lhs);
5337 }
5338 __ Bc1nez(FTMP, label);
5339 break;
5340 default:
5341 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005342 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005343 }
5344 } else {
5345 switch (cond) {
5346 case kCondEQ:
5347 __ CeqS(0, lhs, rhs);
5348 __ Bc1t(0, label);
5349 break;
5350 case kCondNE:
5351 __ CeqS(0, lhs, rhs);
5352 __ Bc1f(0, label);
5353 break;
5354 case kCondLT:
5355 if (gt_bias) {
5356 __ ColtS(0, lhs, rhs);
5357 } else {
5358 __ CultS(0, lhs, rhs);
5359 }
5360 __ Bc1t(0, label);
5361 break;
5362 case kCondLE:
5363 if (gt_bias) {
5364 __ ColeS(0, lhs, rhs);
5365 } else {
5366 __ CuleS(0, lhs, rhs);
5367 }
5368 __ Bc1t(0, label);
5369 break;
5370 case kCondGT:
5371 if (gt_bias) {
5372 __ CultS(0, rhs, lhs);
5373 } else {
5374 __ ColtS(0, rhs, lhs);
5375 }
5376 __ Bc1t(0, label);
5377 break;
5378 case kCondGE:
5379 if (gt_bias) {
5380 __ CuleS(0, rhs, lhs);
5381 } else {
5382 __ ColeS(0, rhs, lhs);
5383 }
5384 __ Bc1t(0, label);
5385 break;
5386 default:
5387 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005388 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005389 }
5390 }
5391 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005393 if (isR6) {
5394 switch (cond) {
5395 case kCondEQ:
5396 __ CmpEqD(FTMP, lhs, rhs);
5397 __ Bc1nez(FTMP, label);
5398 break;
5399 case kCondNE:
5400 __ CmpEqD(FTMP, lhs, rhs);
5401 __ Bc1eqz(FTMP, label);
5402 break;
5403 case kCondLT:
5404 if (gt_bias) {
5405 __ CmpLtD(FTMP, lhs, rhs);
5406 } else {
5407 __ CmpUltD(FTMP, lhs, rhs);
5408 }
5409 __ Bc1nez(FTMP, label);
5410 break;
5411 case kCondLE:
5412 if (gt_bias) {
5413 __ CmpLeD(FTMP, lhs, rhs);
5414 } else {
5415 __ CmpUleD(FTMP, lhs, rhs);
5416 }
5417 __ Bc1nez(FTMP, label);
5418 break;
5419 case kCondGT:
5420 if (gt_bias) {
5421 __ CmpUltD(FTMP, rhs, lhs);
5422 } else {
5423 __ CmpLtD(FTMP, rhs, lhs);
5424 }
5425 __ Bc1nez(FTMP, label);
5426 break;
5427 case kCondGE:
5428 if (gt_bias) {
5429 __ CmpUleD(FTMP, rhs, lhs);
5430 } else {
5431 __ CmpLeD(FTMP, rhs, lhs);
5432 }
5433 __ Bc1nez(FTMP, label);
5434 break;
5435 default:
5436 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005437 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005438 }
5439 } else {
5440 switch (cond) {
5441 case kCondEQ:
5442 __ CeqD(0, lhs, rhs);
5443 __ Bc1t(0, label);
5444 break;
5445 case kCondNE:
5446 __ CeqD(0, lhs, rhs);
5447 __ Bc1f(0, label);
5448 break;
5449 case kCondLT:
5450 if (gt_bias) {
5451 __ ColtD(0, lhs, rhs);
5452 } else {
5453 __ CultD(0, lhs, rhs);
5454 }
5455 __ Bc1t(0, label);
5456 break;
5457 case kCondLE:
5458 if (gt_bias) {
5459 __ ColeD(0, lhs, rhs);
5460 } else {
5461 __ CuleD(0, lhs, rhs);
5462 }
5463 __ Bc1t(0, label);
5464 break;
5465 case kCondGT:
5466 if (gt_bias) {
5467 __ CultD(0, rhs, lhs);
5468 } else {
5469 __ ColtD(0, rhs, lhs);
5470 }
5471 __ Bc1t(0, label);
5472 break;
5473 case kCondGE:
5474 if (gt_bias) {
5475 __ CuleD(0, rhs, lhs);
5476 } else {
5477 __ ColeD(0, rhs, lhs);
5478 }
5479 __ Bc1t(0, label);
5480 break;
5481 default:
5482 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005483 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005484 }
5485 }
5486 }
5487}
5488
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005489void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005490 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005491 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005492 MipsLabel* false_target) {
5493 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005494
David Brazdil0debae72015-11-12 18:37:00 +00005495 if (true_target == nullptr && false_target == nullptr) {
5496 // Nothing to do. The code always falls through.
5497 return;
5498 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005499 // Constant condition, statically compared against "true" (integer value 1).
5500 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005501 if (true_target != nullptr) {
5502 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005503 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005504 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005505 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005506 if (false_target != nullptr) {
5507 __ B(false_target);
5508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005509 }
David Brazdil0debae72015-11-12 18:37:00 +00005510 return;
5511 }
5512
5513 // The following code generates these patterns:
5514 // (1) true_target == nullptr && false_target != nullptr
5515 // - opposite condition true => branch to false_target
5516 // (2) true_target != nullptr && false_target == nullptr
5517 // - condition true => branch to true_target
5518 // (3) true_target != nullptr && false_target != nullptr
5519 // - condition true => branch to true_target
5520 // - branch to false_target
5521 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005522 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005523 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005524 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005525 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005526 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5527 } else {
5528 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5529 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005530 } else {
5531 // The condition instruction has not been materialized, use its inputs as
5532 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005533 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005535 LocationSummary* locations = cond->GetLocations();
5536 IfCondition if_cond = condition->GetCondition();
5537 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005538
David Brazdil0debae72015-11-12 18:37:00 +00005539 if (true_target == nullptr) {
5540 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005541 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005542 }
5543
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005544 switch (type) {
5545 default:
5546 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5547 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005549 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5550 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 case DataType::Type::kFloat32:
5552 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005553 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5554 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005555 }
5556 }
David Brazdil0debae72015-11-12 18:37:00 +00005557
5558 // If neither branch falls through (case 3), the conditional branch to `true_target`
5559 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5560 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561 __ B(false_target);
5562 }
5563}
5564
5565void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005566 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005567 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005568 locations->SetInAt(0, Location::RequiresRegister());
5569 }
5570}
5571
5572void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005573 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5574 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5575 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5576 nullptr : codegen_->GetLabelOf(true_successor);
5577 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5578 nullptr : codegen_->GetLabelOf(false_successor);
5579 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580}
5581
5582void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005583 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005584 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005585 InvokeRuntimeCallingConvention calling_convention;
5586 RegisterSet caller_saves = RegisterSet::Empty();
5587 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5588 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005589 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005590 locations->SetInAt(0, Location::RequiresRegister());
5591 }
5592}
5593
5594void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005595 SlowPathCodeMIPS* slow_path =
5596 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005597 GenerateTestAndBranch(deoptimize,
5598 /* condition_input_index */ 0,
5599 slow_path->GetEntryLabel(),
5600 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005601}
5602
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005603// This function returns true if a conditional move can be generated for HSelect.
5604// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5605// branches and regular moves.
5606//
5607// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5608//
5609// While determining feasibility of a conditional move and setting inputs/outputs
5610// are two distinct tasks, this function does both because they share quite a bit
5611// of common logic.
5612static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5613 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5614 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5615 HCondition* condition = cond->AsCondition();
5616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005617 DataType::Type cond_type =
5618 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5619 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005620
5621 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5622 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5623 bool is_true_value_zero_constant =
5624 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5625 bool is_false_value_zero_constant =
5626 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5627
5628 bool can_move_conditionally = false;
5629 bool use_const_for_false_in = false;
5630 bool use_const_for_true_in = false;
5631
5632 if (!cond->IsConstant()) {
5633 switch (cond_type) {
5634 default:
5635 switch (dst_type) {
5636 default:
5637 // Moving int on int condition.
5638 if (is_r6) {
5639 if (is_true_value_zero_constant) {
5640 // seleqz out_reg, false_reg, cond_reg
5641 can_move_conditionally = true;
5642 use_const_for_true_in = true;
5643 } else if (is_false_value_zero_constant) {
5644 // selnez out_reg, true_reg, cond_reg
5645 can_move_conditionally = true;
5646 use_const_for_false_in = true;
5647 } else if (materialized) {
5648 // Not materializing unmaterialized int conditions
5649 // to keep the instruction count low.
5650 // selnez AT, true_reg, cond_reg
5651 // seleqz TMP, false_reg, cond_reg
5652 // or out_reg, AT, TMP
5653 can_move_conditionally = true;
5654 }
5655 } else {
5656 // movn out_reg, true_reg/ZERO, cond_reg
5657 can_move_conditionally = true;
5658 use_const_for_true_in = is_true_value_zero_constant;
5659 }
5660 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005662 // Moving long on int condition.
5663 if (is_r6) {
5664 if (is_true_value_zero_constant) {
5665 // seleqz out_reg_lo, false_reg_lo, cond_reg
5666 // seleqz out_reg_hi, false_reg_hi, cond_reg
5667 can_move_conditionally = true;
5668 use_const_for_true_in = true;
5669 } else if (is_false_value_zero_constant) {
5670 // selnez out_reg_lo, true_reg_lo, cond_reg
5671 // selnez out_reg_hi, true_reg_hi, cond_reg
5672 can_move_conditionally = true;
5673 use_const_for_false_in = true;
5674 }
5675 // Other long conditional moves would generate 6+ instructions,
5676 // which is too many.
5677 } else {
5678 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5679 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5680 can_move_conditionally = true;
5681 use_const_for_true_in = is_true_value_zero_constant;
5682 }
5683 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005684 case DataType::Type::kFloat32:
5685 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005686 // Moving float/double on int condition.
5687 if (is_r6) {
5688 if (materialized) {
5689 // Not materializing unmaterialized int conditions
5690 // to keep the instruction count low.
5691 can_move_conditionally = true;
5692 if (is_true_value_zero_constant) {
5693 // sltu TMP, ZERO, cond_reg
5694 // mtc1 TMP, temp_cond_reg
5695 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5696 use_const_for_true_in = true;
5697 } else if (is_false_value_zero_constant) {
5698 // sltu TMP, ZERO, cond_reg
5699 // mtc1 TMP, temp_cond_reg
5700 // selnez.fmt out_reg, true_reg, temp_cond_reg
5701 use_const_for_false_in = true;
5702 } else {
5703 // sltu TMP, ZERO, cond_reg
5704 // mtc1 TMP, temp_cond_reg
5705 // sel.fmt temp_cond_reg, false_reg, true_reg
5706 // mov.fmt out_reg, temp_cond_reg
5707 }
5708 }
5709 } else {
5710 // movn.fmt out_reg, true_reg, cond_reg
5711 can_move_conditionally = true;
5712 }
5713 break;
5714 }
5715 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005717 // We don't materialize long comparison now
5718 // and use conditional branches instead.
5719 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat32:
5721 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005722 switch (dst_type) {
5723 default:
5724 // Moving int on float/double condition.
5725 if (is_r6) {
5726 if (is_true_value_zero_constant) {
5727 // mfc1 TMP, temp_cond_reg
5728 // seleqz out_reg, false_reg, TMP
5729 can_move_conditionally = true;
5730 use_const_for_true_in = true;
5731 } else if (is_false_value_zero_constant) {
5732 // mfc1 TMP, temp_cond_reg
5733 // selnez out_reg, true_reg, TMP
5734 can_move_conditionally = true;
5735 use_const_for_false_in = true;
5736 } else {
5737 // mfc1 TMP, temp_cond_reg
5738 // selnez AT, true_reg, TMP
5739 // seleqz TMP, false_reg, TMP
5740 // or out_reg, AT, TMP
5741 can_move_conditionally = true;
5742 }
5743 } else {
5744 // movt out_reg, true_reg/ZERO, cc
5745 can_move_conditionally = true;
5746 use_const_for_true_in = is_true_value_zero_constant;
5747 }
5748 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005750 // Moving long on float/double condition.
5751 if (is_r6) {
5752 if (is_true_value_zero_constant) {
5753 // mfc1 TMP, temp_cond_reg
5754 // seleqz out_reg_lo, false_reg_lo, TMP
5755 // seleqz out_reg_hi, false_reg_hi, TMP
5756 can_move_conditionally = true;
5757 use_const_for_true_in = true;
5758 } else if (is_false_value_zero_constant) {
5759 // mfc1 TMP, temp_cond_reg
5760 // selnez out_reg_lo, true_reg_lo, TMP
5761 // selnez out_reg_hi, true_reg_hi, TMP
5762 can_move_conditionally = true;
5763 use_const_for_false_in = true;
5764 }
5765 // Other long conditional moves would generate 6+ instructions,
5766 // which is too many.
5767 } else {
5768 // movt out_reg_lo, true_reg_lo/ZERO, cc
5769 // movt out_reg_hi, true_reg_hi/ZERO, cc
5770 can_move_conditionally = true;
5771 use_const_for_true_in = is_true_value_zero_constant;
5772 }
5773 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 case DataType::Type::kFloat32:
5775 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005776 // Moving float/double on float/double condition.
5777 if (is_r6) {
5778 can_move_conditionally = true;
5779 if (is_true_value_zero_constant) {
5780 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5781 use_const_for_true_in = true;
5782 } else if (is_false_value_zero_constant) {
5783 // selnez.fmt out_reg, true_reg, temp_cond_reg
5784 use_const_for_false_in = true;
5785 } else {
5786 // sel.fmt temp_cond_reg, false_reg, true_reg
5787 // mov.fmt out_reg, temp_cond_reg
5788 }
5789 } else {
5790 // movt.fmt out_reg, true_reg, cc
5791 can_move_conditionally = true;
5792 }
5793 break;
5794 }
5795 break;
5796 }
5797 }
5798
5799 if (can_move_conditionally) {
5800 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5801 } else {
5802 DCHECK(!use_const_for_false_in);
5803 DCHECK(!use_const_for_true_in);
5804 }
5805
5806 if (locations_to_set != nullptr) {
5807 if (use_const_for_false_in) {
5808 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5809 } else {
5810 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005811 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005812 ? Location::RequiresFpuRegister()
5813 : Location::RequiresRegister());
5814 }
5815 if (use_const_for_true_in) {
5816 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5817 } else {
5818 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005820 ? Location::RequiresFpuRegister()
5821 : Location::RequiresRegister());
5822 }
5823 if (materialized) {
5824 locations_to_set->SetInAt(2, Location::RequiresRegister());
5825 }
5826 // On R6 we don't require the output to be the same as the
5827 // first input for conditional moves unlike on R2.
5828 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5829 if (is_out_same_as_first_in) {
5830 locations_to_set->SetOut(Location::SameAsFirstInput());
5831 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005833 ? Location::RequiresFpuRegister()
5834 : Location::RequiresRegister());
5835 }
5836 }
5837
5838 return can_move_conditionally;
5839}
5840
5841void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5842 LocationSummary* locations = select->GetLocations();
5843 Location dst = locations->Out();
5844 Location src = locations->InAt(1);
5845 Register src_reg = ZERO;
5846 Register src_reg_high = ZERO;
5847 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5848 Register cond_reg = TMP;
5849 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005851 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005852 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005853
5854 if (IsBooleanValueOrMaterializedCondition(cond)) {
5855 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5856 } else {
5857 HCondition* condition = cond->AsCondition();
5858 LocationSummary* cond_locations = cond->GetLocations();
5859 IfCondition if_cond = condition->GetCondition();
5860 cond_type = condition->InputAt(0)->GetType();
5861 switch (cond_type) {
5862 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005864 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5865 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 case DataType::Type::kFloat32:
5867 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005868 cond_inverted = MaterializeFpCompareR2(if_cond,
5869 condition->IsGtBias(),
5870 cond_type,
5871 cond_locations,
5872 cond_cc);
5873 break;
5874 }
5875 }
5876
5877 DCHECK(dst.Equals(locations->InAt(0)));
5878 if (src.IsRegister()) {
5879 src_reg = src.AsRegister<Register>();
5880 } else if (src.IsRegisterPair()) {
5881 src_reg = src.AsRegisterPairLow<Register>();
5882 src_reg_high = src.AsRegisterPairHigh<Register>();
5883 } else if (src.IsConstant()) {
5884 DCHECK(src.GetConstant()->IsZeroBitPattern());
5885 }
5886
5887 switch (cond_type) {
5888 default:
5889 switch (dst_type) {
5890 default:
5891 if (cond_inverted) {
5892 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5893 } else {
5894 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5895 }
5896 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005898 if (cond_inverted) {
5899 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5900 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5901 } else {
5902 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5903 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5904 }
5905 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005906 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005907 if (cond_inverted) {
5908 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5909 } else {
5910 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5911 }
5912 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005914 if (cond_inverted) {
5915 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5916 } else {
5917 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5918 }
5919 break;
5920 }
5921 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005922 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005923 LOG(FATAL) << "Unreachable";
5924 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005925 case DataType::Type::kFloat32:
5926 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005927 switch (dst_type) {
5928 default:
5929 if (cond_inverted) {
5930 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5931 } else {
5932 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5933 }
5934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005936 if (cond_inverted) {
5937 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5938 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5939 } else {
5940 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5941 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5942 }
5943 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005945 if (cond_inverted) {
5946 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5947 } else {
5948 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5949 }
5950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005952 if (cond_inverted) {
5953 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5954 } else {
5955 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5956 }
5957 break;
5958 }
5959 break;
5960 }
5961}
5962
5963void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5964 LocationSummary* locations = select->GetLocations();
5965 Location dst = locations->Out();
5966 Location false_src = locations->InAt(0);
5967 Location true_src = locations->InAt(1);
5968 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5969 Register cond_reg = TMP;
5970 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005972 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005973 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005974
5975 if (IsBooleanValueOrMaterializedCondition(cond)) {
5976 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5977 } else {
5978 HCondition* condition = cond->AsCondition();
5979 LocationSummary* cond_locations = cond->GetLocations();
5980 IfCondition if_cond = condition->GetCondition();
5981 cond_type = condition->InputAt(0)->GetType();
5982 switch (cond_type) {
5983 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005985 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5986 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 case DataType::Type::kFloat32:
5988 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005989 cond_inverted = MaterializeFpCompareR6(if_cond,
5990 condition->IsGtBias(),
5991 cond_type,
5992 cond_locations,
5993 fcond_reg);
5994 break;
5995 }
5996 }
5997
5998 if (true_src.IsConstant()) {
5999 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6000 }
6001 if (false_src.IsConstant()) {
6002 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6003 }
6004
6005 switch (dst_type) {
6006 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006007 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006008 __ Mfc1(cond_reg, fcond_reg);
6009 }
6010 if (true_src.IsConstant()) {
6011 if (cond_inverted) {
6012 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6013 } else {
6014 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6015 }
6016 } else if (false_src.IsConstant()) {
6017 if (cond_inverted) {
6018 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6019 } else {
6020 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6021 }
6022 } else {
6023 DCHECK_NE(cond_reg, AT);
6024 if (cond_inverted) {
6025 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6026 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6027 } else {
6028 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6029 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6030 }
6031 __ Or(dst.AsRegister<Register>(), AT, TMP);
6032 }
6033 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 case DataType::Type::kInt64: {
6035 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006036 __ Mfc1(cond_reg, fcond_reg);
6037 }
6038 Register dst_lo = dst.AsRegisterPairLow<Register>();
6039 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6040 if (true_src.IsConstant()) {
6041 Register src_lo = false_src.AsRegisterPairLow<Register>();
6042 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6043 if (cond_inverted) {
6044 __ Selnez(dst_lo, src_lo, cond_reg);
6045 __ Selnez(dst_hi, src_hi, cond_reg);
6046 } else {
6047 __ Seleqz(dst_lo, src_lo, cond_reg);
6048 __ Seleqz(dst_hi, src_hi, cond_reg);
6049 }
6050 } else {
6051 DCHECK(false_src.IsConstant());
6052 Register src_lo = true_src.AsRegisterPairLow<Register>();
6053 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6054 if (cond_inverted) {
6055 __ Seleqz(dst_lo, src_lo, cond_reg);
6056 __ Seleqz(dst_hi, src_hi, cond_reg);
6057 } else {
6058 __ Selnez(dst_lo, src_lo, cond_reg);
6059 __ Selnez(dst_hi, src_hi, cond_reg);
6060 }
6061 }
6062 break;
6063 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006064 case DataType::Type::kFloat32: {
6065 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006066 // sel*.fmt tests bit 0 of the condition register, account for that.
6067 __ Sltu(TMP, ZERO, cond_reg);
6068 __ Mtc1(TMP, fcond_reg);
6069 }
6070 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6071 if (true_src.IsConstant()) {
6072 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6073 if (cond_inverted) {
6074 __ SelnezS(dst_reg, src_reg, fcond_reg);
6075 } else {
6076 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6077 }
6078 } else if (false_src.IsConstant()) {
6079 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6080 if (cond_inverted) {
6081 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6082 } else {
6083 __ SelnezS(dst_reg, src_reg, fcond_reg);
6084 }
6085 } else {
6086 if (cond_inverted) {
6087 __ SelS(fcond_reg,
6088 true_src.AsFpuRegister<FRegister>(),
6089 false_src.AsFpuRegister<FRegister>());
6090 } else {
6091 __ SelS(fcond_reg,
6092 false_src.AsFpuRegister<FRegister>(),
6093 true_src.AsFpuRegister<FRegister>());
6094 }
6095 __ MovS(dst_reg, fcond_reg);
6096 }
6097 break;
6098 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006099 case DataType::Type::kFloat64: {
6100 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006101 // sel*.fmt tests bit 0 of the condition register, account for that.
6102 __ Sltu(TMP, ZERO, cond_reg);
6103 __ Mtc1(TMP, fcond_reg);
6104 }
6105 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6106 if (true_src.IsConstant()) {
6107 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6108 if (cond_inverted) {
6109 __ SelnezD(dst_reg, src_reg, fcond_reg);
6110 } else {
6111 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6112 }
6113 } else if (false_src.IsConstant()) {
6114 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6115 if (cond_inverted) {
6116 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6117 } else {
6118 __ SelnezD(dst_reg, src_reg, fcond_reg);
6119 }
6120 } else {
6121 if (cond_inverted) {
6122 __ SelD(fcond_reg,
6123 true_src.AsFpuRegister<FRegister>(),
6124 false_src.AsFpuRegister<FRegister>());
6125 } else {
6126 __ SelD(fcond_reg,
6127 false_src.AsFpuRegister<FRegister>(),
6128 true_src.AsFpuRegister<FRegister>());
6129 }
6130 __ MovD(dst_reg, fcond_reg);
6131 }
6132 break;
6133 }
6134 }
6135}
6136
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006137void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006138 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006139 LocationSummary(flag, LocationSummary::kNoCall);
6140 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006141}
6142
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006143void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6144 __ LoadFromOffset(kLoadWord,
6145 flag->GetLocations()->Out().AsRegister<Register>(),
6146 SP,
6147 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006148}
6149
David Brazdil74eb1b22015-12-14 11:44:01 +00006150void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006151 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006152 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006153}
6154
6155void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006156 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6157 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6158 if (is_r6) {
6159 GenConditionalMoveR6(select);
6160 } else {
6161 GenConditionalMoveR2(select);
6162 }
6163 } else {
6164 LocationSummary* locations = select->GetLocations();
6165 MipsLabel false_target;
6166 GenerateTestAndBranch(select,
6167 /* condition_input_index */ 2,
6168 /* true_target */ nullptr,
6169 &false_target);
6170 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6171 __ Bind(&false_target);
6172 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006173}
6174
David Srbecky0cf44932015-12-09 14:09:59 +00006175void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006176 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006177}
6178
David Srbeckyd28f4a02016-03-14 17:14:24 +00006179void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6180 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006181}
6182
6183void CodeGeneratorMIPS::GenerateNop() {
6184 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006185}
6186
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006187void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 DataType::Type field_type = field_info.GetFieldType();
6189 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006190 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006191 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006193 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006194 instruction,
6195 generate_volatile
6196 ? LocationSummary::kCallOnMainOnly
6197 : (object_field_get_with_read_barrier
6198 ? LocationSummary::kCallOnSlowPath
6199 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006200
Alexey Frunzec61c0762017-04-10 13:54:23 -07006201 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6203 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006204 locations->SetInAt(0, Location::RequiresRegister());
6205 if (generate_volatile) {
6206 InvokeRuntimeCallingConvention calling_convention;
6207 // need A0 to hold base + offset
6208 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006209 if (field_type == DataType::Type::kInt64) {
6210 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006211 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006212 // Use Location::Any() to prevent situations when running out of available fp registers.
6213 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006216 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6217 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6218 }
6219 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 locations->SetOut(Location::RequiresFpuRegister());
6222 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006223 // The output overlaps in the case of an object field get with
6224 // read barriers enabled: we do not want the move to overwrite the
6225 // object's location, as we need it to emit the read barrier.
6226 locations->SetOut(Location::RequiresRegister(),
6227 object_field_get_with_read_barrier
6228 ? Location::kOutputOverlap
6229 : Location::kNoOutputOverlap);
6230 }
6231 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6232 // We need a temporary register for the read barrier marking slow
6233 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006234 if (!kBakerReadBarrierThunksEnableForFields) {
6235 locations->AddTemp(Location::RequiresRegister());
6236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006237 }
6238 }
6239}
6240
6241void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6242 const FieldInfo& field_info,
6243 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006244 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6245 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006246 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006247 Location obj_loc = locations->InAt(0);
6248 Register obj = obj_loc.AsRegister<Register>();
6249 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250 LoadOperandType load_type = kLoadUnsignedByte;
6251 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006252 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006253 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006254
6255 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006256 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006257 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006258 load_type = kLoadUnsignedByte;
6259 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006261 load_type = kLoadSignedByte;
6262 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006264 load_type = kLoadUnsignedHalfword;
6265 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006266 case DataType::Type::kInt16:
6267 load_type = kLoadSignedHalfword;
6268 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 case DataType::Type::kInt32:
6270 case DataType::Type::kFloat32:
6271 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006272 load_type = kLoadWord;
6273 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006274 case DataType::Type::kInt64:
6275 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276 load_type = kLoadDoubleword;
6277 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006279 LOG(FATAL) << "Unreachable type " << type;
6280 UNREACHABLE();
6281 }
6282
6283 if (is_volatile && load_type == kLoadDoubleword) {
6284 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006285 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006287 __ LoadFromOffset(kLoadWord,
6288 ZERO,
6289 locations->GetTemp(0).AsRegister<Register>(),
6290 0,
6291 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006292 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006295 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006296 if (dst_loc.IsFpuRegister()) {
6297 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006298 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006299 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006300 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006301 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006302 __ StoreToOffset(kStoreWord,
6303 locations->GetTemp(1).AsRegister<Register>(),
6304 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006305 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006306 __ StoreToOffset(kStoreWord,
6307 locations->GetTemp(2).AsRegister<Register>(),
6308 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006309 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006310 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006311 }
6312 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006313 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006314 // /* HeapReference<Object> */ dst = *(obj + offset)
6315 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006316 Location temp_loc =
6317 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006318 // Note that a potential implicit null check is handled in this
6319 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6320 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6321 dst_loc,
6322 obj,
6323 offset,
6324 temp_loc,
6325 /* needs_null_check */ true);
6326 if (is_volatile) {
6327 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6328 }
6329 } else {
6330 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6331 if (is_volatile) {
6332 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6333 }
6334 // If read barriers are enabled, emit read barriers other than
6335 // Baker's using a slow path (and also unpoison the loaded
6336 // reference, if heap poisoning is enabled).
6337 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6338 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006340 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006342 DCHECK(dst_loc.IsRegisterPair());
6343 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006344 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006345 DCHECK(dst_loc.IsRegister());
6346 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006347 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006348 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006349 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006350 DCHECK(dst_loc.IsFpuRegister());
6351 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006353 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006354 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006355 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006356 }
6357 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006358 }
6359
Alexey Frunze15958152017-02-09 19:08:30 -08006360 // Memory barriers, in the case of references, are handled in the
6361 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006363 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6364 }
6365}
6366
6367void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 DataType::Type field_type = field_info.GetFieldType();
6369 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006371 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006372 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006373
6374 locations->SetInAt(0, Location::RequiresRegister());
6375 if (generate_volatile) {
6376 InvokeRuntimeCallingConvention calling_convention;
6377 // need A0 to hold base + offset
6378 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 locations->SetInAt(1, Location::RegisterPairLocation(
6381 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6382 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006383 // Use Location::Any() to prevent situations when running out of available fp registers.
6384 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 // Pass FP parameters in core registers.
6386 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6387 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6388 }
6389 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006391 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006393 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 }
6395 }
6396}
6397
6398void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6399 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006400 uint32_t dex_pc,
6401 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 LocationSummary* locations = instruction->GetLocations();
6404 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006405 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006406 StoreOperandType store_type = kStoreByte;
6407 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006408 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006409 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006410 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006411
6412 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006414 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006415 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006416 store_type = kStoreByte;
6417 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006418 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006419 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420 store_type = kStoreHalfword;
6421 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006422 case DataType::Type::kInt32:
6423 case DataType::Type::kFloat32:
6424 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425 store_type = kStoreWord;
6426 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006427 case DataType::Type::kInt64:
6428 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006429 store_type = kStoreDoubleword;
6430 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006431 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006432 LOG(FATAL) << "Unreachable type " << type;
6433 UNREACHABLE();
6434 }
6435
6436 if (is_volatile) {
6437 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6438 }
6439
6440 if (is_volatile && store_type == kStoreDoubleword) {
6441 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006442 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006443 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006444 __ LoadFromOffset(kLoadWord,
6445 ZERO,
6446 locations->GetTemp(0).AsRegister<Register>(),
6447 0,
6448 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006451 if (value_location.IsFpuRegister()) {
6452 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6453 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006454 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006455 value_location.AsFpuRegister<FRegister>());
6456 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006457 __ LoadFromOffset(kLoadWord,
6458 locations->GetTemp(1).AsRegister<Register>(),
6459 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006460 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006461 __ LoadFromOffset(kLoadWord,
6462 locations->GetTemp(2).AsRegister<Register>(),
6463 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006464 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006465 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006466 DCHECK(value_location.IsConstant());
6467 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6468 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006469 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6470 locations->GetTemp(1).AsRegister<Register>(),
6471 value);
6472 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006473 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006474 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006475 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6476 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006477 if (value_location.IsConstant()) {
6478 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6479 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006481 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006482 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006483 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006484 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006485 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006486 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006487 if (kPoisonHeapReferences && needs_write_barrier) {
6488 // Note that in the case where `value` is a null reference,
6489 // we do not enter this block, as a null reference does not
6490 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006491 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006492 __ PoisonHeapReference(TMP, src);
6493 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6494 } else {
6495 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6496 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006497 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006498 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006500 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006501 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006502 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006503 }
6504 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006505 }
6506
Alexey Frunzec061de12017-02-14 13:27:23 -08006507 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006508 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006509 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006510 }
6511
6512 if (is_volatile) {
6513 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6514 }
6515}
6516
6517void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6518 HandleFieldGet(instruction, instruction->GetFieldInfo());
6519}
6520
6521void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6522 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6523}
6524
6525void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6526 HandleFieldSet(instruction, instruction->GetFieldInfo());
6527}
6528
6529void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006530 HandleFieldSet(instruction,
6531 instruction->GetFieldInfo(),
6532 instruction->GetDexPc(),
6533 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534}
6535
Alexey Frunze15958152017-02-09 19:08:30 -08006536void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6537 HInstruction* instruction,
6538 Location out,
6539 uint32_t offset,
6540 Location maybe_temp,
6541 ReadBarrierOption read_barrier_option) {
6542 Register out_reg = out.AsRegister<Register>();
6543 if (read_barrier_option == kWithReadBarrier) {
6544 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006545 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6546 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6547 }
Alexey Frunze15958152017-02-09 19:08:30 -08006548 if (kUseBakerReadBarrier) {
6549 // Load with fast path based Baker's read barrier.
6550 // /* HeapReference<Object> */ out = *(out + offset)
6551 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6552 out,
6553 out_reg,
6554 offset,
6555 maybe_temp,
6556 /* needs_null_check */ false);
6557 } else {
6558 // Load with slow path based read barrier.
6559 // Save the value of `out` into `maybe_temp` before overwriting it
6560 // in the following move operation, as we will need it for the
6561 // read barrier below.
6562 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6563 // /* HeapReference<Object> */ out = *(out + offset)
6564 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6565 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6566 }
6567 } else {
6568 // Plain load with no read barrier.
6569 // /* HeapReference<Object> */ out = *(out + offset)
6570 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6571 __ MaybeUnpoisonHeapReference(out_reg);
6572 }
6573}
6574
6575void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6576 HInstruction* instruction,
6577 Location out,
6578 Location obj,
6579 uint32_t offset,
6580 Location maybe_temp,
6581 ReadBarrierOption read_barrier_option) {
6582 Register out_reg = out.AsRegister<Register>();
6583 Register obj_reg = obj.AsRegister<Register>();
6584 if (read_barrier_option == kWithReadBarrier) {
6585 CHECK(kEmitCompilerReadBarrier);
6586 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006587 if (!kBakerReadBarrierThunksEnableForFields) {
6588 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6589 }
Alexey Frunze15958152017-02-09 19:08:30 -08006590 // Load with fast path based Baker's read barrier.
6591 // /* HeapReference<Object> */ out = *(obj + offset)
6592 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6593 out,
6594 obj_reg,
6595 offset,
6596 maybe_temp,
6597 /* needs_null_check */ false);
6598 } else {
6599 // Load with slow path based read barrier.
6600 // /* HeapReference<Object> */ out = *(obj + offset)
6601 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6602 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6603 }
6604 } else {
6605 // Plain load with no read barrier.
6606 // /* HeapReference<Object> */ out = *(obj + offset)
6607 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6608 __ MaybeUnpoisonHeapReference(out_reg);
6609 }
6610}
6611
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006612static inline int GetBakerMarkThunkNumber(Register reg) {
6613 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6614 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6615 return reg - V0;
6616 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6617 return 14 + (reg - S2);
6618 } else if (reg == FP) { // One more.
6619 return 20;
6620 }
6621 LOG(FATAL) << "Unexpected register " << reg;
6622 UNREACHABLE();
6623}
6624
6625static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6626 int num = GetBakerMarkThunkNumber(reg) +
6627 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6628 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6629}
6630
6631static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6632 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6633 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6634}
6635
Alexey Frunze15958152017-02-09 19:08:30 -08006636void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6637 Location root,
6638 Register obj,
6639 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006640 ReadBarrierOption read_barrier_option,
6641 MipsLabel* label_low) {
6642 bool reordering;
6643 if (label_low != nullptr) {
6644 DCHECK_EQ(offset, 0x5678u);
6645 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006646 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006647 if (read_barrier_option == kWithReadBarrier) {
6648 DCHECK(kEmitCompilerReadBarrier);
6649 if (kUseBakerReadBarrier) {
6650 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6651 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006652 if (kBakerReadBarrierThunksEnableForGcRoots) {
6653 // Note that we do not actually check the value of `GetIsGcMarking()`
6654 // to decide whether to mark the loaded GC root or not. Instead, we
6655 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6656 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6657 // vice versa.
6658 //
6659 // We use thunks for the slow path. That thunk checks the reference
6660 // and jumps to the entrypoint if needed.
6661 //
6662 // temp = Thread::Current()->pReadBarrierMarkReg00
6663 // // AKA &art_quick_read_barrier_mark_introspection.
6664 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6665 // if (temp != nullptr) {
6666 // temp = &gc_root_thunk<root_reg>
6667 // root = temp(root)
6668 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006669
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006670 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6671 const int32_t entry_point_offset =
6672 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6673 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6674 int16_t offset_low = Low16Bits(offset);
6675 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6676 // extension in lw.
6677 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6678 Register base = short_offset ? obj : TMP;
6679 // Loading the entrypoint does not require a load acquire since it is only changed when
6680 // threads are suspended or running a checkpoint.
6681 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6682 reordering = __ SetReorder(false);
6683 if (!short_offset) {
6684 DCHECK(!label_low);
6685 __ AddUpper(base, obj, offset_high);
6686 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006687 MipsLabel skip_call;
6688 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006689 if (label_low != nullptr) {
6690 DCHECK(short_offset);
6691 __ Bind(label_low);
6692 }
6693 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6694 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6695 // in delay slot.
6696 if (isR6) {
6697 __ Jialc(T9, thunk_disp);
6698 } else {
6699 __ Addiu(T9, T9, thunk_disp);
6700 __ Jalr(T9);
6701 __ Nop();
6702 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006703 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006704 __ SetReorder(reordering);
6705 } else {
6706 // Note that we do not actually check the value of `GetIsGcMarking()`
6707 // to decide whether to mark the loaded GC root or not. Instead, we
6708 // load into `temp` (T9) the read barrier mark entry point corresponding
6709 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6710 // is false, and vice versa.
6711 //
6712 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6713 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6714 // if (temp != null) {
6715 // root = temp(root)
6716 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006717
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006718 if (label_low != nullptr) {
6719 reordering = __ SetReorder(false);
6720 __ Bind(label_low);
6721 }
6722 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6723 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6724 if (label_low != nullptr) {
6725 __ SetReorder(reordering);
6726 }
6727 static_assert(
6728 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6729 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6730 "have different sizes.");
6731 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6732 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6733 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006734
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006735 // Slow path marking the GC root `root`.
6736 Location temp = Location::RegisterLocation(T9);
6737 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006738 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006739 instruction,
6740 root,
6741 /*entrypoint*/ temp);
6742 codegen_->AddSlowPath(slow_path);
6743
6744 const int32_t entry_point_offset =
6745 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6746 // Loading the entrypoint does not require a load acquire since it is only changed when
6747 // threads are suspended or running a checkpoint.
6748 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6749 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6750 __ Bind(slow_path->GetExitLabel());
6751 }
Alexey Frunze15958152017-02-09 19:08:30 -08006752 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006753 if (label_low != nullptr) {
6754 reordering = __ SetReorder(false);
6755 __ Bind(label_low);
6756 }
Alexey Frunze15958152017-02-09 19:08:30 -08006757 // GC root loaded through a slow path for read barriers other
6758 // than Baker's.
6759 // /* GcRoot<mirror::Object>* */ root = obj + offset
6760 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006761 if (label_low != nullptr) {
6762 __ SetReorder(reordering);
6763 }
Alexey Frunze15958152017-02-09 19:08:30 -08006764 // /* mirror::Object* */ root = root->Read()
6765 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6766 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006767 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006768 if (label_low != nullptr) {
6769 reordering = __ SetReorder(false);
6770 __ Bind(label_low);
6771 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006772 // Plain GC root load with no read barrier.
6773 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6774 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6775 // Note that GC roots are not affected by heap poisoning, thus we
6776 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006777 if (label_low != nullptr) {
6778 __ SetReorder(reordering);
6779 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006780 }
6781}
6782
Alexey Frunze15958152017-02-09 19:08:30 -08006783void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6784 Location ref,
6785 Register obj,
6786 uint32_t offset,
6787 Location temp,
6788 bool needs_null_check) {
6789 DCHECK(kEmitCompilerReadBarrier);
6790 DCHECK(kUseBakerReadBarrier);
6791
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006792 if (kBakerReadBarrierThunksEnableForFields) {
6793 // Note that we do not actually check the value of `GetIsGcMarking()`
6794 // to decide whether to mark the loaded reference or not. Instead, we
6795 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6796 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6797 // vice versa.
6798 //
6799 // We use thunks for the slow path. That thunk checks the reference
6800 // and jumps to the entrypoint if needed. If the holder is not gray,
6801 // it issues a load-load memory barrier and returns to the original
6802 // reference load.
6803 //
6804 // temp = Thread::Current()->pReadBarrierMarkReg00
6805 // // AKA &art_quick_read_barrier_mark_introspection.
6806 // if (temp != nullptr) {
6807 // temp = &field_array_thunk<holder_reg>
6808 // temp()
6809 // }
6810 // not_gray_return_address:
6811 // // If the offset is too large to fit into the lw instruction, we
6812 // // use an adjusted base register (TMP) here. This register
6813 // // receives bits 16 ... 31 of the offset before the thunk invocation
6814 // // and the thunk benefits from it.
6815 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6816 // gray_return_address:
6817
6818 DCHECK(temp.IsInvalid());
6819 bool isR6 = GetInstructionSetFeatures().IsR6();
6820 int16_t offset_low = Low16Bits(offset);
6821 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6822 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6823 bool reordering = __ SetReorder(false);
6824 const int32_t entry_point_offset =
6825 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6826 // There may have or may have not been a null check if the field offset is smaller than
6827 // the page size.
6828 // There must've been a null check in case it's actually a load from an array.
6829 // We will, however, perform an explicit null check in the thunk as it's easier to
6830 // do it than not.
6831 if (instruction->IsArrayGet()) {
6832 DCHECK(!needs_null_check);
6833 }
6834 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6835 // Loading the entrypoint does not require a load acquire since it is only changed when
6836 // threads are suspended or running a checkpoint.
6837 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6838 Register ref_reg = ref.AsRegister<Register>();
6839 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006840 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006841 if (short_offset) {
6842 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006843 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006844 __ Nop(); // In forbidden slot.
6845 __ Jialc(T9, thunk_disp);
6846 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006847 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006848 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6849 __ Jalr(T9);
6850 __ Nop(); // In delay slot.
6851 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006852 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006853 } else {
6854 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006855 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006856 __ Aui(base, obj, offset_high); // In delay slot.
6857 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006858 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006859 } else {
6860 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006861 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006862 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6863 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006864 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006865 __ Addu(base, base, obj); // In delay slot.
6866 }
6867 }
6868 // /* HeapReference<Object> */ ref = *(obj + offset)
6869 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6870 if (needs_null_check) {
6871 MaybeRecordImplicitNullCheck(instruction);
6872 }
6873 __ MaybeUnpoisonHeapReference(ref_reg);
6874 __ SetReorder(reordering);
6875 return;
6876 }
6877
Alexey Frunze15958152017-02-09 19:08:30 -08006878 // /* HeapReference<Object> */ ref = *(obj + offset)
6879 Location no_index = Location::NoLocation();
6880 ScaleFactor no_scale_factor = TIMES_1;
6881 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6882 ref,
6883 obj,
6884 offset,
6885 no_index,
6886 no_scale_factor,
6887 temp,
6888 needs_null_check);
6889}
6890
6891void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6892 Location ref,
6893 Register obj,
6894 uint32_t data_offset,
6895 Location index,
6896 Location temp,
6897 bool needs_null_check) {
6898 DCHECK(kEmitCompilerReadBarrier);
6899 DCHECK(kUseBakerReadBarrier);
6900
6901 static_assert(
6902 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6903 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904 ScaleFactor scale_factor = TIMES_4;
6905
6906 if (kBakerReadBarrierThunksEnableForArrays) {
6907 // Note that we do not actually check the value of `GetIsGcMarking()`
6908 // to decide whether to mark the loaded reference or not. Instead, we
6909 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6910 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6911 // vice versa.
6912 //
6913 // We use thunks for the slow path. That thunk checks the reference
6914 // and jumps to the entrypoint if needed. If the holder is not gray,
6915 // it issues a load-load memory barrier and returns to the original
6916 // reference load.
6917 //
6918 // temp = Thread::Current()->pReadBarrierMarkReg00
6919 // // AKA &art_quick_read_barrier_mark_introspection.
6920 // if (temp != nullptr) {
6921 // temp = &field_array_thunk<holder_reg>
6922 // temp()
6923 // }
6924 // not_gray_return_address:
6925 // // The element address is pre-calculated in the TMP register before the
6926 // // thunk invocation and the thunk benefits from it.
6927 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6928 // gray_return_address:
6929
6930 DCHECK(temp.IsInvalid());
6931 DCHECK(index.IsValid());
6932 bool reordering = __ SetReorder(false);
6933 const int32_t entry_point_offset =
6934 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6935 // We will not do the explicit null check in the thunk as some form of a null check
6936 // must've been done earlier.
6937 DCHECK(!needs_null_check);
6938 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6939 // Loading the entrypoint does not require a load acquire since it is only changed when
6940 // threads are suspended or running a checkpoint.
6941 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6942 Register ref_reg = ref.AsRegister<Register>();
6943 Register index_reg = index.IsRegisterPair()
6944 ? index.AsRegisterPairLow<Register>()
6945 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006946 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006947 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006948 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006949 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6950 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006951 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006952 } else {
6953 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006954 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006955 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6956 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006957 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006958 __ Addu(TMP, TMP, obj); // In delay slot.
6959 }
6960 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6961 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6962 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6963 __ MaybeUnpoisonHeapReference(ref_reg);
6964 __ SetReorder(reordering);
6965 return;
6966 }
6967
Alexey Frunze15958152017-02-09 19:08:30 -08006968 // /* HeapReference<Object> */ ref =
6969 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006970 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6971 ref,
6972 obj,
6973 data_offset,
6974 index,
6975 scale_factor,
6976 temp,
6977 needs_null_check);
6978}
6979
6980void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6981 Location ref,
6982 Register obj,
6983 uint32_t offset,
6984 Location index,
6985 ScaleFactor scale_factor,
6986 Location temp,
6987 bool needs_null_check,
6988 bool always_update_field) {
6989 DCHECK(kEmitCompilerReadBarrier);
6990 DCHECK(kUseBakerReadBarrier);
6991
6992 // In slow path based read barriers, the read barrier call is
6993 // inserted after the original load. However, in fast path based
6994 // Baker's read barriers, we need to perform the load of
6995 // mirror::Object::monitor_ *before* the original reference load.
6996 // This load-load ordering is required by the read barrier.
6997 // The fast path/slow path (for Baker's algorithm) should look like:
6998 //
6999 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7000 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7001 // HeapReference<Object> ref = *src; // Original reference load.
7002 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7003 // if (is_gray) {
7004 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7005 // }
7006 //
7007 // Note: the original implementation in ReadBarrier::Barrier is
7008 // slightly more complex as it performs additional checks that we do
7009 // not do here for performance reasons.
7010
7011 Register ref_reg = ref.AsRegister<Register>();
7012 Register temp_reg = temp.AsRegister<Register>();
7013 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7014
7015 // /* int32_t */ monitor = obj->monitor_
7016 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7017 if (needs_null_check) {
7018 MaybeRecordImplicitNullCheck(instruction);
7019 }
7020 // /* LockWord */ lock_word = LockWord(monitor)
7021 static_assert(sizeof(LockWord) == sizeof(int32_t),
7022 "art::LockWord and int32_t have different sizes.");
7023
7024 __ Sync(0); // Barrier to prevent load-load reordering.
7025
7026 // The actual reference load.
7027 if (index.IsValid()) {
7028 // Load types involving an "index": ArrayGet,
7029 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7030 // intrinsics.
7031 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7032 if (index.IsConstant()) {
7033 size_t computed_offset =
7034 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7035 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7036 } else {
7037 // Handle the special case of the
7038 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7039 // intrinsics, which use a register pair as index ("long
7040 // offset"), of which only the low part contains data.
7041 Register index_reg = index.IsRegisterPair()
7042 ? index.AsRegisterPairLow<Register>()
7043 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007044 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007045 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7046 }
7047 } else {
7048 // /* HeapReference<Object> */ ref = *(obj + offset)
7049 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7050 }
7051
7052 // Object* ref = ref_addr->AsMirrorPtr()
7053 __ MaybeUnpoisonHeapReference(ref_reg);
7054
7055 // Slow path marking the object `ref` when it is gray.
7056 SlowPathCodeMIPS* slow_path;
7057 if (always_update_field) {
7058 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7059 // of the form `obj + field_offset`, where `obj` is a register and
7060 // `field_offset` is a register pair (of which only the lower half
7061 // is used). Thus `offset` and `scale_factor` above are expected
7062 // to be null in this code path.
7063 DCHECK_EQ(offset, 0u);
7064 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007065 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007066 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7067 ref,
7068 obj,
7069 /* field_offset */ index,
7070 temp_reg);
7071 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007072 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007073 }
7074 AddSlowPath(slow_path);
7075
7076 // if (rb_state == ReadBarrier::GrayState())
7077 // ref = ReadBarrier::Mark(ref);
7078 // Given the numeric representation, it's enough to check the low bit of the
7079 // rb_state. We do that by shifting the bit into the sign bit (31) and
7080 // performing a branch on less than zero.
7081 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7082 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7083 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7084 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7085 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7086 __ Bind(slow_path->GetExitLabel());
7087}
7088
7089void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7090 Location out,
7091 Location ref,
7092 Location obj,
7093 uint32_t offset,
7094 Location index) {
7095 DCHECK(kEmitCompilerReadBarrier);
7096
7097 // Insert a slow path based read barrier *after* the reference load.
7098 //
7099 // If heap poisoning is enabled, the unpoisoning of the loaded
7100 // reference will be carried out by the runtime within the slow
7101 // path.
7102 //
7103 // Note that `ref` currently does not get unpoisoned (when heap
7104 // poisoning is enabled), which is alright as the `ref` argument is
7105 // not used by the artReadBarrierSlow entry point.
7106 //
7107 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007108 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007109 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7110 AddSlowPath(slow_path);
7111
7112 __ B(slow_path->GetEntryLabel());
7113 __ Bind(slow_path->GetExitLabel());
7114}
7115
7116void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7117 Location out,
7118 Location ref,
7119 Location obj,
7120 uint32_t offset,
7121 Location index) {
7122 if (kEmitCompilerReadBarrier) {
7123 // Baker's read barriers shall be handled by the fast path
7124 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7125 DCHECK(!kUseBakerReadBarrier);
7126 // If heap poisoning is enabled, unpoisoning will be taken care of
7127 // by the runtime within the slow path.
7128 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7129 } else if (kPoisonHeapReferences) {
7130 __ UnpoisonHeapReference(out.AsRegister<Register>());
7131 }
7132}
7133
7134void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7135 Location out,
7136 Location root) {
7137 DCHECK(kEmitCompilerReadBarrier);
7138
7139 // Insert a slow path based read barrier *after* the GC root load.
7140 //
7141 // Note that GC roots are not affected by heap poisoning, so we do
7142 // not need to do anything special for this here.
7143 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007144 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007145 AddSlowPath(slow_path);
7146
7147 __ B(slow_path->GetEntryLabel());
7148 __ Bind(slow_path->GetExitLabel());
7149}
7150
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007151void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007152 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7153 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007154 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007155 switch (type_check_kind) {
7156 case TypeCheckKind::kExactCheck:
7157 case TypeCheckKind::kAbstractClassCheck:
7158 case TypeCheckKind::kClassHierarchyCheck:
7159 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007160 call_kind =
7161 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007162 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007163 break;
7164 case TypeCheckKind::kArrayCheck:
7165 case TypeCheckKind::kUnresolvedCheck:
7166 case TypeCheckKind::kInterfaceCheck:
7167 call_kind = LocationSummary::kCallOnSlowPath;
7168 break;
7169 }
7170
Vladimir Markoca6fff82017-10-03 14:49:14 +01007171 LocationSummary* locations =
7172 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007173 if (baker_read_barrier_slow_path) {
7174 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7175 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176 locations->SetInAt(0, Location::RequiresRegister());
7177 locations->SetInAt(1, Location::RequiresRegister());
7178 // The output does overlap inputs.
7179 // Note that TypeCheckSlowPathMIPS uses this register too.
7180 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007181 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007182}
7183
7184void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007185 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007186 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007187 Location obj_loc = locations->InAt(0);
7188 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007189 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007190 Location out_loc = locations->Out();
7191 Register out = out_loc.AsRegister<Register>();
7192 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7193 DCHECK_LE(num_temps, 1u);
7194 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007195 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7196 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7197 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7198 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007200 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007201
7202 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007203 // Avoid this check if we know `obj` is not null.
7204 if (instruction->MustDoNullCheck()) {
7205 __ Move(out, ZERO);
7206 __ Beqz(obj, &done);
7207 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007208
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007209 switch (type_check_kind) {
7210 case TypeCheckKind::kExactCheck: {
7211 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007212 GenerateReferenceLoadTwoRegisters(instruction,
7213 out_loc,
7214 obj_loc,
7215 class_offset,
7216 maybe_temp_loc,
7217 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007218 // Classes must be equal for the instanceof to succeed.
7219 __ Xor(out, out, cls);
7220 __ Sltiu(out, out, 1);
7221 break;
7222 }
7223
7224 case TypeCheckKind::kAbstractClassCheck: {
7225 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007226 GenerateReferenceLoadTwoRegisters(instruction,
7227 out_loc,
7228 obj_loc,
7229 class_offset,
7230 maybe_temp_loc,
7231 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007232 // If the class is abstract, we eagerly fetch the super class of the
7233 // object to avoid doing a comparison we know will fail.
7234 MipsLabel loop;
7235 __ Bind(&loop);
7236 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007237 GenerateReferenceLoadOneRegister(instruction,
7238 out_loc,
7239 super_offset,
7240 maybe_temp_loc,
7241 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007242 // If `out` is null, we use it for the result, and jump to `done`.
7243 __ Beqz(out, &done);
7244 __ Bne(out, cls, &loop);
7245 __ LoadConst32(out, 1);
7246 break;
7247 }
7248
7249 case TypeCheckKind::kClassHierarchyCheck: {
7250 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007251 GenerateReferenceLoadTwoRegisters(instruction,
7252 out_loc,
7253 obj_loc,
7254 class_offset,
7255 maybe_temp_loc,
7256 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007257 // Walk over the class hierarchy to find a match.
7258 MipsLabel loop, success;
7259 __ Bind(&loop);
7260 __ Beq(out, cls, &success);
7261 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007262 GenerateReferenceLoadOneRegister(instruction,
7263 out_loc,
7264 super_offset,
7265 maybe_temp_loc,
7266 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007267 __ Bnez(out, &loop);
7268 // If `out` is null, we use it for the result, and jump to `done`.
7269 __ B(&done);
7270 __ Bind(&success);
7271 __ LoadConst32(out, 1);
7272 break;
7273 }
7274
7275 case TypeCheckKind::kArrayObjectCheck: {
7276 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007277 GenerateReferenceLoadTwoRegisters(instruction,
7278 out_loc,
7279 obj_loc,
7280 class_offset,
7281 maybe_temp_loc,
7282 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007283 // Do an exact check.
7284 MipsLabel success;
7285 __ Beq(out, cls, &success);
7286 // Otherwise, we need to check that the object's class is a non-primitive array.
7287 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007288 GenerateReferenceLoadOneRegister(instruction,
7289 out_loc,
7290 component_offset,
7291 maybe_temp_loc,
7292 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007293 // If `out` is null, we use it for the result, and jump to `done`.
7294 __ Beqz(out, &done);
7295 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7296 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7297 __ Sltiu(out, out, 1);
7298 __ B(&done);
7299 __ Bind(&success);
7300 __ LoadConst32(out, 1);
7301 break;
7302 }
7303
7304 case TypeCheckKind::kArrayCheck: {
7305 // No read barrier since the slow path will retry upon failure.
7306 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007307 GenerateReferenceLoadTwoRegisters(instruction,
7308 out_loc,
7309 obj_loc,
7310 class_offset,
7311 maybe_temp_loc,
7312 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007313 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007314 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7315 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007316 codegen_->AddSlowPath(slow_path);
7317 __ Bne(out, cls, slow_path->GetEntryLabel());
7318 __ LoadConst32(out, 1);
7319 break;
7320 }
7321
7322 case TypeCheckKind::kUnresolvedCheck:
7323 case TypeCheckKind::kInterfaceCheck: {
7324 // Note that we indeed only call on slow path, but we always go
7325 // into the slow path for the unresolved and interface check
7326 // cases.
7327 //
7328 // We cannot directly call the InstanceofNonTrivial runtime
7329 // entry point without resorting to a type checking slow path
7330 // here (i.e. by calling InvokeRuntime directly), as it would
7331 // require to assign fixed registers for the inputs of this
7332 // HInstanceOf instruction (following the runtime calling
7333 // convention), which might be cluttered by the potential first
7334 // read barrier emission at the beginning of this method.
7335 //
7336 // TODO: Introduce a new runtime entry point taking the object
7337 // to test (instead of its class) as argument, and let it deal
7338 // with the read barrier issues. This will let us refactor this
7339 // case of the `switch` code as it was previously (with a direct
7340 // call to the runtime not using a type checking slow path).
7341 // This should also be beneficial for the other cases above.
7342 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007343 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7344 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007345 codegen_->AddSlowPath(slow_path);
7346 __ B(slow_path->GetEntryLabel());
7347 break;
7348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007349 }
7350
7351 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007352
7353 if (slow_path != nullptr) {
7354 __ Bind(slow_path->GetExitLabel());
7355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007356}
7357
7358void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007359 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007360 locations->SetOut(Location::ConstantLocation(constant));
7361}
7362
7363void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7364 // Will be generated at use site.
7365}
7366
7367void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007368 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007369 locations->SetOut(Location::ConstantLocation(constant));
7370}
7371
7372void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7373 // Will be generated at use site.
7374}
7375
7376void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7377 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7378 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7379}
7380
7381void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7382 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007383 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007384 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007385 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007386}
7387
7388void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7389 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7390 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007391 Location receiver = invoke->GetLocations()->InAt(0);
7392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007393 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007394
7395 // Set the hidden argument.
7396 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7397 invoke->GetDexMethodIndex());
7398
7399 // temp = object->GetClass();
7400 if (receiver.IsStackSlot()) {
7401 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7402 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7403 } else {
7404 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7405 }
7406 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007407 // Instead of simply (possibly) unpoisoning `temp` here, we should
7408 // emit a read barrier for the previous class reference load.
7409 // However this is not required in practice, as this is an
7410 // intermediate/temporary reference and because the current
7411 // concurrent copying collector keeps the from-space memory
7412 // intact/accessible until the end of the marking phase (the
7413 // concurrent copying collector may not in the future).
7414 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007415 __ LoadFromOffset(kLoadWord, temp, temp,
7416 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7417 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007418 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007419 // temp = temp->GetImtEntryAt(method_offset);
7420 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7421 // T9 = temp->GetEntryPoint();
7422 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7423 // T9();
7424 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007425 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007426 DCHECK(!codegen_->IsLeafMethod());
7427 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7428}
7429
7430void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007431 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7432 if (intrinsic.TryDispatch(invoke)) {
7433 return;
7434 }
7435
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007436 HandleInvoke(invoke);
7437}
7438
7439void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007440 // Explicit clinit checks triggered by static invokes must have been pruned by
7441 // art::PrepareForRegisterAllocation.
7442 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007443
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007444 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007445 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7446 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007447
Chris Larsen701566a2015-10-27 15:29:13 -07007448 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7449 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007450 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7451 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7452 }
Chris Larsen701566a2015-10-27 15:29:13 -07007453 return;
7454 }
7455
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007456 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007457
7458 // Add the extra input register if either the dex cache array base register
7459 // or the PC-relative base register for accessing literals is needed.
7460 if (has_extra_input) {
7461 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7462 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007463}
7464
Orion Hodsonac141392017-01-13 11:53:47 +00007465void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7466 HandleInvoke(invoke);
7467}
7468
7469void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7470 codegen_->GenerateInvokePolymorphicCall(invoke);
7471}
7472
Chris Larsen701566a2015-10-27 15:29:13 -07007473static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007474 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007475 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7476 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007477 return true;
7478 }
7479 return false;
7480}
7481
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007482HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007483 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007484 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007485 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007486 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007487 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007488 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007489 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007490 case HLoadString::LoadKind::kJitTableAddress:
7491 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007492 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007493 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007494 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007495 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007496 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007497 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007498}
7499
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007500HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7501 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007502 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007503 case HLoadClass::LoadKind::kInvalid:
7504 LOG(FATAL) << "UNREACHABLE";
7505 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007506 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007507 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007508 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007509 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007510 case HLoadClass::LoadKind::kBssEntry:
7511 DCHECK(!Runtime::Current()->UseJitCompilation());
7512 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007513 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007514 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007515 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007516 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007517 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007518 break;
7519 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007520 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007521}
7522
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007523Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7524 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007525 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007526 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007527 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7528 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7529 if (!invoke->GetLocations()->Intrinsified()) {
7530 return location.AsRegister<Register>();
7531 }
7532 // For intrinsics we allow any location, so it may be on the stack.
7533 if (!location.IsRegister()) {
7534 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7535 return temp;
7536 }
7537 // For register locations, check if the register was saved. If so, get it from the stack.
7538 // Note: There is a chance that the register was saved but not overwritten, so we could
7539 // save one load. However, since this is just an intrinsic slow path we prefer this
7540 // simple and more robust approach rather that trying to determine if that's the case.
7541 SlowPathCode* slow_path = GetCurrentSlowPath();
7542 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7543 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7544 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7545 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7546 return temp;
7547 }
7548 return location.AsRegister<Register>();
7549}
7550
Vladimir Markodc151b22015-10-15 18:02:30 +01007551HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7552 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007553 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007554 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007555}
7556
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007557void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7558 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007559 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007560 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007561 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7562 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007563 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007564 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7565 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007566 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7567 : ZERO;
7568
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007569 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007570 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007572 uint32_t offset =
7573 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007574 __ LoadFromOffset(kLoadWord,
7575 temp.AsRegister<Register>(),
7576 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007577 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007578 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007579 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007580 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007581 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007582 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007583 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7584 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007585 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7586 PcRelativePatchInfo* info_low =
7587 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007588 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007589 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7590 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007591 break;
7592 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007593 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7594 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7595 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007596 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007597 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007598 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007599 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7600 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007601 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007602 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7603 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007604 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007605 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007606 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7607 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7608 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 }
7610 }
7611
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007612 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007613 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007614 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007615 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007616 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7617 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007618 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619 T9,
7620 callee_method.AsRegister<Register>(),
7621 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007622 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007623 // T9()
7624 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007625 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007626 break;
7627 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007628 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7629
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007630 DCHECK(!IsLeafMethod());
7631}
7632
7633void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007634 // Explicit clinit checks triggered by static invokes must have been pruned by
7635 // art::PrepareForRegisterAllocation.
7636 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007637
7638 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7639 return;
7640 }
7641
7642 LocationSummary* locations = invoke->GetLocations();
7643 codegen_->GenerateStaticOrDirectCall(invoke,
7644 locations->HasTemps()
7645 ? locations->GetTemp(0)
7646 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007647}
7648
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007649void CodeGeneratorMIPS::GenerateVirtualCall(
7650 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007651 // Use the calling convention instead of the location of the receiver, as
7652 // intrinsics may have put the receiver in a different register. In the intrinsics
7653 // slow path, the arguments have been moved to the right place, so here we are
7654 // guaranteed that the receiver is the first register of the calling convention.
7655 InvokeDexCallingConvention calling_convention;
7656 Register receiver = calling_convention.GetRegisterAt(0);
7657
Chris Larsen3acee732015-11-18 13:31:08 -08007658 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007659 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7660 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7661 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007662 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007663
7664 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007665 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007666 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007667 // Instead of simply (possibly) unpoisoning `temp` here, we should
7668 // emit a read barrier for the previous class reference load.
7669 // However this is not required in practice, as this is an
7670 // intermediate/temporary reference and because the current
7671 // concurrent copying collector keeps the from-space memory
7672 // intact/accessible until the end of the marking phase (the
7673 // concurrent copying collector may not in the future).
7674 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007675 // temp = temp->GetMethodAt(method_offset);
7676 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7677 // T9 = temp->GetEntryPoint();
7678 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7679 // T9();
7680 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007681 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007682 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007683}
7684
7685void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7686 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7687 return;
7688 }
7689
7690 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007691 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007692}
7693
7694void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007695 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007696 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007697 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007698 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7699 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007700 return;
7701 }
Vladimir Marko41559982017-01-06 14:04:23 +00007702 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007703 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007704 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007705 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7706 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007707 ? LocationSummary::kCallOnSlowPath
7708 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007709 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007710 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7711 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7712 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007713 switch (load_kind) {
7714 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007715 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007716 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007717 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007718 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007719 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007720 break;
7721 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007722 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007723 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7724 codegen_->ClobberRA();
7725 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007726 break;
7727 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007728 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007729 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007730 locations->SetInAt(0, Location::RequiresRegister());
7731 break;
7732 default:
7733 break;
7734 }
7735 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007736 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7737 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7738 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007739 // Request a temp to hold the BSS entry location for the slow path.
7740 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007741 RegisterSet caller_saves = RegisterSet::Empty();
7742 InvokeRuntimeCallingConvention calling_convention;
7743 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7744 locations->SetCustomSlowPathCallerSaves(caller_saves);
7745 } else {
7746 // For non-Baker read barriers we have a temp-clobbering call.
7747 }
7748 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007749}
7750
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007751// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7752// move.
7753void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007754 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007755 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007756 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007757 return;
7758 }
Vladimir Marko41559982017-01-06 14:04:23 +00007759 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007760
Vladimir Marko41559982017-01-06 14:04:23 +00007761 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007762 Location out_loc = locations->Out();
7763 Register out = out_loc.AsRegister<Register>();
7764 Register base_or_current_method_reg;
7765 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007766 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007767 switch (load_kind) {
7768 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007769 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007770 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007771 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007772 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007773 base_or_current_method_reg =
7774 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007775 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007776 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007777 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007778 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7779 break;
7780 default:
7781 base_or_current_method_reg = ZERO;
7782 break;
7783 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007784
Alexey Frunze15958152017-02-09 19:08:30 -08007785 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7786 ? kWithoutReadBarrier
7787 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007788 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007789 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007790 switch (load_kind) {
7791 case HLoadClass::LoadKind::kReferrersClass: {
7792 DCHECK(!cls->CanCallRuntime());
7793 DCHECK(!cls->MustGenerateClinitCheck());
7794 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7795 GenerateGcRootFieldLoad(cls,
7796 out_loc,
7797 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007798 ArtMethod::DeclaringClassOffset().Int32Value(),
7799 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007800 break;
7801 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007802 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007803 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007804 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007805 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007806 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007807 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7808 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007809 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7810 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007811 base_or_current_method_reg);
7812 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007813 break;
7814 }
7815 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007816 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007817 uint32_t address = dchecked_integral_cast<uint32_t>(
7818 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7819 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007820 if (isR6 || !has_irreducible_loops) {
7821 __ LoadLiteral(out,
7822 base_or_current_method_reg,
7823 codegen_->DeduplicateBootImageAddressLiteral(address));
7824 } else {
7825 __ LoadConst32(out, address);
7826 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007827 break;
7828 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007829 case HLoadClass::LoadKind::kBootImageClassTable: {
7830 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7831 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7832 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7833 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7834 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7835 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7836 out,
7837 base_or_current_method_reg);
7838 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7839 // Extract the reference from the slot data, i.e. clear the hash bits.
7840 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7841 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7842 if (masked_hash != 0) {
7843 __ Addiu(out, out, -masked_hash);
7844 }
7845 break;
7846 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007847 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007848 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7849 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7850 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007851 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007852 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007853 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7854 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007855 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007856 GenerateGcRootFieldLoad(cls,
7857 out_loc,
7858 temp,
7859 /* placeholder */ 0x5678,
7860 read_barrier_option,
7861 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007862 generate_null_check = true;
7863 break;
7864 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007865 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007866 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7867 cls->GetTypeIndex(),
7868 cls->GetClass());
7869 bool reordering = __ SetReorder(false);
7870 __ Bind(&info->high_label);
7871 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007872 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007873 GenerateGcRootFieldLoad(cls,
7874 out_loc,
7875 out,
7876 /* placeholder */ 0x5678,
7877 read_barrier_option,
7878 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007879 break;
7880 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007881 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007882 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007883 LOG(FATAL) << "UNREACHABLE";
7884 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 }
7886
7887 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7888 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007889 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007890 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007891 codegen_->AddSlowPath(slow_path);
7892 if (generate_null_check) {
7893 __ Beqz(out, slow_path->GetEntryLabel());
7894 }
7895 if (cls->MustGenerateClinitCheck()) {
7896 GenerateClassInitializationCheck(slow_path, out);
7897 } else {
7898 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007899 }
7900 }
7901}
7902
7903static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007904 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007905}
7906
7907void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7908 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007909 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007910 locations->SetOut(Location::RequiresRegister());
7911}
7912
7913void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7914 Register out = load->GetLocations()->Out().AsRegister<Register>();
7915 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7916}
7917
7918void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007919 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007920}
7921
7922void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7923 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7924}
7925
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007926void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007927 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007928 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007929 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007930 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007931 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007932 switch (load_kind) {
7933 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007934 case HLoadString::LoadKind::kBootImageAddress:
7935 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007936 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007937 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007938 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007939 break;
7940 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007941 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007942 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7943 codegen_->ClobberRA();
7944 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007945 break;
7946 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007947 FALLTHROUGH_INTENDED;
7948 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007949 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007950 locations->SetInAt(0, Location::RequiresRegister());
7951 break;
7952 default:
7953 break;
7954 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007955 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007956 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007957 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007958 } else {
7959 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007960 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7961 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7962 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007963 // Request a temp to hold the BSS entry location for the slow path.
7964 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007965 RegisterSet caller_saves = RegisterSet::Empty();
7966 InvokeRuntimeCallingConvention calling_convention;
7967 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7968 locations->SetCustomSlowPathCallerSaves(caller_saves);
7969 } else {
7970 // For non-Baker read barriers we have a temp-clobbering call.
7971 }
7972 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007973 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007974}
7975
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007976// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7977// move.
7978void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007979 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007980 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007981 Location out_loc = locations->Out();
7982 Register out = out_loc.AsRegister<Register>();
7983 Register base_or_current_method_reg;
7984 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007985 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007986 switch (load_kind) {
7987 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007988 case HLoadString::LoadKind::kBootImageAddress:
7989 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007990 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007991 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007992 base_or_current_method_reg =
7993 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007994 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007995 default:
7996 base_or_current_method_reg = ZERO;
7997 break;
7998 }
7999
8000 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008001 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008002 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008003 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008004 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008005 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8006 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008007 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8008 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008009 base_or_current_method_reg);
8010 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008011 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008012 }
8013 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008014 uint32_t address = dchecked_integral_cast<uint32_t>(
8015 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8016 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008017 if (isR6 || !has_irreducible_loops) {
8018 __ LoadLiteral(out,
8019 base_or_current_method_reg,
8020 codegen_->DeduplicateBootImageAddressLiteral(address));
8021 } else {
8022 __ LoadConst32(out, address);
8023 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008024 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008025 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008026 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008027 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008028 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008029 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008030 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8031 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008032 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8033 out,
8034 base_or_current_method_reg);
8035 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8036 return;
8037 }
8038 case HLoadString::LoadKind::kBssEntry: {
8039 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8040 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8041 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8042 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8043 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008044 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008045 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008046 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8047 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008048 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008049 GenerateGcRootFieldLoad(load,
8050 out_loc,
8051 temp,
8052 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008053 kCompilerReadBarrierOption,
8054 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008055 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008056 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008057 codegen_->AddSlowPath(slow_path);
8058 __ Beqz(out, slow_path->GetEntryLabel());
8059 __ Bind(slow_path->GetExitLabel());
8060 return;
8061 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008062 case HLoadString::LoadKind::kJitTableAddress: {
8063 CodeGeneratorMIPS::JitPatchInfo* info =
8064 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8065 load->GetStringIndex(),
8066 load->GetString());
8067 bool reordering = __ SetReorder(false);
8068 __ Bind(&info->high_label);
8069 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008070 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008071 GenerateGcRootFieldLoad(load,
8072 out_loc,
8073 out,
8074 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008075 kCompilerReadBarrierOption,
8076 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008077 return;
8078 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008079 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008080 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008081 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008082
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008083 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008084 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008085 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008086 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008087 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008088 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8089 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008090}
8091
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008092void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008093 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008094 locations->SetOut(Location::ConstantLocation(constant));
8095}
8096
8097void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8098 // Will be generated at use site.
8099}
8100
8101void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008102 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8103 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008104 InvokeRuntimeCallingConvention calling_convention;
8105 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8106}
8107
8108void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8109 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008110 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008111 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8112 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008113 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008114 }
8115 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8116}
8117
8118void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8119 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008120 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008121 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008122 case DataType::Type::kInt32:
8123 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008124 locations->SetInAt(0, Location::RequiresRegister());
8125 locations->SetInAt(1, Location::RequiresRegister());
8126 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8127 break;
8128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008129 case DataType::Type::kFloat32:
8130 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008131 locations->SetInAt(0, Location::RequiresFpuRegister());
8132 locations->SetInAt(1, Location::RequiresFpuRegister());
8133 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8134 break;
8135
8136 default:
8137 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8138 }
8139}
8140
8141void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008142 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008143 LocationSummary* locations = instruction->GetLocations();
8144 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8145
8146 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008147 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008148 Register dst = locations->Out().AsRegister<Register>();
8149 Register lhs = locations->InAt(0).AsRegister<Register>();
8150 Register rhs = locations->InAt(1).AsRegister<Register>();
8151
8152 if (isR6) {
8153 __ MulR6(dst, lhs, rhs);
8154 } else {
8155 __ MulR2(dst, lhs, rhs);
8156 }
8157 break;
8158 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008159 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008160 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8161 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8162 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8163 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8164 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8165 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8166
8167 // Extra checks to protect caused by the existance of A1_A2.
8168 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8169 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8170 DCHECK_NE(dst_high, lhs_low);
8171 DCHECK_NE(dst_high, rhs_low);
8172
8173 // A_B * C_D
8174 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8175 // dst_lo: [ low(B*D) ]
8176 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8177
8178 if (isR6) {
8179 __ MulR6(TMP, lhs_high, rhs_low);
8180 __ MulR6(dst_high, lhs_low, rhs_high);
8181 __ Addu(dst_high, dst_high, TMP);
8182 __ MuhuR6(TMP, lhs_low, rhs_low);
8183 __ Addu(dst_high, dst_high, TMP);
8184 __ MulR6(dst_low, lhs_low, rhs_low);
8185 } else {
8186 __ MulR2(TMP, lhs_high, rhs_low);
8187 __ MulR2(dst_high, lhs_low, rhs_high);
8188 __ Addu(dst_high, dst_high, TMP);
8189 __ MultuR2(lhs_low, rhs_low);
8190 __ Mfhi(TMP);
8191 __ Addu(dst_high, dst_high, TMP);
8192 __ Mflo(dst_low);
8193 }
8194 break;
8195 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008196 case DataType::Type::kFloat32:
8197 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008198 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8199 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8200 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008201 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008202 __ MulS(dst, lhs, rhs);
8203 } else {
8204 __ MulD(dst, lhs, rhs);
8205 }
8206 break;
8207 }
8208 default:
8209 LOG(FATAL) << "Unexpected mul type " << type;
8210 }
8211}
8212
8213void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008215 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008216 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008217 case DataType::Type::kInt32:
8218 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008219 locations->SetInAt(0, Location::RequiresRegister());
8220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8221 break;
8222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008223 case DataType::Type::kFloat32:
8224 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008225 locations->SetInAt(0, Location::RequiresFpuRegister());
8226 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8227 break;
8228
8229 default:
8230 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8231 }
8232}
8233
8234void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008235 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008236 LocationSummary* locations = instruction->GetLocations();
8237
8238 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008239 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008240 Register dst = locations->Out().AsRegister<Register>();
8241 Register src = locations->InAt(0).AsRegister<Register>();
8242 __ Subu(dst, ZERO, src);
8243 break;
8244 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008245 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008246 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8247 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8248 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8249 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8250 __ Subu(dst_low, ZERO, src_low);
8251 __ Sltu(TMP, ZERO, dst_low);
8252 __ Subu(dst_high, ZERO, src_high);
8253 __ Subu(dst_high, dst_high, TMP);
8254 break;
8255 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008256 case DataType::Type::kFloat32:
8257 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008258 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8259 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008260 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008261 __ NegS(dst, src);
8262 } else {
8263 __ NegD(dst, src);
8264 }
8265 break;
8266 }
8267 default:
8268 LOG(FATAL) << "Unexpected neg type " << type;
8269 }
8270}
8271
8272void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008273 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8274 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008275 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008276 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008277 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8278 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008279}
8280
8281void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008282 // Note: if heap poisoning is enabled, the entry point takes care
8283 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008284 QuickEntrypointEnum entrypoint =
8285 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8286 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008287 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008288 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008289}
8290
8291void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008292 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8293 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008295 if (instruction->IsStringAlloc()) {
8296 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8297 } else {
8298 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008299 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008300 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008301}
8302
8303void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008304 // Note: if heap poisoning is enabled, the entry point takes care
8305 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008306 if (instruction->IsStringAlloc()) {
8307 // String is allocated through StringFactory. Call NewEmptyString entry point.
8308 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008309 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008310 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8311 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8312 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008313 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008314 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8315 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008316 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008317 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008318 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008319}
8320
8321void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008322 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008323 locations->SetInAt(0, Location::RequiresRegister());
8324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8325}
8326
8327void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008328 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008329 LocationSummary* locations = instruction->GetLocations();
8330
8331 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008332 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008333 Register dst = locations->Out().AsRegister<Register>();
8334 Register src = locations->InAt(0).AsRegister<Register>();
8335 __ Nor(dst, src, ZERO);
8336 break;
8337 }
8338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008339 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008340 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8341 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8342 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8343 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8344 __ Nor(dst_high, src_high, ZERO);
8345 __ Nor(dst_low, src_low, ZERO);
8346 break;
8347 }
8348
8349 default:
8350 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8351 }
8352}
8353
8354void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008355 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008356 locations->SetInAt(0, Location::RequiresRegister());
8357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8358}
8359
8360void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8361 LocationSummary* locations = instruction->GetLocations();
8362 __ Xori(locations->Out().AsRegister<Register>(),
8363 locations->InAt(0).AsRegister<Register>(),
8364 1);
8365}
8366
8367void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008368 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8369 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008370}
8371
Calin Juravle2ae48182016-03-16 14:05:09 +00008372void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8373 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008374 return;
8375 }
8376 Location obj = instruction->GetLocations()->InAt(0);
8377
8378 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008379 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008380}
8381
Calin Juravle2ae48182016-03-16 14:05:09 +00008382void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008383 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008384 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008385
8386 Location obj = instruction->GetLocations()->InAt(0);
8387
8388 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8389}
8390
8391void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008392 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008393}
8394
8395void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8396 HandleBinaryOp(instruction);
8397}
8398
8399void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8400 HandleBinaryOp(instruction);
8401}
8402
8403void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8404 LOG(FATAL) << "Unreachable";
8405}
8406
8407void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008408 if (instruction->GetNext()->IsSuspendCheck() &&
8409 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8410 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8411 // The back edge will generate the suspend check.
8412 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8413 }
8414
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008415 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8416}
8417
8418void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008419 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008420 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8421 if (location.IsStackSlot()) {
8422 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8423 } else if (location.IsDoubleStackSlot()) {
8424 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8425 }
8426 locations->SetOut(location);
8427}
8428
8429void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8430 ATTRIBUTE_UNUSED) {
8431 // Nothing to do, the parameter is already at its location.
8432}
8433
8434void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8435 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008436 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008437 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8438}
8439
8440void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8441 ATTRIBUTE_UNUSED) {
8442 // Nothing to do, the method is already at its location.
8443}
8444
8445void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008446 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008447 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008448 locations->SetInAt(i, Location::Any());
8449 }
8450 locations->SetOut(Location::Any());
8451}
8452
8453void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8454 LOG(FATAL) << "Unreachable";
8455}
8456
8457void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008458 DataType::Type type = rem->GetResultType();
8459 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8460 ? LocationSummary::kNoCall
8461 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008462 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008463
8464 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008465 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008466 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008467 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8469 break;
8470
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008471 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008472 InvokeRuntimeCallingConvention calling_convention;
8473 locations->SetInAt(0, Location::RegisterPairLocation(
8474 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8475 locations->SetInAt(1, Location::RegisterPairLocation(
8476 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8477 locations->SetOut(calling_convention.GetReturnLocation(type));
8478 break;
8479 }
8480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008481 case DataType::Type::kFloat32:
8482 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008483 InvokeRuntimeCallingConvention calling_convention;
8484 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8485 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8486 locations->SetOut(calling_convention.GetReturnLocation(type));
8487 break;
8488 }
8489
8490 default:
8491 LOG(FATAL) << "Unexpected rem type " << type;
8492 }
8493}
8494
8495void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008496 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008497
8498 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008499 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008500 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008501 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008502 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008503 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008504 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8505 break;
8506 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008507 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008508 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008509 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008510 break;
8511 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008512 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008513 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008514 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008515 break;
8516 }
8517 default:
8518 LOG(FATAL) << "Unexpected rem type " << type;
8519 }
8520}
8521
Igor Murashkind01745e2017-04-05 16:40:31 -07008522void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8523 constructor_fence->SetLocations(nullptr);
8524}
8525
8526void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8527 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8528 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8529}
8530
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008531void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8532 memory_barrier->SetLocations(nullptr);
8533}
8534
8535void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8536 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8537}
8538
8539void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008540 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008541 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542 locations->SetInAt(0, MipsReturnLocation(return_type));
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8546 codegen_->GenerateFrameExit();
8547}
8548
8549void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8550 ret->SetLocations(nullptr);
8551}
8552
8553void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8554 codegen_->GenerateFrameExit();
8555}
8556
Alexey Frunze92d90602015-12-18 18:16:36 -08008557void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8558 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008559}
8560
Alexey Frunze92d90602015-12-18 18:16:36 -08008561void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8562 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008563}
8564
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008565void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8566 HandleShift(shl);
8567}
8568
8569void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8570 HandleShift(shl);
8571}
8572
8573void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8574 HandleShift(shr);
8575}
8576
8577void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8578 HandleShift(shr);
8579}
8580
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008581void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8582 HandleBinaryOp(instruction);
8583}
8584
8585void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8586 HandleBinaryOp(instruction);
8587}
8588
8589void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8590 HandleFieldGet(instruction, instruction->GetFieldInfo());
8591}
8592
8593void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8594 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8595}
8596
8597void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8598 HandleFieldSet(instruction, instruction->GetFieldInfo());
8599}
8600
8601void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008602 HandleFieldSet(instruction,
8603 instruction->GetFieldInfo(),
8604 instruction->GetDexPc(),
8605 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606}
8607
8608void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8609 HUnresolvedInstanceFieldGet* instruction) {
8610 FieldAccessCallingConventionMIPS calling_convention;
8611 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8612 instruction->GetFieldType(),
8613 calling_convention);
8614}
8615
8616void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8617 HUnresolvedInstanceFieldGet* instruction) {
8618 FieldAccessCallingConventionMIPS calling_convention;
8619 codegen_->GenerateUnresolvedFieldAccess(instruction,
8620 instruction->GetFieldType(),
8621 instruction->GetFieldIndex(),
8622 instruction->GetDexPc(),
8623 calling_convention);
8624}
8625
8626void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8627 HUnresolvedInstanceFieldSet* instruction) {
8628 FieldAccessCallingConventionMIPS calling_convention;
8629 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8630 instruction->GetFieldType(),
8631 calling_convention);
8632}
8633
8634void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8635 HUnresolvedInstanceFieldSet* instruction) {
8636 FieldAccessCallingConventionMIPS calling_convention;
8637 codegen_->GenerateUnresolvedFieldAccess(instruction,
8638 instruction->GetFieldType(),
8639 instruction->GetFieldIndex(),
8640 instruction->GetDexPc(),
8641 calling_convention);
8642}
8643
8644void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8645 HUnresolvedStaticFieldGet* instruction) {
8646 FieldAccessCallingConventionMIPS calling_convention;
8647 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8648 instruction->GetFieldType(),
8649 calling_convention);
8650}
8651
8652void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8653 HUnresolvedStaticFieldGet* instruction) {
8654 FieldAccessCallingConventionMIPS calling_convention;
8655 codegen_->GenerateUnresolvedFieldAccess(instruction,
8656 instruction->GetFieldType(),
8657 instruction->GetFieldIndex(),
8658 instruction->GetDexPc(),
8659 calling_convention);
8660}
8661
8662void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8663 HUnresolvedStaticFieldSet* instruction) {
8664 FieldAccessCallingConventionMIPS calling_convention;
8665 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8666 instruction->GetFieldType(),
8667 calling_convention);
8668}
8669
8670void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8671 HUnresolvedStaticFieldSet* instruction) {
8672 FieldAccessCallingConventionMIPS calling_convention;
8673 codegen_->GenerateUnresolvedFieldAccess(instruction,
8674 instruction->GetFieldType(),
8675 instruction->GetFieldIndex(),
8676 instruction->GetDexPc(),
8677 calling_convention);
8678}
8679
8680void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008681 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8682 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008683 // In suspend check slow path, usually there are no caller-save registers at all.
8684 // If SIMD instructions are present, however, we force spilling all live SIMD
8685 // registers in full width (since the runtime only saves/restores lower part).
8686 locations->SetCustomSlowPathCallerSaves(
8687 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008688}
8689
8690void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8691 HBasicBlock* block = instruction->GetBlock();
8692 if (block->GetLoopInformation() != nullptr) {
8693 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8694 // The back edge will generate the suspend check.
8695 return;
8696 }
8697 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8698 // The goto will generate the suspend check.
8699 return;
8700 }
8701 GenerateSuspendCheck(instruction, nullptr);
8702}
8703
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008704void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008705 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8706 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008707 InvokeRuntimeCallingConvention calling_convention;
8708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8709}
8710
8711void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008712 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008713 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8714}
8715
8716void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008717 DataType::Type input_type = conversion->GetInputType();
8718 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008719 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8720 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008721 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008723 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8724 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8726 }
8727
8728 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008729 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008730 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8731 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008732 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008733 }
8734
Vladimir Markoca6fff82017-10-03 14:49:14 +01008735 LocationSummary* locations =
8736 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008737
8738 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008739 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008740 locations->SetInAt(0, Location::RequiresFpuRegister());
8741 } else {
8742 locations->SetInAt(0, Location::RequiresRegister());
8743 }
8744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008745 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8747 } else {
8748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8749 }
8750 } else {
8751 InvokeRuntimeCallingConvention calling_convention;
8752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008753 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008754 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8755 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008756 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008757 locations->SetInAt(0, Location::RegisterPairLocation(
8758 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8759 }
8760
8761 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8762 }
8763}
8764
8765void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8766 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008767 DataType::Type result_type = conversion->GetResultType();
8768 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008769 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008770 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008771
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008772 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8773 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008775 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008776 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8777 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8778 Register src = locations->InAt(0).AsRegister<Register>();
8779
Alexey Frunzea871ef12016-06-27 15:20:11 -07008780 if (dst_low != src) {
8781 __ Move(dst_low, src);
8782 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008783 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008784 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008785 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008786 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008787 ? locations->InAt(0).AsRegisterPairLow<Register>()
8788 : locations->InAt(0).AsRegister<Register>();
8789
8790 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008791 case DataType::Type::kUint8:
8792 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008793 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008794 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008795 if (has_sign_extension) {
8796 __ Seb(dst, src);
8797 } else {
8798 __ Sll(dst, src, 24);
8799 __ Sra(dst, dst, 24);
8800 }
8801 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008802 case DataType::Type::kUint16:
8803 __ Andi(dst, src, 0xFFFF);
8804 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008805 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 if (has_sign_extension) {
8807 __ Seh(dst, src);
8808 } else {
8809 __ Sll(dst, src, 16);
8810 __ Sra(dst, dst, 16);
8811 }
8812 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008813 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008814 if (dst != src) {
8815 __ Move(dst, src);
8816 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008817 break;
8818
8819 default:
8820 LOG(FATAL) << "Unexpected type conversion from " << input_type
8821 << " to " << result_type;
8822 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008823 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8824 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008825 if (isR6) {
8826 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8827 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8828 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8829 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8830 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8831 __ Mtc1(src_low, FTMP);
8832 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008833 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008834 __ Cvtsl(dst, FTMP);
8835 } else {
8836 __ Cvtdl(dst, FTMP);
8837 }
8838 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008839 QuickEntrypointEnum entrypoint =
8840 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008841 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008843 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8844 } else {
8845 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8846 }
8847 }
8848 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008849 Register src = locations->InAt(0).AsRegister<Register>();
8850 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8851 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008852 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008853 __ Cvtsw(dst, FTMP);
8854 } else {
8855 __ Cvtdw(dst, FTMP);
8856 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008857 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008858 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8859 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008860
8861 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8862 // value of the output type if the input is outside of the range after the truncation or
8863 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8864 // results. This matches the desired float/double-to-int/long conversion exactly.
8865 //
8866 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8867 // value when the input is either a NaN or is outside of the range of the output type
8868 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8869 // the same result.
8870 //
8871 // The code takes care of the different behaviors by first comparing the input to the
8872 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8873 // If the input is greater than or equal to the minimum, it procedes to the truncate
8874 // instruction, which will handle such an input the same way irrespective of NAN2008.
8875 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8876 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008877 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008878 if (isR6) {
8879 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8880 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8881 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8882 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8883 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008885 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008886 __ TruncLS(FTMP, src);
8887 } else {
8888 __ TruncLD(FTMP, src);
8889 }
8890 __ Mfc1(dst_low, FTMP);
8891 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008892 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008893 QuickEntrypointEnum entrypoint =
8894 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008895 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008896 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008897 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8898 } else {
8899 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8900 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008901 }
8902 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008903 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8904 Register dst = locations->Out().AsRegister<Register>();
8905 MipsLabel truncate;
8906 MipsLabel done;
8907
Lena Djokicf4e23a82017-05-09 15:43:45 +02008908 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008909 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008910 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8911 __ LoadConst32(TMP, min_val);
8912 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008913 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008914 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8915 __ LoadConst32(TMP, High32Bits(min_val));
8916 __ Mtc1(ZERO, FTMP);
8917 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008918 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008919
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008920 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008921 __ ColeS(0, FTMP, src);
8922 } else {
8923 __ ColeD(0, FTMP, src);
8924 }
8925 __ Bc1t(0, &truncate);
8926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008927 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008928 __ CeqS(0, src, src);
8929 } else {
8930 __ CeqD(0, src, src);
8931 }
8932 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8933 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008934
8935 __ B(&done);
8936
8937 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008938 }
8939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008940 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008941 __ TruncWS(FTMP, src);
8942 } else {
8943 __ TruncWD(FTMP, src);
8944 }
8945 __ Mfc1(dst, FTMP);
8946
Lena Djokicf4e23a82017-05-09 15:43:45 +02008947 if (!isR6) {
8948 __ Bind(&done);
8949 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008950 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008951 } else if (DataType::IsFloatingPointType(result_type) &&
8952 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008953 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8954 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008955 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008956 __ Cvtsd(dst, src);
8957 } else {
8958 __ Cvtds(dst, src);
8959 }
8960 } else {
8961 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8962 << " to " << result_type;
8963 }
8964}
8965
8966void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8967 HandleShift(ushr);
8968}
8969
8970void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8971 HandleShift(ushr);
8972}
8973
8974void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8975 HandleBinaryOp(instruction);
8976}
8977
8978void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8979 HandleBinaryOp(instruction);
8980}
8981
8982void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8983 // Nothing to do, this should be removed during prepare for register allocator.
8984 LOG(FATAL) << "Unreachable";
8985}
8986
8987void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8988 // Nothing to do, this should be removed during prepare for register allocator.
8989 LOG(FATAL) << "Unreachable";
8990}
8991
8992void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008993 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008994}
8995
8996void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008997 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008998}
8999
9000void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009001 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009002}
9003
9004void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009005 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009006}
9007
9008void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009009 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009010}
9011
9012void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009013 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009014}
9015
9016void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009017 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009018}
9019
9020void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009021 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009022}
9023
9024void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009025 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009026}
9027
9028void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009029 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009030}
9031
9032void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009033 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009034}
9035
9036void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009037 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009038}
9039
9040void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009041 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009042}
9043
9044void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009045 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009046}
9047
9048void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009049 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009050}
9051
9052void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009053 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009054}
9055
9056void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009057 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009058}
9059
9060void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009061 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009062}
9063
9064void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009065 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009066}
9067
9068void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009069 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009070}
9071
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009072void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9073 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009074 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009075 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009076 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9077 uint32_t num_entries = switch_instr->GetNumEntries();
9078 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9079 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9080 // instruction to simulate PC-relative addressing when accessing the jump table.
9081 // NAL clobbers RA. Make sure RA is preserved.
9082 codegen_->ClobberRA();
9083 }
9084 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009085}
9086
Alexey Frunze96b66822016-09-10 02:32:44 -07009087void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9088 int32_t lower_bound,
9089 uint32_t num_entries,
9090 HBasicBlock* switch_block,
9091 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009092 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009093 Register temp_reg = TMP;
9094 __ Addiu32(temp_reg, value_reg, -lower_bound);
9095 // Jump to default if index is negative
9096 // Note: We don't check the case that index is positive while value < lower_bound, because in
9097 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9098 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9099
Alexey Frunze96b66822016-09-10 02:32:44 -07009100 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009101 // Jump to successors[0] if value == lower_bound.
9102 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9103 int32_t last_index = 0;
9104 for (; num_entries - last_index > 2; last_index += 2) {
9105 __ Addiu(temp_reg, temp_reg, -2);
9106 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9107 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9108 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9109 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9110 }
9111 if (num_entries - last_index == 2) {
9112 // The last missing case_value.
9113 __ Addiu(temp_reg, temp_reg, -1);
9114 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009115 }
9116
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009117 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009118 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009119 __ B(codegen_->GetLabelOf(default_block));
9120 }
9121}
9122
Alexey Frunze96b66822016-09-10 02:32:44 -07009123void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9124 Register constant_area,
9125 int32_t lower_bound,
9126 uint32_t num_entries,
9127 HBasicBlock* switch_block,
9128 HBasicBlock* default_block) {
9129 // Create a jump table.
9130 std::vector<MipsLabel*> labels(num_entries);
9131 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9132 for (uint32_t i = 0; i < num_entries; i++) {
9133 labels[i] = codegen_->GetLabelOf(successors[i]);
9134 }
9135 JumpTable* table = __ CreateJumpTable(std::move(labels));
9136
9137 // Is the value in range?
9138 __ Addiu32(TMP, value_reg, -lower_bound);
9139 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9140 __ Sltiu(AT, TMP, num_entries);
9141 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9142 } else {
9143 __ LoadConst32(AT, num_entries);
9144 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9145 }
9146
9147 // We are in the range of the table.
9148 // Load the target address from the jump table, indexing by the value.
9149 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009150 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009151 __ Lw(TMP, TMP, 0);
9152 // Compute the absolute target address by adding the table start address
9153 // (the table contains offsets to targets relative to its start).
9154 __ Addu(TMP, TMP, AT);
9155 // And jump.
9156 __ Jr(TMP);
9157 __ NopIfNoReordering();
9158}
9159
9160void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9161 int32_t lower_bound = switch_instr->GetStartValue();
9162 uint32_t num_entries = switch_instr->GetNumEntries();
9163 LocationSummary* locations = switch_instr->GetLocations();
9164 Register value_reg = locations->InAt(0).AsRegister<Register>();
9165 HBasicBlock* switch_block = switch_instr->GetBlock();
9166 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9167
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009168 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009169 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009170 //
9171 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9172 // to access the jump table and it is implemented by changing HPackedSwitch to
9173 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9174 // VisitMipsPackedSwitch()).
9175 //
9176 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9177 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9178 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009179 GenTableBasedPackedSwitch(value_reg,
9180 ZERO,
9181 lower_bound,
9182 num_entries,
9183 switch_block,
9184 default_block);
9185 } else {
9186 GenPackedSwitchWithCompares(value_reg,
9187 lower_bound,
9188 num_entries,
9189 switch_block,
9190 default_block);
9191 }
9192}
9193
9194void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009196 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009197 locations->SetInAt(0, Location::RequiresRegister());
9198 // Constant area pointer (HMipsComputeBaseMethodAddress).
9199 locations->SetInAt(1, Location::RequiresRegister());
9200}
9201
9202void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9203 int32_t lower_bound = switch_instr->GetStartValue();
9204 uint32_t num_entries = switch_instr->GetNumEntries();
9205 LocationSummary* locations = switch_instr->GetLocations();
9206 Register value_reg = locations->InAt(0).AsRegister<Register>();
9207 Register constant_area = locations->InAt(1).AsRegister<Register>();
9208 HBasicBlock* switch_block = switch_instr->GetBlock();
9209 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9210
9211 // This is an R2-only path. HPackedSwitch has been changed to
9212 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9213 // required to address the jump table relative to PC.
9214 GenTableBasedPackedSwitch(value_reg,
9215 constant_area,
9216 lower_bound,
9217 num_entries,
9218 switch_block,
9219 default_block);
9220}
9221
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009222void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9223 HMipsComputeBaseMethodAddress* insn) {
9224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009225 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009226 locations->SetOut(Location::RequiresRegister());
9227}
9228
9229void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9230 HMipsComputeBaseMethodAddress* insn) {
9231 LocationSummary* locations = insn->GetLocations();
9232 Register reg = locations->Out().AsRegister<Register>();
9233
9234 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9235
9236 // Generate a dummy PC-relative call to obtain PC.
9237 __ Nal();
9238 // Grab the return address off RA.
9239 __ Move(reg, RA);
9240
9241 // Remember this offset (the obtained PC value) for later use with constant area.
9242 __ BindPcRelBaseLabel();
9243}
9244
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009245void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9246 // The trampoline uses the same calling convention as dex calling conventions,
9247 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9248 // the method_idx.
9249 HandleInvoke(invoke);
9250}
9251
9252void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9253 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9254}
9255
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009256void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9257 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009258 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009259 locations->SetInAt(0, Location::RequiresRegister());
9260 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009261}
9262
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009263void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9264 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009265 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009266 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009267 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009268 __ LoadFromOffset(kLoadWord,
9269 locations->Out().AsRegister<Register>(),
9270 locations->InAt(0).AsRegister<Register>(),
9271 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009272 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009273 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009274 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009275 __ LoadFromOffset(kLoadWord,
9276 locations->Out().AsRegister<Register>(),
9277 locations->InAt(0).AsRegister<Register>(),
9278 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009279 __ LoadFromOffset(kLoadWord,
9280 locations->Out().AsRegister<Register>(),
9281 locations->Out().AsRegister<Register>(),
9282 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009283 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009284}
9285
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009286void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9287 ATTRIBUTE_UNUSED) {
9288 LOG(FATAL) << "Unreachable";
9289}
9290
9291void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9292 ATTRIBUTE_UNUSED) {
9293 LOG(FATAL) << "Unreachable";
9294}
9295
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009296#undef __
9297#undef QUICK_ENTRY_POINT
9298
9299} // namespace mips
9300} // namespace art