blob: cc1480948d814b09c32178d6c79720e05d49144e [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 Marko174b2e22017-10-12 13:34:49 +01001139 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(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
1354 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1355
1356 if (do_overflow_check) {
1357 __ LoadFromOffset(kLoadWord,
1358 ZERO,
1359 SP,
1360 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1361 RecordPcInfo(nullptr, 0);
1362 }
1363
1364 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001365 CHECK_EQ(fpu_spill_mask_, 0u);
1366 CHECK_EQ(core_spill_mask_, 1u << RA);
1367 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368 return;
1369 }
1370
1371 // Make sure the frame size isn't unreasonably large.
1372 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1373 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1374 }
1375
1376 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001377
Alexey Frunze73296a72016-06-03 22:51:46 -07001378 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 __ IncreaseFrameSize(ofs);
1380
Alexey Frunze73296a72016-06-03 22:51:46 -07001381 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1382 Register reg = static_cast<Register>(MostSignificantBit(mask));
1383 mask ^= 1u << reg;
1384 ofs -= kMipsWordSize;
1385 // The ZERO register is only included for alignment.
1386 if (reg != ZERO) {
1387 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001388 __ cfi().RelOffset(DWARFReg(reg), ofs);
1389 }
1390 }
1391
Alexey Frunze73296a72016-06-03 22:51:46 -07001392 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1393 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1394 mask ^= 1u << reg;
1395 ofs -= kMipsDoublewordSize;
1396 __ StoreDToOffset(reg, SP, ofs);
1397 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001398 }
1399
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001400 // Save the current method if we need it. Note that we do not
1401 // do this in HCurrentMethod, as the instruction might have been removed
1402 // in the SSA graph.
1403 if (RequiresCurrentMethod()) {
1404 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1405 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001406
1407 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1408 // Initialize should deoptimize flag to 0.
1409 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1410 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411}
1412
1413void CodeGeneratorMIPS::GenerateFrameExit() {
1414 __ cfi().RememberState();
1415
1416 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001417 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001418
Alexey Frunze73296a72016-06-03 22:51:46 -07001419 // For better instruction scheduling restore RA before other registers.
1420 uint32_t ofs = GetFrameSize();
1421 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1422 Register reg = static_cast<Register>(MostSignificantBit(mask));
1423 mask ^= 1u << reg;
1424 ofs -= kMipsWordSize;
1425 // The ZERO register is only included for alignment.
1426 if (reg != ZERO) {
1427 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001428 __ cfi().Restore(DWARFReg(reg));
1429 }
1430 }
1431
Alexey Frunze73296a72016-06-03 22:51:46 -07001432 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1433 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1434 mask ^= 1u << reg;
1435 ofs -= kMipsDoublewordSize;
1436 __ LoadDFromOffset(reg, SP, ofs);
1437 // TODO: __ cfi().Restore(DWARFReg(reg));
1438 }
1439
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001440 size_t frame_size = GetFrameSize();
1441 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1442 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1443 bool reordering = __ SetReorder(false);
1444 if (exchange) {
1445 __ Jr(RA);
1446 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1447 } else {
1448 __ DecreaseFrameSize(frame_size);
1449 __ Jr(RA);
1450 __ Nop(); // In delay slot.
1451 }
1452 __ SetReorder(reordering);
1453 } else {
1454 __ Jr(RA);
1455 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001456 }
1457
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001458 __ cfi().RestoreState();
1459 __ cfi().DefCFAOffset(GetFrameSize());
1460}
1461
1462void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1463 __ Bind(GetLabelOf(block));
1464}
1465
Lena Djokicca8c2952017-05-29 11:31:46 +02001466VectorRegister VectorRegisterFrom(Location location) {
1467 DCHECK(location.IsFpuRegister());
1468 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1469}
1470
Lena Djokic8098da92017-06-28 12:07:50 +02001471void CodeGeneratorMIPS::MoveLocation(Location destination,
1472 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001474 if (source.Equals(destination)) {
1475 return;
1476 }
1477
Lena Djokic8098da92017-06-28 12:07:50 +02001478 if (source.IsConstant()) {
1479 MoveConstant(destination, source.GetConstant());
1480 } else {
1481 if (destination.IsRegister()) {
1482 if (source.IsRegister()) {
1483 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1484 } else if (source.IsFpuRegister()) {
1485 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1486 } else {
1487 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001488 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001489 }
1490 } else if (destination.IsRegisterPair()) {
1491 if (source.IsRegisterPair()) {
1492 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1493 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1494 } else if (source.IsFpuRegister()) {
1495 Register dst_high = destination.AsRegisterPairHigh<Register>();
1496 Register dst_low = destination.AsRegisterPairLow<Register>();
1497 FRegister src = source.AsFpuRegister<FRegister>();
1498 __ Mfc1(dst_low, src);
1499 __ MoveFromFpuHigh(dst_high, src);
1500 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001501 DCHECK(source.IsDoubleStackSlot())
1502 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001503 int32_t off = source.GetStackIndex();
1504 Register r = destination.AsRegisterPairLow<Register>();
1505 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1506 }
1507 } else if (destination.IsFpuRegister()) {
1508 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001509 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001510 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1511 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001512 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001513 FRegister dst = destination.AsFpuRegister<FRegister>();
1514 Register src_high = source.AsRegisterPairHigh<Register>();
1515 Register src_low = source.AsRegisterPairLow<Register>();
1516 __ Mtc1(src_low, dst);
1517 __ MoveToFpuHigh(src_high, dst);
1518 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001519 if (GetGraph()->HasSIMD()) {
1520 __ MoveV(VectorRegisterFrom(destination),
1521 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001522 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001523 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001524 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1525 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001526 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001527 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1528 }
Lena Djokic8098da92017-06-28 12:07:50 +02001529 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001530 } else if (source.IsSIMDStackSlot()) {
1531 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001532 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001533 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001534 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1535 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001536 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001537 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1538 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1539 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001540 } else if (destination.IsSIMDStackSlot()) {
1541 if (source.IsFpuRegister()) {
1542 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1543 } else {
1544 DCHECK(source.IsSIMDStackSlot());
1545 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1546 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1547 }
Lena Djokic8098da92017-06-28 12:07:50 +02001548 } else if (destination.IsDoubleStackSlot()) {
1549 int32_t dst_offset = destination.GetStackIndex();
1550 if (source.IsRegisterPair()) {
1551 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1552 } else if (source.IsFpuRegister()) {
1553 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1554 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001555 DCHECK(source.IsDoubleStackSlot())
1556 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001557 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1558 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1559 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1560 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1561 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001562 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001563 DCHECK(destination.IsStackSlot()) << destination;
1564 int32_t dst_offset = destination.GetStackIndex();
1565 if (source.IsRegister()) {
1566 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1567 } else if (source.IsFpuRegister()) {
1568 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1569 } else {
1570 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1571 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1572 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001574 }
1575 }
1576}
1577
1578void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1579 if (c->IsIntConstant() || c->IsNullConstant()) {
1580 // Move 32 bit constant.
1581 int32_t value = GetInt32ValueOf(c);
1582 if (destination.IsRegister()) {
1583 Register dst = destination.AsRegister<Register>();
1584 __ LoadConst32(dst, value);
1585 } else {
1586 DCHECK(destination.IsStackSlot())
1587 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001588 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001589 }
1590 } else if (c->IsLongConstant()) {
1591 // Move 64 bit constant.
1592 int64_t value = GetInt64ValueOf(c);
1593 if (destination.IsRegisterPair()) {
1594 Register r_h = destination.AsRegisterPairHigh<Register>();
1595 Register r_l = destination.AsRegisterPairLow<Register>();
1596 __ LoadConst64(r_h, r_l, value);
1597 } else {
1598 DCHECK(destination.IsDoubleStackSlot())
1599 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001600 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001601 }
1602 } else if (c->IsFloatConstant()) {
1603 // Move 32 bit float constant.
1604 int32_t value = GetInt32ValueOf(c);
1605 if (destination.IsFpuRegister()) {
1606 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1607 } else {
1608 DCHECK(destination.IsStackSlot())
1609 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001610 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001611 }
1612 } else {
1613 // Move 64 bit double constant.
1614 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1615 int64_t value = GetInt64ValueOf(c);
1616 if (destination.IsFpuRegister()) {
1617 FRegister fd = destination.AsFpuRegister<FRegister>();
1618 __ LoadDConst64(fd, value, TMP);
1619 } else {
1620 DCHECK(destination.IsDoubleStackSlot())
1621 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001622 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001623 }
1624 }
1625}
1626
1627void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1628 DCHECK(destination.IsRegister());
1629 Register dst = destination.AsRegister<Register>();
1630 __ LoadConst32(dst, value);
1631}
1632
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001633void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1634 if (location.IsRegister()) {
1635 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001636 } else if (location.IsRegisterPair()) {
1637 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1638 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001639 } else {
1640 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1641 }
1642}
1643
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001644template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001645inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1646 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001647 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001648 for (const PcRelativePatchInfo& info : infos) {
1649 const DexFile& dex_file = info.target_dex_file;
1650 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001651 DCHECK(info.label.IsBound());
1652 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001653 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1654 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1656 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1657 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001658 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001659 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001660 }
1661}
1662
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001663void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001664 DCHECK(linker_patches->empty());
1665 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001666 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001667 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001668 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001669 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001670 pc_relative_string_patches_.size() +
1671 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001672 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001673 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001674 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1675 pc_relative_method_patches_, linker_patches);
1676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1677 pc_relative_type_patches_, linker_patches);
1678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1679 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001680 } else {
1681 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001682 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1683 pc_relative_type_patches_, linker_patches);
1684 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1685 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001686 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001687 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1688 method_bss_entry_patches_, linker_patches);
1689 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1690 type_bss_entry_patches_, linker_patches);
1691 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1692 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001693 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001694}
1695
Vladimir Marko65979462017-05-19 17:25:12 +01001696CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001697 MethodReference target_method,
1698 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001699 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001700 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001701 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001702 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001703}
1704
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001705CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001706 MethodReference target_method,
1707 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001708 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001709 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001710 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001711 &method_bss_entry_patches_);
1712}
1713
Alexey Frunze06a46c42016-07-19 15:00:40 -07001714CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001715 const DexFile& dex_file,
1716 dex::TypeIndex type_index,
1717 const PcRelativePatchInfo* info_high) {
1718 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001719}
1720
Vladimir Marko1998cd02017-01-13 13:02:58 +00001721CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001722 const DexFile& dex_file,
1723 dex::TypeIndex type_index,
1724 const PcRelativePatchInfo* info_high) {
1725 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001726}
1727
Vladimir Marko65979462017-05-19 17:25:12 +01001728CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001729 const DexFile& dex_file,
1730 dex::StringIndex string_index,
1731 const PcRelativePatchInfo* info_high) {
1732 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001733}
1734
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001735CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1736 const DexFile& dex_file,
1737 dex::StringIndex string_index,
1738 const PcRelativePatchInfo* info_high) {
1739 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1740}
1741
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001742CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001743 const DexFile& dex_file,
1744 uint32_t offset_or_index,
1745 const PcRelativePatchInfo* info_high,
1746 ArenaDeque<PcRelativePatchInfo>* patches) {
1747 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001748 return &patches->back();
1749}
1750
Alexey Frunze06a46c42016-07-19 15:00:40 -07001751Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1752 return map->GetOrCreate(
1753 value,
1754 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1755}
1756
Alexey Frunze06a46c42016-07-19 15:00:40 -07001757Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001758 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001759}
1760
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001761void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001762 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001763 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001764 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001765 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001766 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001767 if (GetInstructionSetFeatures().IsR6()) {
1768 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001769 __ Bind(&info_high->label);
1770 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001771 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001772 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001773 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001774 } else {
1775 // If base is ZERO, emit NAL to obtain the actual base.
1776 if (base == ZERO) {
1777 // Generate a dummy PC-relative call to obtain PC.
1778 __ Nal();
1779 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001780 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001781 __ Lui(out, /* placeholder */ 0x1234);
1782 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1783 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1784 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001785 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001786 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001787 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001788 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001789 __ Addu(out, out, (base == ZERO) ? RA : base);
1790 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001791 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001792 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001793}
1794
Alexey Frunze627c1a02017-01-30 19:28:14 -08001795CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1796 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001797 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001798 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001799 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1800 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001801 return &jit_string_patches_.back();
1802}
1803
1804CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1805 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001806 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001807 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001808 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1809 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001810 return &jit_class_patches_.back();
1811}
1812
1813void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1814 const uint8_t* roots_data,
1815 const CodeGeneratorMIPS::JitPatchInfo& info,
1816 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001817 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1818 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001819 uintptr_t address =
1820 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1821 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1822 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001823 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1824 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1825 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1826 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001827 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001828 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1829 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001830 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001831 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001832 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1833 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001834 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001835 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1836 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001837}
1838
1839void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1840 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001841 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1842 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001843 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001844 }
1845 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001846 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1847 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001848 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001849 }
1850}
1851
Goran Jakovljevice114da22016-12-26 14:21:43 +01001852void CodeGeneratorMIPS::MarkGCCard(Register object,
1853 Register value,
1854 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001855 MipsLabel done;
1856 Register card = AT;
1857 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001858 if (value_can_be_null) {
1859 __ Beqz(value, &done);
1860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 __ LoadFromOffset(kLoadWord,
1862 card,
1863 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001864 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1866 __ Addu(temp, card, temp);
1867 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001868 if (value_can_be_null) {
1869 __ Bind(&done);
1870 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871}
1872
David Brazdil58282f42016-01-14 12:45:10 +00001873void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1875 blocked_core_registers_[ZERO] = true;
1876 blocked_core_registers_[K0] = true;
1877 blocked_core_registers_[K1] = true;
1878 blocked_core_registers_[GP] = true;
1879 blocked_core_registers_[SP] = true;
1880 blocked_core_registers_[RA] = true;
1881
1882 // AT and TMP(T8) are used as temporary/scratch registers
1883 // (similar to how AT is used by MIPS assemblers).
1884 blocked_core_registers_[AT] = true;
1885 blocked_core_registers_[TMP] = true;
1886 blocked_fpu_registers_[FTMP] = true;
1887
1888 // Reserve suspend and thread registers.
1889 blocked_core_registers_[S0] = true;
1890 blocked_core_registers_[TR] = true;
1891
1892 // Reserve T9 for function calls
1893 blocked_core_registers_[T9] = true;
1894
1895 // Reserve odd-numbered FPU registers.
1896 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1897 blocked_fpu_registers_[i] = true;
1898 }
1899
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001900 if (GetGraph()->IsDebuggable()) {
1901 // Stubs do not save callee-save floating point registers. If the graph
1902 // is debuggable, we need to deal with these registers differently. For
1903 // now, just block them.
1904 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1905 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1906 }
1907 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908}
1909
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1911 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1912 return kMipsWordSize;
1913}
1914
1915size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1916 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1917 return kMipsWordSize;
1918}
1919
1920size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001921 if (GetGraph()->HasSIMD()) {
1922 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1923 } else {
1924 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1925 }
1926 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927}
1928
1929size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001930 if (GetGraph()->HasSIMD()) {
1931 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1932 } else {
1933 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1934 }
1935 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936}
1937
1938void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001939 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940}
1941
1942void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001943 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944}
1945
Serban Constantinescufca16662016-07-14 09:21:59 +01001946constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1947
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1949 HInstruction* instruction,
1950 uint32_t dex_pc,
1951 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001952 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001953 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1954 IsDirectEntrypoint(entrypoint));
1955 if (EntrypointRequiresStackMap(entrypoint)) {
1956 RecordPcInfo(instruction, dex_pc, slow_path);
1957 }
1958}
1959
1960void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1961 HInstruction* instruction,
1962 SlowPathCode* slow_path,
1963 bool direct) {
1964 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1965 GenerateInvokeRuntime(entry_point_offset, direct);
1966}
1967
1968void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001969 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001970 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001971 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001972 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 // Reserve argument space on stack (for $a0-$a3) for
1974 // entrypoints that directly reference native implementations.
1975 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001976 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001978 } else {
1979 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001981 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982}
1983
1984void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1985 Register class_reg) {
1986 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1987 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1988 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1989 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1990 __ Sync(0);
1991 __ Bind(slow_path->GetExitLabel());
1992}
1993
1994void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1995 __ Sync(0); // Only stype 0 is supported.
1996}
1997
1998void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1999 HBasicBlock* successor) {
2000 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002001 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2002
2003 if (slow_path == nullptr) {
2004 slow_path =
2005 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2006 instruction->SetSlowPath(slow_path);
2007 codegen_->AddSlowPath(slow_path);
2008 if (successor != nullptr) {
2009 DCHECK(successor->IsLoopHeader());
2010 }
2011 } else {
2012 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2013 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014
2015 __ LoadFromOffset(kLoadUnsignedHalfword,
2016 TMP,
2017 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002018 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 if (successor == nullptr) {
2020 __ Bnez(TMP, slow_path->GetEntryLabel());
2021 __ Bind(slow_path->GetReturnLabel());
2022 } else {
2023 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2024 __ B(slow_path->GetEntryLabel());
2025 // slow_path will return to GetLabelOf(successor).
2026 }
2027}
2028
2029InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2030 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002031 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032 assembler_(codegen->GetAssembler()),
2033 codegen_(codegen) {}
2034
2035void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2036 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002037 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002038 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002039 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041 locations->SetInAt(0, Location::RequiresRegister());
2042 HInstruction* right = instruction->InputAt(1);
2043 bool can_use_imm = false;
2044 if (right->IsConstant()) {
2045 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2046 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2047 can_use_imm = IsUint<16>(imm);
2048 } else if (instruction->IsAdd()) {
2049 can_use_imm = IsInt<16>(imm);
2050 } else {
2051 DCHECK(instruction->IsSub());
2052 can_use_imm = IsInt<16>(-imm);
2053 }
2054 }
2055 if (can_use_imm)
2056 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2057 else
2058 locations->SetInAt(1, Location::RequiresRegister());
2059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2060 break;
2061 }
2062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002063 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002065 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002067 break;
2068 }
2069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 case DataType::Type::kFloat32:
2071 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002072 DCHECK(instruction->IsAdd() || instruction->IsSub());
2073 locations->SetInAt(0, Location::RequiresFpuRegister());
2074 locations->SetInAt(1, Location::RequiresFpuRegister());
2075 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2080 }
2081}
2082
2083void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 LocationSummary* locations = instruction->GetLocations();
2086
2087 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 Register dst = locations->Out().AsRegister<Register>();
2090 Register lhs = locations->InAt(0).AsRegister<Register>();
2091 Location rhs_location = locations->InAt(1);
2092
2093 Register rhs_reg = ZERO;
2094 int32_t rhs_imm = 0;
2095 bool use_imm = rhs_location.IsConstant();
2096 if (use_imm) {
2097 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2098 } else {
2099 rhs_reg = rhs_location.AsRegister<Register>();
2100 }
2101
2102 if (instruction->IsAnd()) {
2103 if (use_imm)
2104 __ Andi(dst, lhs, rhs_imm);
2105 else
2106 __ And(dst, lhs, rhs_reg);
2107 } else if (instruction->IsOr()) {
2108 if (use_imm)
2109 __ Ori(dst, lhs, rhs_imm);
2110 else
2111 __ Or(dst, lhs, rhs_reg);
2112 } else if (instruction->IsXor()) {
2113 if (use_imm)
2114 __ Xori(dst, lhs, rhs_imm);
2115 else
2116 __ Xor(dst, lhs, rhs_reg);
2117 } else if (instruction->IsAdd()) {
2118 if (use_imm)
2119 __ Addiu(dst, lhs, rhs_imm);
2120 else
2121 __ Addu(dst, lhs, rhs_reg);
2122 } else {
2123 DCHECK(instruction->IsSub());
2124 if (use_imm)
2125 __ Addiu(dst, lhs, -rhs_imm);
2126 else
2127 __ Subu(dst, lhs, rhs_reg);
2128 }
2129 break;
2130 }
2131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2134 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2135 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2136 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002137 Location rhs_location = locations->InAt(1);
2138 bool use_imm = rhs_location.IsConstant();
2139 if (!use_imm) {
2140 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2141 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2142 if (instruction->IsAnd()) {
2143 __ And(dst_low, lhs_low, rhs_low);
2144 __ And(dst_high, lhs_high, rhs_high);
2145 } else if (instruction->IsOr()) {
2146 __ Or(dst_low, lhs_low, rhs_low);
2147 __ Or(dst_high, lhs_high, rhs_high);
2148 } else if (instruction->IsXor()) {
2149 __ Xor(dst_low, lhs_low, rhs_low);
2150 __ Xor(dst_high, lhs_high, rhs_high);
2151 } else if (instruction->IsAdd()) {
2152 if (lhs_low == rhs_low) {
2153 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2154 __ Slt(TMP, lhs_low, ZERO);
2155 __ Addu(dst_low, lhs_low, rhs_low);
2156 } else {
2157 __ Addu(dst_low, lhs_low, rhs_low);
2158 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2159 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2160 }
2161 __ Addu(dst_high, lhs_high, rhs_high);
2162 __ Addu(dst_high, dst_high, TMP);
2163 } else {
2164 DCHECK(instruction->IsSub());
2165 __ Sltu(TMP, lhs_low, rhs_low);
2166 __ Subu(dst_low, lhs_low, rhs_low);
2167 __ Subu(dst_high, lhs_high, rhs_high);
2168 __ Subu(dst_high, dst_high, TMP);
2169 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002171 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2172 if (instruction->IsOr()) {
2173 uint32_t low = Low32Bits(value);
2174 uint32_t high = High32Bits(value);
2175 if (IsUint<16>(low)) {
2176 if (dst_low != lhs_low || low != 0) {
2177 __ Ori(dst_low, lhs_low, low);
2178 }
2179 } else {
2180 __ LoadConst32(TMP, low);
2181 __ Or(dst_low, lhs_low, TMP);
2182 }
2183 if (IsUint<16>(high)) {
2184 if (dst_high != lhs_high || high != 0) {
2185 __ Ori(dst_high, lhs_high, high);
2186 }
2187 } else {
2188 if (high != low) {
2189 __ LoadConst32(TMP, high);
2190 }
2191 __ Or(dst_high, lhs_high, TMP);
2192 }
2193 } else if (instruction->IsXor()) {
2194 uint32_t low = Low32Bits(value);
2195 uint32_t high = High32Bits(value);
2196 if (IsUint<16>(low)) {
2197 if (dst_low != lhs_low || low != 0) {
2198 __ Xori(dst_low, lhs_low, low);
2199 }
2200 } else {
2201 __ LoadConst32(TMP, low);
2202 __ Xor(dst_low, lhs_low, TMP);
2203 }
2204 if (IsUint<16>(high)) {
2205 if (dst_high != lhs_high || high != 0) {
2206 __ Xori(dst_high, lhs_high, high);
2207 }
2208 } else {
2209 if (high != low) {
2210 __ LoadConst32(TMP, high);
2211 }
2212 __ Xor(dst_high, lhs_high, TMP);
2213 }
2214 } else if (instruction->IsAnd()) {
2215 uint32_t low = Low32Bits(value);
2216 uint32_t high = High32Bits(value);
2217 if (IsUint<16>(low)) {
2218 __ Andi(dst_low, lhs_low, low);
2219 } else if (low != 0xFFFFFFFF) {
2220 __ LoadConst32(TMP, low);
2221 __ And(dst_low, lhs_low, TMP);
2222 } else if (dst_low != lhs_low) {
2223 __ Move(dst_low, lhs_low);
2224 }
2225 if (IsUint<16>(high)) {
2226 __ Andi(dst_high, lhs_high, high);
2227 } else if (high != 0xFFFFFFFF) {
2228 if (high != low) {
2229 __ LoadConst32(TMP, high);
2230 }
2231 __ And(dst_high, lhs_high, TMP);
2232 } else if (dst_high != lhs_high) {
2233 __ Move(dst_high, lhs_high);
2234 }
2235 } else {
2236 if (instruction->IsSub()) {
2237 value = -value;
2238 } else {
2239 DCHECK(instruction->IsAdd());
2240 }
2241 int32_t low = Low32Bits(value);
2242 int32_t high = High32Bits(value);
2243 if (IsInt<16>(low)) {
2244 if (dst_low != lhs_low || low != 0) {
2245 __ Addiu(dst_low, lhs_low, low);
2246 }
2247 if (low != 0) {
2248 __ Sltiu(AT, dst_low, low);
2249 }
2250 } else {
2251 __ LoadConst32(TMP, low);
2252 __ Addu(dst_low, lhs_low, TMP);
2253 __ Sltu(AT, dst_low, TMP);
2254 }
2255 if (IsInt<16>(high)) {
2256 if (dst_high != lhs_high || high != 0) {
2257 __ Addiu(dst_high, lhs_high, high);
2258 }
2259 } else {
2260 if (high != low) {
2261 __ LoadConst32(TMP, high);
2262 }
2263 __ Addu(dst_high, lhs_high, TMP);
2264 }
2265 if (low != 0) {
2266 __ Addu(dst_high, dst_high, AT);
2267 }
2268 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 }
2270 break;
2271 }
2272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kFloat32:
2274 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2276 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2277 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2278 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002280 __ AddS(dst, lhs, rhs);
2281 } else {
2282 __ AddD(dst, lhs, rhs);
2283 }
2284 } else {
2285 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287 __ SubS(dst, lhs, rhs);
2288 } else {
2289 __ SubD(dst, lhs, rhs);
2290 }
2291 }
2292 break;
2293 }
2294
2295 default:
2296 LOG(FATAL) << "Unexpected binary operation type " << type;
2297 }
2298}
2299
2300void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002301 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002302
Vladimir Markoca6fff82017-10-03 14:49:14 +01002303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002304 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002305 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2310 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312 locations->SetInAt(0, Location::RequiresRegister());
2313 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2314 locations->SetOut(Location::RequiresRegister());
2315 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316 default:
2317 LOG(FATAL) << "Unexpected shift type " << type;
2318 }
2319}
2320
2321static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2322
2323void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327
2328 Location rhs_location = locations->InAt(1);
2329 bool use_imm = rhs_location.IsConstant();
2330 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2331 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002332 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002334 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002335 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2336 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337
2338 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002339 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002340 Register dst = locations->Out().AsRegister<Register>();
2341 Register lhs = locations->InAt(0).AsRegister<Register>();
2342 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002343 if (shift_value == 0) {
2344 if (dst != lhs) {
2345 __ Move(dst, lhs);
2346 }
2347 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 __ Sll(dst, lhs, shift_value);
2349 } else if (instr->IsShr()) {
2350 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002351 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002352 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002353 } else {
2354 if (has_ins_rotr) {
2355 __ Rotr(dst, lhs, shift_value);
2356 } else {
2357 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2358 __ Srl(dst, lhs, shift_value);
2359 __ Or(dst, dst, TMP);
2360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002361 }
2362 } else {
2363 if (instr->IsShl()) {
2364 __ Sllv(dst, lhs, rhs_reg);
2365 } else if (instr->IsShr()) {
2366 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002367 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002369 } else {
2370 if (has_ins_rotr) {
2371 __ Rotrv(dst, lhs, rhs_reg);
2372 } else {
2373 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002374 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2375 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2376 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2377 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2378 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002379 __ Sllv(TMP, lhs, TMP);
2380 __ Srlv(dst, lhs, rhs_reg);
2381 __ Or(dst, dst, TMP);
2382 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 }
2384 }
2385 break;
2386 }
2387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2390 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2391 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2392 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2393 if (use_imm) {
2394 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002395 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002396 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002397 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002398 if (instr->IsShl()) {
2399 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2400 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2401 __ Sll(dst_low, lhs_low, shift_value);
2402 } else if (instr->IsShr()) {
2403 __ Srl(dst_low, lhs_low, shift_value);
2404 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2405 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002406 } else if (instr->IsUShr()) {
2407 __ Srl(dst_low, lhs_low, shift_value);
2408 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2409 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002410 } else {
2411 __ Srl(dst_low, lhs_low, shift_value);
2412 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2413 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002414 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002415 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002417 if (instr->IsShl()) {
2418 __ Sll(dst_low, lhs_low, shift_value);
2419 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2420 __ Sll(dst_high, lhs_high, shift_value);
2421 __ Or(dst_high, dst_high, TMP);
2422 } else if (instr->IsShr()) {
2423 __ Sra(dst_high, lhs_high, shift_value);
2424 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2425 __ Srl(dst_low, lhs_low, shift_value);
2426 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002427 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002428 __ Srl(dst_high, lhs_high, shift_value);
2429 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2430 __ Srl(dst_low, lhs_low, shift_value);
2431 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002432 } else {
2433 __ Srl(TMP, lhs_low, shift_value);
2434 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2435 __ Or(dst_low, dst_low, TMP);
2436 __ Srl(TMP, lhs_high, shift_value);
2437 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2438 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 }
2441 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002442 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002443 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002444 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 __ Move(dst_low, ZERO);
2446 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002447 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002448 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002449 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002450 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002451 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002453 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002454 // 64-bit rotation by 32 is just a swap.
2455 __ Move(dst_low, lhs_high);
2456 __ Move(dst_high, lhs_low);
2457 } else {
2458 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002459 __ Srl(dst_low, lhs_high, shift_value_high);
2460 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2461 __ Srl(dst_high, lhs_low, shift_value_high);
2462 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002463 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002464 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2465 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002466 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002467 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2468 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002469 __ Or(dst_high, dst_high, TMP);
2470 }
2471 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002472 }
2473 }
2474 } else {
2475 MipsLabel done;
2476 if (instr->IsShl()) {
2477 __ Sllv(dst_low, lhs_low, rhs_reg);
2478 __ Nor(AT, ZERO, rhs_reg);
2479 __ Srl(TMP, lhs_low, 1);
2480 __ Srlv(TMP, TMP, AT);
2481 __ Sllv(dst_high, lhs_high, rhs_reg);
2482 __ Or(dst_high, dst_high, TMP);
2483 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2484 __ Beqz(TMP, &done);
2485 __ Move(dst_high, dst_low);
2486 __ Move(dst_low, ZERO);
2487 } else if (instr->IsShr()) {
2488 __ Srav(dst_high, lhs_high, rhs_reg);
2489 __ Nor(AT, ZERO, rhs_reg);
2490 __ Sll(TMP, lhs_high, 1);
2491 __ Sllv(TMP, TMP, AT);
2492 __ Srlv(dst_low, lhs_low, rhs_reg);
2493 __ Or(dst_low, dst_low, TMP);
2494 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2495 __ Beqz(TMP, &done);
2496 __ Move(dst_low, dst_high);
2497 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002498 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002499 __ Srlv(dst_high, lhs_high, rhs_reg);
2500 __ Nor(AT, ZERO, rhs_reg);
2501 __ Sll(TMP, lhs_high, 1);
2502 __ Sllv(TMP, TMP, AT);
2503 __ Srlv(dst_low, lhs_low, rhs_reg);
2504 __ Or(dst_low, dst_low, TMP);
2505 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2506 __ Beqz(TMP, &done);
2507 __ Move(dst_low, dst_high);
2508 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002509 } else {
2510 __ Nor(AT, ZERO, rhs_reg);
2511 __ Srlv(TMP, lhs_low, rhs_reg);
2512 __ Sll(dst_low, lhs_high, 1);
2513 __ Sllv(dst_low, dst_low, AT);
2514 __ Or(dst_low, dst_low, TMP);
2515 __ Srlv(TMP, lhs_high, rhs_reg);
2516 __ Sll(dst_high, lhs_low, 1);
2517 __ Sllv(dst_high, dst_high, AT);
2518 __ Or(dst_high, dst_high, TMP);
2519 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2520 __ Beqz(TMP, &done);
2521 __ Move(TMP, dst_high);
2522 __ Move(dst_high, dst_low);
2523 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002524 }
2525 __ Bind(&done);
2526 }
2527 break;
2528 }
2529
2530 default:
2531 LOG(FATAL) << "Unexpected shift operation type " << type;
2532 }
2533}
2534
2535void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2536 HandleBinaryOp(instruction);
2537}
2538
2539void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2540 HandleBinaryOp(instruction);
2541}
2542
2543void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2544 HandleBinaryOp(instruction);
2545}
2546
2547void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2548 HandleBinaryOp(instruction);
2549}
2550
2551void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002553 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002555 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002556 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2557 object_array_get_with_read_barrier
2558 ? LocationSummary::kCallOnSlowPath
2559 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002560 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2561 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2562 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2567 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002568 // The output overlaps in the case of an object array get with
2569 // read barriers enabled: we do not want the move to overwrite the
2570 // array's location, as we need it to emit the read barrier.
2571 locations->SetOut(Location::RequiresRegister(),
2572 object_array_get_with_read_barrier
2573 ? Location::kOutputOverlap
2574 : Location::kNoOutputOverlap);
2575 }
2576 // We need a temporary register for the read barrier marking slow
2577 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2578 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002579 bool temp_needed = instruction->GetIndex()->IsConstant()
2580 ? !kBakerReadBarrierThunksEnableForFields
2581 : !kBakerReadBarrierThunksEnableForArrays;
2582 if (temp_needed) {
2583 locations->AddTemp(Location::RequiresRegister());
2584 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585 }
2586}
2587
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002588static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2589 auto null_checker = [codegen, instruction]() {
2590 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002591 };
2592 return null_checker;
2593}
2594
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002595void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2596 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002597 Location obj_loc = locations->InAt(0);
2598 Register obj = obj_loc.AsRegister<Register>();
2599 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002601 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002602 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002604 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002605 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2606 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002608 case DataType::Type::kBool:
2609 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002610 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 if (index.IsConstant()) {
2612 size_t offset =
2613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002614 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002615 } else {
2616 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002617 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002618 }
2619 break;
2620 }
2621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002623 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 if (index.IsConstant()) {
2625 size_t offset =
2626 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002627 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 } else {
2629 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002630 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002631 }
2632 break;
2633 }
2634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002636 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002637 if (maybe_compressed_char_at) {
2638 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2639 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2640 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2641 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2642 "Expecting 0=compressed, 1=uncompressed");
2643 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002644 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002645 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2646 if (maybe_compressed_char_at) {
2647 MipsLabel uncompressed_load, done;
2648 __ Bnez(TMP, &uncompressed_load);
2649 __ LoadFromOffset(kLoadUnsignedByte,
2650 out,
2651 obj,
2652 data_offset + (const_index << TIMES_1));
2653 __ B(&done);
2654 __ Bind(&uncompressed_load);
2655 __ LoadFromOffset(kLoadUnsignedHalfword,
2656 out,
2657 obj,
2658 data_offset + (const_index << TIMES_2));
2659 __ Bind(&done);
2660 } else {
2661 __ LoadFromOffset(kLoadUnsignedHalfword,
2662 out,
2663 obj,
2664 data_offset + (const_index << TIMES_2),
2665 null_checker);
2666 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002668 Register index_reg = index.AsRegister<Register>();
2669 if (maybe_compressed_char_at) {
2670 MipsLabel uncompressed_load, done;
2671 __ Bnez(TMP, &uncompressed_load);
2672 __ Addu(TMP, obj, index_reg);
2673 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2674 __ B(&done);
2675 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002676 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002677 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2678 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002679 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2680 __ Addu(TMP, index_reg, obj);
2681 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002682 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002683 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002684 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2685 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002686 }
2687 break;
2688 }
2689
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002690 case DataType::Type::kInt16: {
2691 Register out = out_loc.AsRegister<Register>();
2692 if (index.IsConstant()) {
2693 size_t offset =
2694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2695 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002696 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2697 __ Addu(TMP, index.AsRegister<Register>(), obj);
2698 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002699 } else {
2700 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2701 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2702 }
2703 break;
2704 }
2705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002708 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 if (index.IsConstant()) {
2710 size_t offset =
2711 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002712 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002713 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2714 __ Addu(TMP, index.AsRegister<Register>(), obj);
2715 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002716 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002717 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002718 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002719 }
2720 break;
2721 }
2722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002724 static_assert(
2725 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2726 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2727 // /* HeapReference<Object> */ out =
2728 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2729 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002730 bool temp_needed = index.IsConstant()
2731 ? !kBakerReadBarrierThunksEnableForFields
2732 : !kBakerReadBarrierThunksEnableForArrays;
2733 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002734 // Note that a potential implicit null check is handled in this
2735 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002736 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2737 if (index.IsConstant()) {
2738 // Array load with a constant index can be treated as a field load.
2739 size_t offset =
2740 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2741 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2742 out_loc,
2743 obj,
2744 offset,
2745 temp,
2746 /* needs_null_check */ false);
2747 } else {
2748 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2749 out_loc,
2750 obj,
2751 data_offset,
2752 index,
2753 temp,
2754 /* needs_null_check */ false);
2755 }
Alexey Frunze15958152017-02-09 19:08:30 -08002756 } else {
2757 Register out = out_loc.AsRegister<Register>();
2758 if (index.IsConstant()) {
2759 size_t offset =
2760 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2761 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2762 // If read barriers are enabled, emit read barriers other than
2763 // Baker's using a slow path (and also unpoison the loaded
2764 // reference, if heap poisoning is enabled).
2765 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2766 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002767 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002768 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2769 // If read barriers are enabled, emit read barriers other than
2770 // Baker's using a slow path (and also unpoison the loaded
2771 // reference, if heap poisoning is enabled).
2772 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2773 out_loc,
2774 out_loc,
2775 obj_loc,
2776 data_offset,
2777 index);
2778 }
2779 }
2780 break;
2781 }
2782
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002784 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002785 if (index.IsConstant()) {
2786 size_t offset =
2787 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002788 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002789 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2790 __ Addu(TMP, index.AsRegister<Register>(), obj);
2791 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002792 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002793 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002794 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 }
2796 break;
2797 }
2798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002800 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002801 if (index.IsConstant()) {
2802 size_t offset =
2803 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002804 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002805 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2806 __ Addu(TMP, index.AsRegister<Register>(), obj);
2807 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002808 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002809 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002810 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002811 }
2812 break;
2813 }
2814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002816 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 if (index.IsConstant()) {
2818 size_t offset =
2819 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002820 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002821 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2822 __ Addu(TMP, index.AsRegister<Register>(), obj);
2823 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002825 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002826 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002827 }
2828 break;
2829 }
2830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002831 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002832 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2833 UNREACHABLE();
2834 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002835}
2836
2837void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002838 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839 locations->SetInAt(0, Location::RequiresRegister());
2840 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2841}
2842
2843void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2844 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002845 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002846 Register obj = locations->InAt(0).AsRegister<Register>();
2847 Register out = locations->Out().AsRegister<Register>();
2848 __ LoadFromOffset(kLoadWord, out, obj, offset);
2849 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002850 // Mask out compression flag from String's array length.
2851 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2852 __ Srl(out, out, 1u);
2853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002854}
2855
Alexey Frunzef58b2482016-09-02 22:14:06 -07002856Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2857 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2858 ? Location::ConstantLocation(instruction->AsConstant())
2859 : Location::RequiresRegister();
2860}
2861
2862Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2863 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2864 // We can store a non-zero float or double constant without first loading it into the FPU,
2865 // but we should only prefer this if the constant has a single use.
2866 if (instruction->IsConstant() &&
2867 (instruction->AsConstant()->IsZeroBitPattern() ||
2868 instruction->GetUses().HasExactlyOneElement())) {
2869 return Location::ConstantLocation(instruction->AsConstant());
2870 // Otherwise fall through and require an FPU register for the constant.
2871 }
2872 return Location::RequiresFpuRegister();
2873}
2874
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002875void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002877
2878 bool needs_write_barrier =
2879 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2880 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2881
Vladimir Markoca6fff82017-10-03 14:49:14 +01002882 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002883 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002884 may_need_runtime_call_for_type_check ?
2885 LocationSummary::kCallOnSlowPath :
2886 LocationSummary::kNoCall);
2887
2888 locations->SetInAt(0, Location::RequiresRegister());
2889 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002891 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002892 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002893 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2894 }
2895 if (needs_write_barrier) {
2896 // Temporary register for the write barrier.
2897 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002898 }
2899}
2900
2901void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2902 LocationSummary* locations = instruction->GetLocations();
2903 Register obj = locations->InAt(0).AsRegister<Register>();
2904 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002905 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002907 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002908 bool needs_write_barrier =
2909 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002910 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002911 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002912
2913 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002915 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002917 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002919 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002921 __ Addu(base_reg, obj, index.AsRegister<Register>());
2922 }
2923 if (value_location.IsConstant()) {
2924 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2925 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2926 } else {
2927 Register value = value_location.AsRegister<Register>();
2928 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929 }
2930 break;
2931 }
2932
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002933 case DataType::Type::kUint16:
2934 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002935 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002936 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002937 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002938 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2939 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002940 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002941 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002942 }
2943 if (value_location.IsConstant()) {
2944 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2945 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2946 } else {
2947 Register value = value_location.AsRegister<Register>();
2948 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002949 }
2950 break;
2951 }
2952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002953 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2955 if (index.IsConstant()) {
2956 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002957 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2958 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002959 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002960 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002961 }
2962 if (value_location.IsConstant()) {
2963 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2964 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2965 } else {
2966 Register value = value_location.AsRegister<Register>();
2967 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2968 }
2969 break;
2970 }
2971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002972 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002973 if (value_location.IsConstant()) {
2974 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002975 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002977 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002978 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002979 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002980 }
Alexey Frunze15958152017-02-09 19:08:30 -08002981 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2982 DCHECK_EQ(value, 0);
2983 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2984 DCHECK(!needs_write_barrier);
2985 DCHECK(!may_need_runtime_call_for_type_check);
2986 break;
2987 }
2988
2989 DCHECK(needs_write_barrier);
2990 Register value = value_location.AsRegister<Register>();
2991 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2992 Register temp2 = TMP; // Doesn't need to survive slow path.
2993 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2994 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2995 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2996 MipsLabel done;
2997 SlowPathCodeMIPS* slow_path = nullptr;
2998
2999 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003000 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003001 codegen_->AddSlowPath(slow_path);
3002 if (instruction->GetValueCanBeNull()) {
3003 MipsLabel non_zero;
3004 __ Bnez(value, &non_zero);
3005 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3006 if (index.IsConstant()) {
3007 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003008 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3009 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003010 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003011 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003012 }
Alexey Frunze15958152017-02-09 19:08:30 -08003013 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3014 __ B(&done);
3015 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003016 }
Alexey Frunze15958152017-02-09 19:08:30 -08003017
3018 // Note that when read barriers are enabled, the type checks
3019 // are performed without read barriers. This is fine, even in
3020 // the case where a class object is in the from-space after
3021 // the flip, as a comparison involving such a type would not
3022 // produce a false positive; it may of course produce a false
3023 // negative, in which case we would take the ArraySet slow
3024 // path.
3025
3026 // /* HeapReference<Class> */ temp1 = obj->klass_
3027 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3028 __ MaybeUnpoisonHeapReference(temp1);
3029
3030 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3031 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3032 // /* HeapReference<Class> */ temp2 = value->klass_
3033 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3034 // If heap poisoning is enabled, no need to unpoison `temp1`
3035 // nor `temp2`, as we are comparing two poisoned references.
3036
3037 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3038 MipsLabel do_put;
3039 __ Beq(temp1, temp2, &do_put);
3040 // If heap poisoning is enabled, the `temp1` reference has
3041 // not been unpoisoned yet; unpoison it now.
3042 __ MaybeUnpoisonHeapReference(temp1);
3043
3044 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3045 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3046 // If heap poisoning is enabled, no need to unpoison
3047 // `temp1`, as we are comparing against null below.
3048 __ Bnez(temp1, slow_path->GetEntryLabel());
3049 __ Bind(&do_put);
3050 } else {
3051 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3052 }
3053 }
3054
3055 Register source = value;
3056 if (kPoisonHeapReferences) {
3057 // Note that in the case where `value` is a null reference,
3058 // we do not enter this block, as a null reference does not
3059 // need poisoning.
3060 __ Move(temp1, value);
3061 __ PoisonHeapReference(temp1);
3062 source = temp1;
3063 }
3064
3065 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3066 if (index.IsConstant()) {
3067 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003069 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003070 }
3071 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3072
3073 if (!may_need_runtime_call_for_type_check) {
3074 codegen_->MaybeRecordImplicitNullCheck(instruction);
3075 }
3076
3077 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3078
3079 if (done.IsLinked()) {
3080 __ Bind(&done);
3081 }
3082
3083 if (slow_path != nullptr) {
3084 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003085 }
3086 break;
3087 }
3088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003091 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003092 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003093 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3094 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003095 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003096 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003097 }
3098 if (value_location.IsConstant()) {
3099 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3100 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3101 } else {
3102 Register value = value_location.AsRegisterPairLow<Register>();
3103 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003104 }
3105 break;
3106 }
3107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003108 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003109 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003110 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003111 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003112 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3113 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003115 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003116 }
3117 if (value_location.IsConstant()) {
3118 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3119 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3120 } else {
3121 FRegister value = value_location.AsFpuRegister<FRegister>();
3122 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003123 }
3124 break;
3125 }
3126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003129 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003130 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003131 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3132 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003133 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003134 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003135 }
3136 if (value_location.IsConstant()) {
3137 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3138 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3139 } else {
3140 FRegister value = value_location.AsFpuRegister<FRegister>();
3141 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003142 }
3143 break;
3144 }
3145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003147 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3148 UNREACHABLE();
3149 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150}
3151
Lena Djokica2901602017-09-21 13:50:52 +02003152void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3153 HIntermediateArrayAddressIndex* instruction) {
3154 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003155 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003156
3157 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3158
3159 locations->SetInAt(0, Location::RequiresRegister());
3160 locations->SetInAt(1, Location::ConstantLocation(shift));
3161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3162}
3163
3164void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3165 HIntermediateArrayAddressIndex* instruction) {
3166 LocationSummary* locations = instruction->GetLocations();
3167 Register index_reg = locations->InAt(0).AsRegister<Register>();
3168 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3169 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3170}
3171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003172void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003173 RegisterSet caller_saves = RegisterSet::Empty();
3174 InvokeRuntimeCallingConvention calling_convention;
3175 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3176 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3177 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003178 locations->SetInAt(0, Location::RequiresRegister());
3179 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003180}
3181
3182void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3183 LocationSummary* locations = instruction->GetLocations();
3184 BoundsCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003185 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003186 codegen_->AddSlowPath(slow_path);
3187
3188 Register index = locations->InAt(0).AsRegister<Register>();
3189 Register length = locations->InAt(1).AsRegister<Register>();
3190
3191 // length is limited by the maximum positive signed 32-bit integer.
3192 // Unsigned comparison of length and index checks for index < 0
3193 // and for length <= index simultaneously.
3194 __ Bgeu(index, length, slow_path->GetEntryLabel());
3195}
3196
Alexey Frunze15958152017-02-09 19:08:30 -08003197// Temp is used for read barrier.
3198static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3199 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003200 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003201 (kUseBakerReadBarrier ||
3202 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3203 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3204 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3205 return 1;
3206 }
3207 return 0;
3208}
3209
3210// Extra temp is used for read barrier.
3211static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3212 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3213}
3214
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003215void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003216 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3217 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3218
3219 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3220 switch (type_check_kind) {
3221 case TypeCheckKind::kExactCheck:
3222 case TypeCheckKind::kAbstractClassCheck:
3223 case TypeCheckKind::kClassHierarchyCheck:
3224 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003225 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003226 ? LocationSummary::kCallOnSlowPath
3227 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3228 break;
3229 case TypeCheckKind::kArrayCheck:
3230 case TypeCheckKind::kUnresolvedCheck:
3231 case TypeCheckKind::kInterfaceCheck:
3232 call_kind = LocationSummary::kCallOnSlowPath;
3233 break;
3234 }
3235
Vladimir Markoca6fff82017-10-03 14:49:14 +01003236 LocationSummary* locations =
3237 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003238 locations->SetInAt(0, Location::RequiresRegister());
3239 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003240 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003241}
3242
3243void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003244 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003245 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003246 Location obj_loc = locations->InAt(0);
3247 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003248 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003249 Location temp_loc = locations->GetTemp(0);
3250 Register temp = temp_loc.AsRegister<Register>();
3251 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3252 DCHECK_LE(num_temps, 2u);
3253 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003254 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3255 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3256 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3257 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3258 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3259 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3260 const uint32_t object_array_data_offset =
3261 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3262 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003263
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003264 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3265 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3266 // read barriers is done for performance and code size reasons.
3267 bool is_type_check_slow_path_fatal = false;
3268 if (!kEmitCompilerReadBarrier) {
3269 is_type_check_slow_path_fatal =
3270 (type_check_kind == TypeCheckKind::kExactCheck ||
3271 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3272 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3273 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3274 !instruction->CanThrowIntoCatchBlock();
3275 }
3276 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003277 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3278 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003279 codegen_->AddSlowPath(slow_path);
3280
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003281 // Avoid this check if we know `obj` is not null.
3282 if (instruction->MustDoNullCheck()) {
3283 __ Beqz(obj, &done);
3284 }
3285
3286 switch (type_check_kind) {
3287 case TypeCheckKind::kExactCheck:
3288 case TypeCheckKind::kArrayCheck: {
3289 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003290 GenerateReferenceLoadTwoRegisters(instruction,
3291 temp_loc,
3292 obj_loc,
3293 class_offset,
3294 maybe_temp2_loc,
3295 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003296 // Jump to slow path for throwing the exception or doing a
3297 // more involved array check.
3298 __ Bne(temp, cls, slow_path->GetEntryLabel());
3299 break;
3300 }
3301
3302 case TypeCheckKind::kAbstractClassCheck: {
3303 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003304 GenerateReferenceLoadTwoRegisters(instruction,
3305 temp_loc,
3306 obj_loc,
3307 class_offset,
3308 maybe_temp2_loc,
3309 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003310 // If the class is abstract, we eagerly fetch the super class of the
3311 // object to avoid doing a comparison we know will fail.
3312 MipsLabel loop;
3313 __ Bind(&loop);
3314 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003315 GenerateReferenceLoadOneRegister(instruction,
3316 temp_loc,
3317 super_offset,
3318 maybe_temp2_loc,
3319 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003320 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3321 // exception.
3322 __ Beqz(temp, slow_path->GetEntryLabel());
3323 // Otherwise, compare the classes.
3324 __ Bne(temp, cls, &loop);
3325 break;
3326 }
3327
3328 case TypeCheckKind::kClassHierarchyCheck: {
3329 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003330 GenerateReferenceLoadTwoRegisters(instruction,
3331 temp_loc,
3332 obj_loc,
3333 class_offset,
3334 maybe_temp2_loc,
3335 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003336 // Walk over the class hierarchy to find a match.
3337 MipsLabel loop;
3338 __ Bind(&loop);
3339 __ Beq(temp, cls, &done);
3340 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003341 GenerateReferenceLoadOneRegister(instruction,
3342 temp_loc,
3343 super_offset,
3344 maybe_temp2_loc,
3345 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003346 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3347 // exception. Otherwise, jump to the beginning of the loop.
3348 __ Bnez(temp, &loop);
3349 __ B(slow_path->GetEntryLabel());
3350 break;
3351 }
3352
3353 case TypeCheckKind::kArrayObjectCheck: {
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 // Do an exact check.
3362 __ Beq(temp, cls, &done);
3363 // Otherwise, we need to check that the object's class is a non-primitive array.
3364 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003365 GenerateReferenceLoadOneRegister(instruction,
3366 temp_loc,
3367 component_offset,
3368 maybe_temp2_loc,
3369 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003370 // If the component type is null, jump to the slow path to throw the exception.
3371 __ Beqz(temp, slow_path->GetEntryLabel());
3372 // Otherwise, the object is indeed an array, further check that this component
3373 // type is not a primitive type.
3374 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3375 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3376 __ Bnez(temp, slow_path->GetEntryLabel());
3377 break;
3378 }
3379
3380 case TypeCheckKind::kUnresolvedCheck:
3381 // We always go into the type check slow path for the unresolved check case.
3382 // We cannot directly call the CheckCast runtime entry point
3383 // without resorting to a type checking slow path here (i.e. by
3384 // calling InvokeRuntime directly), as it would require to
3385 // assign fixed registers for the inputs of this HInstanceOf
3386 // instruction (following the runtime calling convention), which
3387 // might be cluttered by the potential first read barrier
3388 // emission at the beginning of this method.
3389 __ B(slow_path->GetEntryLabel());
3390 break;
3391
3392 case TypeCheckKind::kInterfaceCheck: {
3393 // Avoid read barriers to improve performance of the fast path. We can not get false
3394 // positives by doing this.
3395 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003396 GenerateReferenceLoadTwoRegisters(instruction,
3397 temp_loc,
3398 obj_loc,
3399 class_offset,
3400 maybe_temp2_loc,
3401 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003402 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003403 GenerateReferenceLoadTwoRegisters(instruction,
3404 temp_loc,
3405 temp_loc,
3406 iftable_offset,
3407 maybe_temp2_loc,
3408 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003409 // Iftable is never null.
3410 __ Lw(TMP, temp, array_length_offset);
3411 // Loop through the iftable and check if any class matches.
3412 MipsLabel loop;
3413 __ Bind(&loop);
3414 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3415 __ Beqz(TMP, slow_path->GetEntryLabel());
3416 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3417 __ MaybeUnpoisonHeapReference(AT);
3418 // Go to next interface.
3419 __ Addiu(TMP, TMP, -2);
3420 // Compare the classes and continue the loop if they do not match.
3421 __ Bne(AT, cls, &loop);
3422 break;
3423 }
3424 }
3425
3426 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003427 __ Bind(slow_path->GetExitLabel());
3428}
3429
3430void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3431 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003432 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003433 locations->SetInAt(0, Location::RequiresRegister());
3434 if (check->HasUses()) {
3435 locations->SetOut(Location::SameAsFirstInput());
3436 }
3437}
3438
3439void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3440 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003441 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003442 check->GetLoadClass(),
3443 check,
3444 check->GetDexPc(),
3445 true);
3446 codegen_->AddSlowPath(slow_path);
3447 GenerateClassInitializationCheck(slow_path,
3448 check->GetLocations()->InAt(0).AsRegister<Register>());
3449}
3450
3451void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003452 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003453
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003454 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003455 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003456
3457 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003459 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003460 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003462 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003464 locations->SetInAt(0, Location::RequiresRegister());
3465 locations->SetInAt(1, Location::RequiresRegister());
3466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3467 break;
3468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470 locations->SetInAt(0, Location::RequiresRegister());
3471 locations->SetInAt(1, Location::RequiresRegister());
3472 // Output overlaps because it is written before doing the low comparison.
3473 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3474 break;
3475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 case DataType::Type::kFloat32:
3477 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003478 locations->SetInAt(0, Location::RequiresFpuRegister());
3479 locations->SetInAt(1, Location::RequiresFpuRegister());
3480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003481 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482
3483 default:
3484 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3485 }
3486}
3487
3488void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3489 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003490 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003491 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003492 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003493
3494 // 0 if: left == right
3495 // 1 if: left > right
3496 // -1 if: left < right
3497 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003498 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003499 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003502 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003504 Register lhs = locations->InAt(0).AsRegister<Register>();
3505 Register rhs = locations->InAt(1).AsRegister<Register>();
3506 __ Slt(TMP, lhs, rhs);
3507 __ Slt(res, rhs, lhs);
3508 __ Subu(res, res, TMP);
3509 break;
3510 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003511 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003512 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003513 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3514 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3515 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3516 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3517 // TODO: more efficient (direct) comparison with a constant.
3518 __ Slt(TMP, lhs_high, rhs_high);
3519 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3520 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3521 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3522 __ Sltu(TMP, lhs_low, rhs_low);
3523 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3524 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3525 __ Bind(&done);
3526 break;
3527 }
3528
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003529 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003530 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003531 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3532 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3533 MipsLabel done;
3534 if (isR6) {
3535 __ CmpEqS(FTMP, lhs, rhs);
3536 __ LoadConst32(res, 0);
3537 __ Bc1nez(FTMP, &done);
3538 if (gt_bias) {
3539 __ CmpLtS(FTMP, lhs, rhs);
3540 __ LoadConst32(res, -1);
3541 __ Bc1nez(FTMP, &done);
3542 __ LoadConst32(res, 1);
3543 } else {
3544 __ CmpLtS(FTMP, rhs, lhs);
3545 __ LoadConst32(res, 1);
3546 __ Bc1nez(FTMP, &done);
3547 __ LoadConst32(res, -1);
3548 }
3549 } else {
3550 if (gt_bias) {
3551 __ ColtS(0, lhs, rhs);
3552 __ LoadConst32(res, -1);
3553 __ Bc1t(0, &done);
3554 __ CeqS(0, lhs, rhs);
3555 __ LoadConst32(res, 1);
3556 __ Movt(res, ZERO, 0);
3557 } else {
3558 __ ColtS(0, rhs, lhs);
3559 __ LoadConst32(res, 1);
3560 __ Bc1t(0, &done);
3561 __ CeqS(0, lhs, rhs);
3562 __ LoadConst32(res, -1);
3563 __ Movt(res, ZERO, 0);
3564 }
3565 }
3566 __ Bind(&done);
3567 break;
3568 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003570 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3572 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3573 MipsLabel done;
3574 if (isR6) {
3575 __ CmpEqD(FTMP, lhs, rhs);
3576 __ LoadConst32(res, 0);
3577 __ Bc1nez(FTMP, &done);
3578 if (gt_bias) {
3579 __ CmpLtD(FTMP, lhs, rhs);
3580 __ LoadConst32(res, -1);
3581 __ Bc1nez(FTMP, &done);
3582 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003583 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003584 __ CmpLtD(FTMP, rhs, lhs);
3585 __ LoadConst32(res, 1);
3586 __ Bc1nez(FTMP, &done);
3587 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003588 }
3589 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003590 if (gt_bias) {
3591 __ ColtD(0, lhs, rhs);
3592 __ LoadConst32(res, -1);
3593 __ Bc1t(0, &done);
3594 __ CeqD(0, lhs, rhs);
3595 __ LoadConst32(res, 1);
3596 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003597 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003598 __ ColtD(0, rhs, lhs);
3599 __ LoadConst32(res, 1);
3600 __ Bc1t(0, &done);
3601 __ CeqD(0, lhs, rhs);
3602 __ LoadConst32(res, -1);
3603 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003604 }
3605 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003606 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003607 break;
3608 }
3609
3610 default:
3611 LOG(FATAL) << "Unimplemented compare type " << in_type;
3612 }
3613}
3614
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003615void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003616 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003617 switch (instruction->InputAt(0)->GetType()) {
3618 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003619 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003620 locations->SetInAt(0, Location::RequiresRegister());
3621 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3622 break;
3623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003624 case DataType::Type::kFloat32:
3625 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003626 locations->SetInAt(0, Location::RequiresFpuRegister());
3627 locations->SetInAt(1, Location::RequiresFpuRegister());
3628 break;
3629 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003630 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003631 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3632 }
3633}
3634
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003635void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003636 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637 return;
3638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003640 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003641 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003643 switch (type) {
3644 default:
3645 // Integer case.
3646 GenerateIntCompare(instruction->GetCondition(), locations);
3647 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003650 GenerateLongCompare(instruction->GetCondition(), locations);
3651 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003653 case DataType::Type::kFloat32:
3654 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003655 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3656 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003657 }
3658}
3659
Alexey Frunze7e99e052015-11-24 19:28:01 -08003660void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3661 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003663
3664 LocationSummary* locations = instruction->GetLocations();
3665 Location second = locations->InAt(1);
3666 DCHECK(second.IsConstant());
3667
3668 Register out = locations->Out().AsRegister<Register>();
3669 Register dividend = locations->InAt(0).AsRegister<Register>();
3670 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3671 DCHECK(imm == 1 || imm == -1);
3672
3673 if (instruction->IsRem()) {
3674 __ Move(out, ZERO);
3675 } else {
3676 if (imm == -1) {
3677 __ Subu(out, ZERO, dividend);
3678 } else if (out != dividend) {
3679 __ Move(out, dividend);
3680 }
3681 }
3682}
3683
3684void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3685 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003687
3688 LocationSummary* locations = instruction->GetLocations();
3689 Location second = locations->InAt(1);
3690 DCHECK(second.IsConstant());
3691
3692 Register out = locations->Out().AsRegister<Register>();
3693 Register dividend = locations->InAt(0).AsRegister<Register>();
3694 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003695 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003696 int ctz_imm = CTZ(abs_imm);
3697
3698 if (instruction->IsDiv()) {
3699 if (ctz_imm == 1) {
3700 // Fast path for division by +/-2, which is very common.
3701 __ Srl(TMP, dividend, 31);
3702 } else {
3703 __ Sra(TMP, dividend, 31);
3704 __ Srl(TMP, TMP, 32 - ctz_imm);
3705 }
3706 __ Addu(out, dividend, TMP);
3707 __ Sra(out, out, ctz_imm);
3708 if (imm < 0) {
3709 __ Subu(out, ZERO, out);
3710 }
3711 } else {
3712 if (ctz_imm == 1) {
3713 // Fast path for modulo +/-2, which is very common.
3714 __ Sra(TMP, dividend, 31);
3715 __ Subu(out, dividend, TMP);
3716 __ Andi(out, out, 1);
3717 __ Addu(out, out, TMP);
3718 } else {
3719 __ Sra(TMP, dividend, 31);
3720 __ Srl(TMP, TMP, 32 - ctz_imm);
3721 __ Addu(out, dividend, TMP);
3722 if (IsUint<16>(abs_imm - 1)) {
3723 __ Andi(out, out, abs_imm - 1);
3724 } else {
3725 __ Sll(out, out, 32 - ctz_imm);
3726 __ Srl(out, out, 32 - ctz_imm);
3727 }
3728 __ Subu(out, out, TMP);
3729 }
3730 }
3731}
3732
3733void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3734 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003735 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003736
3737 LocationSummary* locations = instruction->GetLocations();
3738 Location second = locations->InAt(1);
3739 DCHECK(second.IsConstant());
3740
3741 Register out = locations->Out().AsRegister<Register>();
3742 Register dividend = locations->InAt(0).AsRegister<Register>();
3743 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3744
3745 int64_t magic;
3746 int shift;
3747 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3748
3749 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3750
3751 __ LoadConst32(TMP, magic);
3752 if (isR6) {
3753 __ MuhR6(TMP, dividend, TMP);
3754 } else {
3755 __ MultR2(dividend, TMP);
3756 __ Mfhi(TMP);
3757 }
3758 if (imm > 0 && magic < 0) {
3759 __ Addu(TMP, TMP, dividend);
3760 } else if (imm < 0 && magic > 0) {
3761 __ Subu(TMP, TMP, dividend);
3762 }
3763
3764 if (shift != 0) {
3765 __ Sra(TMP, TMP, shift);
3766 }
3767
3768 if (instruction->IsDiv()) {
3769 __ Sra(out, TMP, 31);
3770 __ Subu(out, TMP, out);
3771 } else {
3772 __ Sra(AT, TMP, 31);
3773 __ Subu(AT, TMP, AT);
3774 __ LoadConst32(TMP, imm);
3775 if (isR6) {
3776 __ MulR6(TMP, AT, TMP);
3777 } else {
3778 __ MulR2(TMP, AT, TMP);
3779 }
3780 __ Subu(out, dividend, TMP);
3781 }
3782}
3783
3784void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3785 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003787
3788 LocationSummary* locations = instruction->GetLocations();
3789 Register out = locations->Out().AsRegister<Register>();
3790 Location second = locations->InAt(1);
3791
3792 if (second.IsConstant()) {
3793 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3794 if (imm == 0) {
3795 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3796 } else if (imm == 1 || imm == -1) {
3797 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003798 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003799 DivRemByPowerOfTwo(instruction);
3800 } else {
3801 DCHECK(imm <= -2 || imm >= 2);
3802 GenerateDivRemWithAnyConstant(instruction);
3803 }
3804 } else {
3805 Register dividend = locations->InAt(0).AsRegister<Register>();
3806 Register divisor = second.AsRegister<Register>();
3807 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3808 if (instruction->IsDiv()) {
3809 if (isR6) {
3810 __ DivR6(out, dividend, divisor);
3811 } else {
3812 __ DivR2(out, dividend, divisor);
3813 }
3814 } else {
3815 if (isR6) {
3816 __ ModR6(out, dividend, divisor);
3817 } else {
3818 __ ModR2(out, dividend, divisor);
3819 }
3820 }
3821 }
3822}
3823
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 DataType::Type type = div->GetResultType();
3826 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003827 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003828 : LocationSummary::kNoCall;
3829
Vladimir Markoca6fff82017-10-03 14:49:14 +01003830 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003831
3832 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003834 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003835 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3837 break;
3838
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003839 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003840 InvokeRuntimeCallingConvention calling_convention;
3841 locations->SetInAt(0, Location::RegisterPairLocation(
3842 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3843 locations->SetInAt(1, Location::RegisterPairLocation(
3844 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3845 locations->SetOut(calling_convention.GetReturnLocation(type));
3846 break;
3847 }
3848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003849 case DataType::Type::kFloat32:
3850 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003851 locations->SetInAt(0, Location::RequiresFpuRegister());
3852 locations->SetInAt(1, Location::RequiresFpuRegister());
3853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3854 break;
3855
3856 default:
3857 LOG(FATAL) << "Unexpected div type " << type;
3858 }
3859}
3860
3861void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003863 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003864
3865 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003867 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003868 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003870 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003871 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3872 break;
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 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3877 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3878 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003879 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003880 __ DivS(dst, lhs, rhs);
3881 } else {
3882 __ DivD(dst, lhs, rhs);
3883 }
3884 break;
3885 }
3886 default:
3887 LOG(FATAL) << "Unexpected div type " << type;
3888 }
3889}
3890
3891void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003892 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003893 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003894}
3895
3896void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003897 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003898 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003899 codegen_->AddSlowPath(slow_path);
3900 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003902
3903 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003905 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003906 case DataType::Type::kInt8:
3907 case DataType::Type::kUint16:
3908 case DataType::Type::kInt16:
3909 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003910 if (value.IsConstant()) {
3911 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3912 __ B(slow_path->GetEntryLabel());
3913 } else {
3914 // A division by a non-null constant is valid. We don't need to perform
3915 // any check, so simply fall through.
3916 }
3917 } else {
3918 DCHECK(value.IsRegister()) << value;
3919 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3920 }
3921 break;
3922 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003923 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003924 if (value.IsConstant()) {
3925 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3926 __ B(slow_path->GetEntryLabel());
3927 } else {
3928 // A division by a non-null constant is valid. We don't need to perform
3929 // any check, so simply fall through.
3930 }
3931 } else {
3932 DCHECK(value.IsRegisterPair()) << value;
3933 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3934 __ Beqz(TMP, slow_path->GetEntryLabel());
3935 }
3936 break;
3937 }
3938 default:
3939 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3940 }
3941}
3942
3943void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3944 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003945 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946 locations->SetOut(Location::ConstantLocation(constant));
3947}
3948
3949void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3950 // Will be generated at use site.
3951}
3952
3953void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3954 exit->SetLocations(nullptr);
3955}
3956
3957void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3958}
3959
3960void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3961 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003962 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003963 locations->SetOut(Location::ConstantLocation(constant));
3964}
3965
3966void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3967 // Will be generated at use site.
3968}
3969
3970void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3971 got->SetLocations(nullptr);
3972}
3973
3974void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3975 DCHECK(!successor->IsExitBlock());
3976 HBasicBlock* block = got->GetBlock();
3977 HInstruction* previous = got->GetPrevious();
3978 HLoopInformation* info = block->GetLoopInformation();
3979
3980 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003981 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3982 return;
3983 }
3984 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3985 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3986 }
3987 if (!codegen_->GoesToNextBlock(block, successor)) {
3988 __ B(codegen_->GetLabelOf(successor));
3989 }
3990}
3991
3992void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3993 HandleGoto(got, got->GetSuccessor());
3994}
3995
3996void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3997 try_boundary->SetLocations(nullptr);
3998}
3999
4000void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4001 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4002 if (!successor->IsExitBlock()) {
4003 HandleGoto(try_boundary, successor);
4004 }
4005}
4006
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004007void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4008 LocationSummary* locations) {
4009 Register dst = locations->Out().AsRegister<Register>();
4010 Register lhs = locations->InAt(0).AsRegister<Register>();
4011 Location rhs_location = locations->InAt(1);
4012 Register rhs_reg = ZERO;
4013 int64_t rhs_imm = 0;
4014 bool use_imm = rhs_location.IsConstant();
4015 if (use_imm) {
4016 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4017 } else {
4018 rhs_reg = rhs_location.AsRegister<Register>();
4019 }
4020
4021 switch (cond) {
4022 case kCondEQ:
4023 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004024 if (use_imm && IsInt<16>(-rhs_imm)) {
4025 if (rhs_imm == 0) {
4026 if (cond == kCondEQ) {
4027 __ Sltiu(dst, lhs, 1);
4028 } else {
4029 __ Sltu(dst, ZERO, lhs);
4030 }
4031 } else {
4032 __ Addiu(dst, lhs, -rhs_imm);
4033 if (cond == kCondEQ) {
4034 __ Sltiu(dst, dst, 1);
4035 } else {
4036 __ Sltu(dst, ZERO, dst);
4037 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004038 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004039 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004040 if (use_imm && IsUint<16>(rhs_imm)) {
4041 __ Xori(dst, lhs, rhs_imm);
4042 } else {
4043 if (use_imm) {
4044 rhs_reg = TMP;
4045 __ LoadConst32(rhs_reg, rhs_imm);
4046 }
4047 __ Xor(dst, lhs, rhs_reg);
4048 }
4049 if (cond == kCondEQ) {
4050 __ Sltiu(dst, dst, 1);
4051 } else {
4052 __ Sltu(dst, ZERO, dst);
4053 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004054 }
4055 break;
4056
4057 case kCondLT:
4058 case kCondGE:
4059 if (use_imm && IsInt<16>(rhs_imm)) {
4060 __ Slti(dst, lhs, rhs_imm);
4061 } else {
4062 if (use_imm) {
4063 rhs_reg = TMP;
4064 __ LoadConst32(rhs_reg, rhs_imm);
4065 }
4066 __ Slt(dst, lhs, rhs_reg);
4067 }
4068 if (cond == kCondGE) {
4069 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4070 // only the slt instruction but no sge.
4071 __ Xori(dst, dst, 1);
4072 }
4073 break;
4074
4075 case kCondLE:
4076 case kCondGT:
4077 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4078 // Simulate lhs <= rhs via lhs < rhs + 1.
4079 __ Slti(dst, lhs, rhs_imm + 1);
4080 if (cond == kCondGT) {
4081 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4082 // only the slti instruction but no sgti.
4083 __ Xori(dst, dst, 1);
4084 }
4085 } else {
4086 if (use_imm) {
4087 rhs_reg = TMP;
4088 __ LoadConst32(rhs_reg, rhs_imm);
4089 }
4090 __ Slt(dst, rhs_reg, lhs);
4091 if (cond == kCondLE) {
4092 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4093 // only the slt instruction but no sle.
4094 __ Xori(dst, dst, 1);
4095 }
4096 }
4097 break;
4098
4099 case kCondB:
4100 case kCondAE:
4101 if (use_imm && IsInt<16>(rhs_imm)) {
4102 // Sltiu sign-extends its 16-bit immediate operand before
4103 // the comparison and thus lets us compare directly with
4104 // unsigned values in the ranges [0, 0x7fff] and
4105 // [0xffff8000, 0xffffffff].
4106 __ Sltiu(dst, lhs, rhs_imm);
4107 } else {
4108 if (use_imm) {
4109 rhs_reg = TMP;
4110 __ LoadConst32(rhs_reg, rhs_imm);
4111 }
4112 __ Sltu(dst, lhs, rhs_reg);
4113 }
4114 if (cond == kCondAE) {
4115 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4116 // only the sltu instruction but no sgeu.
4117 __ Xori(dst, dst, 1);
4118 }
4119 break;
4120
4121 case kCondBE:
4122 case kCondA:
4123 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4124 // Simulate lhs <= rhs via lhs < rhs + 1.
4125 // Note that this only works if rhs + 1 does not overflow
4126 // to 0, hence the check above.
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 + 1);
4132 if (cond == kCondA) {
4133 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4134 // only the sltiu instruction but no sgtiu.
4135 __ Xori(dst, dst, 1);
4136 }
4137 } else {
4138 if (use_imm) {
4139 rhs_reg = TMP;
4140 __ LoadConst32(rhs_reg, rhs_imm);
4141 }
4142 __ Sltu(dst, rhs_reg, lhs);
4143 if (cond == kCondBE) {
4144 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4145 // only the sltu instruction but no sleu.
4146 __ Xori(dst, dst, 1);
4147 }
4148 }
4149 break;
4150 }
4151}
4152
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004153bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4154 LocationSummary* input_locations,
4155 Register dst) {
4156 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4157 Location rhs_location = input_locations->InAt(1);
4158 Register rhs_reg = ZERO;
4159 int64_t rhs_imm = 0;
4160 bool use_imm = rhs_location.IsConstant();
4161 if (use_imm) {
4162 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4163 } else {
4164 rhs_reg = rhs_location.AsRegister<Register>();
4165 }
4166
4167 switch (cond) {
4168 case kCondEQ:
4169 case kCondNE:
4170 if (use_imm && IsInt<16>(-rhs_imm)) {
4171 __ Addiu(dst, lhs, -rhs_imm);
4172 } else if (use_imm && IsUint<16>(rhs_imm)) {
4173 __ Xori(dst, lhs, rhs_imm);
4174 } else {
4175 if (use_imm) {
4176 rhs_reg = TMP;
4177 __ LoadConst32(rhs_reg, rhs_imm);
4178 }
4179 __ Xor(dst, lhs, rhs_reg);
4180 }
4181 return (cond == kCondEQ);
4182
4183 case kCondLT:
4184 case kCondGE:
4185 if (use_imm && IsInt<16>(rhs_imm)) {
4186 __ Slti(dst, lhs, rhs_imm);
4187 } else {
4188 if (use_imm) {
4189 rhs_reg = TMP;
4190 __ LoadConst32(rhs_reg, rhs_imm);
4191 }
4192 __ Slt(dst, lhs, rhs_reg);
4193 }
4194 return (cond == kCondGE);
4195
4196 case kCondLE:
4197 case kCondGT:
4198 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4199 // Simulate lhs <= rhs via lhs < rhs + 1.
4200 __ Slti(dst, lhs, rhs_imm + 1);
4201 return (cond == kCondGT);
4202 } else {
4203 if (use_imm) {
4204 rhs_reg = TMP;
4205 __ LoadConst32(rhs_reg, rhs_imm);
4206 }
4207 __ Slt(dst, rhs_reg, lhs);
4208 return (cond == kCondLE);
4209 }
4210
4211 case kCondB:
4212 case kCondAE:
4213 if (use_imm && IsInt<16>(rhs_imm)) {
4214 // Sltiu sign-extends its 16-bit immediate operand before
4215 // the comparison and thus lets us compare directly with
4216 // unsigned values in the ranges [0, 0x7fff] and
4217 // [0xffff8000, 0xffffffff].
4218 __ Sltiu(dst, lhs, rhs_imm);
4219 } else {
4220 if (use_imm) {
4221 rhs_reg = TMP;
4222 __ LoadConst32(rhs_reg, rhs_imm);
4223 }
4224 __ Sltu(dst, lhs, rhs_reg);
4225 }
4226 return (cond == kCondAE);
4227
4228 case kCondBE:
4229 case kCondA:
4230 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4231 // Simulate lhs <= rhs via lhs < rhs + 1.
4232 // Note that this only works if rhs + 1 does not overflow
4233 // to 0, hence the check above.
4234 // Sltiu sign-extends its 16-bit immediate operand before
4235 // the comparison and thus lets us compare directly with
4236 // unsigned values in the ranges [0, 0x7fff] and
4237 // [0xffff8000, 0xffffffff].
4238 __ Sltiu(dst, lhs, rhs_imm + 1);
4239 return (cond == kCondA);
4240 } else {
4241 if (use_imm) {
4242 rhs_reg = TMP;
4243 __ LoadConst32(rhs_reg, rhs_imm);
4244 }
4245 __ Sltu(dst, rhs_reg, lhs);
4246 return (cond == kCondBE);
4247 }
4248 }
4249}
4250
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004251void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4252 LocationSummary* locations,
4253 MipsLabel* label) {
4254 Register lhs = locations->InAt(0).AsRegister<Register>();
4255 Location rhs_location = locations->InAt(1);
4256 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004257 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004258 bool use_imm = rhs_location.IsConstant();
4259 if (use_imm) {
4260 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4261 } else {
4262 rhs_reg = rhs_location.AsRegister<Register>();
4263 }
4264
4265 if (use_imm && rhs_imm == 0) {
4266 switch (cond) {
4267 case kCondEQ:
4268 case kCondBE: // <= 0 if zero
4269 __ Beqz(lhs, label);
4270 break;
4271 case kCondNE:
4272 case kCondA: // > 0 if non-zero
4273 __ Bnez(lhs, label);
4274 break;
4275 case kCondLT:
4276 __ Bltz(lhs, label);
4277 break;
4278 case kCondGE:
4279 __ Bgez(lhs, label);
4280 break;
4281 case kCondLE:
4282 __ Blez(lhs, label);
4283 break;
4284 case kCondGT:
4285 __ Bgtz(lhs, label);
4286 break;
4287 case kCondB: // always false
4288 break;
4289 case kCondAE: // always true
4290 __ B(label);
4291 break;
4292 }
4293 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004294 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4295 if (isR6 || !use_imm) {
4296 if (use_imm) {
4297 rhs_reg = TMP;
4298 __ LoadConst32(rhs_reg, rhs_imm);
4299 }
4300 switch (cond) {
4301 case kCondEQ:
4302 __ Beq(lhs, rhs_reg, label);
4303 break;
4304 case kCondNE:
4305 __ Bne(lhs, rhs_reg, label);
4306 break;
4307 case kCondLT:
4308 __ Blt(lhs, rhs_reg, label);
4309 break;
4310 case kCondGE:
4311 __ Bge(lhs, rhs_reg, label);
4312 break;
4313 case kCondLE:
4314 __ Bge(rhs_reg, lhs, label);
4315 break;
4316 case kCondGT:
4317 __ Blt(rhs_reg, lhs, label);
4318 break;
4319 case kCondB:
4320 __ Bltu(lhs, rhs_reg, label);
4321 break;
4322 case kCondAE:
4323 __ Bgeu(lhs, rhs_reg, label);
4324 break;
4325 case kCondBE:
4326 __ Bgeu(rhs_reg, lhs, label);
4327 break;
4328 case kCondA:
4329 __ Bltu(rhs_reg, lhs, label);
4330 break;
4331 }
4332 } else {
4333 // Special cases for more efficient comparison with constants on R2.
4334 switch (cond) {
4335 case kCondEQ:
4336 __ LoadConst32(TMP, rhs_imm);
4337 __ Beq(lhs, TMP, label);
4338 break;
4339 case kCondNE:
4340 __ LoadConst32(TMP, rhs_imm);
4341 __ Bne(lhs, TMP, label);
4342 break;
4343 case kCondLT:
4344 if (IsInt<16>(rhs_imm)) {
4345 __ Slti(TMP, lhs, rhs_imm);
4346 __ Bnez(TMP, label);
4347 } else {
4348 __ LoadConst32(TMP, rhs_imm);
4349 __ Blt(lhs, TMP, label);
4350 }
4351 break;
4352 case kCondGE:
4353 if (IsInt<16>(rhs_imm)) {
4354 __ Slti(TMP, lhs, rhs_imm);
4355 __ Beqz(TMP, label);
4356 } else {
4357 __ LoadConst32(TMP, rhs_imm);
4358 __ Bge(lhs, TMP, label);
4359 }
4360 break;
4361 case kCondLE:
4362 if (IsInt<16>(rhs_imm + 1)) {
4363 // Simulate lhs <= rhs via lhs < rhs + 1.
4364 __ Slti(TMP, lhs, rhs_imm + 1);
4365 __ Bnez(TMP, label);
4366 } else {
4367 __ LoadConst32(TMP, rhs_imm);
4368 __ Bge(TMP, lhs, label);
4369 }
4370 break;
4371 case kCondGT:
4372 if (IsInt<16>(rhs_imm + 1)) {
4373 // Simulate lhs > rhs via !(lhs < rhs + 1).
4374 __ Slti(TMP, lhs, rhs_imm + 1);
4375 __ Beqz(TMP, label);
4376 } else {
4377 __ LoadConst32(TMP, rhs_imm);
4378 __ Blt(TMP, lhs, label);
4379 }
4380 break;
4381 case kCondB:
4382 if (IsInt<16>(rhs_imm)) {
4383 __ Sltiu(TMP, lhs, rhs_imm);
4384 __ Bnez(TMP, label);
4385 } else {
4386 __ LoadConst32(TMP, rhs_imm);
4387 __ Bltu(lhs, TMP, label);
4388 }
4389 break;
4390 case kCondAE:
4391 if (IsInt<16>(rhs_imm)) {
4392 __ Sltiu(TMP, lhs, rhs_imm);
4393 __ Beqz(TMP, label);
4394 } else {
4395 __ LoadConst32(TMP, rhs_imm);
4396 __ Bgeu(lhs, TMP, label);
4397 }
4398 break;
4399 case kCondBE:
4400 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4401 // Simulate lhs <= rhs via lhs < rhs + 1.
4402 // Note that this only works if rhs + 1 does not overflow
4403 // to 0, hence the check above.
4404 __ Sltiu(TMP, lhs, rhs_imm + 1);
4405 __ Bnez(TMP, label);
4406 } else {
4407 __ LoadConst32(TMP, rhs_imm);
4408 __ Bgeu(TMP, lhs, label);
4409 }
4410 break;
4411 case kCondA:
4412 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4413 // Simulate lhs > rhs via !(lhs < rhs + 1).
4414 // Note that this only works if rhs + 1 does not overflow
4415 // to 0, hence the check above.
4416 __ Sltiu(TMP, lhs, rhs_imm + 1);
4417 __ Beqz(TMP, label);
4418 } else {
4419 __ LoadConst32(TMP, rhs_imm);
4420 __ Bltu(TMP, lhs, label);
4421 }
4422 break;
4423 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004424 }
4425 }
4426}
4427
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004428void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4429 LocationSummary* locations) {
4430 Register dst = locations->Out().AsRegister<Register>();
4431 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4432 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4433 Location rhs_location = locations->InAt(1);
4434 Register rhs_high = ZERO;
4435 Register rhs_low = ZERO;
4436 int64_t imm = 0;
4437 uint32_t imm_high = 0;
4438 uint32_t imm_low = 0;
4439 bool use_imm = rhs_location.IsConstant();
4440 if (use_imm) {
4441 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4442 imm_high = High32Bits(imm);
4443 imm_low = Low32Bits(imm);
4444 } else {
4445 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4446 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4447 }
4448 if (use_imm && imm == 0) {
4449 switch (cond) {
4450 case kCondEQ:
4451 case kCondBE: // <= 0 if zero
4452 __ Or(dst, lhs_high, lhs_low);
4453 __ Sltiu(dst, dst, 1);
4454 break;
4455 case kCondNE:
4456 case kCondA: // > 0 if non-zero
4457 __ Or(dst, lhs_high, lhs_low);
4458 __ Sltu(dst, ZERO, dst);
4459 break;
4460 case kCondLT:
4461 __ Slt(dst, lhs_high, ZERO);
4462 break;
4463 case kCondGE:
4464 __ Slt(dst, lhs_high, ZERO);
4465 __ Xori(dst, dst, 1);
4466 break;
4467 case kCondLE:
4468 __ Or(TMP, lhs_high, lhs_low);
4469 __ Sra(AT, lhs_high, 31);
4470 __ Sltu(dst, AT, TMP);
4471 __ Xori(dst, dst, 1);
4472 break;
4473 case kCondGT:
4474 __ Or(TMP, lhs_high, lhs_low);
4475 __ Sra(AT, lhs_high, 31);
4476 __ Sltu(dst, AT, TMP);
4477 break;
4478 case kCondB: // always false
4479 __ Andi(dst, dst, 0);
4480 break;
4481 case kCondAE: // always true
4482 __ Ori(dst, ZERO, 1);
4483 break;
4484 }
4485 } else if (use_imm) {
4486 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4487 switch (cond) {
4488 case kCondEQ:
4489 __ LoadConst32(TMP, imm_high);
4490 __ Xor(TMP, TMP, lhs_high);
4491 __ LoadConst32(AT, imm_low);
4492 __ Xor(AT, AT, lhs_low);
4493 __ Or(dst, TMP, AT);
4494 __ Sltiu(dst, dst, 1);
4495 break;
4496 case kCondNE:
4497 __ LoadConst32(TMP, imm_high);
4498 __ Xor(TMP, TMP, lhs_high);
4499 __ LoadConst32(AT, imm_low);
4500 __ Xor(AT, AT, lhs_low);
4501 __ Or(dst, TMP, AT);
4502 __ Sltu(dst, ZERO, dst);
4503 break;
4504 case kCondLT:
4505 case kCondGE:
4506 if (dst == lhs_low) {
4507 __ LoadConst32(TMP, imm_low);
4508 __ Sltu(dst, lhs_low, TMP);
4509 }
4510 __ LoadConst32(TMP, imm_high);
4511 __ Slt(AT, lhs_high, TMP);
4512 __ Slt(TMP, TMP, lhs_high);
4513 if (dst != lhs_low) {
4514 __ LoadConst32(dst, imm_low);
4515 __ Sltu(dst, lhs_low, dst);
4516 }
4517 __ Slt(dst, TMP, dst);
4518 __ Or(dst, dst, AT);
4519 if (cond == kCondGE) {
4520 __ Xori(dst, dst, 1);
4521 }
4522 break;
4523 case kCondGT:
4524 case kCondLE:
4525 if (dst == lhs_low) {
4526 __ LoadConst32(TMP, imm_low);
4527 __ Sltu(dst, TMP, lhs_low);
4528 }
4529 __ LoadConst32(TMP, imm_high);
4530 __ Slt(AT, TMP, lhs_high);
4531 __ Slt(TMP, lhs_high, TMP);
4532 if (dst != lhs_low) {
4533 __ LoadConst32(dst, imm_low);
4534 __ Sltu(dst, dst, lhs_low);
4535 }
4536 __ Slt(dst, TMP, dst);
4537 __ Or(dst, dst, AT);
4538 if (cond == kCondLE) {
4539 __ Xori(dst, dst, 1);
4540 }
4541 break;
4542 case kCondB:
4543 case kCondAE:
4544 if (dst == lhs_low) {
4545 __ LoadConst32(TMP, imm_low);
4546 __ Sltu(dst, lhs_low, TMP);
4547 }
4548 __ LoadConst32(TMP, imm_high);
4549 __ Sltu(AT, lhs_high, TMP);
4550 __ Sltu(TMP, TMP, lhs_high);
4551 if (dst != lhs_low) {
4552 __ LoadConst32(dst, imm_low);
4553 __ Sltu(dst, lhs_low, dst);
4554 }
4555 __ Slt(dst, TMP, dst);
4556 __ Or(dst, dst, AT);
4557 if (cond == kCondAE) {
4558 __ Xori(dst, dst, 1);
4559 }
4560 break;
4561 case kCondA:
4562 case kCondBE:
4563 if (dst == lhs_low) {
4564 __ LoadConst32(TMP, imm_low);
4565 __ Sltu(dst, TMP, lhs_low);
4566 }
4567 __ LoadConst32(TMP, imm_high);
4568 __ Sltu(AT, TMP, lhs_high);
4569 __ Sltu(TMP, lhs_high, TMP);
4570 if (dst != lhs_low) {
4571 __ LoadConst32(dst, imm_low);
4572 __ Sltu(dst, dst, lhs_low);
4573 }
4574 __ Slt(dst, TMP, dst);
4575 __ Or(dst, dst, AT);
4576 if (cond == kCondBE) {
4577 __ Xori(dst, dst, 1);
4578 }
4579 break;
4580 }
4581 } else {
4582 switch (cond) {
4583 case kCondEQ:
4584 __ Xor(TMP, lhs_high, rhs_high);
4585 __ Xor(AT, lhs_low, rhs_low);
4586 __ Or(dst, TMP, AT);
4587 __ Sltiu(dst, dst, 1);
4588 break;
4589 case kCondNE:
4590 __ Xor(TMP, lhs_high, rhs_high);
4591 __ Xor(AT, lhs_low, rhs_low);
4592 __ Or(dst, TMP, AT);
4593 __ Sltu(dst, ZERO, dst);
4594 break;
4595 case kCondLT:
4596 case kCondGE:
4597 __ Slt(TMP, rhs_high, lhs_high);
4598 __ Sltu(AT, lhs_low, rhs_low);
4599 __ Slt(TMP, TMP, AT);
4600 __ Slt(AT, lhs_high, rhs_high);
4601 __ Or(dst, AT, TMP);
4602 if (cond == kCondGE) {
4603 __ Xori(dst, dst, 1);
4604 }
4605 break;
4606 case kCondGT:
4607 case kCondLE:
4608 __ Slt(TMP, lhs_high, rhs_high);
4609 __ Sltu(AT, rhs_low, lhs_low);
4610 __ Slt(TMP, TMP, AT);
4611 __ Slt(AT, rhs_high, lhs_high);
4612 __ Or(dst, AT, TMP);
4613 if (cond == kCondLE) {
4614 __ Xori(dst, dst, 1);
4615 }
4616 break;
4617 case kCondB:
4618 case kCondAE:
4619 __ Sltu(TMP, rhs_high, lhs_high);
4620 __ Sltu(AT, lhs_low, rhs_low);
4621 __ Slt(TMP, TMP, AT);
4622 __ Sltu(AT, lhs_high, rhs_high);
4623 __ Or(dst, AT, TMP);
4624 if (cond == kCondAE) {
4625 __ Xori(dst, dst, 1);
4626 }
4627 break;
4628 case kCondA:
4629 case kCondBE:
4630 __ Sltu(TMP, lhs_high, rhs_high);
4631 __ Sltu(AT, rhs_low, lhs_low);
4632 __ Slt(TMP, TMP, AT);
4633 __ Sltu(AT, rhs_high, lhs_high);
4634 __ Or(dst, AT, TMP);
4635 if (cond == kCondBE) {
4636 __ Xori(dst, dst, 1);
4637 }
4638 break;
4639 }
4640 }
4641}
4642
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004643void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4644 LocationSummary* locations,
4645 MipsLabel* label) {
4646 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4647 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4648 Location rhs_location = locations->InAt(1);
4649 Register rhs_high = ZERO;
4650 Register rhs_low = ZERO;
4651 int64_t imm = 0;
4652 uint32_t imm_high = 0;
4653 uint32_t imm_low = 0;
4654 bool use_imm = rhs_location.IsConstant();
4655 if (use_imm) {
4656 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4657 imm_high = High32Bits(imm);
4658 imm_low = Low32Bits(imm);
4659 } else {
4660 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4661 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4662 }
4663
4664 if (use_imm && imm == 0) {
4665 switch (cond) {
4666 case kCondEQ:
4667 case kCondBE: // <= 0 if zero
4668 __ Or(TMP, lhs_high, lhs_low);
4669 __ Beqz(TMP, label);
4670 break;
4671 case kCondNE:
4672 case kCondA: // > 0 if non-zero
4673 __ Or(TMP, lhs_high, lhs_low);
4674 __ Bnez(TMP, label);
4675 break;
4676 case kCondLT:
4677 __ Bltz(lhs_high, label);
4678 break;
4679 case kCondGE:
4680 __ Bgez(lhs_high, label);
4681 break;
4682 case kCondLE:
4683 __ Or(TMP, lhs_high, lhs_low);
4684 __ Sra(AT, lhs_high, 31);
4685 __ Bgeu(AT, TMP, label);
4686 break;
4687 case kCondGT:
4688 __ Or(TMP, lhs_high, lhs_low);
4689 __ Sra(AT, lhs_high, 31);
4690 __ Bltu(AT, TMP, label);
4691 break;
4692 case kCondB: // always false
4693 break;
4694 case kCondAE: // always true
4695 __ B(label);
4696 break;
4697 }
4698 } else if (use_imm) {
4699 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4700 switch (cond) {
4701 case kCondEQ:
4702 __ LoadConst32(TMP, imm_high);
4703 __ Xor(TMP, TMP, lhs_high);
4704 __ LoadConst32(AT, imm_low);
4705 __ Xor(AT, AT, lhs_low);
4706 __ Or(TMP, TMP, AT);
4707 __ Beqz(TMP, label);
4708 break;
4709 case kCondNE:
4710 __ LoadConst32(TMP, imm_high);
4711 __ Xor(TMP, TMP, lhs_high);
4712 __ LoadConst32(AT, imm_low);
4713 __ Xor(AT, AT, lhs_low);
4714 __ Or(TMP, TMP, AT);
4715 __ Bnez(TMP, label);
4716 break;
4717 case kCondLT:
4718 __ LoadConst32(TMP, imm_high);
4719 __ Blt(lhs_high, TMP, label);
4720 __ Slt(TMP, TMP, lhs_high);
4721 __ LoadConst32(AT, imm_low);
4722 __ Sltu(AT, lhs_low, AT);
4723 __ Blt(TMP, AT, label);
4724 break;
4725 case kCondGE:
4726 __ LoadConst32(TMP, imm_high);
4727 __ Blt(TMP, lhs_high, label);
4728 __ Slt(TMP, lhs_high, TMP);
4729 __ LoadConst32(AT, imm_low);
4730 __ Sltu(AT, lhs_low, AT);
4731 __ Or(TMP, TMP, AT);
4732 __ Beqz(TMP, label);
4733 break;
4734 case kCondLE:
4735 __ LoadConst32(TMP, imm_high);
4736 __ Blt(lhs_high, TMP, label);
4737 __ Slt(TMP, TMP, lhs_high);
4738 __ LoadConst32(AT, imm_low);
4739 __ Sltu(AT, AT, lhs_low);
4740 __ Or(TMP, TMP, AT);
4741 __ Beqz(TMP, label);
4742 break;
4743 case kCondGT:
4744 __ LoadConst32(TMP, imm_high);
4745 __ Blt(TMP, lhs_high, label);
4746 __ Slt(TMP, lhs_high, TMP);
4747 __ LoadConst32(AT, imm_low);
4748 __ Sltu(AT, AT, lhs_low);
4749 __ Blt(TMP, AT, label);
4750 break;
4751 case kCondB:
4752 __ LoadConst32(TMP, imm_high);
4753 __ Bltu(lhs_high, TMP, label);
4754 __ Sltu(TMP, TMP, lhs_high);
4755 __ LoadConst32(AT, imm_low);
4756 __ Sltu(AT, lhs_low, AT);
4757 __ Blt(TMP, AT, label);
4758 break;
4759 case kCondAE:
4760 __ LoadConst32(TMP, imm_high);
4761 __ Bltu(TMP, lhs_high, label);
4762 __ Sltu(TMP, lhs_high, TMP);
4763 __ LoadConst32(AT, imm_low);
4764 __ Sltu(AT, lhs_low, AT);
4765 __ Or(TMP, TMP, AT);
4766 __ Beqz(TMP, label);
4767 break;
4768 case kCondBE:
4769 __ LoadConst32(TMP, imm_high);
4770 __ Bltu(lhs_high, TMP, label);
4771 __ Sltu(TMP, TMP, lhs_high);
4772 __ LoadConst32(AT, imm_low);
4773 __ Sltu(AT, AT, lhs_low);
4774 __ Or(TMP, TMP, AT);
4775 __ Beqz(TMP, label);
4776 break;
4777 case kCondA:
4778 __ LoadConst32(TMP, imm_high);
4779 __ Bltu(TMP, lhs_high, label);
4780 __ Sltu(TMP, lhs_high, TMP);
4781 __ LoadConst32(AT, imm_low);
4782 __ Sltu(AT, AT, lhs_low);
4783 __ Blt(TMP, AT, label);
4784 break;
4785 }
4786 } else {
4787 switch (cond) {
4788 case kCondEQ:
4789 __ Xor(TMP, lhs_high, rhs_high);
4790 __ Xor(AT, lhs_low, rhs_low);
4791 __ Or(TMP, TMP, AT);
4792 __ Beqz(TMP, label);
4793 break;
4794 case kCondNE:
4795 __ Xor(TMP, lhs_high, rhs_high);
4796 __ Xor(AT, lhs_low, rhs_low);
4797 __ Or(TMP, TMP, AT);
4798 __ Bnez(TMP, label);
4799 break;
4800 case kCondLT:
4801 __ Blt(lhs_high, rhs_high, label);
4802 __ Slt(TMP, rhs_high, lhs_high);
4803 __ Sltu(AT, lhs_low, rhs_low);
4804 __ Blt(TMP, AT, label);
4805 break;
4806 case kCondGE:
4807 __ Blt(rhs_high, lhs_high, label);
4808 __ Slt(TMP, lhs_high, rhs_high);
4809 __ Sltu(AT, lhs_low, rhs_low);
4810 __ Or(TMP, TMP, AT);
4811 __ Beqz(TMP, label);
4812 break;
4813 case kCondLE:
4814 __ Blt(lhs_high, rhs_high, label);
4815 __ Slt(TMP, rhs_high, lhs_high);
4816 __ Sltu(AT, rhs_low, lhs_low);
4817 __ Or(TMP, TMP, AT);
4818 __ Beqz(TMP, label);
4819 break;
4820 case kCondGT:
4821 __ Blt(rhs_high, lhs_high, label);
4822 __ Slt(TMP, lhs_high, rhs_high);
4823 __ Sltu(AT, rhs_low, lhs_low);
4824 __ Blt(TMP, AT, label);
4825 break;
4826 case kCondB:
4827 __ Bltu(lhs_high, rhs_high, label);
4828 __ Sltu(TMP, rhs_high, lhs_high);
4829 __ Sltu(AT, lhs_low, rhs_low);
4830 __ Blt(TMP, AT, label);
4831 break;
4832 case kCondAE:
4833 __ Bltu(rhs_high, lhs_high, label);
4834 __ Sltu(TMP, lhs_high, rhs_high);
4835 __ Sltu(AT, lhs_low, rhs_low);
4836 __ Or(TMP, TMP, AT);
4837 __ Beqz(TMP, label);
4838 break;
4839 case kCondBE:
4840 __ Bltu(lhs_high, rhs_high, label);
4841 __ Sltu(TMP, rhs_high, lhs_high);
4842 __ Sltu(AT, rhs_low, lhs_low);
4843 __ Or(TMP, TMP, AT);
4844 __ Beqz(TMP, label);
4845 break;
4846 case kCondA:
4847 __ Bltu(rhs_high, lhs_high, label);
4848 __ Sltu(TMP, lhs_high, rhs_high);
4849 __ Sltu(AT, rhs_low, lhs_low);
4850 __ Blt(TMP, AT, label);
4851 break;
4852 }
4853 }
4854}
4855
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004856void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4857 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004858 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004859 LocationSummary* locations) {
4860 Register dst = locations->Out().AsRegister<Register>();
4861 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4862 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4863 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004865 if (isR6) {
4866 switch (cond) {
4867 case kCondEQ:
4868 __ CmpEqS(FTMP, lhs, rhs);
4869 __ Mfc1(dst, FTMP);
4870 __ Andi(dst, dst, 1);
4871 break;
4872 case kCondNE:
4873 __ CmpEqS(FTMP, lhs, rhs);
4874 __ Mfc1(dst, FTMP);
4875 __ Addiu(dst, dst, 1);
4876 break;
4877 case kCondLT:
4878 if (gt_bias) {
4879 __ CmpLtS(FTMP, lhs, rhs);
4880 } else {
4881 __ CmpUltS(FTMP, lhs, rhs);
4882 }
4883 __ Mfc1(dst, FTMP);
4884 __ Andi(dst, dst, 1);
4885 break;
4886 case kCondLE:
4887 if (gt_bias) {
4888 __ CmpLeS(FTMP, lhs, rhs);
4889 } else {
4890 __ CmpUleS(FTMP, lhs, rhs);
4891 }
4892 __ Mfc1(dst, FTMP);
4893 __ Andi(dst, dst, 1);
4894 break;
4895 case kCondGT:
4896 if (gt_bias) {
4897 __ CmpUltS(FTMP, rhs, lhs);
4898 } else {
4899 __ CmpLtS(FTMP, rhs, lhs);
4900 }
4901 __ Mfc1(dst, FTMP);
4902 __ Andi(dst, dst, 1);
4903 break;
4904 case kCondGE:
4905 if (gt_bias) {
4906 __ CmpUleS(FTMP, rhs, lhs);
4907 } else {
4908 __ CmpLeS(FTMP, rhs, lhs);
4909 }
4910 __ Mfc1(dst, FTMP);
4911 __ Andi(dst, dst, 1);
4912 break;
4913 default:
4914 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4915 UNREACHABLE();
4916 }
4917 } else {
4918 switch (cond) {
4919 case kCondEQ:
4920 __ CeqS(0, lhs, rhs);
4921 __ LoadConst32(dst, 1);
4922 __ Movf(dst, ZERO, 0);
4923 break;
4924 case kCondNE:
4925 __ CeqS(0, lhs, rhs);
4926 __ LoadConst32(dst, 1);
4927 __ Movt(dst, ZERO, 0);
4928 break;
4929 case kCondLT:
4930 if (gt_bias) {
4931 __ ColtS(0, lhs, rhs);
4932 } else {
4933 __ CultS(0, lhs, rhs);
4934 }
4935 __ LoadConst32(dst, 1);
4936 __ Movf(dst, ZERO, 0);
4937 break;
4938 case kCondLE:
4939 if (gt_bias) {
4940 __ ColeS(0, lhs, rhs);
4941 } else {
4942 __ CuleS(0, lhs, rhs);
4943 }
4944 __ LoadConst32(dst, 1);
4945 __ Movf(dst, ZERO, 0);
4946 break;
4947 case kCondGT:
4948 if (gt_bias) {
4949 __ CultS(0, rhs, lhs);
4950 } else {
4951 __ ColtS(0, rhs, lhs);
4952 }
4953 __ LoadConst32(dst, 1);
4954 __ Movf(dst, ZERO, 0);
4955 break;
4956 case kCondGE:
4957 if (gt_bias) {
4958 __ CuleS(0, rhs, lhs);
4959 } else {
4960 __ ColeS(0, rhs, lhs);
4961 }
4962 __ LoadConst32(dst, 1);
4963 __ Movf(dst, ZERO, 0);
4964 break;
4965 default:
4966 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4967 UNREACHABLE();
4968 }
4969 }
4970 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004972 if (isR6) {
4973 switch (cond) {
4974 case kCondEQ:
4975 __ CmpEqD(FTMP, lhs, rhs);
4976 __ Mfc1(dst, FTMP);
4977 __ Andi(dst, dst, 1);
4978 break;
4979 case kCondNE:
4980 __ CmpEqD(FTMP, lhs, rhs);
4981 __ Mfc1(dst, FTMP);
4982 __ Addiu(dst, dst, 1);
4983 break;
4984 case kCondLT:
4985 if (gt_bias) {
4986 __ CmpLtD(FTMP, lhs, rhs);
4987 } else {
4988 __ CmpUltD(FTMP, lhs, rhs);
4989 }
4990 __ Mfc1(dst, FTMP);
4991 __ Andi(dst, dst, 1);
4992 break;
4993 case kCondLE:
4994 if (gt_bias) {
4995 __ CmpLeD(FTMP, lhs, rhs);
4996 } else {
4997 __ CmpUleD(FTMP, lhs, rhs);
4998 }
4999 __ Mfc1(dst, FTMP);
5000 __ Andi(dst, dst, 1);
5001 break;
5002 case kCondGT:
5003 if (gt_bias) {
5004 __ CmpUltD(FTMP, rhs, lhs);
5005 } else {
5006 __ CmpLtD(FTMP, rhs, lhs);
5007 }
5008 __ Mfc1(dst, FTMP);
5009 __ Andi(dst, dst, 1);
5010 break;
5011 case kCondGE:
5012 if (gt_bias) {
5013 __ CmpUleD(FTMP, rhs, lhs);
5014 } else {
5015 __ CmpLeD(FTMP, rhs, lhs);
5016 }
5017 __ Mfc1(dst, FTMP);
5018 __ Andi(dst, dst, 1);
5019 break;
5020 default:
5021 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5022 UNREACHABLE();
5023 }
5024 } else {
5025 switch (cond) {
5026 case kCondEQ:
5027 __ CeqD(0, lhs, rhs);
5028 __ LoadConst32(dst, 1);
5029 __ Movf(dst, ZERO, 0);
5030 break;
5031 case kCondNE:
5032 __ CeqD(0, lhs, rhs);
5033 __ LoadConst32(dst, 1);
5034 __ Movt(dst, ZERO, 0);
5035 break;
5036 case kCondLT:
5037 if (gt_bias) {
5038 __ ColtD(0, lhs, rhs);
5039 } else {
5040 __ CultD(0, lhs, rhs);
5041 }
5042 __ LoadConst32(dst, 1);
5043 __ Movf(dst, ZERO, 0);
5044 break;
5045 case kCondLE:
5046 if (gt_bias) {
5047 __ ColeD(0, lhs, rhs);
5048 } else {
5049 __ CuleD(0, lhs, rhs);
5050 }
5051 __ LoadConst32(dst, 1);
5052 __ Movf(dst, ZERO, 0);
5053 break;
5054 case kCondGT:
5055 if (gt_bias) {
5056 __ CultD(0, rhs, lhs);
5057 } else {
5058 __ ColtD(0, rhs, lhs);
5059 }
5060 __ LoadConst32(dst, 1);
5061 __ Movf(dst, ZERO, 0);
5062 break;
5063 case kCondGE:
5064 if (gt_bias) {
5065 __ CuleD(0, rhs, lhs);
5066 } else {
5067 __ ColeD(0, rhs, lhs);
5068 }
5069 __ LoadConst32(dst, 1);
5070 __ Movf(dst, ZERO, 0);
5071 break;
5072 default:
5073 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5074 UNREACHABLE();
5075 }
5076 }
5077 }
5078}
5079
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005080bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5081 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005082 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005083 LocationSummary* input_locations,
5084 int cc) {
5085 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5086 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5087 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005088 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005089 switch (cond) {
5090 case kCondEQ:
5091 __ CeqS(cc, lhs, rhs);
5092 return false;
5093 case kCondNE:
5094 __ CeqS(cc, lhs, rhs);
5095 return true;
5096 case kCondLT:
5097 if (gt_bias) {
5098 __ ColtS(cc, lhs, rhs);
5099 } else {
5100 __ CultS(cc, lhs, rhs);
5101 }
5102 return false;
5103 case kCondLE:
5104 if (gt_bias) {
5105 __ ColeS(cc, lhs, rhs);
5106 } else {
5107 __ CuleS(cc, lhs, rhs);
5108 }
5109 return false;
5110 case kCondGT:
5111 if (gt_bias) {
5112 __ CultS(cc, rhs, lhs);
5113 } else {
5114 __ ColtS(cc, rhs, lhs);
5115 }
5116 return false;
5117 case kCondGE:
5118 if (gt_bias) {
5119 __ CuleS(cc, rhs, lhs);
5120 } else {
5121 __ ColeS(cc, rhs, lhs);
5122 }
5123 return false;
5124 default:
5125 LOG(FATAL) << "Unexpected non-floating-point condition";
5126 UNREACHABLE();
5127 }
5128 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005129 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005130 switch (cond) {
5131 case kCondEQ:
5132 __ CeqD(cc, lhs, rhs);
5133 return false;
5134 case kCondNE:
5135 __ CeqD(cc, lhs, rhs);
5136 return true;
5137 case kCondLT:
5138 if (gt_bias) {
5139 __ ColtD(cc, lhs, rhs);
5140 } else {
5141 __ CultD(cc, lhs, rhs);
5142 }
5143 return false;
5144 case kCondLE:
5145 if (gt_bias) {
5146 __ ColeD(cc, lhs, rhs);
5147 } else {
5148 __ CuleD(cc, lhs, rhs);
5149 }
5150 return false;
5151 case kCondGT:
5152 if (gt_bias) {
5153 __ CultD(cc, rhs, lhs);
5154 } else {
5155 __ ColtD(cc, rhs, lhs);
5156 }
5157 return false;
5158 case kCondGE:
5159 if (gt_bias) {
5160 __ CuleD(cc, rhs, lhs);
5161 } else {
5162 __ ColeD(cc, rhs, lhs);
5163 }
5164 return false;
5165 default:
5166 LOG(FATAL) << "Unexpected non-floating-point condition";
5167 UNREACHABLE();
5168 }
5169 }
5170}
5171
5172bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5173 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005174 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005175 LocationSummary* input_locations,
5176 FRegister dst) {
5177 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5178 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5179 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005180 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005181 switch (cond) {
5182 case kCondEQ:
5183 __ CmpEqS(dst, lhs, rhs);
5184 return false;
5185 case kCondNE:
5186 __ CmpEqS(dst, lhs, rhs);
5187 return true;
5188 case kCondLT:
5189 if (gt_bias) {
5190 __ CmpLtS(dst, lhs, rhs);
5191 } else {
5192 __ CmpUltS(dst, lhs, rhs);
5193 }
5194 return false;
5195 case kCondLE:
5196 if (gt_bias) {
5197 __ CmpLeS(dst, lhs, rhs);
5198 } else {
5199 __ CmpUleS(dst, lhs, rhs);
5200 }
5201 return false;
5202 case kCondGT:
5203 if (gt_bias) {
5204 __ CmpUltS(dst, rhs, lhs);
5205 } else {
5206 __ CmpLtS(dst, rhs, lhs);
5207 }
5208 return false;
5209 case kCondGE:
5210 if (gt_bias) {
5211 __ CmpUleS(dst, rhs, lhs);
5212 } else {
5213 __ CmpLeS(dst, rhs, lhs);
5214 }
5215 return false;
5216 default:
5217 LOG(FATAL) << "Unexpected non-floating-point condition";
5218 UNREACHABLE();
5219 }
5220 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005221 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005222 switch (cond) {
5223 case kCondEQ:
5224 __ CmpEqD(dst, lhs, rhs);
5225 return false;
5226 case kCondNE:
5227 __ CmpEqD(dst, lhs, rhs);
5228 return true;
5229 case kCondLT:
5230 if (gt_bias) {
5231 __ CmpLtD(dst, lhs, rhs);
5232 } else {
5233 __ CmpUltD(dst, lhs, rhs);
5234 }
5235 return false;
5236 case kCondLE:
5237 if (gt_bias) {
5238 __ CmpLeD(dst, lhs, rhs);
5239 } else {
5240 __ CmpUleD(dst, lhs, rhs);
5241 }
5242 return false;
5243 case kCondGT:
5244 if (gt_bias) {
5245 __ CmpUltD(dst, rhs, lhs);
5246 } else {
5247 __ CmpLtD(dst, rhs, lhs);
5248 }
5249 return false;
5250 case kCondGE:
5251 if (gt_bias) {
5252 __ CmpUleD(dst, rhs, lhs);
5253 } else {
5254 __ CmpLeD(dst, rhs, lhs);
5255 }
5256 return false;
5257 default:
5258 LOG(FATAL) << "Unexpected non-floating-point condition";
5259 UNREACHABLE();
5260 }
5261 }
5262}
5263
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005264void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5265 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005267 LocationSummary* locations,
5268 MipsLabel* label) {
5269 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5270 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5271 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005273 if (isR6) {
5274 switch (cond) {
5275 case kCondEQ:
5276 __ CmpEqS(FTMP, lhs, rhs);
5277 __ Bc1nez(FTMP, label);
5278 break;
5279 case kCondNE:
5280 __ CmpEqS(FTMP, lhs, rhs);
5281 __ Bc1eqz(FTMP, label);
5282 break;
5283 case kCondLT:
5284 if (gt_bias) {
5285 __ CmpLtS(FTMP, lhs, rhs);
5286 } else {
5287 __ CmpUltS(FTMP, lhs, rhs);
5288 }
5289 __ Bc1nez(FTMP, label);
5290 break;
5291 case kCondLE:
5292 if (gt_bias) {
5293 __ CmpLeS(FTMP, lhs, rhs);
5294 } else {
5295 __ CmpUleS(FTMP, lhs, rhs);
5296 }
5297 __ Bc1nez(FTMP, label);
5298 break;
5299 case kCondGT:
5300 if (gt_bias) {
5301 __ CmpUltS(FTMP, rhs, lhs);
5302 } else {
5303 __ CmpLtS(FTMP, rhs, lhs);
5304 }
5305 __ Bc1nez(FTMP, label);
5306 break;
5307 case kCondGE:
5308 if (gt_bias) {
5309 __ CmpUleS(FTMP, rhs, lhs);
5310 } else {
5311 __ CmpLeS(FTMP, rhs, lhs);
5312 }
5313 __ Bc1nez(FTMP, label);
5314 break;
5315 default:
5316 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005317 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005318 }
5319 } else {
5320 switch (cond) {
5321 case kCondEQ:
5322 __ CeqS(0, lhs, rhs);
5323 __ Bc1t(0, label);
5324 break;
5325 case kCondNE:
5326 __ CeqS(0, lhs, rhs);
5327 __ Bc1f(0, label);
5328 break;
5329 case kCondLT:
5330 if (gt_bias) {
5331 __ ColtS(0, lhs, rhs);
5332 } else {
5333 __ CultS(0, lhs, rhs);
5334 }
5335 __ Bc1t(0, label);
5336 break;
5337 case kCondLE:
5338 if (gt_bias) {
5339 __ ColeS(0, lhs, rhs);
5340 } else {
5341 __ CuleS(0, lhs, rhs);
5342 }
5343 __ Bc1t(0, label);
5344 break;
5345 case kCondGT:
5346 if (gt_bias) {
5347 __ CultS(0, rhs, lhs);
5348 } else {
5349 __ ColtS(0, rhs, lhs);
5350 }
5351 __ Bc1t(0, label);
5352 break;
5353 case kCondGE:
5354 if (gt_bias) {
5355 __ CuleS(0, rhs, lhs);
5356 } else {
5357 __ ColeS(0, rhs, lhs);
5358 }
5359 __ Bc1t(0, label);
5360 break;
5361 default:
5362 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005363 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005364 }
5365 }
5366 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005367 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005368 if (isR6) {
5369 switch (cond) {
5370 case kCondEQ:
5371 __ CmpEqD(FTMP, lhs, rhs);
5372 __ Bc1nez(FTMP, label);
5373 break;
5374 case kCondNE:
5375 __ CmpEqD(FTMP, lhs, rhs);
5376 __ Bc1eqz(FTMP, label);
5377 break;
5378 case kCondLT:
5379 if (gt_bias) {
5380 __ CmpLtD(FTMP, lhs, rhs);
5381 } else {
5382 __ CmpUltD(FTMP, lhs, rhs);
5383 }
5384 __ Bc1nez(FTMP, label);
5385 break;
5386 case kCondLE:
5387 if (gt_bias) {
5388 __ CmpLeD(FTMP, lhs, rhs);
5389 } else {
5390 __ CmpUleD(FTMP, lhs, rhs);
5391 }
5392 __ Bc1nez(FTMP, label);
5393 break;
5394 case kCondGT:
5395 if (gt_bias) {
5396 __ CmpUltD(FTMP, rhs, lhs);
5397 } else {
5398 __ CmpLtD(FTMP, rhs, lhs);
5399 }
5400 __ Bc1nez(FTMP, label);
5401 break;
5402 case kCondGE:
5403 if (gt_bias) {
5404 __ CmpUleD(FTMP, rhs, lhs);
5405 } else {
5406 __ CmpLeD(FTMP, rhs, lhs);
5407 }
5408 __ Bc1nez(FTMP, label);
5409 break;
5410 default:
5411 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005412 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005413 }
5414 } else {
5415 switch (cond) {
5416 case kCondEQ:
5417 __ CeqD(0, lhs, rhs);
5418 __ Bc1t(0, label);
5419 break;
5420 case kCondNE:
5421 __ CeqD(0, lhs, rhs);
5422 __ Bc1f(0, label);
5423 break;
5424 case kCondLT:
5425 if (gt_bias) {
5426 __ ColtD(0, lhs, rhs);
5427 } else {
5428 __ CultD(0, lhs, rhs);
5429 }
5430 __ Bc1t(0, label);
5431 break;
5432 case kCondLE:
5433 if (gt_bias) {
5434 __ ColeD(0, lhs, rhs);
5435 } else {
5436 __ CuleD(0, lhs, rhs);
5437 }
5438 __ Bc1t(0, label);
5439 break;
5440 case kCondGT:
5441 if (gt_bias) {
5442 __ CultD(0, rhs, lhs);
5443 } else {
5444 __ ColtD(0, rhs, lhs);
5445 }
5446 __ Bc1t(0, label);
5447 break;
5448 case kCondGE:
5449 if (gt_bias) {
5450 __ CuleD(0, rhs, lhs);
5451 } else {
5452 __ ColeD(0, rhs, lhs);
5453 }
5454 __ Bc1t(0, label);
5455 break;
5456 default:
5457 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005458 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005459 }
5460 }
5461 }
5462}
5463
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005464void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005465 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005466 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005467 MipsLabel* false_target) {
5468 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005469
David Brazdil0debae72015-11-12 18:37:00 +00005470 if (true_target == nullptr && false_target == nullptr) {
5471 // Nothing to do. The code always falls through.
5472 return;
5473 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005474 // Constant condition, statically compared against "true" (integer value 1).
5475 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005476 if (true_target != nullptr) {
5477 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005478 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005479 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005480 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005481 if (false_target != nullptr) {
5482 __ B(false_target);
5483 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005484 }
David Brazdil0debae72015-11-12 18:37:00 +00005485 return;
5486 }
5487
5488 // The following code generates these patterns:
5489 // (1) true_target == nullptr && false_target != nullptr
5490 // - opposite condition true => branch to false_target
5491 // (2) true_target != nullptr && false_target == nullptr
5492 // - condition true => branch to true_target
5493 // (3) true_target != nullptr && false_target != nullptr
5494 // - condition true => branch to true_target
5495 // - branch to false_target
5496 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005497 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005498 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005499 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005500 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005501 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5502 } else {
5503 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5504 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005505 } else {
5506 // The condition instruction has not been materialized, use its inputs as
5507 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005508 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005509 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005510 LocationSummary* locations = cond->GetLocations();
5511 IfCondition if_cond = condition->GetCondition();
5512 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005513
David Brazdil0debae72015-11-12 18:37:00 +00005514 if (true_target == nullptr) {
5515 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005516 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005517 }
5518
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005519 switch (type) {
5520 default:
5521 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5522 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005524 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5525 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005526 case DataType::Type::kFloat32:
5527 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005528 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5529 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005530 }
5531 }
David Brazdil0debae72015-11-12 18:37:00 +00005532
5533 // If neither branch falls through (case 3), the conditional branch to `true_target`
5534 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5535 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 __ B(false_target);
5537 }
5538}
5539
5540void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005541 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005542 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005543 locations->SetInAt(0, Location::RequiresRegister());
5544 }
5545}
5546
5547void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005548 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5549 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5550 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5551 nullptr : codegen_->GetLabelOf(true_successor);
5552 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5553 nullptr : codegen_->GetLabelOf(false_successor);
5554 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005555}
5556
5557void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005558 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005559 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005560 InvokeRuntimeCallingConvention calling_convention;
5561 RegisterSet caller_saves = RegisterSet::Empty();
5562 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5563 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005564 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005565 locations->SetInAt(0, Location::RequiresRegister());
5566 }
5567}
5568
5569void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005570 SlowPathCodeMIPS* slow_path =
5571 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005572 GenerateTestAndBranch(deoptimize,
5573 /* condition_input_index */ 0,
5574 slow_path->GetEntryLabel(),
5575 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005576}
5577
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005578// This function returns true if a conditional move can be generated for HSelect.
5579// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5580// branches and regular moves.
5581//
5582// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5583//
5584// While determining feasibility of a conditional move and setting inputs/outputs
5585// are two distinct tasks, this function does both because they share quite a bit
5586// of common logic.
5587static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5588 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5589 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5590 HCondition* condition = cond->AsCondition();
5591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 DataType::Type cond_type =
5593 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5594 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005595
5596 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5597 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5598 bool is_true_value_zero_constant =
5599 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5600 bool is_false_value_zero_constant =
5601 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5602
5603 bool can_move_conditionally = false;
5604 bool use_const_for_false_in = false;
5605 bool use_const_for_true_in = false;
5606
5607 if (!cond->IsConstant()) {
5608 switch (cond_type) {
5609 default:
5610 switch (dst_type) {
5611 default:
5612 // Moving int on int condition.
5613 if (is_r6) {
5614 if (is_true_value_zero_constant) {
5615 // seleqz out_reg, false_reg, cond_reg
5616 can_move_conditionally = true;
5617 use_const_for_true_in = true;
5618 } else if (is_false_value_zero_constant) {
5619 // selnez out_reg, true_reg, cond_reg
5620 can_move_conditionally = true;
5621 use_const_for_false_in = true;
5622 } else if (materialized) {
5623 // Not materializing unmaterialized int conditions
5624 // to keep the instruction count low.
5625 // selnez AT, true_reg, cond_reg
5626 // seleqz TMP, false_reg, cond_reg
5627 // or out_reg, AT, TMP
5628 can_move_conditionally = true;
5629 }
5630 } else {
5631 // movn out_reg, true_reg/ZERO, cond_reg
5632 can_move_conditionally = true;
5633 use_const_for_true_in = is_true_value_zero_constant;
5634 }
5635 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005637 // Moving long on int condition.
5638 if (is_r6) {
5639 if (is_true_value_zero_constant) {
5640 // seleqz out_reg_lo, false_reg_lo, cond_reg
5641 // seleqz out_reg_hi, false_reg_hi, cond_reg
5642 can_move_conditionally = true;
5643 use_const_for_true_in = true;
5644 } else if (is_false_value_zero_constant) {
5645 // selnez out_reg_lo, true_reg_lo, cond_reg
5646 // selnez out_reg_hi, true_reg_hi, cond_reg
5647 can_move_conditionally = true;
5648 use_const_for_false_in = true;
5649 }
5650 // Other long conditional moves would generate 6+ instructions,
5651 // which is too many.
5652 } else {
5653 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5654 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5655 can_move_conditionally = true;
5656 use_const_for_true_in = is_true_value_zero_constant;
5657 }
5658 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005659 case DataType::Type::kFloat32:
5660 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005661 // Moving float/double on int condition.
5662 if (is_r6) {
5663 if (materialized) {
5664 // Not materializing unmaterialized int conditions
5665 // to keep the instruction count low.
5666 can_move_conditionally = true;
5667 if (is_true_value_zero_constant) {
5668 // sltu TMP, ZERO, cond_reg
5669 // mtc1 TMP, temp_cond_reg
5670 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5671 use_const_for_true_in = true;
5672 } else if (is_false_value_zero_constant) {
5673 // sltu TMP, ZERO, cond_reg
5674 // mtc1 TMP, temp_cond_reg
5675 // selnez.fmt out_reg, true_reg, temp_cond_reg
5676 use_const_for_false_in = true;
5677 } else {
5678 // sltu TMP, ZERO, cond_reg
5679 // mtc1 TMP, temp_cond_reg
5680 // sel.fmt temp_cond_reg, false_reg, true_reg
5681 // mov.fmt out_reg, temp_cond_reg
5682 }
5683 }
5684 } else {
5685 // movn.fmt out_reg, true_reg, cond_reg
5686 can_move_conditionally = true;
5687 }
5688 break;
5689 }
5690 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005692 // We don't materialize long comparison now
5693 // and use conditional branches instead.
5694 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 case DataType::Type::kFloat32:
5696 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005697 switch (dst_type) {
5698 default:
5699 // Moving int on float/double condition.
5700 if (is_r6) {
5701 if (is_true_value_zero_constant) {
5702 // mfc1 TMP, temp_cond_reg
5703 // seleqz out_reg, false_reg, TMP
5704 can_move_conditionally = true;
5705 use_const_for_true_in = true;
5706 } else if (is_false_value_zero_constant) {
5707 // mfc1 TMP, temp_cond_reg
5708 // selnez out_reg, true_reg, TMP
5709 can_move_conditionally = true;
5710 use_const_for_false_in = true;
5711 } else {
5712 // mfc1 TMP, temp_cond_reg
5713 // selnez AT, true_reg, TMP
5714 // seleqz TMP, false_reg, TMP
5715 // or out_reg, AT, TMP
5716 can_move_conditionally = true;
5717 }
5718 } else {
5719 // movt out_reg, true_reg/ZERO, cc
5720 can_move_conditionally = true;
5721 use_const_for_true_in = is_true_value_zero_constant;
5722 }
5723 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005725 // Moving long on float/double condition.
5726 if (is_r6) {
5727 if (is_true_value_zero_constant) {
5728 // mfc1 TMP, temp_cond_reg
5729 // seleqz out_reg_lo, false_reg_lo, TMP
5730 // seleqz out_reg_hi, false_reg_hi, TMP
5731 can_move_conditionally = true;
5732 use_const_for_true_in = true;
5733 } else if (is_false_value_zero_constant) {
5734 // mfc1 TMP, temp_cond_reg
5735 // selnez out_reg_lo, true_reg_lo, TMP
5736 // selnez out_reg_hi, true_reg_hi, TMP
5737 can_move_conditionally = true;
5738 use_const_for_false_in = true;
5739 }
5740 // Other long conditional moves would generate 6+ instructions,
5741 // which is too many.
5742 } else {
5743 // movt out_reg_lo, true_reg_lo/ZERO, cc
5744 // movt out_reg_hi, true_reg_hi/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::kFloat32:
5750 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005751 // Moving float/double on float/double condition.
5752 if (is_r6) {
5753 can_move_conditionally = true;
5754 if (is_true_value_zero_constant) {
5755 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5756 use_const_for_true_in = true;
5757 } else if (is_false_value_zero_constant) {
5758 // selnez.fmt out_reg, true_reg, temp_cond_reg
5759 use_const_for_false_in = true;
5760 } else {
5761 // sel.fmt temp_cond_reg, false_reg, true_reg
5762 // mov.fmt out_reg, temp_cond_reg
5763 }
5764 } else {
5765 // movt.fmt out_reg, true_reg, cc
5766 can_move_conditionally = true;
5767 }
5768 break;
5769 }
5770 break;
5771 }
5772 }
5773
5774 if (can_move_conditionally) {
5775 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5776 } else {
5777 DCHECK(!use_const_for_false_in);
5778 DCHECK(!use_const_for_true_in);
5779 }
5780
5781 if (locations_to_set != nullptr) {
5782 if (use_const_for_false_in) {
5783 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5784 } else {
5785 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005786 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005787 ? Location::RequiresFpuRegister()
5788 : Location::RequiresRegister());
5789 }
5790 if (use_const_for_true_in) {
5791 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5792 } else {
5793 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005794 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005795 ? Location::RequiresFpuRegister()
5796 : Location::RequiresRegister());
5797 }
5798 if (materialized) {
5799 locations_to_set->SetInAt(2, Location::RequiresRegister());
5800 }
5801 // On R6 we don't require the output to be the same as the
5802 // first input for conditional moves unlike on R2.
5803 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5804 if (is_out_same_as_first_in) {
5805 locations_to_set->SetOut(Location::SameAsFirstInput());
5806 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005808 ? Location::RequiresFpuRegister()
5809 : Location::RequiresRegister());
5810 }
5811 }
5812
5813 return can_move_conditionally;
5814}
5815
5816void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5817 LocationSummary* locations = select->GetLocations();
5818 Location dst = locations->Out();
5819 Location src = locations->InAt(1);
5820 Register src_reg = ZERO;
5821 Register src_reg_high = ZERO;
5822 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5823 Register cond_reg = TMP;
5824 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005826 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005828
5829 if (IsBooleanValueOrMaterializedCondition(cond)) {
5830 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5831 } else {
5832 HCondition* condition = cond->AsCondition();
5833 LocationSummary* cond_locations = cond->GetLocations();
5834 IfCondition if_cond = condition->GetCondition();
5835 cond_type = condition->InputAt(0)->GetType();
5836 switch (cond_type) {
5837 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005839 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5840 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005841 case DataType::Type::kFloat32:
5842 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005843 cond_inverted = MaterializeFpCompareR2(if_cond,
5844 condition->IsGtBias(),
5845 cond_type,
5846 cond_locations,
5847 cond_cc);
5848 break;
5849 }
5850 }
5851
5852 DCHECK(dst.Equals(locations->InAt(0)));
5853 if (src.IsRegister()) {
5854 src_reg = src.AsRegister<Register>();
5855 } else if (src.IsRegisterPair()) {
5856 src_reg = src.AsRegisterPairLow<Register>();
5857 src_reg_high = src.AsRegisterPairHigh<Register>();
5858 } else if (src.IsConstant()) {
5859 DCHECK(src.GetConstant()->IsZeroBitPattern());
5860 }
5861
5862 switch (cond_type) {
5863 default:
5864 switch (dst_type) {
5865 default:
5866 if (cond_inverted) {
5867 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5868 } else {
5869 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5870 }
5871 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005873 if (cond_inverted) {
5874 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5875 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5876 } else {
5877 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5878 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5879 }
5880 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005882 if (cond_inverted) {
5883 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5884 } else {
5885 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5886 }
5887 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005889 if (cond_inverted) {
5890 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5891 } else {
5892 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5893 }
5894 break;
5895 }
5896 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005898 LOG(FATAL) << "Unreachable";
5899 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005900 case DataType::Type::kFloat32:
5901 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005902 switch (dst_type) {
5903 default:
5904 if (cond_inverted) {
5905 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5906 } else {
5907 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5908 }
5909 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005911 if (cond_inverted) {
5912 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5913 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5914 } else {
5915 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5916 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5917 }
5918 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005920 if (cond_inverted) {
5921 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5922 } else {
5923 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5924 }
5925 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005926 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005927 if (cond_inverted) {
5928 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5929 } else {
5930 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5931 }
5932 break;
5933 }
5934 break;
5935 }
5936}
5937
5938void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5939 LocationSummary* locations = select->GetLocations();
5940 Location dst = locations->Out();
5941 Location false_src = locations->InAt(0);
5942 Location true_src = locations->InAt(1);
5943 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5944 Register cond_reg = TMP;
5945 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005946 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005947 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005949
5950 if (IsBooleanValueOrMaterializedCondition(cond)) {
5951 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5952 } else {
5953 HCondition* condition = cond->AsCondition();
5954 LocationSummary* cond_locations = cond->GetLocations();
5955 IfCondition if_cond = condition->GetCondition();
5956 cond_type = condition->InputAt(0)->GetType();
5957 switch (cond_type) {
5958 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005959 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005960 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5961 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kFloat32:
5963 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005964 cond_inverted = MaterializeFpCompareR6(if_cond,
5965 condition->IsGtBias(),
5966 cond_type,
5967 cond_locations,
5968 fcond_reg);
5969 break;
5970 }
5971 }
5972
5973 if (true_src.IsConstant()) {
5974 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5975 }
5976 if (false_src.IsConstant()) {
5977 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5978 }
5979
5980 switch (dst_type) {
5981 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005983 __ Mfc1(cond_reg, fcond_reg);
5984 }
5985 if (true_src.IsConstant()) {
5986 if (cond_inverted) {
5987 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5988 } else {
5989 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5990 }
5991 } else if (false_src.IsConstant()) {
5992 if (cond_inverted) {
5993 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5994 } else {
5995 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5996 }
5997 } else {
5998 DCHECK_NE(cond_reg, AT);
5999 if (cond_inverted) {
6000 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6001 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6002 } else {
6003 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6004 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6005 }
6006 __ Or(dst.AsRegister<Register>(), AT, TMP);
6007 }
6008 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006009 case DataType::Type::kInt64: {
6010 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006011 __ Mfc1(cond_reg, fcond_reg);
6012 }
6013 Register dst_lo = dst.AsRegisterPairLow<Register>();
6014 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6015 if (true_src.IsConstant()) {
6016 Register src_lo = false_src.AsRegisterPairLow<Register>();
6017 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6018 if (cond_inverted) {
6019 __ Selnez(dst_lo, src_lo, cond_reg);
6020 __ Selnez(dst_hi, src_hi, cond_reg);
6021 } else {
6022 __ Seleqz(dst_lo, src_lo, cond_reg);
6023 __ Seleqz(dst_hi, src_hi, cond_reg);
6024 }
6025 } else {
6026 DCHECK(false_src.IsConstant());
6027 Register src_lo = true_src.AsRegisterPairLow<Register>();
6028 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6029 if (cond_inverted) {
6030 __ Seleqz(dst_lo, src_lo, cond_reg);
6031 __ Seleqz(dst_hi, src_hi, cond_reg);
6032 } else {
6033 __ Selnez(dst_lo, src_lo, cond_reg);
6034 __ Selnez(dst_hi, src_hi, cond_reg);
6035 }
6036 }
6037 break;
6038 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 case DataType::Type::kFloat32: {
6040 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006041 // sel*.fmt tests bit 0 of the condition register, account for that.
6042 __ Sltu(TMP, ZERO, cond_reg);
6043 __ Mtc1(TMP, fcond_reg);
6044 }
6045 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6046 if (true_src.IsConstant()) {
6047 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6048 if (cond_inverted) {
6049 __ SelnezS(dst_reg, src_reg, fcond_reg);
6050 } else {
6051 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6052 }
6053 } else if (false_src.IsConstant()) {
6054 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6055 if (cond_inverted) {
6056 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6057 } else {
6058 __ SelnezS(dst_reg, src_reg, fcond_reg);
6059 }
6060 } else {
6061 if (cond_inverted) {
6062 __ SelS(fcond_reg,
6063 true_src.AsFpuRegister<FRegister>(),
6064 false_src.AsFpuRegister<FRegister>());
6065 } else {
6066 __ SelS(fcond_reg,
6067 false_src.AsFpuRegister<FRegister>(),
6068 true_src.AsFpuRegister<FRegister>());
6069 }
6070 __ MovS(dst_reg, fcond_reg);
6071 }
6072 break;
6073 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006074 case DataType::Type::kFloat64: {
6075 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006076 // sel*.fmt tests bit 0 of the condition register, account for that.
6077 __ Sltu(TMP, ZERO, cond_reg);
6078 __ Mtc1(TMP, fcond_reg);
6079 }
6080 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6081 if (true_src.IsConstant()) {
6082 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6083 if (cond_inverted) {
6084 __ SelnezD(dst_reg, src_reg, fcond_reg);
6085 } else {
6086 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6087 }
6088 } else if (false_src.IsConstant()) {
6089 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6090 if (cond_inverted) {
6091 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6092 } else {
6093 __ SelnezD(dst_reg, src_reg, fcond_reg);
6094 }
6095 } else {
6096 if (cond_inverted) {
6097 __ SelD(fcond_reg,
6098 true_src.AsFpuRegister<FRegister>(),
6099 false_src.AsFpuRegister<FRegister>());
6100 } else {
6101 __ SelD(fcond_reg,
6102 false_src.AsFpuRegister<FRegister>(),
6103 true_src.AsFpuRegister<FRegister>());
6104 }
6105 __ MovD(dst_reg, fcond_reg);
6106 }
6107 break;
6108 }
6109 }
6110}
6111
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006112void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006113 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006114 LocationSummary(flag, LocationSummary::kNoCall);
6115 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006116}
6117
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006118void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6119 __ LoadFromOffset(kLoadWord,
6120 flag->GetLocations()->Out().AsRegister<Register>(),
6121 SP,
6122 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006123}
6124
David Brazdil74eb1b22015-12-14 11:44:01 +00006125void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006126 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006127 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006128}
6129
6130void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006131 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6132 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6133 if (is_r6) {
6134 GenConditionalMoveR6(select);
6135 } else {
6136 GenConditionalMoveR2(select);
6137 }
6138 } else {
6139 LocationSummary* locations = select->GetLocations();
6140 MipsLabel false_target;
6141 GenerateTestAndBranch(select,
6142 /* condition_input_index */ 2,
6143 /* true_target */ nullptr,
6144 &false_target);
6145 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6146 __ Bind(&false_target);
6147 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006148}
6149
David Srbecky0cf44932015-12-09 14:09:59 +00006150void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006151 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006152}
6153
David Srbeckyd28f4a02016-03-14 17:14:24 +00006154void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6155 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006156}
6157
6158void CodeGeneratorMIPS::GenerateNop() {
6159 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006160}
6161
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006162void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006163 DataType::Type field_type = field_info.GetFieldType();
6164 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006165 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006166 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006167 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006168 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006169 instruction,
6170 generate_volatile
6171 ? LocationSummary::kCallOnMainOnly
6172 : (object_field_get_with_read_barrier
6173 ? LocationSummary::kCallOnSlowPath
6174 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006175
Alexey Frunzec61c0762017-04-10 13:54:23 -07006176 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6177 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6178 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006179 locations->SetInAt(0, Location::RequiresRegister());
6180 if (generate_volatile) {
6181 InvokeRuntimeCallingConvention calling_convention;
6182 // need A0 to hold base + offset
6183 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006184 if (field_type == DataType::Type::kInt64) {
6185 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006186 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006187 // Use Location::Any() to prevent situations when running out of available fp registers.
6188 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006189 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006191 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6192 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6193 }
6194 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006195 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006196 locations->SetOut(Location::RequiresFpuRegister());
6197 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006198 // The output overlaps in the case of an object field get with
6199 // read barriers enabled: we do not want the move to overwrite the
6200 // object's location, as we need it to emit the read barrier.
6201 locations->SetOut(Location::RequiresRegister(),
6202 object_field_get_with_read_barrier
6203 ? Location::kOutputOverlap
6204 : Location::kNoOutputOverlap);
6205 }
6206 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6207 // We need a temporary register for the read barrier marking slow
6208 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006209 if (!kBakerReadBarrierThunksEnableForFields) {
6210 locations->AddTemp(Location::RequiresRegister());
6211 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006212 }
6213 }
6214}
6215
6216void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6217 const FieldInfo& field_info,
6218 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006219 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6220 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006222 Location obj_loc = locations->InAt(0);
6223 Register obj = obj_loc.AsRegister<Register>();
6224 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006225 LoadOperandType load_type = kLoadUnsignedByte;
6226 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006227 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006228 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006229
6230 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006231 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006232 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006233 load_type = kLoadUnsignedByte;
6234 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006235 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006236 load_type = kLoadSignedByte;
6237 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006239 load_type = kLoadUnsignedHalfword;
6240 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006241 case DataType::Type::kInt16:
6242 load_type = kLoadSignedHalfword;
6243 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006244 case DataType::Type::kInt32:
6245 case DataType::Type::kFloat32:
6246 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006247 load_type = kLoadWord;
6248 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006249 case DataType::Type::kInt64:
6250 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006251 load_type = kLoadDoubleword;
6252 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006254 LOG(FATAL) << "Unreachable type " << type;
6255 UNREACHABLE();
6256 }
6257
6258 if (is_volatile && load_type == kLoadDoubleword) {
6259 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006260 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006261 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006262 __ LoadFromOffset(kLoadWord,
6263 ZERO,
6264 locations->GetTemp(0).AsRegister<Register>(),
6265 0,
6266 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006267 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006268 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006270 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006271 if (dst_loc.IsFpuRegister()) {
6272 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006273 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006274 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006275 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006276 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006277 __ StoreToOffset(kStoreWord,
6278 locations->GetTemp(1).AsRegister<Register>(),
6279 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006280 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006281 __ StoreToOffset(kStoreWord,
6282 locations->GetTemp(2).AsRegister<Register>(),
6283 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006284 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006285 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286 }
6287 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006289 // /* HeapReference<Object> */ dst = *(obj + offset)
6290 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006291 Location temp_loc =
6292 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006293 // Note that a potential implicit null check is handled in this
6294 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6295 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6296 dst_loc,
6297 obj,
6298 offset,
6299 temp_loc,
6300 /* needs_null_check */ true);
6301 if (is_volatile) {
6302 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6303 }
6304 } else {
6305 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6306 if (is_volatile) {
6307 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6308 }
6309 // If read barriers are enabled, emit read barriers other than
6310 // Baker's using a slow path (and also unpoison the loaded
6311 // reference, if heap poisoning is enabled).
6312 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6313 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006315 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006316 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006317 DCHECK(dst_loc.IsRegisterPair());
6318 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006319 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006320 DCHECK(dst_loc.IsRegister());
6321 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006322 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006323 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006324 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006325 DCHECK(dst_loc.IsFpuRegister());
6326 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006328 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006330 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006331 }
6332 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006333 }
6334
Alexey Frunze15958152017-02-09 19:08:30 -08006335 // Memory barriers, in the case of references, are handled in the
6336 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006338 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6339 }
6340}
6341
6342void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006343 DataType::Type field_type = field_info.GetFieldType();
6344 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006345 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006346 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006347 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006348
6349 locations->SetInAt(0, Location::RequiresRegister());
6350 if (generate_volatile) {
6351 InvokeRuntimeCallingConvention calling_convention;
6352 // need A0 to hold base + offset
6353 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006354 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006355 locations->SetInAt(1, Location::RegisterPairLocation(
6356 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6357 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006358 // Use Location::Any() to prevent situations when running out of available fp registers.
6359 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006360 // Pass FP parameters in core registers.
6361 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6362 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6363 }
6364 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006366 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006367 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006368 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006369 }
6370 }
6371}
6372
6373void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6374 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006375 uint32_t dex_pc,
6376 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006377 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 LocationSummary* locations = instruction->GetLocations();
6379 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006380 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 StoreOperandType store_type = kStoreByte;
6382 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006383 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006384 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006385 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006386
6387 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006389 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006391 store_type = kStoreByte;
6392 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006393 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006394 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006395 store_type = kStoreHalfword;
6396 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 case DataType::Type::kInt32:
6398 case DataType::Type::kFloat32:
6399 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006400 store_type = kStoreWord;
6401 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 case DataType::Type::kInt64:
6403 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006404 store_type = kStoreDoubleword;
6405 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006407 LOG(FATAL) << "Unreachable type " << type;
6408 UNREACHABLE();
6409 }
6410
6411 if (is_volatile) {
6412 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6413 }
6414
6415 if (is_volatile && store_type == kStoreDoubleword) {
6416 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006417 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006418 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006419 __ LoadFromOffset(kLoadWord,
6420 ZERO,
6421 locations->GetTemp(0).AsRegister<Register>(),
6422 0,
6423 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006424 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006426 if (value_location.IsFpuRegister()) {
6427 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6428 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006429 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006430 value_location.AsFpuRegister<FRegister>());
6431 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006432 __ LoadFromOffset(kLoadWord,
6433 locations->GetTemp(1).AsRegister<Register>(),
6434 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006435 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006436 __ LoadFromOffset(kLoadWord,
6437 locations->GetTemp(2).AsRegister<Register>(),
6438 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006439 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006440 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006441 DCHECK(value_location.IsConstant());
6442 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6443 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006444 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6445 locations->GetTemp(1).AsRegister<Register>(),
6446 value);
6447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006448 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006449 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6451 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006452 if (value_location.IsConstant()) {
6453 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6454 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006455 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006456 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006457 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006458 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006459 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006460 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006461 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006462 if (kPoisonHeapReferences && needs_write_barrier) {
6463 // Note that in the case where `value` is a null reference,
6464 // we do not enter this block, as a null reference does not
6465 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006466 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006467 __ PoisonHeapReference(TMP, src);
6468 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6469 } else {
6470 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6471 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006472 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006473 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006474 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006475 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006476 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006477 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006478 }
6479 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 }
6481
Alexey Frunzec061de12017-02-14 13:27:23 -08006482 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006483 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006484 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006485 }
6486
6487 if (is_volatile) {
6488 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6489 }
6490}
6491
6492void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6493 HandleFieldGet(instruction, instruction->GetFieldInfo());
6494}
6495
6496void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6497 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6498}
6499
6500void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6501 HandleFieldSet(instruction, instruction->GetFieldInfo());
6502}
6503
6504void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006505 HandleFieldSet(instruction,
6506 instruction->GetFieldInfo(),
6507 instruction->GetDexPc(),
6508 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006509}
6510
Alexey Frunze15958152017-02-09 19:08:30 -08006511void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6512 HInstruction* instruction,
6513 Location out,
6514 uint32_t offset,
6515 Location maybe_temp,
6516 ReadBarrierOption read_barrier_option) {
6517 Register out_reg = out.AsRegister<Register>();
6518 if (read_barrier_option == kWithReadBarrier) {
6519 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006520 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6521 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6522 }
Alexey Frunze15958152017-02-09 19:08:30 -08006523 if (kUseBakerReadBarrier) {
6524 // Load with fast path based Baker's read barrier.
6525 // /* HeapReference<Object> */ out = *(out + offset)
6526 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6527 out,
6528 out_reg,
6529 offset,
6530 maybe_temp,
6531 /* needs_null_check */ false);
6532 } else {
6533 // Load with slow path based read barrier.
6534 // Save the value of `out` into `maybe_temp` before overwriting it
6535 // in the following move operation, as we will need it for the
6536 // read barrier below.
6537 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6538 // /* HeapReference<Object> */ out = *(out + offset)
6539 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6540 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6541 }
6542 } else {
6543 // Plain load with no read barrier.
6544 // /* HeapReference<Object> */ out = *(out + offset)
6545 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6546 __ MaybeUnpoisonHeapReference(out_reg);
6547 }
6548}
6549
6550void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6551 HInstruction* instruction,
6552 Location out,
6553 Location obj,
6554 uint32_t offset,
6555 Location maybe_temp,
6556 ReadBarrierOption read_barrier_option) {
6557 Register out_reg = out.AsRegister<Register>();
6558 Register obj_reg = obj.AsRegister<Register>();
6559 if (read_barrier_option == kWithReadBarrier) {
6560 CHECK(kEmitCompilerReadBarrier);
6561 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006562 if (!kBakerReadBarrierThunksEnableForFields) {
6563 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6564 }
Alexey Frunze15958152017-02-09 19:08:30 -08006565 // Load with fast path based Baker's read barrier.
6566 // /* HeapReference<Object> */ out = *(obj + offset)
6567 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6568 out,
6569 obj_reg,
6570 offset,
6571 maybe_temp,
6572 /* needs_null_check */ false);
6573 } else {
6574 // Load with slow path based read barrier.
6575 // /* HeapReference<Object> */ out = *(obj + offset)
6576 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6577 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6578 }
6579 } else {
6580 // Plain load with no read barrier.
6581 // /* HeapReference<Object> */ out = *(obj + offset)
6582 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6583 __ MaybeUnpoisonHeapReference(out_reg);
6584 }
6585}
6586
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006587static inline int GetBakerMarkThunkNumber(Register reg) {
6588 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6589 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6590 return reg - V0;
6591 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6592 return 14 + (reg - S2);
6593 } else if (reg == FP) { // One more.
6594 return 20;
6595 }
6596 LOG(FATAL) << "Unexpected register " << reg;
6597 UNREACHABLE();
6598}
6599
6600static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6601 int num = GetBakerMarkThunkNumber(reg) +
6602 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6603 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6604}
6605
6606static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6607 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6608 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6609}
6610
Alexey Frunze15958152017-02-09 19:08:30 -08006611void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6612 Location root,
6613 Register obj,
6614 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006615 ReadBarrierOption read_barrier_option,
6616 MipsLabel* label_low) {
6617 bool reordering;
6618 if (label_low != nullptr) {
6619 DCHECK_EQ(offset, 0x5678u);
6620 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006621 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006622 if (read_barrier_option == kWithReadBarrier) {
6623 DCHECK(kEmitCompilerReadBarrier);
6624 if (kUseBakerReadBarrier) {
6625 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6626 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006627 if (kBakerReadBarrierThunksEnableForGcRoots) {
6628 // Note that we do not actually check the value of `GetIsGcMarking()`
6629 // to decide whether to mark the loaded GC root or not. Instead, we
6630 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6631 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6632 // vice versa.
6633 //
6634 // We use thunks for the slow path. That thunk checks the reference
6635 // and jumps to the entrypoint if needed.
6636 //
6637 // temp = Thread::Current()->pReadBarrierMarkReg00
6638 // // AKA &art_quick_read_barrier_mark_introspection.
6639 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6640 // if (temp != nullptr) {
6641 // temp = &gc_root_thunk<root_reg>
6642 // root = temp(root)
6643 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006644
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006645 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6646 const int32_t entry_point_offset =
6647 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6648 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6649 int16_t offset_low = Low16Bits(offset);
6650 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6651 // extension in lw.
6652 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6653 Register base = short_offset ? obj : TMP;
6654 // Loading the entrypoint does not require a load acquire since it is only changed when
6655 // threads are suspended or running a checkpoint.
6656 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6657 reordering = __ SetReorder(false);
6658 if (!short_offset) {
6659 DCHECK(!label_low);
6660 __ AddUpper(base, obj, offset_high);
6661 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006662 MipsLabel skip_call;
6663 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006664 if (label_low != nullptr) {
6665 DCHECK(short_offset);
6666 __ Bind(label_low);
6667 }
6668 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6669 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6670 // in delay slot.
6671 if (isR6) {
6672 __ Jialc(T9, thunk_disp);
6673 } else {
6674 __ Addiu(T9, T9, thunk_disp);
6675 __ Jalr(T9);
6676 __ Nop();
6677 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006678 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006679 __ SetReorder(reordering);
6680 } else {
6681 // Note that we do not actually check the value of `GetIsGcMarking()`
6682 // to decide whether to mark the loaded GC root or not. Instead, we
6683 // load into `temp` (T9) the read barrier mark entry point corresponding
6684 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6685 // is false, and vice versa.
6686 //
6687 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6688 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6689 // if (temp != null) {
6690 // root = temp(root)
6691 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006692
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006693 if (label_low != nullptr) {
6694 reordering = __ SetReorder(false);
6695 __ Bind(label_low);
6696 }
6697 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6698 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6699 if (label_low != nullptr) {
6700 __ SetReorder(reordering);
6701 }
6702 static_assert(
6703 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6704 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6705 "have different sizes.");
6706 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6707 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6708 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006709
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006710 // Slow path marking the GC root `root`.
6711 Location temp = Location::RegisterLocation(T9);
6712 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006713 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006714 instruction,
6715 root,
6716 /*entrypoint*/ temp);
6717 codegen_->AddSlowPath(slow_path);
6718
6719 const int32_t entry_point_offset =
6720 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6721 // Loading the entrypoint does not require a load acquire since it is only changed when
6722 // threads are suspended or running a checkpoint.
6723 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6724 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6725 __ Bind(slow_path->GetExitLabel());
6726 }
Alexey Frunze15958152017-02-09 19:08:30 -08006727 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006728 if (label_low != nullptr) {
6729 reordering = __ SetReorder(false);
6730 __ Bind(label_low);
6731 }
Alexey Frunze15958152017-02-09 19:08:30 -08006732 // GC root loaded through a slow path for read barriers other
6733 // than Baker's.
6734 // /* GcRoot<mirror::Object>* */ root = obj + offset
6735 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006736 if (label_low != nullptr) {
6737 __ SetReorder(reordering);
6738 }
Alexey Frunze15958152017-02-09 19:08:30 -08006739 // /* mirror::Object* */ root = root->Read()
6740 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6741 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006742 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006743 if (label_low != nullptr) {
6744 reordering = __ SetReorder(false);
6745 __ Bind(label_low);
6746 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006747 // Plain GC root load with no read barrier.
6748 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6749 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6750 // Note that GC roots are not affected by heap poisoning, thus we
6751 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006752 if (label_low != nullptr) {
6753 __ SetReorder(reordering);
6754 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006755 }
6756}
6757
Alexey Frunze15958152017-02-09 19:08:30 -08006758void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6759 Location ref,
6760 Register obj,
6761 uint32_t offset,
6762 Location temp,
6763 bool needs_null_check) {
6764 DCHECK(kEmitCompilerReadBarrier);
6765 DCHECK(kUseBakerReadBarrier);
6766
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006767 if (kBakerReadBarrierThunksEnableForFields) {
6768 // Note that we do not actually check the value of `GetIsGcMarking()`
6769 // to decide whether to mark the loaded reference or not. Instead, we
6770 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6771 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6772 // vice versa.
6773 //
6774 // We use thunks for the slow path. That thunk checks the reference
6775 // and jumps to the entrypoint if needed. If the holder is not gray,
6776 // it issues a load-load memory barrier and returns to the original
6777 // reference load.
6778 //
6779 // temp = Thread::Current()->pReadBarrierMarkReg00
6780 // // AKA &art_quick_read_barrier_mark_introspection.
6781 // if (temp != nullptr) {
6782 // temp = &field_array_thunk<holder_reg>
6783 // temp()
6784 // }
6785 // not_gray_return_address:
6786 // // If the offset is too large to fit into the lw instruction, we
6787 // // use an adjusted base register (TMP) here. This register
6788 // // receives bits 16 ... 31 of the offset before the thunk invocation
6789 // // and the thunk benefits from it.
6790 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6791 // gray_return_address:
6792
6793 DCHECK(temp.IsInvalid());
6794 bool isR6 = GetInstructionSetFeatures().IsR6();
6795 int16_t offset_low = Low16Bits(offset);
6796 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6797 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6798 bool reordering = __ SetReorder(false);
6799 const int32_t entry_point_offset =
6800 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6801 // There may have or may have not been a null check if the field offset is smaller than
6802 // the page size.
6803 // There must've been a null check in case it's actually a load from an array.
6804 // We will, however, perform an explicit null check in the thunk as it's easier to
6805 // do it than not.
6806 if (instruction->IsArrayGet()) {
6807 DCHECK(!needs_null_check);
6808 }
6809 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6810 // Loading the entrypoint does not require a load acquire since it is only changed when
6811 // threads are suspended or running a checkpoint.
6812 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6813 Register ref_reg = ref.AsRegister<Register>();
6814 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006815 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006816 if (short_offset) {
6817 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006818 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006819 __ Nop(); // In forbidden slot.
6820 __ Jialc(T9, thunk_disp);
6821 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006822 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006823 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6824 __ Jalr(T9);
6825 __ Nop(); // In delay slot.
6826 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006827 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006828 } else {
6829 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006830 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006831 __ Aui(base, obj, offset_high); // In delay slot.
6832 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006833 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006834 } else {
6835 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006836 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006837 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6838 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006839 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006840 __ Addu(base, base, obj); // In delay slot.
6841 }
6842 }
6843 // /* HeapReference<Object> */ ref = *(obj + offset)
6844 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6845 if (needs_null_check) {
6846 MaybeRecordImplicitNullCheck(instruction);
6847 }
6848 __ MaybeUnpoisonHeapReference(ref_reg);
6849 __ SetReorder(reordering);
6850 return;
6851 }
6852
Alexey Frunze15958152017-02-09 19:08:30 -08006853 // /* HeapReference<Object> */ ref = *(obj + offset)
6854 Location no_index = Location::NoLocation();
6855 ScaleFactor no_scale_factor = TIMES_1;
6856 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6857 ref,
6858 obj,
6859 offset,
6860 no_index,
6861 no_scale_factor,
6862 temp,
6863 needs_null_check);
6864}
6865
6866void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6867 Location ref,
6868 Register obj,
6869 uint32_t data_offset,
6870 Location index,
6871 Location temp,
6872 bool needs_null_check) {
6873 DCHECK(kEmitCompilerReadBarrier);
6874 DCHECK(kUseBakerReadBarrier);
6875
6876 static_assert(
6877 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6878 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006879 ScaleFactor scale_factor = TIMES_4;
6880
6881 if (kBakerReadBarrierThunksEnableForArrays) {
6882 // Note that we do not actually check the value of `GetIsGcMarking()`
6883 // to decide whether to mark the loaded reference or not. Instead, we
6884 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6885 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6886 // vice versa.
6887 //
6888 // We use thunks for the slow path. That thunk checks the reference
6889 // and jumps to the entrypoint if needed. If the holder is not gray,
6890 // it issues a load-load memory barrier and returns to the original
6891 // reference load.
6892 //
6893 // temp = Thread::Current()->pReadBarrierMarkReg00
6894 // // AKA &art_quick_read_barrier_mark_introspection.
6895 // if (temp != nullptr) {
6896 // temp = &field_array_thunk<holder_reg>
6897 // temp()
6898 // }
6899 // not_gray_return_address:
6900 // // The element address is pre-calculated in the TMP register before the
6901 // // thunk invocation and the thunk benefits from it.
6902 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6903 // gray_return_address:
6904
6905 DCHECK(temp.IsInvalid());
6906 DCHECK(index.IsValid());
6907 bool reordering = __ SetReorder(false);
6908 const int32_t entry_point_offset =
6909 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6910 // We will not do the explicit null check in the thunk as some form of a null check
6911 // must've been done earlier.
6912 DCHECK(!needs_null_check);
6913 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6914 // Loading the entrypoint does not require a load acquire since it is only changed when
6915 // threads are suspended or running a checkpoint.
6916 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6917 Register ref_reg = ref.AsRegister<Register>();
6918 Register index_reg = index.IsRegisterPair()
6919 ? index.AsRegisterPairLow<Register>()
6920 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006921 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006922 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006923 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006924 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6925 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006926 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006927 } else {
6928 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006929 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006930 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6931 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006932 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006933 __ Addu(TMP, TMP, obj); // In delay slot.
6934 }
6935 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6936 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6937 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6938 __ MaybeUnpoisonHeapReference(ref_reg);
6939 __ SetReorder(reordering);
6940 return;
6941 }
6942
Alexey Frunze15958152017-02-09 19:08:30 -08006943 // /* HeapReference<Object> */ ref =
6944 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006945 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6946 ref,
6947 obj,
6948 data_offset,
6949 index,
6950 scale_factor,
6951 temp,
6952 needs_null_check);
6953}
6954
6955void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6956 Location ref,
6957 Register obj,
6958 uint32_t offset,
6959 Location index,
6960 ScaleFactor scale_factor,
6961 Location temp,
6962 bool needs_null_check,
6963 bool always_update_field) {
6964 DCHECK(kEmitCompilerReadBarrier);
6965 DCHECK(kUseBakerReadBarrier);
6966
6967 // In slow path based read barriers, the read barrier call is
6968 // inserted after the original load. However, in fast path based
6969 // Baker's read barriers, we need to perform the load of
6970 // mirror::Object::monitor_ *before* the original reference load.
6971 // This load-load ordering is required by the read barrier.
6972 // The fast path/slow path (for Baker's algorithm) should look like:
6973 //
6974 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6975 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6976 // HeapReference<Object> ref = *src; // Original reference load.
6977 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6978 // if (is_gray) {
6979 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6980 // }
6981 //
6982 // Note: the original implementation in ReadBarrier::Barrier is
6983 // slightly more complex as it performs additional checks that we do
6984 // not do here for performance reasons.
6985
6986 Register ref_reg = ref.AsRegister<Register>();
6987 Register temp_reg = temp.AsRegister<Register>();
6988 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6989
6990 // /* int32_t */ monitor = obj->monitor_
6991 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6992 if (needs_null_check) {
6993 MaybeRecordImplicitNullCheck(instruction);
6994 }
6995 // /* LockWord */ lock_word = LockWord(monitor)
6996 static_assert(sizeof(LockWord) == sizeof(int32_t),
6997 "art::LockWord and int32_t have different sizes.");
6998
6999 __ Sync(0); // Barrier to prevent load-load reordering.
7000
7001 // The actual reference load.
7002 if (index.IsValid()) {
7003 // Load types involving an "index": ArrayGet,
7004 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7005 // intrinsics.
7006 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7007 if (index.IsConstant()) {
7008 size_t computed_offset =
7009 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7010 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7011 } else {
7012 // Handle the special case of the
7013 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7014 // intrinsics, which use a register pair as index ("long
7015 // offset"), of which only the low part contains data.
7016 Register index_reg = index.IsRegisterPair()
7017 ? index.AsRegisterPairLow<Register>()
7018 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007019 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007020 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7021 }
7022 } else {
7023 // /* HeapReference<Object> */ ref = *(obj + offset)
7024 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7025 }
7026
7027 // Object* ref = ref_addr->AsMirrorPtr()
7028 __ MaybeUnpoisonHeapReference(ref_reg);
7029
7030 // Slow path marking the object `ref` when it is gray.
7031 SlowPathCodeMIPS* slow_path;
7032 if (always_update_field) {
7033 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7034 // of the form `obj + field_offset`, where `obj` is a register and
7035 // `field_offset` is a register pair (of which only the lower half
7036 // is used). Thus `offset` and `scale_factor` above are expected
7037 // to be null in this code path.
7038 DCHECK_EQ(offset, 0u);
7039 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007040 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007041 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7042 ref,
7043 obj,
7044 /* field_offset */ index,
7045 temp_reg);
7046 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007047 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007048 }
7049 AddSlowPath(slow_path);
7050
7051 // if (rb_state == ReadBarrier::GrayState())
7052 // ref = ReadBarrier::Mark(ref);
7053 // Given the numeric representation, it's enough to check the low bit of the
7054 // rb_state. We do that by shifting the bit into the sign bit (31) and
7055 // performing a branch on less than zero.
7056 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7057 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7058 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7059 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7060 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7061 __ Bind(slow_path->GetExitLabel());
7062}
7063
7064void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7065 Location out,
7066 Location ref,
7067 Location obj,
7068 uint32_t offset,
7069 Location index) {
7070 DCHECK(kEmitCompilerReadBarrier);
7071
7072 // Insert a slow path based read barrier *after* the reference load.
7073 //
7074 // If heap poisoning is enabled, the unpoisoning of the loaded
7075 // reference will be carried out by the runtime within the slow
7076 // path.
7077 //
7078 // Note that `ref` currently does not get unpoisoned (when heap
7079 // poisoning is enabled), which is alright as the `ref` argument is
7080 // not used by the artReadBarrierSlow entry point.
7081 //
7082 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007083 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007084 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7085 AddSlowPath(slow_path);
7086
7087 __ B(slow_path->GetEntryLabel());
7088 __ Bind(slow_path->GetExitLabel());
7089}
7090
7091void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7092 Location out,
7093 Location ref,
7094 Location obj,
7095 uint32_t offset,
7096 Location index) {
7097 if (kEmitCompilerReadBarrier) {
7098 // Baker's read barriers shall be handled by the fast path
7099 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7100 DCHECK(!kUseBakerReadBarrier);
7101 // If heap poisoning is enabled, unpoisoning will be taken care of
7102 // by the runtime within the slow path.
7103 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7104 } else if (kPoisonHeapReferences) {
7105 __ UnpoisonHeapReference(out.AsRegister<Register>());
7106 }
7107}
7108
7109void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7110 Location out,
7111 Location root) {
7112 DCHECK(kEmitCompilerReadBarrier);
7113
7114 // Insert a slow path based read barrier *after* the GC root load.
7115 //
7116 // Note that GC roots are not affected by heap poisoning, so we do
7117 // not need to do anything special for this here.
7118 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007119 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007120 AddSlowPath(slow_path);
7121
7122 __ B(slow_path->GetEntryLabel());
7123 __ Bind(slow_path->GetExitLabel());
7124}
7125
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007126void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007127 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7128 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007129 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007130 switch (type_check_kind) {
7131 case TypeCheckKind::kExactCheck:
7132 case TypeCheckKind::kAbstractClassCheck:
7133 case TypeCheckKind::kClassHierarchyCheck:
7134 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007135 call_kind =
7136 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007137 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007138 break;
7139 case TypeCheckKind::kArrayCheck:
7140 case TypeCheckKind::kUnresolvedCheck:
7141 case TypeCheckKind::kInterfaceCheck:
7142 call_kind = LocationSummary::kCallOnSlowPath;
7143 break;
7144 }
7145
Vladimir Markoca6fff82017-10-03 14:49:14 +01007146 LocationSummary* locations =
7147 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007148 if (baker_read_barrier_slow_path) {
7149 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7150 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007151 locations->SetInAt(0, Location::RequiresRegister());
7152 locations->SetInAt(1, Location::RequiresRegister());
7153 // The output does overlap inputs.
7154 // Note that TypeCheckSlowPathMIPS uses this register too.
7155 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007156 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007157}
7158
7159void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007160 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007161 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007162 Location obj_loc = locations->InAt(0);
7163 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007164 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007165 Location out_loc = locations->Out();
7166 Register out = out_loc.AsRegister<Register>();
7167 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7168 DCHECK_LE(num_temps, 1u);
7169 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007170 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7171 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7172 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7173 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007174 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007175 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176
7177 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007178 // Avoid this check if we know `obj` is not null.
7179 if (instruction->MustDoNullCheck()) {
7180 __ Move(out, ZERO);
7181 __ Beqz(obj, &done);
7182 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007183
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007184 switch (type_check_kind) {
7185 case TypeCheckKind::kExactCheck: {
7186 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007187 GenerateReferenceLoadTwoRegisters(instruction,
7188 out_loc,
7189 obj_loc,
7190 class_offset,
7191 maybe_temp_loc,
7192 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007193 // Classes must be equal for the instanceof to succeed.
7194 __ Xor(out, out, cls);
7195 __ Sltiu(out, out, 1);
7196 break;
7197 }
7198
7199 case TypeCheckKind::kAbstractClassCheck: {
7200 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007201 GenerateReferenceLoadTwoRegisters(instruction,
7202 out_loc,
7203 obj_loc,
7204 class_offset,
7205 maybe_temp_loc,
7206 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007207 // If the class is abstract, we eagerly fetch the super class of the
7208 // object to avoid doing a comparison we know will fail.
7209 MipsLabel loop;
7210 __ Bind(&loop);
7211 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007212 GenerateReferenceLoadOneRegister(instruction,
7213 out_loc,
7214 super_offset,
7215 maybe_temp_loc,
7216 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007217 // If `out` is null, we use it for the result, and jump to `done`.
7218 __ Beqz(out, &done);
7219 __ Bne(out, cls, &loop);
7220 __ LoadConst32(out, 1);
7221 break;
7222 }
7223
7224 case TypeCheckKind::kClassHierarchyCheck: {
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 // Walk over the class hierarchy to find a match.
7233 MipsLabel loop, success;
7234 __ Bind(&loop);
7235 __ Beq(out, cls, &success);
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 __ Bnez(out, &loop);
7243 // If `out` is null, we use it for the result, and jump to `done`.
7244 __ B(&done);
7245 __ Bind(&success);
7246 __ LoadConst32(out, 1);
7247 break;
7248 }
7249
7250 case TypeCheckKind::kArrayObjectCheck: {
7251 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007252 GenerateReferenceLoadTwoRegisters(instruction,
7253 out_loc,
7254 obj_loc,
7255 class_offset,
7256 maybe_temp_loc,
7257 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007258 // Do an exact check.
7259 MipsLabel success;
7260 __ Beq(out, cls, &success);
7261 // Otherwise, we need to check that the object's class is a non-primitive array.
7262 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007263 GenerateReferenceLoadOneRegister(instruction,
7264 out_loc,
7265 component_offset,
7266 maybe_temp_loc,
7267 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007268 // If `out` is null, we use it for the result, and jump to `done`.
7269 __ Beqz(out, &done);
7270 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7271 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7272 __ Sltiu(out, out, 1);
7273 __ B(&done);
7274 __ Bind(&success);
7275 __ LoadConst32(out, 1);
7276 break;
7277 }
7278
7279 case TypeCheckKind::kArrayCheck: {
7280 // No read barrier since the slow path will retry upon failure.
7281 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007282 GenerateReferenceLoadTwoRegisters(instruction,
7283 out_loc,
7284 obj_loc,
7285 class_offset,
7286 maybe_temp_loc,
7287 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007288 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007289 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7290 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007291 codegen_->AddSlowPath(slow_path);
7292 __ Bne(out, cls, slow_path->GetEntryLabel());
7293 __ LoadConst32(out, 1);
7294 break;
7295 }
7296
7297 case TypeCheckKind::kUnresolvedCheck:
7298 case TypeCheckKind::kInterfaceCheck: {
7299 // Note that we indeed only call on slow path, but we always go
7300 // into the slow path for the unresolved and interface check
7301 // cases.
7302 //
7303 // We cannot directly call the InstanceofNonTrivial runtime
7304 // entry point without resorting to a type checking slow path
7305 // here (i.e. by calling InvokeRuntime directly), as it would
7306 // require to assign fixed registers for the inputs of this
7307 // HInstanceOf instruction (following the runtime calling
7308 // convention), which might be cluttered by the potential first
7309 // read barrier emission at the beginning of this method.
7310 //
7311 // TODO: Introduce a new runtime entry point taking the object
7312 // to test (instead of its class) as argument, and let it deal
7313 // with the read barrier issues. This will let us refactor this
7314 // case of the `switch` code as it was previously (with a direct
7315 // call to the runtime not using a type checking slow path).
7316 // This should also be beneficial for the other cases above.
7317 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007318 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7319 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007320 codegen_->AddSlowPath(slow_path);
7321 __ B(slow_path->GetEntryLabel());
7322 break;
7323 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007324 }
7325
7326 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007327
7328 if (slow_path != nullptr) {
7329 __ Bind(slow_path->GetExitLabel());
7330 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007331}
7332
7333void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007334 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007335 locations->SetOut(Location::ConstantLocation(constant));
7336}
7337
7338void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7339 // Will be generated at use site.
7340}
7341
7342void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007343 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007344 locations->SetOut(Location::ConstantLocation(constant));
7345}
7346
7347void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7348 // Will be generated at use site.
7349}
7350
7351void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7352 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7353 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7354}
7355
7356void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7357 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007358 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007359 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007360 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007361}
7362
7363void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7364 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7365 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007366 Location receiver = invoke->GetLocations()->InAt(0);
7367 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007368 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007369
7370 // Set the hidden argument.
7371 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7372 invoke->GetDexMethodIndex());
7373
7374 // temp = object->GetClass();
7375 if (receiver.IsStackSlot()) {
7376 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7377 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7378 } else {
7379 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7380 }
7381 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007382 // Instead of simply (possibly) unpoisoning `temp` here, we should
7383 // emit a read barrier for the previous class reference load.
7384 // However this is not required in practice, as this is an
7385 // intermediate/temporary reference and because the current
7386 // concurrent copying collector keeps the from-space memory
7387 // intact/accessible until the end of the marking phase (the
7388 // concurrent copying collector may not in the future).
7389 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007390 __ LoadFromOffset(kLoadWord, temp, temp,
7391 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7392 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007393 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007394 // temp = temp->GetImtEntryAt(method_offset);
7395 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7396 // T9 = temp->GetEntryPoint();
7397 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7398 // T9();
7399 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007400 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007401 DCHECK(!codegen_->IsLeafMethod());
7402 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7403}
7404
7405void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007406 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7407 if (intrinsic.TryDispatch(invoke)) {
7408 return;
7409 }
7410
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007411 HandleInvoke(invoke);
7412}
7413
7414void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007415 // Explicit clinit checks triggered by static invokes must have been pruned by
7416 // art::PrepareForRegisterAllocation.
7417 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007418
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007419 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007420 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7421 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007422
Chris Larsen701566a2015-10-27 15:29:13 -07007423 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7424 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007425 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7426 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7427 }
Chris Larsen701566a2015-10-27 15:29:13 -07007428 return;
7429 }
7430
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007431 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007432
7433 // Add the extra input register if either the dex cache array base register
7434 // or the PC-relative base register for accessing literals is needed.
7435 if (has_extra_input) {
7436 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7437 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007438}
7439
Orion Hodsonac141392017-01-13 11:53:47 +00007440void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7441 HandleInvoke(invoke);
7442}
7443
7444void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7445 codegen_->GenerateInvokePolymorphicCall(invoke);
7446}
7447
Chris Larsen701566a2015-10-27 15:29:13 -07007448static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007449 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007450 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7451 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007452 return true;
7453 }
7454 return false;
7455}
7456
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007457HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007458 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007459 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007460 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007461 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007462 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007463 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007464 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007465 case HLoadString::LoadKind::kJitTableAddress:
7466 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007467 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007468 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007469 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007470 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007471 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007472 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007473}
7474
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007475HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7476 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007477 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007478 case HLoadClass::LoadKind::kInvalid:
7479 LOG(FATAL) << "UNREACHABLE";
7480 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007481 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007482 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007483 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007484 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007485 case HLoadClass::LoadKind::kBssEntry:
7486 DCHECK(!Runtime::Current()->UseJitCompilation());
7487 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007488 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007489 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007490 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007491 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007492 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007493 break;
7494 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007495 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007496}
7497
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007498Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7499 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007500 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007501 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007502 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7503 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7504 if (!invoke->GetLocations()->Intrinsified()) {
7505 return location.AsRegister<Register>();
7506 }
7507 // For intrinsics we allow any location, so it may be on the stack.
7508 if (!location.IsRegister()) {
7509 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7510 return temp;
7511 }
7512 // For register locations, check if the register was saved. If so, get it from the stack.
7513 // Note: There is a chance that the register was saved but not overwritten, so we could
7514 // save one load. However, since this is just an intrinsic slow path we prefer this
7515 // simple and more robust approach rather that trying to determine if that's the case.
7516 SlowPathCode* slow_path = GetCurrentSlowPath();
7517 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7518 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7519 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7520 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7521 return temp;
7522 }
7523 return location.AsRegister<Register>();
7524}
7525
Vladimir Markodc151b22015-10-15 18:02:30 +01007526HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7527 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007528 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007529 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007530}
7531
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007532void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7533 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007534 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007535 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007536 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7537 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007538 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007539 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7540 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007541 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7542 : ZERO;
7543
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007544 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007545 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007546 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007547 uint32_t offset =
7548 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007549 __ LoadFromOffset(kLoadWord,
7550 temp.AsRegister<Register>(),
7551 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007552 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007553 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007554 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007555 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007556 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007557 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007558 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7559 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007560 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7561 PcRelativePatchInfo* info_low =
7562 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007563 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007564 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7565 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007566 break;
7567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007568 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7569 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7570 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007571 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007572 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007573 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007574 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7575 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007576 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007577 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7578 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007579 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007580 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007581 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7582 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7583 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007584 }
7585 }
7586
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007587 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007588 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007589 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007590 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007591 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7592 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007593 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007594 T9,
7595 callee_method.AsRegister<Register>(),
7596 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007597 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007598 // T9()
7599 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007600 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007601 break;
7602 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007603 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7604
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007605 DCHECK(!IsLeafMethod());
7606}
7607
7608void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007609 // Explicit clinit checks triggered by static invokes must have been pruned by
7610 // art::PrepareForRegisterAllocation.
7611 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007612
7613 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7614 return;
7615 }
7616
7617 LocationSummary* locations = invoke->GetLocations();
7618 codegen_->GenerateStaticOrDirectCall(invoke,
7619 locations->HasTemps()
7620 ? locations->GetTemp(0)
7621 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007622}
7623
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007624void CodeGeneratorMIPS::GenerateVirtualCall(
7625 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007626 // Use the calling convention instead of the location of the receiver, as
7627 // intrinsics may have put the receiver in a different register. In the intrinsics
7628 // slow path, the arguments have been moved to the right place, so here we are
7629 // guaranteed that the receiver is the first register of the calling convention.
7630 InvokeDexCallingConvention calling_convention;
7631 Register receiver = calling_convention.GetRegisterAt(0);
7632
Chris Larsen3acee732015-11-18 13:31:08 -08007633 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007634 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7635 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7636 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007637 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007638
7639 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007640 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007641 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007642 // Instead of simply (possibly) unpoisoning `temp` here, we should
7643 // emit a read barrier for the previous class reference load.
7644 // However this is not required in practice, as this is an
7645 // intermediate/temporary reference and because the current
7646 // concurrent copying collector keeps the from-space memory
7647 // intact/accessible until the end of the marking phase (the
7648 // concurrent copying collector may not in the future).
7649 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007650 // temp = temp->GetMethodAt(method_offset);
7651 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7652 // T9 = temp->GetEntryPoint();
7653 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7654 // T9();
7655 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007656 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007657 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007658}
7659
7660void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7661 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7662 return;
7663 }
7664
7665 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007666 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007667}
7668
7669void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007670 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007671 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007672 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007673 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7674 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007675 return;
7676 }
Vladimir Marko41559982017-01-06 14:04:23 +00007677 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007678 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007679 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007680 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7681 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007682 ? LocationSummary::kCallOnSlowPath
7683 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007684 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007685 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7686 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7687 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007688 switch (load_kind) {
7689 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007690 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007691 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007692 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007693 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007694 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007695 break;
7696 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007697 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007698 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7699 codegen_->ClobberRA();
7700 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007701 break;
7702 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007703 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007704 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007705 locations->SetInAt(0, Location::RequiresRegister());
7706 break;
7707 default:
7708 break;
7709 }
7710 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007711 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7712 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7713 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007714 // Request a temp to hold the BSS entry location for the slow path.
7715 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007716 RegisterSet caller_saves = RegisterSet::Empty();
7717 InvokeRuntimeCallingConvention calling_convention;
7718 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7719 locations->SetCustomSlowPathCallerSaves(caller_saves);
7720 } else {
7721 // For non-Baker read barriers we have a temp-clobbering call.
7722 }
7723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007724}
7725
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007726// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7727// move.
7728void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007729 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007730 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007731 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007732 return;
7733 }
Vladimir Marko41559982017-01-06 14:04:23 +00007734 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007735
Vladimir Marko41559982017-01-06 14:04:23 +00007736 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007737 Location out_loc = locations->Out();
7738 Register out = out_loc.AsRegister<Register>();
7739 Register base_or_current_method_reg;
7740 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007741 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007742 switch (load_kind) {
7743 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007744 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007745 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007746 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007747 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007748 base_or_current_method_reg =
7749 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007750 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007751 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007752 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007753 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7754 break;
7755 default:
7756 base_or_current_method_reg = ZERO;
7757 break;
7758 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007759
Alexey Frunze15958152017-02-09 19:08:30 -08007760 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7761 ? kWithoutReadBarrier
7762 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007763 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007764 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007765 switch (load_kind) {
7766 case HLoadClass::LoadKind::kReferrersClass: {
7767 DCHECK(!cls->CanCallRuntime());
7768 DCHECK(!cls->MustGenerateClinitCheck());
7769 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7770 GenerateGcRootFieldLoad(cls,
7771 out_loc,
7772 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007773 ArtMethod::DeclaringClassOffset().Int32Value(),
7774 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007775 break;
7776 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007777 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007778 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007779 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007780 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007781 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007782 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7783 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007784 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7785 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007786 base_or_current_method_reg);
7787 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007788 break;
7789 }
7790 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007791 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007792 uint32_t address = dchecked_integral_cast<uint32_t>(
7793 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7794 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007795 if (isR6 || !has_irreducible_loops) {
7796 __ LoadLiteral(out,
7797 base_or_current_method_reg,
7798 codegen_->DeduplicateBootImageAddressLiteral(address));
7799 } else {
7800 __ LoadConst32(out, address);
7801 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007802 break;
7803 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007804 case HLoadClass::LoadKind::kBootImageClassTable: {
7805 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7806 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7807 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7808 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7809 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7810 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7811 out,
7812 base_or_current_method_reg);
7813 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7814 // Extract the reference from the slot data, i.e. clear the hash bits.
7815 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7816 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7817 if (masked_hash != 0) {
7818 __ Addiu(out, out, -masked_hash);
7819 }
7820 break;
7821 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007822 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007823 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7824 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7825 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007826 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007827 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007828 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7829 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007830 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007831 GenerateGcRootFieldLoad(cls,
7832 out_loc,
7833 temp,
7834 /* placeholder */ 0x5678,
7835 read_barrier_option,
7836 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007837 generate_null_check = true;
7838 break;
7839 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007840 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007841 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7842 cls->GetTypeIndex(),
7843 cls->GetClass());
7844 bool reordering = __ SetReorder(false);
7845 __ Bind(&info->high_label);
7846 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007847 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007848 GenerateGcRootFieldLoad(cls,
7849 out_loc,
7850 out,
7851 /* placeholder */ 0x5678,
7852 read_barrier_option,
7853 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007854 break;
7855 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007856 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007857 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007858 LOG(FATAL) << "UNREACHABLE";
7859 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007860 }
7861
7862 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7863 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007864 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007865 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007866 codegen_->AddSlowPath(slow_path);
7867 if (generate_null_check) {
7868 __ Beqz(out, slow_path->GetEntryLabel());
7869 }
7870 if (cls->MustGenerateClinitCheck()) {
7871 GenerateClassInitializationCheck(slow_path, out);
7872 } else {
7873 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007874 }
7875 }
7876}
7877
7878static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007879 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007880}
7881
7882void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7883 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007884 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007885 locations->SetOut(Location::RequiresRegister());
7886}
7887
7888void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7889 Register out = load->GetLocations()->Out().AsRegister<Register>();
7890 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7891}
7892
7893void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007894 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007895}
7896
7897void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7898 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7899}
7900
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007901void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007902 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007903 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007904 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007905 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007906 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007907 switch (load_kind) {
7908 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007909 case HLoadString::LoadKind::kBootImageAddress:
7910 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007911 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007912 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007913 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007914 break;
7915 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007916 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007917 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7918 codegen_->ClobberRA();
7919 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007920 break;
7921 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007922 FALLTHROUGH_INTENDED;
7923 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007924 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007925 locations->SetInAt(0, Location::RequiresRegister());
7926 break;
7927 default:
7928 break;
7929 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007930 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007931 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007932 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007933 } else {
7934 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007935 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7936 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7937 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007938 // Request a temp to hold the BSS entry location for the slow path.
7939 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007940 RegisterSet caller_saves = RegisterSet::Empty();
7941 InvokeRuntimeCallingConvention calling_convention;
7942 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7943 locations->SetCustomSlowPathCallerSaves(caller_saves);
7944 } else {
7945 // For non-Baker read barriers we have a temp-clobbering call.
7946 }
7947 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007948 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007949}
7950
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007951// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7952// move.
7953void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007954 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007955 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007956 Location out_loc = locations->Out();
7957 Register out = out_loc.AsRegister<Register>();
7958 Register base_or_current_method_reg;
7959 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007960 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007961 switch (load_kind) {
7962 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007963 case HLoadString::LoadKind::kBootImageAddress:
7964 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007965 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007966 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007967 base_or_current_method_reg =
7968 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007969 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007970 default:
7971 base_or_current_method_reg = ZERO;
7972 break;
7973 }
7974
7975 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007976 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007977 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007978 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007979 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007980 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7981 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007982 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7983 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007984 base_or_current_method_reg);
7985 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007986 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007987 }
7988 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007989 uint32_t address = dchecked_integral_cast<uint32_t>(
7990 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7991 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007992 if (isR6 || !has_irreducible_loops) {
7993 __ LoadLiteral(out,
7994 base_or_current_method_reg,
7995 codegen_->DeduplicateBootImageAddressLiteral(address));
7996 } else {
7997 __ LoadConst32(out, address);
7998 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007999 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008000 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008001 case HLoadString::LoadKind::kBootImageInternTable: {
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);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008007 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8008 out,
8009 base_or_current_method_reg);
8010 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8011 return;
8012 }
8013 case HLoadString::LoadKind::kBssEntry: {
8014 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8015 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8016 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8017 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8018 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008019 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008020 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008021 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8022 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008023 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008024 GenerateGcRootFieldLoad(load,
8025 out_loc,
8026 temp,
8027 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008028 kCompilerReadBarrierOption,
8029 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008030 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008031 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008032 codegen_->AddSlowPath(slow_path);
8033 __ Beqz(out, slow_path->GetEntryLabel());
8034 __ Bind(slow_path->GetExitLabel());
8035 return;
8036 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008037 case HLoadString::LoadKind::kJitTableAddress: {
8038 CodeGeneratorMIPS::JitPatchInfo* info =
8039 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8040 load->GetStringIndex(),
8041 load->GetString());
8042 bool reordering = __ SetReorder(false);
8043 __ Bind(&info->high_label);
8044 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008045 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008046 GenerateGcRootFieldLoad(load,
8047 out_loc,
8048 out,
8049 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008050 kCompilerReadBarrierOption,
8051 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008052 return;
8053 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008054 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008055 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008056 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008057
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008058 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008059 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008060 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008061 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008062 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008063 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8064 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008065}
8066
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008067void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008068 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008069 locations->SetOut(Location::ConstantLocation(constant));
8070}
8071
8072void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8073 // Will be generated at use site.
8074}
8075
8076void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008077 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8078 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008079 InvokeRuntimeCallingConvention calling_convention;
8080 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8081}
8082
8083void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8084 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008085 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008086 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8087 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008088 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008089 }
8090 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8091}
8092
8093void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8094 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008095 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008096 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008097 case DataType::Type::kInt32:
8098 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008099 locations->SetInAt(0, Location::RequiresRegister());
8100 locations->SetInAt(1, Location::RequiresRegister());
8101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8102 break;
8103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008104 case DataType::Type::kFloat32:
8105 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008106 locations->SetInAt(0, Location::RequiresFpuRegister());
8107 locations->SetInAt(1, Location::RequiresFpuRegister());
8108 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8109 break;
8110
8111 default:
8112 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8113 }
8114}
8115
8116void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008117 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008118 LocationSummary* locations = instruction->GetLocations();
8119 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8120
8121 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008122 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008123 Register dst = locations->Out().AsRegister<Register>();
8124 Register lhs = locations->InAt(0).AsRegister<Register>();
8125 Register rhs = locations->InAt(1).AsRegister<Register>();
8126
8127 if (isR6) {
8128 __ MulR6(dst, lhs, rhs);
8129 } else {
8130 __ MulR2(dst, lhs, rhs);
8131 }
8132 break;
8133 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008134 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008135 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8136 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8137 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8138 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8139 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8140 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8141
8142 // Extra checks to protect caused by the existance of A1_A2.
8143 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8144 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8145 DCHECK_NE(dst_high, lhs_low);
8146 DCHECK_NE(dst_high, rhs_low);
8147
8148 // A_B * C_D
8149 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8150 // dst_lo: [ low(B*D) ]
8151 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8152
8153 if (isR6) {
8154 __ MulR6(TMP, lhs_high, rhs_low);
8155 __ MulR6(dst_high, lhs_low, rhs_high);
8156 __ Addu(dst_high, dst_high, TMP);
8157 __ MuhuR6(TMP, lhs_low, rhs_low);
8158 __ Addu(dst_high, dst_high, TMP);
8159 __ MulR6(dst_low, lhs_low, rhs_low);
8160 } else {
8161 __ MulR2(TMP, lhs_high, rhs_low);
8162 __ MulR2(dst_high, lhs_low, rhs_high);
8163 __ Addu(dst_high, dst_high, TMP);
8164 __ MultuR2(lhs_low, rhs_low);
8165 __ Mfhi(TMP);
8166 __ Addu(dst_high, dst_high, TMP);
8167 __ Mflo(dst_low);
8168 }
8169 break;
8170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008171 case DataType::Type::kFloat32:
8172 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008173 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8174 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8175 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008176 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008177 __ MulS(dst, lhs, rhs);
8178 } else {
8179 __ MulD(dst, lhs, rhs);
8180 }
8181 break;
8182 }
8183 default:
8184 LOG(FATAL) << "Unexpected mul type " << type;
8185 }
8186}
8187
8188void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008190 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008191 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008192 case DataType::Type::kInt32:
8193 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008194 locations->SetInAt(0, Location::RequiresRegister());
8195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8196 break;
8197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008198 case DataType::Type::kFloat32:
8199 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008200 locations->SetInAt(0, Location::RequiresFpuRegister());
8201 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8202 break;
8203
8204 default:
8205 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8206 }
8207}
8208
8209void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008210 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008211 LocationSummary* locations = instruction->GetLocations();
8212
8213 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008214 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008215 Register dst = locations->Out().AsRegister<Register>();
8216 Register src = locations->InAt(0).AsRegister<Register>();
8217 __ Subu(dst, ZERO, src);
8218 break;
8219 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008220 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008221 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8222 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8223 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8224 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8225 __ Subu(dst_low, ZERO, src_low);
8226 __ Sltu(TMP, ZERO, dst_low);
8227 __ Subu(dst_high, ZERO, src_high);
8228 __ Subu(dst_high, dst_high, TMP);
8229 break;
8230 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008231 case DataType::Type::kFloat32:
8232 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008233 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8234 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008235 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008236 __ NegS(dst, src);
8237 } else {
8238 __ NegD(dst, src);
8239 }
8240 break;
8241 }
8242 default:
8243 LOG(FATAL) << "Unexpected neg type " << type;
8244 }
8245}
8246
8247void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008248 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8249 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008250 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008251 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008252 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8253 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008254}
8255
8256void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008257 // Note: if heap poisoning is enabled, the entry point takes care
8258 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008259 QuickEntrypointEnum entrypoint =
8260 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8261 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008262 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008263 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008264}
8265
8266void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008267 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8268 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008269 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008270 if (instruction->IsStringAlloc()) {
8271 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8272 } else {
8273 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008274 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008275 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008276}
8277
8278void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008279 // Note: if heap poisoning is enabled, the entry point takes care
8280 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008281 if (instruction->IsStringAlloc()) {
8282 // String is allocated through StringFactory. Call NewEmptyString entry point.
8283 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008284 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008285 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8286 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8287 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008288 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008289 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8290 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008291 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008292 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008293 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294}
8295
8296void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008297 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298 locations->SetInAt(0, Location::RequiresRegister());
8299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8300}
8301
8302void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008303 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008304 LocationSummary* locations = instruction->GetLocations();
8305
8306 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008307 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008308 Register dst = locations->Out().AsRegister<Register>();
8309 Register src = locations->InAt(0).AsRegister<Register>();
8310 __ Nor(dst, src, ZERO);
8311 break;
8312 }
8313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008314 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008315 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8316 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8317 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8318 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8319 __ Nor(dst_high, src_high, ZERO);
8320 __ Nor(dst_low, src_low, ZERO);
8321 break;
8322 }
8323
8324 default:
8325 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8326 }
8327}
8328
8329void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008330 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 locations->SetInAt(0, Location::RequiresRegister());
8332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8333}
8334
8335void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8336 LocationSummary* locations = instruction->GetLocations();
8337 __ Xori(locations->Out().AsRegister<Register>(),
8338 locations->InAt(0).AsRegister<Register>(),
8339 1);
8340}
8341
8342void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008343 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8344 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345}
8346
Calin Juravle2ae48182016-03-16 14:05:09 +00008347void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8348 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008349 return;
8350 }
8351 Location obj = instruction->GetLocations()->InAt(0);
8352
8353 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008354 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008355}
8356
Calin Juravle2ae48182016-03-16 14:05:09 +00008357void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008358 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008359 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008360
8361 Location obj = instruction->GetLocations()->InAt(0);
8362
8363 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8364}
8365
8366void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008367 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008368}
8369
8370void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8371 HandleBinaryOp(instruction);
8372}
8373
8374void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8375 HandleBinaryOp(instruction);
8376}
8377
8378void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8379 LOG(FATAL) << "Unreachable";
8380}
8381
8382void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008383 if (instruction->GetNext()->IsSuspendCheck() &&
8384 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8385 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8386 // The back edge will generate the suspend check.
8387 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8388 }
8389
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008390 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8391}
8392
8393void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008394 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008395 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8396 if (location.IsStackSlot()) {
8397 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8398 } else if (location.IsDoubleStackSlot()) {
8399 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8400 }
8401 locations->SetOut(location);
8402}
8403
8404void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8405 ATTRIBUTE_UNUSED) {
8406 // Nothing to do, the parameter is already at its location.
8407}
8408
8409void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8410 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008411 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008412 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8413}
8414
8415void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8416 ATTRIBUTE_UNUSED) {
8417 // Nothing to do, the method is already at its location.
8418}
8419
8420void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008421 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008422 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008423 locations->SetInAt(i, Location::Any());
8424 }
8425 locations->SetOut(Location::Any());
8426}
8427
8428void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8429 LOG(FATAL) << "Unreachable";
8430}
8431
8432void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008433 DataType::Type type = rem->GetResultType();
8434 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8435 ? LocationSummary::kNoCall
8436 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008437 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008438
8439 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008440 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008441 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008442 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8444 break;
8445
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008446 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008447 InvokeRuntimeCallingConvention calling_convention;
8448 locations->SetInAt(0, Location::RegisterPairLocation(
8449 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8450 locations->SetInAt(1, Location::RegisterPairLocation(
8451 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8452 locations->SetOut(calling_convention.GetReturnLocation(type));
8453 break;
8454 }
8455
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008456 case DataType::Type::kFloat32:
8457 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008458 InvokeRuntimeCallingConvention calling_convention;
8459 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8460 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8461 locations->SetOut(calling_convention.GetReturnLocation(type));
8462 break;
8463 }
8464
8465 default:
8466 LOG(FATAL) << "Unexpected rem type " << type;
8467 }
8468}
8469
8470void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008471 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008472
8473 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008474 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008475 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008476 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008477 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008478 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008479 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8480 break;
8481 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008482 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008483 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008484 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485 break;
8486 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008487 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008488 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008489 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008490 break;
8491 }
8492 default:
8493 LOG(FATAL) << "Unexpected rem type " << type;
8494 }
8495}
8496
Igor Murashkind01745e2017-04-05 16:40:31 -07008497void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8498 constructor_fence->SetLocations(nullptr);
8499}
8500
8501void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8502 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8503 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8504}
8505
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008506void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8507 memory_barrier->SetLocations(nullptr);
8508}
8509
8510void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8511 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8512}
8513
8514void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008515 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008516 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008517 locations->SetInAt(0, MipsReturnLocation(return_type));
8518}
8519
8520void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8521 codegen_->GenerateFrameExit();
8522}
8523
8524void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8525 ret->SetLocations(nullptr);
8526}
8527
8528void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8529 codegen_->GenerateFrameExit();
8530}
8531
Alexey Frunze92d90602015-12-18 18:16:36 -08008532void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8533 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008534}
8535
Alexey Frunze92d90602015-12-18 18:16:36 -08008536void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8537 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008538}
8539
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008540void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8541 HandleShift(shl);
8542}
8543
8544void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8545 HandleShift(shl);
8546}
8547
8548void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8549 HandleShift(shr);
8550}
8551
8552void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8553 HandleShift(shr);
8554}
8555
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008556void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8557 HandleBinaryOp(instruction);
8558}
8559
8560void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8561 HandleBinaryOp(instruction);
8562}
8563
8564void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8565 HandleFieldGet(instruction, instruction->GetFieldInfo());
8566}
8567
8568void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8569 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8570}
8571
8572void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8573 HandleFieldSet(instruction, instruction->GetFieldInfo());
8574}
8575
8576void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008577 HandleFieldSet(instruction,
8578 instruction->GetFieldInfo(),
8579 instruction->GetDexPc(),
8580 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008581}
8582
8583void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8584 HUnresolvedInstanceFieldGet* instruction) {
8585 FieldAccessCallingConventionMIPS calling_convention;
8586 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8587 instruction->GetFieldType(),
8588 calling_convention);
8589}
8590
8591void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8592 HUnresolvedInstanceFieldGet* instruction) {
8593 FieldAccessCallingConventionMIPS calling_convention;
8594 codegen_->GenerateUnresolvedFieldAccess(instruction,
8595 instruction->GetFieldType(),
8596 instruction->GetFieldIndex(),
8597 instruction->GetDexPc(),
8598 calling_convention);
8599}
8600
8601void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8602 HUnresolvedInstanceFieldSet* instruction) {
8603 FieldAccessCallingConventionMIPS calling_convention;
8604 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8605 instruction->GetFieldType(),
8606 calling_convention);
8607}
8608
8609void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8610 HUnresolvedInstanceFieldSet* instruction) {
8611 FieldAccessCallingConventionMIPS calling_convention;
8612 codegen_->GenerateUnresolvedFieldAccess(instruction,
8613 instruction->GetFieldType(),
8614 instruction->GetFieldIndex(),
8615 instruction->GetDexPc(),
8616 calling_convention);
8617}
8618
8619void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8620 HUnresolvedStaticFieldGet* instruction) {
8621 FieldAccessCallingConventionMIPS calling_convention;
8622 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8623 instruction->GetFieldType(),
8624 calling_convention);
8625}
8626
8627void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8628 HUnresolvedStaticFieldGet* instruction) {
8629 FieldAccessCallingConventionMIPS calling_convention;
8630 codegen_->GenerateUnresolvedFieldAccess(instruction,
8631 instruction->GetFieldType(),
8632 instruction->GetFieldIndex(),
8633 instruction->GetDexPc(),
8634 calling_convention);
8635}
8636
8637void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8638 HUnresolvedStaticFieldSet* instruction) {
8639 FieldAccessCallingConventionMIPS calling_convention;
8640 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8641 instruction->GetFieldType(),
8642 calling_convention);
8643}
8644
8645void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8646 HUnresolvedStaticFieldSet* instruction) {
8647 FieldAccessCallingConventionMIPS calling_convention;
8648 codegen_->GenerateUnresolvedFieldAccess(instruction,
8649 instruction->GetFieldType(),
8650 instruction->GetFieldIndex(),
8651 instruction->GetDexPc(),
8652 calling_convention);
8653}
8654
8655void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008656 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8657 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008658 // In suspend check slow path, usually there are no caller-save registers at all.
8659 // If SIMD instructions are present, however, we force spilling all live SIMD
8660 // registers in full width (since the runtime only saves/restores lower part).
8661 locations->SetCustomSlowPathCallerSaves(
8662 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008663}
8664
8665void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8666 HBasicBlock* block = instruction->GetBlock();
8667 if (block->GetLoopInformation() != nullptr) {
8668 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8669 // The back edge will generate the suspend check.
8670 return;
8671 }
8672 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8673 // The goto will generate the suspend check.
8674 return;
8675 }
8676 GenerateSuspendCheck(instruction, nullptr);
8677}
8678
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008679void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008680 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8681 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682 InvokeRuntimeCallingConvention calling_convention;
8683 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8684}
8685
8686void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008687 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008688 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8689}
8690
8691void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008692 DataType::Type input_type = conversion->GetInputType();
8693 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008694 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8695 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008696 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008698 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8699 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008700 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8701 }
8702
8703 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008704 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008705 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8706 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008707 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008708 }
8709
Vladimir Markoca6fff82017-10-03 14:49:14 +01008710 LocationSummary* locations =
8711 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008712
8713 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008714 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008715 locations->SetInAt(0, Location::RequiresFpuRegister());
8716 } else {
8717 locations->SetInAt(0, Location::RequiresRegister());
8718 }
8719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008720 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008721 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8722 } else {
8723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8724 }
8725 } else {
8726 InvokeRuntimeCallingConvention calling_convention;
8727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008728 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008729 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8730 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008731 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008732 locations->SetInAt(0, Location::RegisterPairLocation(
8733 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8734 }
8735
8736 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8737 }
8738}
8739
8740void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8741 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008742 DataType::Type result_type = conversion->GetResultType();
8743 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008745 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008747 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8748 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008750 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008751 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8752 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8753 Register src = locations->InAt(0).AsRegister<Register>();
8754
Alexey Frunzea871ef12016-06-27 15:20:11 -07008755 if (dst_low != src) {
8756 __ Move(dst_low, src);
8757 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008758 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008759 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008760 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008761 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008762 ? locations->InAt(0).AsRegisterPairLow<Register>()
8763 : locations->InAt(0).AsRegister<Register>();
8764
8765 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008766 case DataType::Type::kUint8:
8767 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008768 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008769 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770 if (has_sign_extension) {
8771 __ Seb(dst, src);
8772 } else {
8773 __ Sll(dst, src, 24);
8774 __ Sra(dst, dst, 24);
8775 }
8776 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008777 case DataType::Type::kUint16:
8778 __ Andi(dst, src, 0xFFFF);
8779 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008780 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008781 if (has_sign_extension) {
8782 __ Seh(dst, src);
8783 } else {
8784 __ Sll(dst, src, 16);
8785 __ Sra(dst, dst, 16);
8786 }
8787 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008788 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008789 if (dst != src) {
8790 __ Move(dst, src);
8791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008792 break;
8793
8794 default:
8795 LOG(FATAL) << "Unexpected type conversion from " << input_type
8796 << " to " << result_type;
8797 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008798 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8799 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008800 if (isR6) {
8801 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8802 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8803 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8804 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8805 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8806 __ Mtc1(src_low, FTMP);
8807 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008808 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008809 __ Cvtsl(dst, FTMP);
8810 } else {
8811 __ Cvtdl(dst, FTMP);
8812 }
8813 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008814 QuickEntrypointEnum entrypoint =
8815 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008816 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008817 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008818 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8819 } else {
8820 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8821 }
8822 }
8823 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008824 Register src = locations->InAt(0).AsRegister<Register>();
8825 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8826 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008827 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008828 __ Cvtsw(dst, FTMP);
8829 } else {
8830 __ Cvtdw(dst, FTMP);
8831 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008832 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008833 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8834 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008835
8836 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8837 // value of the output type if the input is outside of the range after the truncation or
8838 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8839 // results. This matches the desired float/double-to-int/long conversion exactly.
8840 //
8841 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8842 // value when the input is either a NaN or is outside of the range of the output type
8843 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8844 // the same result.
8845 //
8846 // The code takes care of the different behaviors by first comparing the input to the
8847 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8848 // If the input is greater than or equal to the minimum, it procedes to the truncate
8849 // instruction, which will handle such an input the same way irrespective of NAN2008.
8850 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8851 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008852 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008853 if (isR6) {
8854 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8855 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8856 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8857 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8858 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008860 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008861 __ TruncLS(FTMP, src);
8862 } else {
8863 __ TruncLD(FTMP, src);
8864 }
8865 __ Mfc1(dst_low, FTMP);
8866 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008867 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008868 QuickEntrypointEnum entrypoint =
8869 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008870 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008871 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008872 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8873 } else {
8874 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8875 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008876 }
8877 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008878 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8879 Register dst = locations->Out().AsRegister<Register>();
8880 MipsLabel truncate;
8881 MipsLabel done;
8882
Lena Djokicf4e23a82017-05-09 15:43:45 +02008883 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008884 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008885 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8886 __ LoadConst32(TMP, min_val);
8887 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008888 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008889 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8890 __ LoadConst32(TMP, High32Bits(min_val));
8891 __ Mtc1(ZERO, FTMP);
8892 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008893 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008895 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008896 __ ColeS(0, FTMP, src);
8897 } else {
8898 __ ColeD(0, FTMP, src);
8899 }
8900 __ Bc1t(0, &truncate);
8901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008902 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008903 __ CeqS(0, src, src);
8904 } else {
8905 __ CeqD(0, src, src);
8906 }
8907 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8908 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008909
8910 __ B(&done);
8911
8912 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008913 }
8914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008915 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008916 __ TruncWS(FTMP, src);
8917 } else {
8918 __ TruncWD(FTMP, src);
8919 }
8920 __ Mfc1(dst, FTMP);
8921
Lena Djokicf4e23a82017-05-09 15:43:45 +02008922 if (!isR6) {
8923 __ Bind(&done);
8924 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008925 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008926 } else if (DataType::IsFloatingPointType(result_type) &&
8927 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008928 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8929 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008930 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008931 __ Cvtsd(dst, src);
8932 } else {
8933 __ Cvtds(dst, src);
8934 }
8935 } else {
8936 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8937 << " to " << result_type;
8938 }
8939}
8940
8941void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8942 HandleShift(ushr);
8943}
8944
8945void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8946 HandleShift(ushr);
8947}
8948
8949void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8950 HandleBinaryOp(instruction);
8951}
8952
8953void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8954 HandleBinaryOp(instruction);
8955}
8956
8957void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8958 // Nothing to do, this should be removed during prepare for register allocator.
8959 LOG(FATAL) << "Unreachable";
8960}
8961
8962void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8963 // Nothing to do, this should be removed during prepare for register allocator.
8964 LOG(FATAL) << "Unreachable";
8965}
8966
8967void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008968 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008969}
8970
8971void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008972 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008973}
8974
8975void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008976 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008977}
8978
8979void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008980 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008981}
8982
8983void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008984 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008985}
8986
8987void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008988 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008989}
8990
8991void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008992 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008993}
8994
8995void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008996 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008997}
8998
8999void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009000 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009001}
9002
9003void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009004 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009005}
9006
9007void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009008 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009009}
9010
9011void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009012 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009013}
9014
9015void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009016 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009017}
9018
9019void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009020 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009021}
9022
9023void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009024 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009025}
9026
9027void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009028 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009029}
9030
9031void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009032 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009033}
9034
9035void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009036 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009037}
9038
9039void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009040 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009041}
9042
9043void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009044 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009045}
9046
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009047void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9048 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009049 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009050 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009051 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9052 uint32_t num_entries = switch_instr->GetNumEntries();
9053 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9054 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9055 // instruction to simulate PC-relative addressing when accessing the jump table.
9056 // NAL clobbers RA. Make sure RA is preserved.
9057 codegen_->ClobberRA();
9058 }
9059 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009060}
9061
Alexey Frunze96b66822016-09-10 02:32:44 -07009062void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9063 int32_t lower_bound,
9064 uint32_t num_entries,
9065 HBasicBlock* switch_block,
9066 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009067 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009068 Register temp_reg = TMP;
9069 __ Addiu32(temp_reg, value_reg, -lower_bound);
9070 // Jump to default if index is negative
9071 // Note: We don't check the case that index is positive while value < lower_bound, because in
9072 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9073 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9074
Alexey Frunze96b66822016-09-10 02:32:44 -07009075 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009076 // Jump to successors[0] if value == lower_bound.
9077 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9078 int32_t last_index = 0;
9079 for (; num_entries - last_index > 2; last_index += 2) {
9080 __ Addiu(temp_reg, temp_reg, -2);
9081 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9082 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9083 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9084 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9085 }
9086 if (num_entries - last_index == 2) {
9087 // The last missing case_value.
9088 __ Addiu(temp_reg, temp_reg, -1);
9089 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009090 }
9091
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009092 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009093 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009094 __ B(codegen_->GetLabelOf(default_block));
9095 }
9096}
9097
Alexey Frunze96b66822016-09-10 02:32:44 -07009098void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9099 Register constant_area,
9100 int32_t lower_bound,
9101 uint32_t num_entries,
9102 HBasicBlock* switch_block,
9103 HBasicBlock* default_block) {
9104 // Create a jump table.
9105 std::vector<MipsLabel*> labels(num_entries);
9106 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9107 for (uint32_t i = 0; i < num_entries; i++) {
9108 labels[i] = codegen_->GetLabelOf(successors[i]);
9109 }
9110 JumpTable* table = __ CreateJumpTable(std::move(labels));
9111
9112 // Is the value in range?
9113 __ Addiu32(TMP, value_reg, -lower_bound);
9114 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9115 __ Sltiu(AT, TMP, num_entries);
9116 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9117 } else {
9118 __ LoadConst32(AT, num_entries);
9119 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9120 }
9121
9122 // We are in the range of the table.
9123 // Load the target address from the jump table, indexing by the value.
9124 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009125 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009126 __ Lw(TMP, TMP, 0);
9127 // Compute the absolute target address by adding the table start address
9128 // (the table contains offsets to targets relative to its start).
9129 __ Addu(TMP, TMP, AT);
9130 // And jump.
9131 __ Jr(TMP);
9132 __ NopIfNoReordering();
9133}
9134
9135void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9136 int32_t lower_bound = switch_instr->GetStartValue();
9137 uint32_t num_entries = switch_instr->GetNumEntries();
9138 LocationSummary* locations = switch_instr->GetLocations();
9139 Register value_reg = locations->InAt(0).AsRegister<Register>();
9140 HBasicBlock* switch_block = switch_instr->GetBlock();
9141 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9142
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009143 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009144 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009145 //
9146 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9147 // to access the jump table and it is implemented by changing HPackedSwitch to
9148 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9149 // VisitMipsPackedSwitch()).
9150 //
9151 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9152 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9153 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009154 GenTableBasedPackedSwitch(value_reg,
9155 ZERO,
9156 lower_bound,
9157 num_entries,
9158 switch_block,
9159 default_block);
9160 } else {
9161 GenPackedSwitchWithCompares(value_reg,
9162 lower_bound,
9163 num_entries,
9164 switch_block,
9165 default_block);
9166 }
9167}
9168
9169void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9170 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009171 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009172 locations->SetInAt(0, Location::RequiresRegister());
9173 // Constant area pointer (HMipsComputeBaseMethodAddress).
9174 locations->SetInAt(1, Location::RequiresRegister());
9175}
9176
9177void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9178 int32_t lower_bound = switch_instr->GetStartValue();
9179 uint32_t num_entries = switch_instr->GetNumEntries();
9180 LocationSummary* locations = switch_instr->GetLocations();
9181 Register value_reg = locations->InAt(0).AsRegister<Register>();
9182 Register constant_area = locations->InAt(1).AsRegister<Register>();
9183 HBasicBlock* switch_block = switch_instr->GetBlock();
9184 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9185
9186 // This is an R2-only path. HPackedSwitch has been changed to
9187 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9188 // required to address the jump table relative to PC.
9189 GenTableBasedPackedSwitch(value_reg,
9190 constant_area,
9191 lower_bound,
9192 num_entries,
9193 switch_block,
9194 default_block);
9195}
9196
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009197void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9198 HMipsComputeBaseMethodAddress* insn) {
9199 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009200 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009201 locations->SetOut(Location::RequiresRegister());
9202}
9203
9204void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9205 HMipsComputeBaseMethodAddress* insn) {
9206 LocationSummary* locations = insn->GetLocations();
9207 Register reg = locations->Out().AsRegister<Register>();
9208
9209 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9210
9211 // Generate a dummy PC-relative call to obtain PC.
9212 __ Nal();
9213 // Grab the return address off RA.
9214 __ Move(reg, RA);
9215
9216 // Remember this offset (the obtained PC value) for later use with constant area.
9217 __ BindPcRelBaseLabel();
9218}
9219
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009220void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9221 // The trampoline uses the same calling convention as dex calling conventions,
9222 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9223 // the method_idx.
9224 HandleInvoke(invoke);
9225}
9226
9227void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9228 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9229}
9230
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009231void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009233 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009234 locations->SetInAt(0, Location::RequiresRegister());
9235 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009236}
9237
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009238void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9239 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009240 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009241 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009242 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009243 __ LoadFromOffset(kLoadWord,
9244 locations->Out().AsRegister<Register>(),
9245 locations->InAt(0).AsRegister<Register>(),
9246 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009247 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009248 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009249 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009250 __ LoadFromOffset(kLoadWord,
9251 locations->Out().AsRegister<Register>(),
9252 locations->InAt(0).AsRegister<Register>(),
9253 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009254 __ LoadFromOffset(kLoadWord,
9255 locations->Out().AsRegister<Register>(),
9256 locations->Out().AsRegister<Register>(),
9257 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009258 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009259}
9260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009261#undef __
9262#undef QUICK_ENTRY_POINT
9263
9264} // namespace mips
9265} // namespace art