blob: c6346eb3b177f59519a6ded1a7edd8ef2f334277 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
61 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020062 return Location::RegisterLocation(V0);
63
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010064 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020065 return Location::RegisterPairLocation(V0, V1);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kFloat32:
68 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020069 return Location::FpuRegisterLocation(F0);
70
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010071 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location();
73 }
74 UNREACHABLE();
75}
76
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010077Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020078 return MipsReturnLocation(type);
79}
80
81Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
82 return Location::RegisterLocation(kMethodRegisterArgument);
83}
84
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020086 Location next_location;
87
88 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010089 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kInt8:
93 case DataType::Type::kUint16:
94 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010095 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020096 uint32_t gp_index = gp_index_++;
97 if (gp_index < calling_convention.GetNumberOfRegisters()) {
98 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
99 } else {
100 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
101 next_location = Location::StackSlot(stack_offset);
102 }
103 break;
104 }
105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200107 uint32_t gp_index = gp_index_;
108 gp_index_ += 2;
109 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800110 Register reg = calling_convention.GetRegisterAt(gp_index);
111 if (reg == A1 || reg == A3) {
112 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200113 gp_index++;
114 }
115 Register low_even = calling_convention.GetRegisterAt(gp_index);
116 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
117 DCHECK_EQ(low_even + 1, high_odd);
118 next_location = Location::RegisterPairLocation(low_even, high_odd);
119 } else {
120 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
121 next_location = Location::DoubleStackSlot(stack_offset);
122 }
123 break;
124 }
125
126 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
127 // will take up the even/odd pair, while floats are stored in even regs only.
128 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 case DataType::Type::kFloat32:
130 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200131 uint32_t float_index = float_index_++;
132 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
133 next_location = Location::FpuRegisterLocation(
134 calling_convention.GetFpuRegisterAt(float_index));
135 } else {
136 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
138 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200139 }
140 break;
141 }
142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200144 LOG(FATAL) << "Unexpected parameter type " << type;
145 break;
146 }
147
148 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 return next_location;
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155 return MipsReturnLocation(type);
156}
157
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100158// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
159#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700160#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
163 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000164 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
167 LocationSummary* locations = instruction_->GetLocations();
168 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
169 __ Bind(GetEntryLabel());
170 if (instruction_->CanThrowIntoCatchBlock()) {
171 // Live registers will be restored in the catch block if caught.
172 SaveLiveRegisters(codegen, instruction_->GetLocations());
173 }
174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
176 InvokeRuntimeCallingConvention calling_convention;
177 codegen->EmitParallelMoves(locations->InAt(0),
178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100179 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 locations->InAt(1),
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100183 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
184 ? kQuickThrowStringBounds
185 : kQuickThrowArrayBounds;
186 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100187 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
189 }
190
191 bool IsFatal() const OVERRIDE { return true; }
192
193 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
194
195 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
197};
198
199class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
200 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202
203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
204 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
205 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100206 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
208 }
209
210 bool IsFatal() const OVERRIDE { return true; }
211
212 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
213
214 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200215 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
216};
217
218class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
219 public:
220 LoadClassSlowPathMIPS(HLoadClass* cls,
221 HInstruction* at,
222 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700223 bool do_clinit,
224 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
225 : SlowPathCodeMIPS(at),
226 cls_(cls),
227 dex_pc_(dex_pc),
228 do_clinit_(do_clinit),
229 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
231 }
232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000234 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700237 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700238 InvokeRuntimeCallingConvention calling_convention;
239 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
240 const bool is_load_class_bss_entry =
241 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200242 __ Bind(GetEntryLabel());
243 SaveLiveRegisters(codegen, locations);
244
Alexey Frunzec61c0762017-04-10 13:54:23 -0700245 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
246 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700247 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700248 Register temp = locations->GetTemp(0).AsRegister<Register>();
249 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
250 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
251 // kSaveEverything call.
252 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
253 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
254 if (temp_is_a0) {
255 __ Move(entry_address, temp);
256 }
257 }
258
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000259 dex::TypeIndex type_index = cls_->GetTypeIndex();
260 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100261 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
262 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
269
Alexey Frunzec61c0762017-04-10 13:54:23 -0700270 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700271 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700272 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700273 DCHECK(bss_info_high_);
274 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
275 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700276 __ Sw(calling_convention.GetRegisterAt(0),
277 entry_address,
278 /* placeholder */ 0x5678,
279 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700280 }
281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283 if (out.IsValid()) {
284 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100285 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700286 mips_codegen->MoveLocation(out,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
288 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291
292 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700293 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
294 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700296 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200297 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
298 Register base =
299 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700300 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000301 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700302 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
303 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700304 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
305 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 __ B(GetExitLabel());
308 }
309
310 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
311
312 private:
313 // The class this slow path will load.
314 HLoadClass* const cls_;
315
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 // The dex PC of `at_`.
317 const uint32_t dex_pc_;
318
319 // Whether to initialize the class.
320 const bool do_clinit_;
321
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700322 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
323 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
324
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200325 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
326};
327
328class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
329 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700330 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
331 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
332 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333
334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700335 DCHECK(instruction_->IsLoadString());
336 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 LocationSummary* locations = instruction_->GetLocations();
338 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700339 HLoadString* load = instruction_->AsLoadString();
340 const dex::StringIndex string_index = load->GetStringIndex();
341 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700343 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700344 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 __ Bind(GetEntryLabel());
346 SaveLiveRegisters(codegen, locations);
347
Alexey Frunzec61c0762017-04-10 13:54:23 -0700348 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
349 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700350 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700351 Register temp = locations->GetTemp(0).AsRegister<Register>();
352 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
353 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
354 // kSaveEverything call.
355 entry_address = temp_is_a0 ? out : temp;
356 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
357 if (temp_is_a0) {
358 __ Move(entry_address, temp);
359 }
360 }
361
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100363 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200364 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700365
366 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700367 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700368 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700369 DCHECK(bss_info_high_);
370 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100371 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700372 __ Sw(calling_convention.GetRegisterAt(0),
373 entry_address,
374 /* placeholder */ 0x5678,
375 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700376 }
377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100378 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700380 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000383
Alexey Frunzec61c0762017-04-10 13:54:23 -0700384 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700385 if (!baker_or_no_read_barriers) {
386 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700387 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700388 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200389 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
390 Register base =
391 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700392 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100393 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700394 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100395 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700396 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
397 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700398 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 __ B(GetExitLabel());
400 }
401
402 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
403
404 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700405 // Pointer to the high half PC-relative patch info.
406 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
407
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200408 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
409};
410
411class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
412 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000413 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
417 __ Bind(GetEntryLabel());
418 if (instruction_->CanThrowIntoCatchBlock()) {
419 // Live registers will be restored in the catch block if caught.
420 SaveLiveRegisters(codegen, instruction_->GetLocations());
421 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100422 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 instruction_,
424 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100425 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200426 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
427 }
428
429 bool IsFatal() const OVERRIDE { return true; }
430
431 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
432
433 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
435};
436
437class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
438 public:
439 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000440 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441
442 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200443 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
445 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200446 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100447 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200449 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 if (successor_ == nullptr) {
451 __ B(GetReturnLabel());
452 } else {
453 __ B(mips_codegen->GetLabelOf(successor_));
454 }
455 }
456
457 MipsLabel* GetReturnLabel() {
458 DCHECK(successor_ == nullptr);
459 return &return_label_;
460 }
461
462 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
463
464 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200465 // If not null, the block to branch to after the suspend check.
466 HBasicBlock* const successor_;
467
468 // If `successor_` is null, the label to branch to after the suspend check.
469 MipsLabel return_label_;
470
471 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
472};
473
474class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
475 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800476 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
477 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200478
479 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
480 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200481 uint32_t dex_pc = instruction_->GetDexPc();
482 DCHECK(instruction_->IsCheckCast()
483 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
484 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
485
486 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800487 if (!is_fatal_) {
488 SaveLiveRegisters(codegen, locations);
489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490
491 // We're moving two locations to locations that could overlap, so we need a parallel
492 // move resolver.
493 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200495 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800497 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100499 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200500 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100501 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800502 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100503 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200504 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
505 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200506 } else {
507 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800508 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
509 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200510 }
511
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800512 if (!is_fatal_) {
513 RestoreLiveRegisters(codegen, locations);
514 __ B(GetExitLabel());
515 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200516 }
517
518 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
519
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800520 bool IsFatal() const OVERRIDE { return is_fatal_; }
521
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200522 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800523 const bool is_fatal_;
524
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200525 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
526};
527
528class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
529 public:
Aart Bik42249c32016-01-07 15:33:50 -0800530 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800534 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200535 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100536 LocationSummary* locations = instruction_->GetLocations();
537 SaveLiveRegisters(codegen, locations);
538 InvokeRuntimeCallingConvention calling_convention;
539 __ LoadConst32(calling_convention.GetRegisterAt(0),
540 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100541 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100542 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200543 }
544
545 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
546
547 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200548 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
549};
550
Alexey Frunze15958152017-02-09 19:08:30 -0800551class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
552 public:
553 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
554
555 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
556 LocationSummary* locations = instruction_->GetLocations();
557 __ Bind(GetEntryLabel());
558 SaveLiveRegisters(codegen, locations);
559
560 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100561 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800562 parallel_move.AddMove(
563 locations->InAt(0),
564 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100565 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800566 nullptr);
567 parallel_move.AddMove(
568 locations->InAt(1),
569 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100570 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800571 nullptr);
572 parallel_move.AddMove(
573 locations->InAt(2),
574 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100575 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800576 nullptr);
577 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
578
579 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
580 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
581 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
582 RestoreLiveRegisters(codegen, locations);
583 __ B(GetExitLabel());
584 }
585
586 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
587
588 private:
589 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
590};
591
592// Slow path marking an object reference `ref` during a read
593// barrier. The field `obj.field` in the object `obj` holding this
594// reference does not get updated by this slow path after marking (see
595// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
596//
597// This means that after the execution of this slow path, `ref` will
598// always be up-to-date, but `obj.field` may not; i.e., after the
599// flip, `ref` will be a to-space reference, but `obj.field` will
600// probably still be a from-space reference (unless it gets updated by
601// another thread, or if another thread installed another object
602// reference (different from `ref`) in `obj.field`).
603//
604// If `entrypoint` is a valid location it is assumed to already be
605// holding the entrypoint. The case where the entrypoint is passed in
606// is for the GcRoot read barrier.
607class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
608 public:
609 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
610 Location ref,
611 Location entrypoint = Location::NoLocation())
612 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
613 DCHECK(kEmitCompilerReadBarrier);
614 }
615
616 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
617
618 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
619 LocationSummary* locations = instruction_->GetLocations();
620 Register ref_reg = ref_.AsRegister<Register>();
621 DCHECK(locations->CanCall());
622 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
623 DCHECK(instruction_->IsInstanceFieldGet() ||
624 instruction_->IsStaticFieldGet() ||
625 instruction_->IsArrayGet() ||
626 instruction_->IsArraySet() ||
627 instruction_->IsLoadClass() ||
628 instruction_->IsLoadString() ||
629 instruction_->IsInstanceOf() ||
630 instruction_->IsCheckCast() ||
631 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
632 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
633 << "Unexpected instruction in read barrier marking slow path: "
634 << instruction_->DebugName();
635
636 __ Bind(GetEntryLabel());
637 // No need to save live registers; it's taken care of by the
638 // entrypoint. Also, there is no need to update the stack mask,
639 // as this runtime call will not trigger a garbage collection.
640 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
641 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
642 (S2 <= ref_reg && ref_reg <= S7) ||
643 (ref_reg == FP)) << ref_reg;
644 // "Compact" slow path, saving two moves.
645 //
646 // Instead of using the standard runtime calling convention (input
647 // and output in A0 and V0 respectively):
648 //
649 // A0 <- ref
650 // V0 <- ReadBarrierMark(A0)
651 // ref <- V0
652 //
653 // we just use rX (the register containing `ref`) as input and output
654 // of a dedicated entrypoint:
655 //
656 // rX <- ReadBarrierMarkRegX(rX)
657 //
658 if (entrypoint_.IsValid()) {
659 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
660 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
661 __ Jalr(entrypoint_.AsRegister<Register>());
662 __ NopIfNoReordering();
663 } else {
664 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100665 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800666 // This runtime call does not require a stack map.
667 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
668 instruction_,
669 this,
670 /* direct */ false);
671 }
672 __ B(GetExitLabel());
673 }
674
675 private:
676 // The location (register) of the marked object reference.
677 const Location ref_;
678
679 // The location of the entrypoint if already loaded.
680 const Location entrypoint_;
681
682 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
683};
684
685// Slow path marking an object reference `ref` during a read barrier,
686// and if needed, atomically updating the field `obj.field` in the
687// object `obj` holding this reference after marking (contrary to
688// ReadBarrierMarkSlowPathMIPS above, which never tries to update
689// `obj.field`).
690//
691// This means that after the execution of this slow path, both `ref`
692// and `obj.field` will be up-to-date; i.e., after the flip, both will
693// hold the same to-space reference (unless another thread installed
694// another object reference (different from `ref`) in `obj.field`).
695class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
696 public:
697 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
698 Location ref,
699 Register obj,
700 Location field_offset,
701 Register temp1)
702 : SlowPathCodeMIPS(instruction),
703 ref_(ref),
704 obj_(obj),
705 field_offset_(field_offset),
706 temp1_(temp1) {
707 DCHECK(kEmitCompilerReadBarrier);
708 }
709
710 const char* GetDescription() const OVERRIDE {
711 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
712 }
713
714 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
715 LocationSummary* locations = instruction_->GetLocations();
716 Register ref_reg = ref_.AsRegister<Register>();
717 DCHECK(locations->CanCall());
718 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
719 // This slow path is only used by the UnsafeCASObject intrinsic.
720 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
721 << "Unexpected instruction in read barrier marking and field updating slow path: "
722 << instruction_->DebugName();
723 DCHECK(instruction_->GetLocations()->Intrinsified());
724 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
725 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
726
727 __ Bind(GetEntryLabel());
728
729 // Save the old reference.
730 // Note that we cannot use AT or TMP to save the old reference, as those
731 // are used by the code that follows, but we need the old reference after
732 // the call to the ReadBarrierMarkRegX entry point.
733 DCHECK_NE(temp1_, AT);
734 DCHECK_NE(temp1_, TMP);
735 __ Move(temp1_, ref_reg);
736
737 // No need to save live registers; it's taken care of by the
738 // entrypoint. Also, there is no need to update the stack mask,
739 // as this runtime call will not trigger a garbage collection.
740 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
741 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
742 (S2 <= ref_reg && ref_reg <= S7) ||
743 (ref_reg == FP)) << ref_reg;
744 // "Compact" slow path, saving two moves.
745 //
746 // Instead of using the standard runtime calling convention (input
747 // and output in A0 and V0 respectively):
748 //
749 // A0 <- ref
750 // V0 <- ReadBarrierMark(A0)
751 // ref <- V0
752 //
753 // we just use rX (the register containing `ref`) as input and output
754 // of a dedicated entrypoint:
755 //
756 // rX <- ReadBarrierMarkRegX(rX)
757 //
758 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100759 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800760 // This runtime call does not require a stack map.
761 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
762 instruction_,
763 this,
764 /* direct */ false);
765
766 // If the new reference is different from the old reference,
767 // update the field in the holder (`*(obj_ + field_offset_)`).
768 //
769 // Note that this field could also hold a different object, if
770 // another thread had concurrently changed it. In that case, the
771 // the compare-and-set (CAS) loop below would abort, leaving the
772 // field as-is.
773 MipsLabel done;
774 __ Beq(temp1_, ref_reg, &done);
775
776 // Update the the holder's field atomically. This may fail if
777 // mutator updates before us, but it's OK. This is achieved
778 // using a strong compare-and-set (CAS) operation with relaxed
779 // memory synchronization ordering, where the expected value is
780 // the old reference and the desired value is the new reference.
781
782 // Convenience aliases.
783 Register base = obj_;
784 // The UnsafeCASObject intrinsic uses a register pair as field
785 // offset ("long offset"), of which only the low part contains
786 // data.
787 Register offset = field_offset_.AsRegisterPairLow<Register>();
788 Register expected = temp1_;
789 Register value = ref_reg;
790 Register tmp_ptr = TMP; // Pointer to actual memory.
791 Register tmp = AT; // Value in memory.
792
793 __ Addu(tmp_ptr, base, offset);
794
795 if (kPoisonHeapReferences) {
796 __ PoisonHeapReference(expected);
797 // Do not poison `value` if it is the same register as
798 // `expected`, which has just been poisoned.
799 if (value != expected) {
800 __ PoisonHeapReference(value);
801 }
802 }
803
804 // do {
805 // tmp = [r_ptr] - expected;
806 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
807
808 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
809 MipsLabel loop_head, exit_loop;
810 __ Bind(&loop_head);
811 if (is_r6) {
812 __ LlR6(tmp, tmp_ptr);
813 } else {
814 __ LlR2(tmp, tmp_ptr);
815 }
816 __ Bne(tmp, expected, &exit_loop);
817 __ Move(tmp, value);
818 if (is_r6) {
819 __ ScR6(tmp, tmp_ptr);
820 } else {
821 __ ScR2(tmp, tmp_ptr);
822 }
823 __ Beqz(tmp, &loop_head);
824 __ Bind(&exit_loop);
825
826 if (kPoisonHeapReferences) {
827 __ UnpoisonHeapReference(expected);
828 // Do not unpoison `value` if it is the same register as
829 // `expected`, which has just been unpoisoned.
830 if (value != expected) {
831 __ UnpoisonHeapReference(value);
832 }
833 }
834
835 __ Bind(&done);
836 __ B(GetExitLabel());
837 }
838
839 private:
840 // The location (register) of the marked object reference.
841 const Location ref_;
842 // The register containing the object holding the marked object reference field.
843 const Register obj_;
844 // The location of the offset of the marked reference field within `obj_`.
845 Location field_offset_;
846
847 const Register temp1_;
848
849 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
850};
851
852// Slow path generating a read barrier for a heap reference.
853class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
854 public:
855 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
856 Location out,
857 Location ref,
858 Location obj,
859 uint32_t offset,
860 Location index)
861 : SlowPathCodeMIPS(instruction),
862 out_(out),
863 ref_(ref),
864 obj_(obj),
865 offset_(offset),
866 index_(index) {
867 DCHECK(kEmitCompilerReadBarrier);
868 // If `obj` is equal to `out` or `ref`, it means the initial object
869 // has been overwritten by (or after) the heap object reference load
870 // to be instrumented, e.g.:
871 //
872 // __ LoadFromOffset(kLoadWord, out, out, offset);
873 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
874 //
875 // In that case, we have lost the information about the original
876 // object, and the emitted read barrier cannot work properly.
877 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
878 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
879 }
880
881 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
882 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
883 LocationSummary* locations = instruction_->GetLocations();
884 Register reg_out = out_.AsRegister<Register>();
885 DCHECK(locations->CanCall());
886 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
887 DCHECK(instruction_->IsInstanceFieldGet() ||
888 instruction_->IsStaticFieldGet() ||
889 instruction_->IsArrayGet() ||
890 instruction_->IsInstanceOf() ||
891 instruction_->IsCheckCast() ||
892 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
893 << "Unexpected instruction in read barrier for heap reference slow path: "
894 << instruction_->DebugName();
895
896 __ Bind(GetEntryLabel());
897 SaveLiveRegisters(codegen, locations);
898
899 // We may have to change the index's value, but as `index_` is a
900 // constant member (like other "inputs" of this slow path),
901 // introduce a copy of it, `index`.
902 Location index = index_;
903 if (index_.IsValid()) {
904 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
905 if (instruction_->IsArrayGet()) {
906 // Compute the actual memory offset and store it in `index`.
907 Register index_reg = index_.AsRegister<Register>();
908 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
909 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
910 // We are about to change the value of `index_reg` (see the
911 // calls to art::mips::MipsAssembler::Sll and
912 // art::mips::MipsAssembler::Addiu32 below), but it has
913 // not been saved by the previous call to
914 // art::SlowPathCode::SaveLiveRegisters, as it is a
915 // callee-save register --
916 // art::SlowPathCode::SaveLiveRegisters does not consider
917 // callee-save registers, as it has been designed with the
918 // assumption that callee-save registers are supposed to be
919 // handled by the called function. So, as a callee-save
920 // register, `index_reg` _would_ eventually be saved onto
921 // the stack, but it would be too late: we would have
922 // changed its value earlier. Therefore, we manually save
923 // it here into another freely available register,
924 // `free_reg`, chosen of course among the caller-save
925 // registers (as a callee-save `free_reg` register would
926 // exhibit the same problem).
927 //
928 // Note we could have requested a temporary register from
929 // the register allocator instead; but we prefer not to, as
930 // this is a slow path, and we know we can find a
931 // caller-save register that is available.
932 Register free_reg = FindAvailableCallerSaveRegister(codegen);
933 __ Move(free_reg, index_reg);
934 index_reg = free_reg;
935 index = Location::RegisterLocation(index_reg);
936 } else {
937 // The initial register stored in `index_` has already been
938 // saved in the call to art::SlowPathCode::SaveLiveRegisters
939 // (as it is not a callee-save register), so we can freely
940 // use it.
941 }
942 // Shifting the index value contained in `index_reg` by the scale
943 // factor (2) cannot overflow in practice, as the runtime is
944 // unable to allocate object arrays with a size larger than
945 // 2^26 - 1 (that is, 2^28 - 4 bytes).
946 __ Sll(index_reg, index_reg, TIMES_4);
947 static_assert(
948 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
949 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
950 __ Addiu32(index_reg, index_reg, offset_);
951 } else {
952 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
953 // intrinsics, `index_` is not shifted by a scale factor of 2
954 // (as in the case of ArrayGet), as it is actually an offset
955 // to an object field within an object.
956 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
957 DCHECK(instruction_->GetLocations()->Intrinsified());
958 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
959 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
960 << instruction_->AsInvoke()->GetIntrinsic();
961 DCHECK_EQ(offset_, 0U);
962 DCHECK(index_.IsRegisterPair());
963 // UnsafeGet's offset location is a register pair, the low
964 // part contains the correct offset.
965 index = index_.ToLow();
966 }
967 }
968
969 // We're moving two or three locations to locations that could
970 // overlap, so we need a parallel move resolver.
971 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800973 parallel_move.AddMove(ref_,
974 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800976 nullptr);
977 parallel_move.AddMove(obj_,
978 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100979 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800980 nullptr);
981 if (index.IsValid()) {
982 parallel_move.AddMove(index,
983 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100984 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800985 nullptr);
986 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
987 } else {
988 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
989 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
990 }
991 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
992 instruction_,
993 instruction_->GetDexPc(),
994 this);
995 CheckEntrypointTypes<
996 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200997 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100998 calling_convention.GetReturnLocation(DataType::Type::kReference),
999 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001000
1001 RestoreLiveRegisters(codegen, locations);
1002 __ B(GetExitLabel());
1003 }
1004
1005 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1006
1007 private:
1008 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1009 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1010 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1011 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1012 if (i != ref &&
1013 i != obj &&
1014 !codegen->IsCoreCalleeSaveRegister(i) &&
1015 !codegen->IsBlockedCoreRegister(i)) {
1016 return static_cast<Register>(i);
1017 }
1018 }
1019 // We shall never fail to find a free caller-save register, as
1020 // there are more than two core caller-save registers on MIPS
1021 // (meaning it is possible to find one which is different from
1022 // `ref` and `obj`).
1023 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1024 LOG(FATAL) << "Could not find a free caller-save register";
1025 UNREACHABLE();
1026 }
1027
1028 const Location out_;
1029 const Location ref_;
1030 const Location obj_;
1031 const uint32_t offset_;
1032 // An additional location containing an index to an array.
1033 // Only used for HArrayGet and the UnsafeGetObject &
1034 // UnsafeGetObjectVolatile intrinsics.
1035 const Location index_;
1036
1037 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1038};
1039
1040// Slow path generating a read barrier for a GC root.
1041class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1042 public:
1043 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1044 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1045 DCHECK(kEmitCompilerReadBarrier);
1046 }
1047
1048 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1049 LocationSummary* locations = instruction_->GetLocations();
1050 Register reg_out = out_.AsRegister<Register>();
1051 DCHECK(locations->CanCall());
1052 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1053 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1054 << "Unexpected instruction in read barrier for GC root slow path: "
1055 << instruction_->DebugName();
1056
1057 __ Bind(GetEntryLabel());
1058 SaveLiveRegisters(codegen, locations);
1059
1060 InvokeRuntimeCallingConvention calling_convention;
1061 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001062 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1063 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001064 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001065 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1066 instruction_,
1067 instruction_->GetDexPc(),
1068 this);
1069 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001070 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001071 calling_convention.GetReturnLocation(DataType::Type::kReference),
1072 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001073
1074 RestoreLiveRegisters(codegen, locations);
1075 __ B(GetExitLabel());
1076 }
1077
1078 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1079
1080 private:
1081 const Location out_;
1082 const Location root_;
1083
1084 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1085};
1086
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001087CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1088 const MipsInstructionSetFeatures& isa_features,
1089 const CompilerOptions& compiler_options,
1090 OptimizingCompilerStats* stats)
1091 : CodeGenerator(graph,
1092 kNumberOfCoreRegisters,
1093 kNumberOfFRegisters,
1094 kNumberOfRegisterPairs,
1095 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1096 arraysize(kCoreCalleeSaves)),
1097 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1098 arraysize(kFpuCalleeSaves)),
1099 compiler_options,
1100 stats),
1101 block_labels_(nullptr),
1102 location_builder_(graph, this),
1103 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001104 move_resolver_(graph->GetAllocator(), this),
1105 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001106 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001107 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1109 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1110 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1111 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1112 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1113 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1114 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1115 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1116 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001117 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001118 // Save RA (containing the return address) to mimic Quick.
1119 AddAllocatedRegister(Location::RegisterLocation(RA));
1120}
1121
1122#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001123// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1124#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001125#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001126
1127void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1128 // Ensure that we fix up branches.
1129 __ FinalizeCode();
1130
1131 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001132 StackMapStream* stack_map_stream = GetStackMapStream();
1133 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001134 uint32_t old_position =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001135 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001136 uint32_t new_position = __ GetAdjustedPosition(old_position);
1137 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001138 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001139 }
1140
1141 // Adjust pc offsets for the disassembly information.
1142 if (disasm_info_ != nullptr) {
1143 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1144 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1145 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1146 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1147 it.second.start = __ GetAdjustedPosition(it.second.start);
1148 it.second.end = __ GetAdjustedPosition(it.second.end);
1149 }
1150 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1151 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1152 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1153 }
1154 }
1155
1156 CodeGenerator::Finalize(allocator);
1157}
1158
1159MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1160 return codegen_->GetAssembler();
1161}
1162
1163void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1164 DCHECK_LT(index, moves_.size());
1165 MoveOperands* move = moves_[index];
1166 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1167}
1168
1169void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1170 DCHECK_LT(index, moves_.size());
1171 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001172 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001173 Location loc1 = move->GetDestination();
1174 Location loc2 = move->GetSource();
1175
1176 DCHECK(!loc1.IsConstant());
1177 DCHECK(!loc2.IsConstant());
1178
1179 if (loc1.Equals(loc2)) {
1180 return;
1181 }
1182
1183 if (loc1.IsRegister() && loc2.IsRegister()) {
1184 // Swap 2 GPRs.
1185 Register r1 = loc1.AsRegister<Register>();
1186 Register r2 = loc2.AsRegister<Register>();
1187 __ Move(TMP, r2);
1188 __ Move(r2, r1);
1189 __ Move(r1, TMP);
1190 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1191 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1192 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194 __ MovS(FTMP, f2);
1195 __ MovS(f2, f1);
1196 __ MovS(f1, FTMP);
1197 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001198 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001199 __ MovD(FTMP, f2);
1200 __ MovD(f2, f1);
1201 __ MovD(f1, FTMP);
1202 }
1203 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1204 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1205 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001207 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1208 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001209 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 __ Move(TMP, r2);
1211 __ Mfc1(r2, f1);
1212 __ Mtc1(TMP, f1);
1213 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1214 // Swap 2 GPR register pairs.
1215 Register r1 = loc1.AsRegisterPairLow<Register>();
1216 Register r2 = loc2.AsRegisterPairLow<Register>();
1217 __ Move(TMP, r2);
1218 __ Move(r2, r1);
1219 __ Move(r1, TMP);
1220 r1 = loc1.AsRegisterPairHigh<Register>();
1221 r2 = loc2.AsRegisterPairHigh<Register>();
1222 __ Move(TMP, r2);
1223 __ Move(r2, r1);
1224 __ Move(r1, TMP);
1225 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1226 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1227 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001228 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001229 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1230 : loc2.AsFpuRegister<FRegister>();
1231 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1232 : loc2.AsRegisterPairLow<Register>();
1233 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1234 : loc2.AsRegisterPairHigh<Register>();
1235 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1236 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1237 // unpredictable and the following mfch1 will fail.
1238 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001239 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001241 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242 __ Move(r2_l, TMP);
1243 __ Move(r2_h, AT);
1244 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1245 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1246 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1247 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001248 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1249 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001250 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1251 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001252 __ Move(TMP, reg);
1253 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1254 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1255 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1256 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1257 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1258 : loc2.AsRegisterPairLow<Register>();
1259 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1260 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001261 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001262 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1263 : loc2.GetHighStackIndex(kMipsWordSize);
1264 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001265 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001266 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001267 __ Move(TMP, reg_h);
1268 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1269 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001270 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1271 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1272 : loc2.AsFpuRegister<FRegister>();
1273 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001275 __ MovS(FTMP, reg);
1276 __ LoadSFromOffset(reg, SP, offset);
1277 __ StoreSToOffset(FTMP, SP, offset);
1278 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001280 __ MovD(FTMP, reg);
1281 __ LoadDFromOffset(reg, SP, offset);
1282 __ StoreDToOffset(FTMP, SP, offset);
1283 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284 } else {
1285 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1286 }
1287}
1288
1289void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1290 __ Pop(static_cast<Register>(reg));
1291}
1292
1293void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1294 __ Push(static_cast<Register>(reg));
1295}
1296
1297void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1298 // Allocate a scratch register other than TMP, if available.
1299 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1300 // automatically unspilled when the scratch scope object is destroyed).
1301 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1302 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1303 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1304 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1305 __ LoadFromOffset(kLoadWord,
1306 Register(ensure_scratch.GetRegister()),
1307 SP,
1308 index1 + stack_offset);
1309 __ LoadFromOffset(kLoadWord,
1310 TMP,
1311 SP,
1312 index2 + stack_offset);
1313 __ StoreToOffset(kStoreWord,
1314 Register(ensure_scratch.GetRegister()),
1315 SP,
1316 index2 + stack_offset);
1317 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1318 }
1319}
1320
Alexey Frunze73296a72016-06-03 22:51:46 -07001321void CodeGeneratorMIPS::ComputeSpillMask() {
1322 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1323 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1324 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1325 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1326 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1327 // within the stack frame.
1328 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1329 core_spill_mask_ |= (1 << ZERO);
1330 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001331}
1332
1333bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001334 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001335 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1336 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1337 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001338 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001339}
1340
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001341static dwarf::Reg DWARFReg(Register reg) {
1342 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1343}
1344
1345// TODO: mapping of floating-point registers to DWARF.
1346
1347void CodeGeneratorMIPS::GenerateFrameEntry() {
1348 __ Bind(&frame_entry_label_);
1349
1350 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1351
1352 if (do_overflow_check) {
1353 __ LoadFromOffset(kLoadWord,
1354 ZERO,
1355 SP,
1356 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1357 RecordPcInfo(nullptr, 0);
1358 }
1359
1360 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001361 CHECK_EQ(fpu_spill_mask_, 0u);
1362 CHECK_EQ(core_spill_mask_, 1u << RA);
1363 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001364 return;
1365 }
1366
1367 // Make sure the frame size isn't unreasonably large.
1368 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1369 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1370 }
1371
1372 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001373
Alexey Frunze73296a72016-06-03 22:51:46 -07001374 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001375 __ IncreaseFrameSize(ofs);
1376
Alexey Frunze73296a72016-06-03 22:51:46 -07001377 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1378 Register reg = static_cast<Register>(MostSignificantBit(mask));
1379 mask ^= 1u << reg;
1380 ofs -= kMipsWordSize;
1381 // The ZERO register is only included for alignment.
1382 if (reg != ZERO) {
1383 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001384 __ cfi().RelOffset(DWARFReg(reg), ofs);
1385 }
1386 }
1387
Alexey Frunze73296a72016-06-03 22:51:46 -07001388 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1389 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1390 mask ^= 1u << reg;
1391 ofs -= kMipsDoublewordSize;
1392 __ StoreDToOffset(reg, SP, ofs);
1393 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001394 }
1395
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001396 // Save the current method if we need it. Note that we do not
1397 // do this in HCurrentMethod, as the instruction might have been removed
1398 // in the SSA graph.
1399 if (RequiresCurrentMethod()) {
1400 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1401 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001402
1403 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1404 // Initialize should deoptimize flag to 0.
1405 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1406 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001407}
1408
1409void CodeGeneratorMIPS::GenerateFrameExit() {
1410 __ cfi().RememberState();
1411
1412 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001414
Alexey Frunze73296a72016-06-03 22:51:46 -07001415 // For better instruction scheduling restore RA before other registers.
1416 uint32_t ofs = GetFrameSize();
1417 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1418 Register reg = static_cast<Register>(MostSignificantBit(mask));
1419 mask ^= 1u << reg;
1420 ofs -= kMipsWordSize;
1421 // The ZERO register is only included for alignment.
1422 if (reg != ZERO) {
1423 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001424 __ cfi().Restore(DWARFReg(reg));
1425 }
1426 }
1427
Alexey Frunze73296a72016-06-03 22:51:46 -07001428 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1429 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1430 mask ^= 1u << reg;
1431 ofs -= kMipsDoublewordSize;
1432 __ LoadDFromOffset(reg, SP, ofs);
1433 // TODO: __ cfi().Restore(DWARFReg(reg));
1434 }
1435
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001436 size_t frame_size = GetFrameSize();
1437 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1438 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1439 bool reordering = __ SetReorder(false);
1440 if (exchange) {
1441 __ Jr(RA);
1442 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1443 } else {
1444 __ DecreaseFrameSize(frame_size);
1445 __ Jr(RA);
1446 __ Nop(); // In delay slot.
1447 }
1448 __ SetReorder(reordering);
1449 } else {
1450 __ Jr(RA);
1451 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001452 }
1453
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001454 __ cfi().RestoreState();
1455 __ cfi().DefCFAOffset(GetFrameSize());
1456}
1457
1458void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1459 __ Bind(GetLabelOf(block));
1460}
1461
Lena Djokicca8c2952017-05-29 11:31:46 +02001462VectorRegister VectorRegisterFrom(Location location) {
1463 DCHECK(location.IsFpuRegister());
1464 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1465}
1466
Lena Djokic8098da92017-06-28 12:07:50 +02001467void CodeGeneratorMIPS::MoveLocation(Location destination,
1468 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001469 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001470 if (source.Equals(destination)) {
1471 return;
1472 }
1473
Lena Djokic8098da92017-06-28 12:07:50 +02001474 if (source.IsConstant()) {
1475 MoveConstant(destination, source.GetConstant());
1476 } else {
1477 if (destination.IsRegister()) {
1478 if (source.IsRegister()) {
1479 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1480 } else if (source.IsFpuRegister()) {
1481 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1482 } else {
1483 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001484 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001485 }
1486 } else if (destination.IsRegisterPair()) {
1487 if (source.IsRegisterPair()) {
1488 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1489 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1490 } else if (source.IsFpuRegister()) {
1491 Register dst_high = destination.AsRegisterPairHigh<Register>();
1492 Register dst_low = destination.AsRegisterPairLow<Register>();
1493 FRegister src = source.AsFpuRegister<FRegister>();
1494 __ Mfc1(dst_low, src);
1495 __ MoveFromFpuHigh(dst_high, src);
1496 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001497 DCHECK(source.IsDoubleStackSlot())
1498 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001499 int32_t off = source.GetStackIndex();
1500 Register r = destination.AsRegisterPairLow<Register>();
1501 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1502 }
1503 } else if (destination.IsFpuRegister()) {
1504 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001506 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1507 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001508 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001509 FRegister dst = destination.AsFpuRegister<FRegister>();
1510 Register src_high = source.AsRegisterPairHigh<Register>();
1511 Register src_low = source.AsRegisterPairLow<Register>();
1512 __ Mtc1(src_low, dst);
1513 __ MoveToFpuHigh(src_high, dst);
1514 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001515 if (GetGraph()->HasSIMD()) {
1516 __ MoveV(VectorRegisterFrom(destination),
1517 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001518 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001519 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001520 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1521 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001522 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001523 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1524 }
Lena Djokic8098da92017-06-28 12:07:50 +02001525 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001526 } else if (source.IsSIMDStackSlot()) {
1527 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001528 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001530 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1531 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001532 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001533 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1534 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1535 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001536 } else if (destination.IsSIMDStackSlot()) {
1537 if (source.IsFpuRegister()) {
1538 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1539 } else {
1540 DCHECK(source.IsSIMDStackSlot());
1541 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1542 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1543 }
Lena Djokic8098da92017-06-28 12:07:50 +02001544 } else if (destination.IsDoubleStackSlot()) {
1545 int32_t dst_offset = destination.GetStackIndex();
1546 if (source.IsRegisterPair()) {
1547 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1548 } else if (source.IsFpuRegister()) {
1549 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1550 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001551 DCHECK(source.IsDoubleStackSlot())
1552 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001553 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1554 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1555 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1556 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1557 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001558 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001559 DCHECK(destination.IsStackSlot()) << destination;
1560 int32_t dst_offset = destination.GetStackIndex();
1561 if (source.IsRegister()) {
1562 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1563 } else if (source.IsFpuRegister()) {
1564 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1565 } else {
1566 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1567 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1568 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570 }
1571 }
1572}
1573
1574void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1575 if (c->IsIntConstant() || c->IsNullConstant()) {
1576 // Move 32 bit constant.
1577 int32_t value = GetInt32ValueOf(c);
1578 if (destination.IsRegister()) {
1579 Register dst = destination.AsRegister<Register>();
1580 __ LoadConst32(dst, value);
1581 } else {
1582 DCHECK(destination.IsStackSlot())
1583 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001584 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001585 }
1586 } else if (c->IsLongConstant()) {
1587 // Move 64 bit constant.
1588 int64_t value = GetInt64ValueOf(c);
1589 if (destination.IsRegisterPair()) {
1590 Register r_h = destination.AsRegisterPairHigh<Register>();
1591 Register r_l = destination.AsRegisterPairLow<Register>();
1592 __ LoadConst64(r_h, r_l, value);
1593 } else {
1594 DCHECK(destination.IsDoubleStackSlot())
1595 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001596 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001597 }
1598 } else if (c->IsFloatConstant()) {
1599 // Move 32 bit float constant.
1600 int32_t value = GetInt32ValueOf(c);
1601 if (destination.IsFpuRegister()) {
1602 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1603 } else {
1604 DCHECK(destination.IsStackSlot())
1605 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001606 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001607 }
1608 } else {
1609 // Move 64 bit double constant.
1610 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1611 int64_t value = GetInt64ValueOf(c);
1612 if (destination.IsFpuRegister()) {
1613 FRegister fd = destination.AsFpuRegister<FRegister>();
1614 __ LoadDConst64(fd, value, TMP);
1615 } else {
1616 DCHECK(destination.IsDoubleStackSlot())
1617 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001618 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001619 }
1620 }
1621}
1622
1623void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1624 DCHECK(destination.IsRegister());
1625 Register dst = destination.AsRegister<Register>();
1626 __ LoadConst32(dst, value);
1627}
1628
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001629void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1630 if (location.IsRegister()) {
1631 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001632 } else if (location.IsRegisterPair()) {
1633 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1634 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001635 } else {
1636 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1637 }
1638}
1639
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001640template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001641inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1642 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001643 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001644 for (const PcRelativePatchInfo& info : infos) {
1645 const DexFile& dex_file = info.target_dex_file;
1646 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001647 DCHECK(info.label.IsBound());
1648 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001649 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1650 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001651 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1652 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1653 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001654 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001656 }
1657}
1658
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001659void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001660 DCHECK(linker_patches->empty());
1661 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001662 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001663 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001664 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001665 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001666 pc_relative_string_patches_.size() +
1667 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001668 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001669 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1671 pc_relative_method_patches_, linker_patches);
1672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1673 pc_relative_type_patches_, linker_patches);
1674 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1675 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001676 } else {
1677 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1679 pc_relative_type_patches_, linker_patches);
1680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1681 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001682 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1684 method_bss_entry_patches_, linker_patches);
1685 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1686 type_bss_entry_patches_, linker_patches);
1687 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1688 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001689 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001690}
1691
Vladimir Marko65979462017-05-19 17:25:12 +01001692CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001693 MethodReference target_method,
1694 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001695 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001696 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001697 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001698 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001699}
1700
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001701CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001702 MethodReference target_method,
1703 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001704 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001705 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001706 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001707 &method_bss_entry_patches_);
1708}
1709
Alexey Frunze06a46c42016-07-19 15:00:40 -07001710CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001711 const DexFile& dex_file,
1712 dex::TypeIndex type_index,
1713 const PcRelativePatchInfo* info_high) {
1714 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001715}
1716
Vladimir Marko1998cd02017-01-13 13:02:58 +00001717CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001718 const DexFile& dex_file,
1719 dex::TypeIndex type_index,
1720 const PcRelativePatchInfo* info_high) {
1721 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001722}
1723
Vladimir Marko65979462017-05-19 17:25:12 +01001724CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001725 const DexFile& dex_file,
1726 dex::StringIndex string_index,
1727 const PcRelativePatchInfo* info_high) {
1728 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001729}
1730
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001731CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1732 const DexFile& dex_file,
1733 dex::StringIndex string_index,
1734 const PcRelativePatchInfo* info_high) {
1735 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1736}
1737
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001738CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001739 const DexFile& dex_file,
1740 uint32_t offset_or_index,
1741 const PcRelativePatchInfo* info_high,
1742 ArenaDeque<PcRelativePatchInfo>* patches) {
1743 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001744 return &patches->back();
1745}
1746
Alexey Frunze06a46c42016-07-19 15:00:40 -07001747Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1748 return map->GetOrCreate(
1749 value,
1750 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1751}
1752
Alexey Frunze06a46c42016-07-19 15:00:40 -07001753Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001754 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001755}
1756
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001757void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001758 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001759 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001760 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001761 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001762 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001763 if (GetInstructionSetFeatures().IsR6()) {
1764 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001765 __ Bind(&info_high->label);
1766 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001767 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001768 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001769 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001770 } else {
1771 // If base is ZERO, emit NAL to obtain the actual base.
1772 if (base == ZERO) {
1773 // Generate a dummy PC-relative call to obtain PC.
1774 __ Nal();
1775 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001776 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001777 __ Lui(out, /* placeholder */ 0x1234);
1778 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1779 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1780 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001781 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001782 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001783 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001784 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001785 __ Addu(out, out, (base == ZERO) ? RA : base);
1786 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001787 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001788 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001789}
1790
Alexey Frunze627c1a02017-01-30 19:28:14 -08001791CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1792 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001793 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001794 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001795 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1796 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797 return &jit_string_patches_.back();
1798}
1799
1800CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1801 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001802 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001803 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001804 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1805 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001806 return &jit_class_patches_.back();
1807}
1808
1809void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1810 const uint8_t* roots_data,
1811 const CodeGeneratorMIPS::JitPatchInfo& info,
1812 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001813 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1814 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001815 uintptr_t address =
1816 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1817 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1818 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1820 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1821 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1822 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001823 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001824 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1825 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001826 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001827 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001828 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1829 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001830 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001831 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1832 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833}
1834
1835void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1836 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001837 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1838 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001839 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001840 }
1841 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001842 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1843 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001844 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001845 }
1846}
1847
Goran Jakovljevice114da22016-12-26 14:21:43 +01001848void CodeGeneratorMIPS::MarkGCCard(Register object,
1849 Register value,
1850 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851 MipsLabel done;
1852 Register card = AT;
1853 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001854 if (value_can_be_null) {
1855 __ Beqz(value, &done);
1856 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 __ LoadFromOffset(kLoadWord,
1858 card,
1859 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001860 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1862 __ Addu(temp, card, temp);
1863 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001864 if (value_can_be_null) {
1865 __ Bind(&done);
1866 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867}
1868
David Brazdil58282f42016-01-14 12:45:10 +00001869void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001870 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1871 blocked_core_registers_[ZERO] = true;
1872 blocked_core_registers_[K0] = true;
1873 blocked_core_registers_[K1] = true;
1874 blocked_core_registers_[GP] = true;
1875 blocked_core_registers_[SP] = true;
1876 blocked_core_registers_[RA] = true;
1877
1878 // AT and TMP(T8) are used as temporary/scratch registers
1879 // (similar to how AT is used by MIPS assemblers).
1880 blocked_core_registers_[AT] = true;
1881 blocked_core_registers_[TMP] = true;
1882 blocked_fpu_registers_[FTMP] = true;
1883
1884 // Reserve suspend and thread registers.
1885 blocked_core_registers_[S0] = true;
1886 blocked_core_registers_[TR] = true;
1887
1888 // Reserve T9 for function calls
1889 blocked_core_registers_[T9] = true;
1890
1891 // Reserve odd-numbered FPU registers.
1892 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1893 blocked_fpu_registers_[i] = true;
1894 }
1895
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001896 if (GetGraph()->IsDebuggable()) {
1897 // Stubs do not save callee-save floating point registers. If the graph
1898 // is debuggable, we need to deal with these registers differently. For
1899 // now, just block them.
1900 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1901 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1902 }
1903 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001904}
1905
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1907 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1908 return kMipsWordSize;
1909}
1910
1911size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1912 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1913 return kMipsWordSize;
1914}
1915
1916size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001917 if (GetGraph()->HasSIMD()) {
1918 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1919 } else {
1920 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1921 }
1922 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923}
1924
1925size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001926 if (GetGraph()->HasSIMD()) {
1927 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1928 } else {
1929 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1930 }
1931 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001932}
1933
1934void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001935 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936}
1937
1938void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001939 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001940}
1941
Serban Constantinescufca16662016-07-14 09:21:59 +01001942constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1943
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1945 HInstruction* instruction,
1946 uint32_t dex_pc,
1947 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001948 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001949 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1950 IsDirectEntrypoint(entrypoint));
1951 if (EntrypointRequiresStackMap(entrypoint)) {
1952 RecordPcInfo(instruction, dex_pc, slow_path);
1953 }
1954}
1955
1956void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1957 HInstruction* instruction,
1958 SlowPathCode* slow_path,
1959 bool direct) {
1960 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1961 GenerateInvokeRuntime(entry_point_offset, direct);
1962}
1963
1964void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001965 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001966 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001967 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001968 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969 // Reserve argument space on stack (for $a0-$a3) for
1970 // entrypoints that directly reference native implementations.
1971 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001972 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001974 } else {
1975 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001976 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001977 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001978}
1979
1980void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1981 Register class_reg) {
1982 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1983 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1984 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1985 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1986 __ Sync(0);
1987 __ Bind(slow_path->GetExitLabel());
1988}
1989
1990void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1991 __ Sync(0); // Only stype 0 is supported.
1992}
1993
1994void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1995 HBasicBlock* successor) {
1996 SuspendCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001997 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 codegen_->AddSlowPath(slow_path);
1999
2000 __ LoadFromOffset(kLoadUnsignedHalfword,
2001 TMP,
2002 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002003 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002004 if (successor == nullptr) {
2005 __ Bnez(TMP, slow_path->GetEntryLabel());
2006 __ Bind(slow_path->GetReturnLabel());
2007 } else {
2008 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2009 __ B(slow_path->GetEntryLabel());
2010 // slow_path will return to GetLabelOf(successor).
2011 }
2012}
2013
2014InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2015 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002016 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 assembler_(codegen->GetAssembler()),
2018 codegen_(codegen) {}
2019
2020void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2021 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002022 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 locations->SetInAt(0, Location::RequiresRegister());
2027 HInstruction* right = instruction->InputAt(1);
2028 bool can_use_imm = false;
2029 if (right->IsConstant()) {
2030 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2031 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2032 can_use_imm = IsUint<16>(imm);
2033 } else if (instruction->IsAdd()) {
2034 can_use_imm = IsInt<16>(imm);
2035 } else {
2036 DCHECK(instruction->IsSub());
2037 can_use_imm = IsInt<16>(-imm);
2038 }
2039 }
2040 if (can_use_imm)
2041 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2042 else
2043 locations->SetInAt(1, Location::RequiresRegister());
2044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2045 break;
2046 }
2047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002050 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2051 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 break;
2053 }
2054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002055 case DataType::Type::kFloat32:
2056 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 DCHECK(instruction->IsAdd() || instruction->IsSub());
2058 locations->SetInAt(0, Location::RequiresFpuRegister());
2059 locations->SetInAt(1, Location::RequiresFpuRegister());
2060 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2061 break;
2062
2063 default:
2064 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2065 }
2066}
2067
2068void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002070 LocationSummary* locations = instruction->GetLocations();
2071
2072 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 Register dst = locations->Out().AsRegister<Register>();
2075 Register lhs = locations->InAt(0).AsRegister<Register>();
2076 Location rhs_location = locations->InAt(1);
2077
2078 Register rhs_reg = ZERO;
2079 int32_t rhs_imm = 0;
2080 bool use_imm = rhs_location.IsConstant();
2081 if (use_imm) {
2082 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2083 } else {
2084 rhs_reg = rhs_location.AsRegister<Register>();
2085 }
2086
2087 if (instruction->IsAnd()) {
2088 if (use_imm)
2089 __ Andi(dst, lhs, rhs_imm);
2090 else
2091 __ And(dst, lhs, rhs_reg);
2092 } else if (instruction->IsOr()) {
2093 if (use_imm)
2094 __ Ori(dst, lhs, rhs_imm);
2095 else
2096 __ Or(dst, lhs, rhs_reg);
2097 } else if (instruction->IsXor()) {
2098 if (use_imm)
2099 __ Xori(dst, lhs, rhs_imm);
2100 else
2101 __ Xor(dst, lhs, rhs_reg);
2102 } else if (instruction->IsAdd()) {
2103 if (use_imm)
2104 __ Addiu(dst, lhs, rhs_imm);
2105 else
2106 __ Addu(dst, lhs, rhs_reg);
2107 } else {
2108 DCHECK(instruction->IsSub());
2109 if (use_imm)
2110 __ Addiu(dst, lhs, -rhs_imm);
2111 else
2112 __ Subu(dst, lhs, rhs_reg);
2113 }
2114 break;
2115 }
2116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2119 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2120 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2121 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002122 Location rhs_location = locations->InAt(1);
2123 bool use_imm = rhs_location.IsConstant();
2124 if (!use_imm) {
2125 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2126 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2127 if (instruction->IsAnd()) {
2128 __ And(dst_low, lhs_low, rhs_low);
2129 __ And(dst_high, lhs_high, rhs_high);
2130 } else if (instruction->IsOr()) {
2131 __ Or(dst_low, lhs_low, rhs_low);
2132 __ Or(dst_high, lhs_high, rhs_high);
2133 } else if (instruction->IsXor()) {
2134 __ Xor(dst_low, lhs_low, rhs_low);
2135 __ Xor(dst_high, lhs_high, rhs_high);
2136 } else if (instruction->IsAdd()) {
2137 if (lhs_low == rhs_low) {
2138 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2139 __ Slt(TMP, lhs_low, ZERO);
2140 __ Addu(dst_low, lhs_low, rhs_low);
2141 } else {
2142 __ Addu(dst_low, lhs_low, rhs_low);
2143 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2144 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2145 }
2146 __ Addu(dst_high, lhs_high, rhs_high);
2147 __ Addu(dst_high, dst_high, TMP);
2148 } else {
2149 DCHECK(instruction->IsSub());
2150 __ Sltu(TMP, lhs_low, rhs_low);
2151 __ Subu(dst_low, lhs_low, rhs_low);
2152 __ Subu(dst_high, lhs_high, rhs_high);
2153 __ Subu(dst_high, dst_high, TMP);
2154 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002156 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2157 if (instruction->IsOr()) {
2158 uint32_t low = Low32Bits(value);
2159 uint32_t high = High32Bits(value);
2160 if (IsUint<16>(low)) {
2161 if (dst_low != lhs_low || low != 0) {
2162 __ Ori(dst_low, lhs_low, low);
2163 }
2164 } else {
2165 __ LoadConst32(TMP, low);
2166 __ Or(dst_low, lhs_low, TMP);
2167 }
2168 if (IsUint<16>(high)) {
2169 if (dst_high != lhs_high || high != 0) {
2170 __ Ori(dst_high, lhs_high, high);
2171 }
2172 } else {
2173 if (high != low) {
2174 __ LoadConst32(TMP, high);
2175 }
2176 __ Or(dst_high, lhs_high, TMP);
2177 }
2178 } else if (instruction->IsXor()) {
2179 uint32_t low = Low32Bits(value);
2180 uint32_t high = High32Bits(value);
2181 if (IsUint<16>(low)) {
2182 if (dst_low != lhs_low || low != 0) {
2183 __ Xori(dst_low, lhs_low, low);
2184 }
2185 } else {
2186 __ LoadConst32(TMP, low);
2187 __ Xor(dst_low, lhs_low, TMP);
2188 }
2189 if (IsUint<16>(high)) {
2190 if (dst_high != lhs_high || high != 0) {
2191 __ Xori(dst_high, lhs_high, high);
2192 }
2193 } else {
2194 if (high != low) {
2195 __ LoadConst32(TMP, high);
2196 }
2197 __ Xor(dst_high, lhs_high, TMP);
2198 }
2199 } else if (instruction->IsAnd()) {
2200 uint32_t low = Low32Bits(value);
2201 uint32_t high = High32Bits(value);
2202 if (IsUint<16>(low)) {
2203 __ Andi(dst_low, lhs_low, low);
2204 } else if (low != 0xFFFFFFFF) {
2205 __ LoadConst32(TMP, low);
2206 __ And(dst_low, lhs_low, TMP);
2207 } else if (dst_low != lhs_low) {
2208 __ Move(dst_low, lhs_low);
2209 }
2210 if (IsUint<16>(high)) {
2211 __ Andi(dst_high, lhs_high, high);
2212 } else if (high != 0xFFFFFFFF) {
2213 if (high != low) {
2214 __ LoadConst32(TMP, high);
2215 }
2216 __ And(dst_high, lhs_high, TMP);
2217 } else if (dst_high != lhs_high) {
2218 __ Move(dst_high, lhs_high);
2219 }
2220 } else {
2221 if (instruction->IsSub()) {
2222 value = -value;
2223 } else {
2224 DCHECK(instruction->IsAdd());
2225 }
2226 int32_t low = Low32Bits(value);
2227 int32_t high = High32Bits(value);
2228 if (IsInt<16>(low)) {
2229 if (dst_low != lhs_low || low != 0) {
2230 __ Addiu(dst_low, lhs_low, low);
2231 }
2232 if (low != 0) {
2233 __ Sltiu(AT, dst_low, low);
2234 }
2235 } else {
2236 __ LoadConst32(TMP, low);
2237 __ Addu(dst_low, lhs_low, TMP);
2238 __ Sltu(AT, dst_low, TMP);
2239 }
2240 if (IsInt<16>(high)) {
2241 if (dst_high != lhs_high || high != 0) {
2242 __ Addiu(dst_high, lhs_high, high);
2243 }
2244 } else {
2245 if (high != low) {
2246 __ LoadConst32(TMP, high);
2247 }
2248 __ Addu(dst_high, lhs_high, TMP);
2249 }
2250 if (low != 0) {
2251 __ Addu(dst_high, dst_high, AT);
2252 }
2253 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254 }
2255 break;
2256 }
2257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 case DataType::Type::kFloat32:
2259 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2261 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2262 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2263 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002265 __ AddS(dst, lhs, rhs);
2266 } else {
2267 __ AddD(dst, lhs, rhs);
2268 }
2269 } else {
2270 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 __ SubS(dst, lhs, rhs);
2273 } else {
2274 __ SubD(dst, lhs, rhs);
2275 }
2276 }
2277 break;
2278 }
2279
2280 default:
2281 LOG(FATAL) << "Unexpected binary operation type " << type;
2282 }
2283}
2284
2285void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002286 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287
Vladimir Markoca6fff82017-10-03 14:49:14 +01002288 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002290 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002292 locations->SetInAt(0, Location::RequiresRegister());
2293 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2295 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297 locations->SetInAt(0, Location::RequiresRegister());
2298 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2299 locations->SetOut(Location::RequiresRegister());
2300 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 default:
2302 LOG(FATAL) << "Unexpected shift type " << type;
2303 }
2304}
2305
2306static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2307
2308void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002309 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312
2313 Location rhs_location = locations->InAt(1);
2314 bool use_imm = rhs_location.IsConstant();
2315 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2316 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002317 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002319 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002320 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2321 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002322
2323 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 Register dst = locations->Out().AsRegister<Register>();
2326 Register lhs = locations->InAt(0).AsRegister<Register>();
2327 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002328 if (shift_value == 0) {
2329 if (dst != lhs) {
2330 __ Move(dst, lhs);
2331 }
2332 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002333 __ Sll(dst, lhs, shift_value);
2334 } else if (instr->IsShr()) {
2335 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002336 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002338 } else {
2339 if (has_ins_rotr) {
2340 __ Rotr(dst, lhs, shift_value);
2341 } else {
2342 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2343 __ Srl(dst, lhs, shift_value);
2344 __ Or(dst, dst, TMP);
2345 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 }
2347 } else {
2348 if (instr->IsShl()) {
2349 __ Sllv(dst, lhs, rhs_reg);
2350 } else if (instr->IsShr()) {
2351 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002352 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 } else {
2355 if (has_ins_rotr) {
2356 __ Rotrv(dst, lhs, rhs_reg);
2357 } else {
2358 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002359 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2360 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2361 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2362 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2363 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002364 __ Sllv(TMP, lhs, TMP);
2365 __ Srlv(dst, lhs, rhs_reg);
2366 __ Or(dst, dst, TMP);
2367 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 }
2369 }
2370 break;
2371 }
2372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2375 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2376 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2377 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2378 if (use_imm) {
2379 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002380 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002383 if (instr->IsShl()) {
2384 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2385 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2386 __ Sll(dst_low, lhs_low, shift_value);
2387 } else if (instr->IsShr()) {
2388 __ Srl(dst_low, lhs_low, shift_value);
2389 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2390 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002391 } else if (instr->IsUShr()) {
2392 __ Srl(dst_low, lhs_low, shift_value);
2393 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2394 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002395 } else {
2396 __ Srl(dst_low, lhs_low, shift_value);
2397 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2398 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002399 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002402 if (instr->IsShl()) {
2403 __ Sll(dst_low, lhs_low, shift_value);
2404 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2405 __ Sll(dst_high, lhs_high, shift_value);
2406 __ Or(dst_high, dst_high, TMP);
2407 } else if (instr->IsShr()) {
2408 __ Sra(dst_high, lhs_high, shift_value);
2409 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2410 __ Srl(dst_low, lhs_low, shift_value);
2411 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002412 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002413 __ Srl(dst_high, lhs_high, shift_value);
2414 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2415 __ Srl(dst_low, lhs_low, shift_value);
2416 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002417 } else {
2418 __ Srl(TMP, lhs_low, shift_value);
2419 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2420 __ Or(dst_low, dst_low, TMP);
2421 __ Srl(TMP, lhs_high, shift_value);
2422 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2423 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002424 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 }
2426 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002427 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002428 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 __ Move(dst_low, ZERO);
2431 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002432 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002435 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002437 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002438 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 // 64-bit rotation by 32 is just a swap.
2440 __ Move(dst_low, lhs_high);
2441 __ Move(dst_high, lhs_low);
2442 } else {
2443 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002444 __ Srl(dst_low, lhs_high, shift_value_high);
2445 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2446 __ Srl(dst_high, lhs_low, shift_value_high);
2447 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002448 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002449 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2450 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002451 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002452 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2453 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002454 __ Or(dst_high, dst_high, TMP);
2455 }
2456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002457 }
2458 }
2459 } else {
2460 MipsLabel done;
2461 if (instr->IsShl()) {
2462 __ Sllv(dst_low, lhs_low, rhs_reg);
2463 __ Nor(AT, ZERO, rhs_reg);
2464 __ Srl(TMP, lhs_low, 1);
2465 __ Srlv(TMP, TMP, AT);
2466 __ Sllv(dst_high, lhs_high, rhs_reg);
2467 __ Or(dst_high, dst_high, TMP);
2468 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2469 __ Beqz(TMP, &done);
2470 __ Move(dst_high, dst_low);
2471 __ Move(dst_low, ZERO);
2472 } else if (instr->IsShr()) {
2473 __ Srav(dst_high, lhs_high, rhs_reg);
2474 __ Nor(AT, ZERO, rhs_reg);
2475 __ Sll(TMP, lhs_high, 1);
2476 __ Sllv(TMP, TMP, AT);
2477 __ Srlv(dst_low, lhs_low, rhs_reg);
2478 __ Or(dst_low, dst_low, TMP);
2479 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2480 __ Beqz(TMP, &done);
2481 __ Move(dst_low, dst_high);
2482 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002483 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002484 __ Srlv(dst_high, lhs_high, rhs_reg);
2485 __ Nor(AT, ZERO, rhs_reg);
2486 __ Sll(TMP, lhs_high, 1);
2487 __ Sllv(TMP, TMP, AT);
2488 __ Srlv(dst_low, lhs_low, rhs_reg);
2489 __ Or(dst_low, dst_low, TMP);
2490 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2491 __ Beqz(TMP, &done);
2492 __ Move(dst_low, dst_high);
2493 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002494 } else {
2495 __ Nor(AT, ZERO, rhs_reg);
2496 __ Srlv(TMP, lhs_low, rhs_reg);
2497 __ Sll(dst_low, lhs_high, 1);
2498 __ Sllv(dst_low, dst_low, AT);
2499 __ Or(dst_low, dst_low, TMP);
2500 __ Srlv(TMP, lhs_high, rhs_reg);
2501 __ Sll(dst_high, lhs_low, 1);
2502 __ Sllv(dst_high, dst_high, AT);
2503 __ Or(dst_high, dst_high, TMP);
2504 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2505 __ Beqz(TMP, &done);
2506 __ Move(TMP, dst_high);
2507 __ Move(dst_high, dst_low);
2508 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002509 }
2510 __ Bind(&done);
2511 }
2512 break;
2513 }
2514
2515 default:
2516 LOG(FATAL) << "Unexpected shift operation type " << type;
2517 }
2518}
2519
2520void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2521 HandleBinaryOp(instruction);
2522}
2523
2524void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2525 HandleBinaryOp(instruction);
2526}
2527
2528void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2529 HandleBinaryOp(instruction);
2530}
2531
2532void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2533 HandleBinaryOp(instruction);
2534}
2535
2536void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002538 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002540 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002541 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2542 object_array_get_with_read_barrier
2543 ? LocationSummary::kCallOnSlowPath
2544 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002545 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2546 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002548 locations->SetInAt(0, Location::RequiresRegister());
2549 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2552 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002553 // The output overlaps in the case of an object array get with
2554 // read barriers enabled: we do not want the move to overwrite the
2555 // array's location, as we need it to emit the read barrier.
2556 locations->SetOut(Location::RequiresRegister(),
2557 object_array_get_with_read_barrier
2558 ? Location::kOutputOverlap
2559 : Location::kNoOutputOverlap);
2560 }
2561 // We need a temporary register for the read barrier marking slow
2562 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2563 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002564 bool temp_needed = instruction->GetIndex()->IsConstant()
2565 ? !kBakerReadBarrierThunksEnableForFields
2566 : !kBakerReadBarrierThunksEnableForArrays;
2567 if (temp_needed) {
2568 locations->AddTemp(Location::RequiresRegister());
2569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002570 }
2571}
2572
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002573static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2574 auto null_checker = [codegen, instruction]() {
2575 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002576 };
2577 return null_checker;
2578}
2579
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2581 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002582 Location obj_loc = locations->InAt(0);
2583 Register obj = obj_loc.AsRegister<Register>();
2584 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002586 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002587 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002590 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2591 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002593 case DataType::Type::kBool:
2594 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002595 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 if (index.IsConstant()) {
2597 size_t offset =
2598 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002599 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 } else {
2601 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002602 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 }
2604 break;
2605 }
2606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002608 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 if (index.IsConstant()) {
2610 size_t offset =
2611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002612 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 } else {
2614 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002615 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002616 }
2617 break;
2618 }
2619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002621 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002622 if (maybe_compressed_char_at) {
2623 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2624 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2625 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2626 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2627 "Expecting 0=compressed, 1=uncompressed");
2628 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002630 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2631 if (maybe_compressed_char_at) {
2632 MipsLabel uncompressed_load, done;
2633 __ Bnez(TMP, &uncompressed_load);
2634 __ LoadFromOffset(kLoadUnsignedByte,
2635 out,
2636 obj,
2637 data_offset + (const_index << TIMES_1));
2638 __ B(&done);
2639 __ Bind(&uncompressed_load);
2640 __ LoadFromOffset(kLoadUnsignedHalfword,
2641 out,
2642 obj,
2643 data_offset + (const_index << TIMES_2));
2644 __ Bind(&done);
2645 } else {
2646 __ LoadFromOffset(kLoadUnsignedHalfword,
2647 out,
2648 obj,
2649 data_offset + (const_index << TIMES_2),
2650 null_checker);
2651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002652 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002653 Register index_reg = index.AsRegister<Register>();
2654 if (maybe_compressed_char_at) {
2655 MipsLabel uncompressed_load, done;
2656 __ Bnez(TMP, &uncompressed_load);
2657 __ Addu(TMP, obj, index_reg);
2658 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2659 __ B(&done);
2660 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002661 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002662 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2663 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002664 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2665 __ Addu(TMP, index_reg, obj);
2666 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002667 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002668 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002669 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2670 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 }
2672 break;
2673 }
2674
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002675 case DataType::Type::kInt16: {
2676 Register out = out_loc.AsRegister<Register>();
2677 if (index.IsConstant()) {
2678 size_t offset =
2679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2680 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002681 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2682 __ Addu(TMP, index.AsRegister<Register>(), obj);
2683 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002684 } else {
2685 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2686 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2687 }
2688 break;
2689 }
2690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002693 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694 if (index.IsConstant()) {
2695 size_t offset =
2696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002697 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002698 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2699 __ Addu(TMP, index.AsRegister<Register>(), obj);
2700 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002702 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002703 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 }
2705 break;
2706 }
2707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002709 static_assert(
2710 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2711 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2712 // /* HeapReference<Object> */ out =
2713 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2714 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002715 bool temp_needed = index.IsConstant()
2716 ? !kBakerReadBarrierThunksEnableForFields
2717 : !kBakerReadBarrierThunksEnableForArrays;
2718 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002719 // Note that a potential implicit null check is handled in this
2720 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002721 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2722 if (index.IsConstant()) {
2723 // Array load with a constant index can be treated as a field load.
2724 size_t offset =
2725 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2726 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2727 out_loc,
2728 obj,
2729 offset,
2730 temp,
2731 /* needs_null_check */ false);
2732 } else {
2733 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2734 out_loc,
2735 obj,
2736 data_offset,
2737 index,
2738 temp,
2739 /* needs_null_check */ false);
2740 }
Alexey Frunze15958152017-02-09 19:08:30 -08002741 } else {
2742 Register out = out_loc.AsRegister<Register>();
2743 if (index.IsConstant()) {
2744 size_t offset =
2745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2746 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2747 // If read barriers are enabled, emit read barriers other than
2748 // Baker's using a slow path (and also unpoison the loaded
2749 // reference, if heap poisoning is enabled).
2750 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2751 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002752 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002753 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2754 // If read barriers are enabled, emit read barriers other than
2755 // Baker's using a slow path (and also unpoison the loaded
2756 // reference, if heap poisoning is enabled).
2757 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2758 out_loc,
2759 out_loc,
2760 obj_loc,
2761 data_offset,
2762 index);
2763 }
2764 }
2765 break;
2766 }
2767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002769 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002770 if (index.IsConstant()) {
2771 size_t offset =
2772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002773 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002774 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2775 __ Addu(TMP, index.AsRegister<Register>(), obj);
2776 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002777 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002778 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002779 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002780 }
2781 break;
2782 }
2783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002784 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002785 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002786 if (index.IsConstant()) {
2787 size_t offset =
2788 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002789 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002790 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2791 __ Addu(TMP, index.AsRegister<Register>(), obj);
2792 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002793 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002794 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002795 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002796 }
2797 break;
2798 }
2799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002801 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802 if (index.IsConstant()) {
2803 size_t offset =
2804 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002805 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002806 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2807 __ Addu(TMP, index.AsRegister<Register>(), obj);
2808 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002809 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002810 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002811 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 }
2813 break;
2814 }
2815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2818 UNREACHABLE();
2819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820}
2821
2822void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002823 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824 locations->SetInAt(0, Location::RequiresRegister());
2825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2826}
2827
2828void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2829 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002830 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002831 Register obj = locations->InAt(0).AsRegister<Register>();
2832 Register out = locations->Out().AsRegister<Register>();
2833 __ LoadFromOffset(kLoadWord, out, obj, offset);
2834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002835 // Mask out compression flag from String's array length.
2836 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2837 __ Srl(out, out, 1u);
2838 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839}
2840
Alexey Frunzef58b2482016-09-02 22:14:06 -07002841Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2842 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2843 ? Location::ConstantLocation(instruction->AsConstant())
2844 : Location::RequiresRegister();
2845}
2846
2847Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2848 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2849 // We can store a non-zero float or double constant without first loading it into the FPU,
2850 // but we should only prefer this if the constant has a single use.
2851 if (instruction->IsConstant() &&
2852 (instruction->AsConstant()->IsZeroBitPattern() ||
2853 instruction->GetUses().HasExactlyOneElement())) {
2854 return Location::ConstantLocation(instruction->AsConstant());
2855 // Otherwise fall through and require an FPU register for the constant.
2856 }
2857 return Location::RequiresFpuRegister();
2858}
2859
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002862
2863 bool needs_write_barrier =
2864 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2865 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2866
Vladimir Markoca6fff82017-10-03 14:49:14 +01002867 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002869 may_need_runtime_call_for_type_check ?
2870 LocationSummary::kCallOnSlowPath :
2871 LocationSummary::kNoCall);
2872
2873 locations->SetInAt(0, Location::RequiresRegister());
2874 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002876 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002877 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002878 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2879 }
2880 if (needs_write_barrier) {
2881 // Temporary register for the write barrier.
2882 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002883 }
2884}
2885
2886void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2887 LocationSummary* locations = instruction->GetLocations();
2888 Register obj = locations->InAt(0).AsRegister<Register>();
2889 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002890 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002892 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002893 bool needs_write_barrier =
2894 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002895 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002896 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897
2898 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002900 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002902 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002903 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002904 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002906 __ Addu(base_reg, obj, index.AsRegister<Register>());
2907 }
2908 if (value_location.IsConstant()) {
2909 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2910 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2911 } else {
2912 Register value = value_location.AsRegister<Register>();
2913 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914 }
2915 break;
2916 }
2917
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002918 case DataType::Type::kUint16:
2919 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002921 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002922 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002923 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2924 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002925 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002926 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002927 }
2928 if (value_location.IsConstant()) {
2929 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2930 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2931 } else {
2932 Register value = value_location.AsRegister<Register>();
2933 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 }
2935 break;
2936 }
2937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2940 if (index.IsConstant()) {
2941 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002942 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2943 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002944 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002945 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002946 }
2947 if (value_location.IsConstant()) {
2948 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2949 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2950 } else {
2951 Register value = value_location.AsRegister<Register>();
2952 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2953 }
2954 break;
2955 }
2956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002958 if (value_location.IsConstant()) {
2959 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002960 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002961 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002962 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002964 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 }
Alexey Frunze15958152017-02-09 19:08:30 -08002966 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2967 DCHECK_EQ(value, 0);
2968 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2969 DCHECK(!needs_write_barrier);
2970 DCHECK(!may_need_runtime_call_for_type_check);
2971 break;
2972 }
2973
2974 DCHECK(needs_write_barrier);
2975 Register value = value_location.AsRegister<Register>();
2976 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2977 Register temp2 = TMP; // Doesn't need to survive slow path.
2978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2979 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2980 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2981 MipsLabel done;
2982 SlowPathCodeMIPS* slow_path = nullptr;
2983
2984 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002985 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002986 codegen_->AddSlowPath(slow_path);
2987 if (instruction->GetValueCanBeNull()) {
2988 MipsLabel non_zero;
2989 __ Bnez(value, &non_zero);
2990 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2991 if (index.IsConstant()) {
2992 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002993 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2994 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002995 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002996 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002997 }
Alexey Frunze15958152017-02-09 19:08:30 -08002998 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2999 __ B(&done);
3000 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003001 }
Alexey Frunze15958152017-02-09 19:08:30 -08003002
3003 // Note that when read barriers are enabled, the type checks
3004 // are performed without read barriers. This is fine, even in
3005 // the case where a class object is in the from-space after
3006 // the flip, as a comparison involving such a type would not
3007 // produce a false positive; it may of course produce a false
3008 // negative, in which case we would take the ArraySet slow
3009 // path.
3010
3011 // /* HeapReference<Class> */ temp1 = obj->klass_
3012 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3013 __ MaybeUnpoisonHeapReference(temp1);
3014
3015 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3016 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3017 // /* HeapReference<Class> */ temp2 = value->klass_
3018 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3019 // If heap poisoning is enabled, no need to unpoison `temp1`
3020 // nor `temp2`, as we are comparing two poisoned references.
3021
3022 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3023 MipsLabel do_put;
3024 __ Beq(temp1, temp2, &do_put);
3025 // If heap poisoning is enabled, the `temp1` reference has
3026 // not been unpoisoned yet; unpoison it now.
3027 __ MaybeUnpoisonHeapReference(temp1);
3028
3029 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3030 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3031 // If heap poisoning is enabled, no need to unpoison
3032 // `temp1`, as we are comparing against null below.
3033 __ Bnez(temp1, slow_path->GetEntryLabel());
3034 __ Bind(&do_put);
3035 } else {
3036 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3037 }
3038 }
3039
3040 Register source = value;
3041 if (kPoisonHeapReferences) {
3042 // Note that in the case where `value` is a null reference,
3043 // we do not enter this block, as a null reference does not
3044 // need poisoning.
3045 __ Move(temp1, value);
3046 __ PoisonHeapReference(temp1);
3047 source = temp1;
3048 }
3049
3050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3051 if (index.IsConstant()) {
3052 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003053 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003054 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003055 }
3056 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3057
3058 if (!may_need_runtime_call_for_type_check) {
3059 codegen_->MaybeRecordImplicitNullCheck(instruction);
3060 }
3061
3062 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3063
3064 if (done.IsLinked()) {
3065 __ Bind(&done);
3066 }
3067
3068 if (slow_path != nullptr) {
3069 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003070 }
3071 break;
3072 }
3073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003076 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003077 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003078 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3079 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003081 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003082 }
3083 if (value_location.IsConstant()) {
3084 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3085 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3086 } else {
3087 Register value = value_location.AsRegisterPairLow<Register>();
3088 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 }
3090 break;
3091 }
3092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003093 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003094 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003095 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003096 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003097 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3098 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003100 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003101 }
3102 if (value_location.IsConstant()) {
3103 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3104 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3105 } else {
3106 FRegister value = value_location.AsFpuRegister<FRegister>();
3107 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 }
3109 break;
3110 }
3111
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003115 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003116 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3117 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003118 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003119 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003120 }
3121 if (value_location.IsConstant()) {
3122 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3123 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3124 } else {
3125 FRegister value = value_location.AsFpuRegister<FRegister>();
3126 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 }
3128 break;
3129 }
3130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003132 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3133 UNREACHABLE();
3134 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135}
3136
Lena Djokica2901602017-09-21 13:50:52 +02003137void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3138 HIntermediateArrayAddressIndex* instruction) {
3139 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003140 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003141
3142 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3143
3144 locations->SetInAt(0, Location::RequiresRegister());
3145 locations->SetInAt(1, Location::ConstantLocation(shift));
3146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3147}
3148
3149void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3150 HIntermediateArrayAddressIndex* instruction) {
3151 LocationSummary* locations = instruction->GetLocations();
3152 Register index_reg = locations->InAt(0).AsRegister<Register>();
3153 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3154 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3155}
3156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003157void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003158 RegisterSet caller_saves = RegisterSet::Empty();
3159 InvokeRuntimeCallingConvention calling_convention;
3160 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3161 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3162 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003163 locations->SetInAt(0, Location::RequiresRegister());
3164 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003165}
3166
3167void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3168 LocationSummary* locations = instruction->GetLocations();
3169 BoundsCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003170 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003171 codegen_->AddSlowPath(slow_path);
3172
3173 Register index = locations->InAt(0).AsRegister<Register>();
3174 Register length = locations->InAt(1).AsRegister<Register>();
3175
3176 // length is limited by the maximum positive signed 32-bit integer.
3177 // Unsigned comparison of length and index checks for index < 0
3178 // and for length <= index simultaneously.
3179 __ Bgeu(index, length, slow_path->GetEntryLabel());
3180}
3181
Alexey Frunze15958152017-02-09 19:08:30 -08003182// Temp is used for read barrier.
3183static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3184 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003185 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003186 (kUseBakerReadBarrier ||
3187 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3188 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3189 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3190 return 1;
3191 }
3192 return 0;
3193}
3194
3195// Extra temp is used for read barrier.
3196static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3197 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3198}
3199
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003200void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003201 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3202 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3203
3204 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3205 switch (type_check_kind) {
3206 case TypeCheckKind::kExactCheck:
3207 case TypeCheckKind::kAbstractClassCheck:
3208 case TypeCheckKind::kClassHierarchyCheck:
3209 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003210 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003211 ? LocationSummary::kCallOnSlowPath
3212 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3213 break;
3214 case TypeCheckKind::kArrayCheck:
3215 case TypeCheckKind::kUnresolvedCheck:
3216 case TypeCheckKind::kInterfaceCheck:
3217 call_kind = LocationSummary::kCallOnSlowPath;
3218 break;
3219 }
3220
Vladimir Markoca6fff82017-10-03 14:49:14 +01003221 LocationSummary* locations =
3222 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003223 locations->SetInAt(0, Location::RequiresRegister());
3224 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003225 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003226}
3227
3228void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003229 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003230 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003231 Location obj_loc = locations->InAt(0);
3232 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003233 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003234 Location temp_loc = locations->GetTemp(0);
3235 Register temp = temp_loc.AsRegister<Register>();
3236 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3237 DCHECK_LE(num_temps, 2u);
3238 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003239 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3240 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3241 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3242 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3243 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3244 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3245 const uint32_t object_array_data_offset =
3246 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3247 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003248
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003249 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3250 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3251 // read barriers is done for performance and code size reasons.
3252 bool is_type_check_slow_path_fatal = false;
3253 if (!kEmitCompilerReadBarrier) {
3254 is_type_check_slow_path_fatal =
3255 (type_check_kind == TypeCheckKind::kExactCheck ||
3256 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3257 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3258 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3259 !instruction->CanThrowIntoCatchBlock();
3260 }
3261 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003262 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3263 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003264 codegen_->AddSlowPath(slow_path);
3265
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003266 // Avoid this check if we know `obj` is not null.
3267 if (instruction->MustDoNullCheck()) {
3268 __ Beqz(obj, &done);
3269 }
3270
3271 switch (type_check_kind) {
3272 case TypeCheckKind::kExactCheck:
3273 case TypeCheckKind::kArrayCheck: {
3274 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003275 GenerateReferenceLoadTwoRegisters(instruction,
3276 temp_loc,
3277 obj_loc,
3278 class_offset,
3279 maybe_temp2_loc,
3280 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003281 // Jump to slow path for throwing the exception or doing a
3282 // more involved array check.
3283 __ Bne(temp, cls, slow_path->GetEntryLabel());
3284 break;
3285 }
3286
3287 case TypeCheckKind::kAbstractClassCheck: {
3288 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003289 GenerateReferenceLoadTwoRegisters(instruction,
3290 temp_loc,
3291 obj_loc,
3292 class_offset,
3293 maybe_temp2_loc,
3294 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003295 // If the class is abstract, we eagerly fetch the super class of the
3296 // object to avoid doing a comparison we know will fail.
3297 MipsLabel loop;
3298 __ Bind(&loop);
3299 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003300 GenerateReferenceLoadOneRegister(instruction,
3301 temp_loc,
3302 super_offset,
3303 maybe_temp2_loc,
3304 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003305 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3306 // exception.
3307 __ Beqz(temp, slow_path->GetEntryLabel());
3308 // Otherwise, compare the classes.
3309 __ Bne(temp, cls, &loop);
3310 break;
3311 }
3312
3313 case TypeCheckKind::kClassHierarchyCheck: {
3314 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003315 GenerateReferenceLoadTwoRegisters(instruction,
3316 temp_loc,
3317 obj_loc,
3318 class_offset,
3319 maybe_temp2_loc,
3320 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003321 // Walk over the class hierarchy to find a match.
3322 MipsLabel loop;
3323 __ Bind(&loop);
3324 __ Beq(temp, cls, &done);
3325 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003326 GenerateReferenceLoadOneRegister(instruction,
3327 temp_loc,
3328 super_offset,
3329 maybe_temp2_loc,
3330 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003331 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3332 // exception. Otherwise, jump to the beginning of the loop.
3333 __ Bnez(temp, &loop);
3334 __ B(slow_path->GetEntryLabel());
3335 break;
3336 }
3337
3338 case TypeCheckKind::kArrayObjectCheck: {
3339 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003340 GenerateReferenceLoadTwoRegisters(instruction,
3341 temp_loc,
3342 obj_loc,
3343 class_offset,
3344 maybe_temp2_loc,
3345 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003346 // Do an exact check.
3347 __ Beq(temp, cls, &done);
3348 // Otherwise, we need to check that the object's class is a non-primitive array.
3349 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003350 GenerateReferenceLoadOneRegister(instruction,
3351 temp_loc,
3352 component_offset,
3353 maybe_temp2_loc,
3354 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003355 // If the component type is null, jump to the slow path to throw the exception.
3356 __ Beqz(temp, slow_path->GetEntryLabel());
3357 // Otherwise, the object is indeed an array, further check that this component
3358 // type is not a primitive type.
3359 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3360 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3361 __ Bnez(temp, slow_path->GetEntryLabel());
3362 break;
3363 }
3364
3365 case TypeCheckKind::kUnresolvedCheck:
3366 // We always go into the type check slow path for the unresolved check case.
3367 // We cannot directly call the CheckCast runtime entry point
3368 // without resorting to a type checking slow path here (i.e. by
3369 // calling InvokeRuntime directly), as it would require to
3370 // assign fixed registers for the inputs of this HInstanceOf
3371 // instruction (following the runtime calling convention), which
3372 // might be cluttered by the potential first read barrier
3373 // emission at the beginning of this method.
3374 __ B(slow_path->GetEntryLabel());
3375 break;
3376
3377 case TypeCheckKind::kInterfaceCheck: {
3378 // Avoid read barriers to improve performance of the fast path. We can not get false
3379 // positives by doing this.
3380 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003381 GenerateReferenceLoadTwoRegisters(instruction,
3382 temp_loc,
3383 obj_loc,
3384 class_offset,
3385 maybe_temp2_loc,
3386 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003387 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003388 GenerateReferenceLoadTwoRegisters(instruction,
3389 temp_loc,
3390 temp_loc,
3391 iftable_offset,
3392 maybe_temp2_loc,
3393 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003394 // Iftable is never null.
3395 __ Lw(TMP, temp, array_length_offset);
3396 // Loop through the iftable and check if any class matches.
3397 MipsLabel loop;
3398 __ Bind(&loop);
3399 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3400 __ Beqz(TMP, slow_path->GetEntryLabel());
3401 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3402 __ MaybeUnpoisonHeapReference(AT);
3403 // Go to next interface.
3404 __ Addiu(TMP, TMP, -2);
3405 // Compare the classes and continue the loop if they do not match.
3406 __ Bne(AT, cls, &loop);
3407 break;
3408 }
3409 }
3410
3411 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 __ Bind(slow_path->GetExitLabel());
3413}
3414
3415void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3416 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003417 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003418 locations->SetInAt(0, Location::RequiresRegister());
3419 if (check->HasUses()) {
3420 locations->SetOut(Location::SameAsFirstInput());
3421 }
3422}
3423
3424void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3425 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003426 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003427 check->GetLoadClass(),
3428 check,
3429 check->GetDexPc(),
3430 true);
3431 codegen_->AddSlowPath(slow_path);
3432 GenerateClassInitializationCheck(slow_path,
3433 check->GetLocations()->InAt(0).AsRegister<Register>());
3434}
3435
3436void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003437 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003439 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003440 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003441
3442 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003443 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003444 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003447 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003448 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003449 locations->SetInAt(0, Location::RequiresRegister());
3450 locations->SetInAt(1, Location::RequiresRegister());
3451 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3452 break;
3453
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003454 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003455 locations->SetInAt(0, Location::RequiresRegister());
3456 locations->SetInAt(1, Location::RequiresRegister());
3457 // Output overlaps because it is written before doing the low comparison.
3458 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3459 break;
3460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kFloat32:
3462 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003463 locations->SetInAt(0, Location::RequiresFpuRegister());
3464 locations->SetInAt(1, Location::RequiresFpuRegister());
3465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003466 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467
3468 default:
3469 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3470 }
3471}
3472
3473void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3474 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003475 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003477 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003478
3479 // 0 if: left == right
3480 // 1 if: left > right
3481 // -1 if: left < right
3482 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003483 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003484 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003485 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003487 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003488 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003489 Register lhs = locations->InAt(0).AsRegister<Register>();
3490 Register rhs = locations->InAt(1).AsRegister<Register>();
3491 __ Slt(TMP, lhs, rhs);
3492 __ Slt(res, rhs, lhs);
3493 __ Subu(res, res, TMP);
3494 break;
3495 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003497 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003498 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3499 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3500 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3501 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3502 // TODO: more efficient (direct) comparison with a constant.
3503 __ Slt(TMP, lhs_high, rhs_high);
3504 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3505 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3506 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3507 __ Sltu(TMP, lhs_low, rhs_low);
3508 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3509 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3510 __ Bind(&done);
3511 break;
3512 }
3513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003515 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003516 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3517 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3518 MipsLabel done;
3519 if (isR6) {
3520 __ CmpEqS(FTMP, lhs, rhs);
3521 __ LoadConst32(res, 0);
3522 __ Bc1nez(FTMP, &done);
3523 if (gt_bias) {
3524 __ CmpLtS(FTMP, lhs, rhs);
3525 __ LoadConst32(res, -1);
3526 __ Bc1nez(FTMP, &done);
3527 __ LoadConst32(res, 1);
3528 } else {
3529 __ CmpLtS(FTMP, rhs, lhs);
3530 __ LoadConst32(res, 1);
3531 __ Bc1nez(FTMP, &done);
3532 __ LoadConst32(res, -1);
3533 }
3534 } else {
3535 if (gt_bias) {
3536 __ ColtS(0, lhs, rhs);
3537 __ LoadConst32(res, -1);
3538 __ Bc1t(0, &done);
3539 __ CeqS(0, lhs, rhs);
3540 __ LoadConst32(res, 1);
3541 __ Movt(res, ZERO, 0);
3542 } else {
3543 __ ColtS(0, rhs, lhs);
3544 __ LoadConst32(res, 1);
3545 __ Bc1t(0, &done);
3546 __ CeqS(0, lhs, rhs);
3547 __ LoadConst32(res, -1);
3548 __ Movt(res, ZERO, 0);
3549 }
3550 }
3551 __ Bind(&done);
3552 break;
3553 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003555 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003556 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3557 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3558 MipsLabel done;
3559 if (isR6) {
3560 __ CmpEqD(FTMP, lhs, rhs);
3561 __ LoadConst32(res, 0);
3562 __ Bc1nez(FTMP, &done);
3563 if (gt_bias) {
3564 __ CmpLtD(FTMP, lhs, rhs);
3565 __ LoadConst32(res, -1);
3566 __ Bc1nez(FTMP, &done);
3567 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003568 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003569 __ CmpLtD(FTMP, rhs, lhs);
3570 __ LoadConst32(res, 1);
3571 __ Bc1nez(FTMP, &done);
3572 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003573 }
3574 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003575 if (gt_bias) {
3576 __ ColtD(0, lhs, rhs);
3577 __ LoadConst32(res, -1);
3578 __ Bc1t(0, &done);
3579 __ CeqD(0, lhs, rhs);
3580 __ LoadConst32(res, 1);
3581 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003582 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003583 __ ColtD(0, rhs, lhs);
3584 __ LoadConst32(res, 1);
3585 __ Bc1t(0, &done);
3586 __ CeqD(0, lhs, rhs);
3587 __ LoadConst32(res, -1);
3588 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003589 }
3590 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003591 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003592 break;
3593 }
3594
3595 default:
3596 LOG(FATAL) << "Unimplemented compare type " << in_type;
3597 }
3598}
3599
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003600void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003601 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003602 switch (instruction->InputAt(0)->GetType()) {
3603 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003604 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003605 locations->SetInAt(0, Location::RequiresRegister());
3606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3607 break;
3608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kFloat32:
3610 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003611 locations->SetInAt(0, Location::RequiresFpuRegister());
3612 locations->SetInAt(1, Location::RequiresFpuRegister());
3613 break;
3614 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003615 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3617 }
3618}
3619
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003620void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003621 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003622 return;
3623 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003627
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003628 switch (type) {
3629 default:
3630 // Integer case.
3631 GenerateIntCompare(instruction->GetCondition(), locations);
3632 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003634 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003635 GenerateLongCompare(instruction->GetCondition(), locations);
3636 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 case DataType::Type::kFloat32:
3639 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003640 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3641 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642 }
3643}
3644
Alexey Frunze7e99e052015-11-24 19:28:01 -08003645void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3646 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003647 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003648
3649 LocationSummary* locations = instruction->GetLocations();
3650 Location second = locations->InAt(1);
3651 DCHECK(second.IsConstant());
3652
3653 Register out = locations->Out().AsRegister<Register>();
3654 Register dividend = locations->InAt(0).AsRegister<Register>();
3655 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3656 DCHECK(imm == 1 || imm == -1);
3657
3658 if (instruction->IsRem()) {
3659 __ Move(out, ZERO);
3660 } else {
3661 if (imm == -1) {
3662 __ Subu(out, ZERO, dividend);
3663 } else if (out != dividend) {
3664 __ Move(out, dividend);
3665 }
3666 }
3667}
3668
3669void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3670 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003672
3673 LocationSummary* locations = instruction->GetLocations();
3674 Location second = locations->InAt(1);
3675 DCHECK(second.IsConstant());
3676
3677 Register out = locations->Out().AsRegister<Register>();
3678 Register dividend = locations->InAt(0).AsRegister<Register>();
3679 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003680 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003681 int ctz_imm = CTZ(abs_imm);
3682
3683 if (instruction->IsDiv()) {
3684 if (ctz_imm == 1) {
3685 // Fast path for division by +/-2, which is very common.
3686 __ Srl(TMP, dividend, 31);
3687 } else {
3688 __ Sra(TMP, dividend, 31);
3689 __ Srl(TMP, TMP, 32 - ctz_imm);
3690 }
3691 __ Addu(out, dividend, TMP);
3692 __ Sra(out, out, ctz_imm);
3693 if (imm < 0) {
3694 __ Subu(out, ZERO, out);
3695 }
3696 } else {
3697 if (ctz_imm == 1) {
3698 // Fast path for modulo +/-2, which is very common.
3699 __ Sra(TMP, dividend, 31);
3700 __ Subu(out, dividend, TMP);
3701 __ Andi(out, out, 1);
3702 __ Addu(out, out, TMP);
3703 } else {
3704 __ Sra(TMP, dividend, 31);
3705 __ Srl(TMP, TMP, 32 - ctz_imm);
3706 __ Addu(out, dividend, TMP);
3707 if (IsUint<16>(abs_imm - 1)) {
3708 __ Andi(out, out, abs_imm - 1);
3709 } else {
3710 __ Sll(out, out, 32 - ctz_imm);
3711 __ Srl(out, out, 32 - ctz_imm);
3712 }
3713 __ Subu(out, out, TMP);
3714 }
3715 }
3716}
3717
3718void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3719 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003721
3722 LocationSummary* locations = instruction->GetLocations();
3723 Location second = locations->InAt(1);
3724 DCHECK(second.IsConstant());
3725
3726 Register out = locations->Out().AsRegister<Register>();
3727 Register dividend = locations->InAt(0).AsRegister<Register>();
3728 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3729
3730 int64_t magic;
3731 int shift;
3732 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3733
3734 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3735
3736 __ LoadConst32(TMP, magic);
3737 if (isR6) {
3738 __ MuhR6(TMP, dividend, TMP);
3739 } else {
3740 __ MultR2(dividend, TMP);
3741 __ Mfhi(TMP);
3742 }
3743 if (imm > 0 && magic < 0) {
3744 __ Addu(TMP, TMP, dividend);
3745 } else if (imm < 0 && magic > 0) {
3746 __ Subu(TMP, TMP, dividend);
3747 }
3748
3749 if (shift != 0) {
3750 __ Sra(TMP, TMP, shift);
3751 }
3752
3753 if (instruction->IsDiv()) {
3754 __ Sra(out, TMP, 31);
3755 __ Subu(out, TMP, out);
3756 } else {
3757 __ Sra(AT, TMP, 31);
3758 __ Subu(AT, TMP, AT);
3759 __ LoadConst32(TMP, imm);
3760 if (isR6) {
3761 __ MulR6(TMP, AT, TMP);
3762 } else {
3763 __ MulR2(TMP, AT, TMP);
3764 }
3765 __ Subu(out, dividend, TMP);
3766 }
3767}
3768
3769void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3770 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003772
3773 LocationSummary* locations = instruction->GetLocations();
3774 Register out = locations->Out().AsRegister<Register>();
3775 Location second = locations->InAt(1);
3776
3777 if (second.IsConstant()) {
3778 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3779 if (imm == 0) {
3780 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3781 } else if (imm == 1 || imm == -1) {
3782 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003783 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003784 DivRemByPowerOfTwo(instruction);
3785 } else {
3786 DCHECK(imm <= -2 || imm >= 2);
3787 GenerateDivRemWithAnyConstant(instruction);
3788 }
3789 } else {
3790 Register dividend = locations->InAt(0).AsRegister<Register>();
3791 Register divisor = second.AsRegister<Register>();
3792 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3793 if (instruction->IsDiv()) {
3794 if (isR6) {
3795 __ DivR6(out, dividend, divisor);
3796 } else {
3797 __ DivR2(out, dividend, divisor);
3798 }
3799 } else {
3800 if (isR6) {
3801 __ ModR6(out, dividend, divisor);
3802 } else {
3803 __ ModR2(out, dividend, divisor);
3804 }
3805 }
3806 }
3807}
3808
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 DataType::Type type = div->GetResultType();
3811 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003812 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003813 : LocationSummary::kNoCall;
3814
Vladimir Markoca6fff82017-10-03 14:49:14 +01003815 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003816
3817 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003818 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003819 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003820 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3822 break;
3823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003824 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825 InvokeRuntimeCallingConvention calling_convention;
3826 locations->SetInAt(0, Location::RegisterPairLocation(
3827 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3828 locations->SetInAt(1, Location::RegisterPairLocation(
3829 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3830 locations->SetOut(calling_convention.GetReturnLocation(type));
3831 break;
3832 }
3833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kFloat32:
3835 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836 locations->SetInAt(0, Location::RequiresFpuRegister());
3837 locations->SetInAt(1, Location::RequiresFpuRegister());
3838 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3839 break;
3840
3841 default:
3842 LOG(FATAL) << "Unexpected div type " << type;
3843 }
3844}
3845
3846void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003847 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003848 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849
3850 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003852 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003855 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3857 break;
3858 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 case DataType::Type::kFloat32:
3860 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3862 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3863 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 __ DivS(dst, lhs, rhs);
3866 } else {
3867 __ DivD(dst, lhs, rhs);
3868 }
3869 break;
3870 }
3871 default:
3872 LOG(FATAL) << "Unexpected div type " << type;
3873 }
3874}
3875
3876void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003877 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003878 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003879}
3880
3881void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003882 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003883 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003884 codegen_->AddSlowPath(slow_path);
3885 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003887
3888 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003890 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt8:
3892 case DataType::Type::kUint16:
3893 case DataType::Type::kInt16:
3894 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003895 if (value.IsConstant()) {
3896 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3897 __ B(slow_path->GetEntryLabel());
3898 } else {
3899 // A division by a non-null constant is valid. We don't need to perform
3900 // any check, so simply fall through.
3901 }
3902 } else {
3903 DCHECK(value.IsRegister()) << value;
3904 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3905 }
3906 break;
3907 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003908 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 if (value.IsConstant()) {
3910 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3911 __ B(slow_path->GetEntryLabel());
3912 } else {
3913 // A division by a non-null constant is valid. We don't need to perform
3914 // any check, so simply fall through.
3915 }
3916 } else {
3917 DCHECK(value.IsRegisterPair()) << value;
3918 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3919 __ Beqz(TMP, slow_path->GetEntryLabel());
3920 }
3921 break;
3922 }
3923 default:
3924 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3925 }
3926}
3927
3928void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003930 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003931 locations->SetOut(Location::ConstantLocation(constant));
3932}
3933
3934void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3935 // Will be generated at use site.
3936}
3937
3938void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3939 exit->SetLocations(nullptr);
3940}
3941
3942void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3943}
3944
3945void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3946 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003947 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948 locations->SetOut(Location::ConstantLocation(constant));
3949}
3950
3951void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3952 // Will be generated at use site.
3953}
3954
3955void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3956 got->SetLocations(nullptr);
3957}
3958
3959void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3960 DCHECK(!successor->IsExitBlock());
3961 HBasicBlock* block = got->GetBlock();
3962 HInstruction* previous = got->GetPrevious();
3963 HLoopInformation* info = block->GetLoopInformation();
3964
3965 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003966 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3967 return;
3968 }
3969 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3970 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3971 }
3972 if (!codegen_->GoesToNextBlock(block, successor)) {
3973 __ B(codegen_->GetLabelOf(successor));
3974 }
3975}
3976
3977void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3978 HandleGoto(got, got->GetSuccessor());
3979}
3980
3981void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3982 try_boundary->SetLocations(nullptr);
3983}
3984
3985void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3986 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3987 if (!successor->IsExitBlock()) {
3988 HandleGoto(try_boundary, successor);
3989 }
3990}
3991
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003992void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3993 LocationSummary* locations) {
3994 Register dst = locations->Out().AsRegister<Register>();
3995 Register lhs = locations->InAt(0).AsRegister<Register>();
3996 Location rhs_location = locations->InAt(1);
3997 Register rhs_reg = ZERO;
3998 int64_t rhs_imm = 0;
3999 bool use_imm = rhs_location.IsConstant();
4000 if (use_imm) {
4001 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4002 } else {
4003 rhs_reg = rhs_location.AsRegister<Register>();
4004 }
4005
4006 switch (cond) {
4007 case kCondEQ:
4008 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004009 if (use_imm && IsInt<16>(-rhs_imm)) {
4010 if (rhs_imm == 0) {
4011 if (cond == kCondEQ) {
4012 __ Sltiu(dst, lhs, 1);
4013 } else {
4014 __ Sltu(dst, ZERO, lhs);
4015 }
4016 } else {
4017 __ Addiu(dst, lhs, -rhs_imm);
4018 if (cond == kCondEQ) {
4019 __ Sltiu(dst, dst, 1);
4020 } else {
4021 __ Sltu(dst, ZERO, dst);
4022 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004023 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004024 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004025 if (use_imm && IsUint<16>(rhs_imm)) {
4026 __ Xori(dst, lhs, rhs_imm);
4027 } else {
4028 if (use_imm) {
4029 rhs_reg = TMP;
4030 __ LoadConst32(rhs_reg, rhs_imm);
4031 }
4032 __ Xor(dst, lhs, rhs_reg);
4033 }
4034 if (cond == kCondEQ) {
4035 __ Sltiu(dst, dst, 1);
4036 } else {
4037 __ Sltu(dst, ZERO, dst);
4038 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004039 }
4040 break;
4041
4042 case kCondLT:
4043 case kCondGE:
4044 if (use_imm && IsInt<16>(rhs_imm)) {
4045 __ Slti(dst, lhs, rhs_imm);
4046 } else {
4047 if (use_imm) {
4048 rhs_reg = TMP;
4049 __ LoadConst32(rhs_reg, rhs_imm);
4050 }
4051 __ Slt(dst, lhs, rhs_reg);
4052 }
4053 if (cond == kCondGE) {
4054 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4055 // only the slt instruction but no sge.
4056 __ Xori(dst, dst, 1);
4057 }
4058 break;
4059
4060 case kCondLE:
4061 case kCondGT:
4062 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4063 // Simulate lhs <= rhs via lhs < rhs + 1.
4064 __ Slti(dst, lhs, rhs_imm + 1);
4065 if (cond == kCondGT) {
4066 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4067 // only the slti instruction but no sgti.
4068 __ Xori(dst, dst, 1);
4069 }
4070 } else {
4071 if (use_imm) {
4072 rhs_reg = TMP;
4073 __ LoadConst32(rhs_reg, rhs_imm);
4074 }
4075 __ Slt(dst, rhs_reg, lhs);
4076 if (cond == kCondLE) {
4077 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4078 // only the slt instruction but no sle.
4079 __ Xori(dst, dst, 1);
4080 }
4081 }
4082 break;
4083
4084 case kCondB:
4085 case kCondAE:
4086 if (use_imm && IsInt<16>(rhs_imm)) {
4087 // Sltiu sign-extends its 16-bit immediate operand before
4088 // the comparison and thus lets us compare directly with
4089 // unsigned values in the ranges [0, 0x7fff] and
4090 // [0xffff8000, 0xffffffff].
4091 __ Sltiu(dst, lhs, rhs_imm);
4092 } else {
4093 if (use_imm) {
4094 rhs_reg = TMP;
4095 __ LoadConst32(rhs_reg, rhs_imm);
4096 }
4097 __ Sltu(dst, lhs, rhs_reg);
4098 }
4099 if (cond == kCondAE) {
4100 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4101 // only the sltu instruction but no sgeu.
4102 __ Xori(dst, dst, 1);
4103 }
4104 break;
4105
4106 case kCondBE:
4107 case kCondA:
4108 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4109 // Simulate lhs <= rhs via lhs < rhs + 1.
4110 // Note that this only works if rhs + 1 does not overflow
4111 // to 0, hence the check above.
4112 // Sltiu sign-extends its 16-bit immediate operand before
4113 // the comparison and thus lets us compare directly with
4114 // unsigned values in the ranges [0, 0x7fff] and
4115 // [0xffff8000, 0xffffffff].
4116 __ Sltiu(dst, lhs, rhs_imm + 1);
4117 if (cond == kCondA) {
4118 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4119 // only the sltiu instruction but no sgtiu.
4120 __ Xori(dst, dst, 1);
4121 }
4122 } else {
4123 if (use_imm) {
4124 rhs_reg = TMP;
4125 __ LoadConst32(rhs_reg, rhs_imm);
4126 }
4127 __ Sltu(dst, rhs_reg, lhs);
4128 if (cond == kCondBE) {
4129 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4130 // only the sltu instruction but no sleu.
4131 __ Xori(dst, dst, 1);
4132 }
4133 }
4134 break;
4135 }
4136}
4137
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004138bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4139 LocationSummary* input_locations,
4140 Register dst) {
4141 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4142 Location rhs_location = input_locations->InAt(1);
4143 Register rhs_reg = ZERO;
4144 int64_t rhs_imm = 0;
4145 bool use_imm = rhs_location.IsConstant();
4146 if (use_imm) {
4147 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4148 } else {
4149 rhs_reg = rhs_location.AsRegister<Register>();
4150 }
4151
4152 switch (cond) {
4153 case kCondEQ:
4154 case kCondNE:
4155 if (use_imm && IsInt<16>(-rhs_imm)) {
4156 __ Addiu(dst, lhs, -rhs_imm);
4157 } else if (use_imm && IsUint<16>(rhs_imm)) {
4158 __ Xori(dst, lhs, rhs_imm);
4159 } else {
4160 if (use_imm) {
4161 rhs_reg = TMP;
4162 __ LoadConst32(rhs_reg, rhs_imm);
4163 }
4164 __ Xor(dst, lhs, rhs_reg);
4165 }
4166 return (cond == kCondEQ);
4167
4168 case kCondLT:
4169 case kCondGE:
4170 if (use_imm && IsInt<16>(rhs_imm)) {
4171 __ Slti(dst, lhs, rhs_imm);
4172 } else {
4173 if (use_imm) {
4174 rhs_reg = TMP;
4175 __ LoadConst32(rhs_reg, rhs_imm);
4176 }
4177 __ Slt(dst, lhs, rhs_reg);
4178 }
4179 return (cond == kCondGE);
4180
4181 case kCondLE:
4182 case kCondGT:
4183 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4184 // Simulate lhs <= rhs via lhs < rhs + 1.
4185 __ Slti(dst, lhs, rhs_imm + 1);
4186 return (cond == kCondGT);
4187 } else {
4188 if (use_imm) {
4189 rhs_reg = TMP;
4190 __ LoadConst32(rhs_reg, rhs_imm);
4191 }
4192 __ Slt(dst, rhs_reg, lhs);
4193 return (cond == kCondLE);
4194 }
4195
4196 case kCondB:
4197 case kCondAE:
4198 if (use_imm && IsInt<16>(rhs_imm)) {
4199 // Sltiu sign-extends its 16-bit immediate operand before
4200 // the comparison and thus lets us compare directly with
4201 // unsigned values in the ranges [0, 0x7fff] and
4202 // [0xffff8000, 0xffffffff].
4203 __ Sltiu(dst, lhs, rhs_imm);
4204 } else {
4205 if (use_imm) {
4206 rhs_reg = TMP;
4207 __ LoadConst32(rhs_reg, rhs_imm);
4208 }
4209 __ Sltu(dst, lhs, rhs_reg);
4210 }
4211 return (cond == kCondAE);
4212
4213 case kCondBE:
4214 case kCondA:
4215 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4216 // Simulate lhs <= rhs via lhs < rhs + 1.
4217 // Note that this only works if rhs + 1 does not overflow
4218 // to 0, hence the check above.
4219 // Sltiu sign-extends its 16-bit immediate operand before
4220 // the comparison and thus lets us compare directly with
4221 // unsigned values in the ranges [0, 0x7fff] and
4222 // [0xffff8000, 0xffffffff].
4223 __ Sltiu(dst, lhs, rhs_imm + 1);
4224 return (cond == kCondA);
4225 } else {
4226 if (use_imm) {
4227 rhs_reg = TMP;
4228 __ LoadConst32(rhs_reg, rhs_imm);
4229 }
4230 __ Sltu(dst, rhs_reg, lhs);
4231 return (cond == kCondBE);
4232 }
4233 }
4234}
4235
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004236void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4237 LocationSummary* locations,
4238 MipsLabel* label) {
4239 Register lhs = locations->InAt(0).AsRegister<Register>();
4240 Location rhs_location = locations->InAt(1);
4241 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004242 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004243 bool use_imm = rhs_location.IsConstant();
4244 if (use_imm) {
4245 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4246 } else {
4247 rhs_reg = rhs_location.AsRegister<Register>();
4248 }
4249
4250 if (use_imm && rhs_imm == 0) {
4251 switch (cond) {
4252 case kCondEQ:
4253 case kCondBE: // <= 0 if zero
4254 __ Beqz(lhs, label);
4255 break;
4256 case kCondNE:
4257 case kCondA: // > 0 if non-zero
4258 __ Bnez(lhs, label);
4259 break;
4260 case kCondLT:
4261 __ Bltz(lhs, label);
4262 break;
4263 case kCondGE:
4264 __ Bgez(lhs, label);
4265 break;
4266 case kCondLE:
4267 __ Blez(lhs, label);
4268 break;
4269 case kCondGT:
4270 __ Bgtz(lhs, label);
4271 break;
4272 case kCondB: // always false
4273 break;
4274 case kCondAE: // always true
4275 __ B(label);
4276 break;
4277 }
4278 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004279 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4280 if (isR6 || !use_imm) {
4281 if (use_imm) {
4282 rhs_reg = TMP;
4283 __ LoadConst32(rhs_reg, rhs_imm);
4284 }
4285 switch (cond) {
4286 case kCondEQ:
4287 __ Beq(lhs, rhs_reg, label);
4288 break;
4289 case kCondNE:
4290 __ Bne(lhs, rhs_reg, label);
4291 break;
4292 case kCondLT:
4293 __ Blt(lhs, rhs_reg, label);
4294 break;
4295 case kCondGE:
4296 __ Bge(lhs, rhs_reg, label);
4297 break;
4298 case kCondLE:
4299 __ Bge(rhs_reg, lhs, label);
4300 break;
4301 case kCondGT:
4302 __ Blt(rhs_reg, lhs, label);
4303 break;
4304 case kCondB:
4305 __ Bltu(lhs, rhs_reg, label);
4306 break;
4307 case kCondAE:
4308 __ Bgeu(lhs, rhs_reg, label);
4309 break;
4310 case kCondBE:
4311 __ Bgeu(rhs_reg, lhs, label);
4312 break;
4313 case kCondA:
4314 __ Bltu(rhs_reg, lhs, label);
4315 break;
4316 }
4317 } else {
4318 // Special cases for more efficient comparison with constants on R2.
4319 switch (cond) {
4320 case kCondEQ:
4321 __ LoadConst32(TMP, rhs_imm);
4322 __ Beq(lhs, TMP, label);
4323 break;
4324 case kCondNE:
4325 __ LoadConst32(TMP, rhs_imm);
4326 __ Bne(lhs, TMP, label);
4327 break;
4328 case kCondLT:
4329 if (IsInt<16>(rhs_imm)) {
4330 __ Slti(TMP, lhs, rhs_imm);
4331 __ Bnez(TMP, label);
4332 } else {
4333 __ LoadConst32(TMP, rhs_imm);
4334 __ Blt(lhs, TMP, label);
4335 }
4336 break;
4337 case kCondGE:
4338 if (IsInt<16>(rhs_imm)) {
4339 __ Slti(TMP, lhs, rhs_imm);
4340 __ Beqz(TMP, label);
4341 } else {
4342 __ LoadConst32(TMP, rhs_imm);
4343 __ Bge(lhs, TMP, label);
4344 }
4345 break;
4346 case kCondLE:
4347 if (IsInt<16>(rhs_imm + 1)) {
4348 // Simulate lhs <= rhs via lhs < rhs + 1.
4349 __ Slti(TMP, lhs, rhs_imm + 1);
4350 __ Bnez(TMP, label);
4351 } else {
4352 __ LoadConst32(TMP, rhs_imm);
4353 __ Bge(TMP, lhs, label);
4354 }
4355 break;
4356 case kCondGT:
4357 if (IsInt<16>(rhs_imm + 1)) {
4358 // Simulate lhs > rhs via !(lhs < rhs + 1).
4359 __ Slti(TMP, lhs, rhs_imm + 1);
4360 __ Beqz(TMP, label);
4361 } else {
4362 __ LoadConst32(TMP, rhs_imm);
4363 __ Blt(TMP, lhs, label);
4364 }
4365 break;
4366 case kCondB:
4367 if (IsInt<16>(rhs_imm)) {
4368 __ Sltiu(TMP, lhs, rhs_imm);
4369 __ Bnez(TMP, label);
4370 } else {
4371 __ LoadConst32(TMP, rhs_imm);
4372 __ Bltu(lhs, TMP, label);
4373 }
4374 break;
4375 case kCondAE:
4376 if (IsInt<16>(rhs_imm)) {
4377 __ Sltiu(TMP, lhs, rhs_imm);
4378 __ Beqz(TMP, label);
4379 } else {
4380 __ LoadConst32(TMP, rhs_imm);
4381 __ Bgeu(lhs, TMP, label);
4382 }
4383 break;
4384 case kCondBE:
4385 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4386 // Simulate lhs <= rhs via lhs < rhs + 1.
4387 // Note that this only works if rhs + 1 does not overflow
4388 // to 0, hence the check above.
4389 __ Sltiu(TMP, lhs, rhs_imm + 1);
4390 __ Bnez(TMP, label);
4391 } else {
4392 __ LoadConst32(TMP, rhs_imm);
4393 __ Bgeu(TMP, lhs, label);
4394 }
4395 break;
4396 case kCondA:
4397 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4398 // Simulate lhs > rhs via !(lhs < rhs + 1).
4399 // Note that this only works if rhs + 1 does not overflow
4400 // to 0, hence the check above.
4401 __ Sltiu(TMP, lhs, rhs_imm + 1);
4402 __ Beqz(TMP, label);
4403 } else {
4404 __ LoadConst32(TMP, rhs_imm);
4405 __ Bltu(TMP, lhs, label);
4406 }
4407 break;
4408 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004409 }
4410 }
4411}
4412
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004413void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4414 LocationSummary* locations) {
4415 Register dst = locations->Out().AsRegister<Register>();
4416 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4417 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4418 Location rhs_location = locations->InAt(1);
4419 Register rhs_high = ZERO;
4420 Register rhs_low = ZERO;
4421 int64_t imm = 0;
4422 uint32_t imm_high = 0;
4423 uint32_t imm_low = 0;
4424 bool use_imm = rhs_location.IsConstant();
4425 if (use_imm) {
4426 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4427 imm_high = High32Bits(imm);
4428 imm_low = Low32Bits(imm);
4429 } else {
4430 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4431 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4432 }
4433 if (use_imm && imm == 0) {
4434 switch (cond) {
4435 case kCondEQ:
4436 case kCondBE: // <= 0 if zero
4437 __ Or(dst, lhs_high, lhs_low);
4438 __ Sltiu(dst, dst, 1);
4439 break;
4440 case kCondNE:
4441 case kCondA: // > 0 if non-zero
4442 __ Or(dst, lhs_high, lhs_low);
4443 __ Sltu(dst, ZERO, dst);
4444 break;
4445 case kCondLT:
4446 __ Slt(dst, lhs_high, ZERO);
4447 break;
4448 case kCondGE:
4449 __ Slt(dst, lhs_high, ZERO);
4450 __ Xori(dst, dst, 1);
4451 break;
4452 case kCondLE:
4453 __ Or(TMP, lhs_high, lhs_low);
4454 __ Sra(AT, lhs_high, 31);
4455 __ Sltu(dst, AT, TMP);
4456 __ Xori(dst, dst, 1);
4457 break;
4458 case kCondGT:
4459 __ Or(TMP, lhs_high, lhs_low);
4460 __ Sra(AT, lhs_high, 31);
4461 __ Sltu(dst, AT, TMP);
4462 break;
4463 case kCondB: // always false
4464 __ Andi(dst, dst, 0);
4465 break;
4466 case kCondAE: // always true
4467 __ Ori(dst, ZERO, 1);
4468 break;
4469 }
4470 } else if (use_imm) {
4471 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4472 switch (cond) {
4473 case kCondEQ:
4474 __ LoadConst32(TMP, imm_high);
4475 __ Xor(TMP, TMP, lhs_high);
4476 __ LoadConst32(AT, imm_low);
4477 __ Xor(AT, AT, lhs_low);
4478 __ Or(dst, TMP, AT);
4479 __ Sltiu(dst, dst, 1);
4480 break;
4481 case kCondNE:
4482 __ LoadConst32(TMP, imm_high);
4483 __ Xor(TMP, TMP, lhs_high);
4484 __ LoadConst32(AT, imm_low);
4485 __ Xor(AT, AT, lhs_low);
4486 __ Or(dst, TMP, AT);
4487 __ Sltu(dst, ZERO, dst);
4488 break;
4489 case kCondLT:
4490 case kCondGE:
4491 if (dst == lhs_low) {
4492 __ LoadConst32(TMP, imm_low);
4493 __ Sltu(dst, lhs_low, TMP);
4494 }
4495 __ LoadConst32(TMP, imm_high);
4496 __ Slt(AT, lhs_high, TMP);
4497 __ Slt(TMP, TMP, lhs_high);
4498 if (dst != lhs_low) {
4499 __ LoadConst32(dst, imm_low);
4500 __ Sltu(dst, lhs_low, dst);
4501 }
4502 __ Slt(dst, TMP, dst);
4503 __ Or(dst, dst, AT);
4504 if (cond == kCondGE) {
4505 __ Xori(dst, dst, 1);
4506 }
4507 break;
4508 case kCondGT:
4509 case kCondLE:
4510 if (dst == lhs_low) {
4511 __ LoadConst32(TMP, imm_low);
4512 __ Sltu(dst, TMP, lhs_low);
4513 }
4514 __ LoadConst32(TMP, imm_high);
4515 __ Slt(AT, TMP, lhs_high);
4516 __ Slt(TMP, lhs_high, TMP);
4517 if (dst != lhs_low) {
4518 __ LoadConst32(dst, imm_low);
4519 __ Sltu(dst, dst, lhs_low);
4520 }
4521 __ Slt(dst, TMP, dst);
4522 __ Or(dst, dst, AT);
4523 if (cond == kCondLE) {
4524 __ Xori(dst, dst, 1);
4525 }
4526 break;
4527 case kCondB:
4528 case kCondAE:
4529 if (dst == lhs_low) {
4530 __ LoadConst32(TMP, imm_low);
4531 __ Sltu(dst, lhs_low, TMP);
4532 }
4533 __ LoadConst32(TMP, imm_high);
4534 __ Sltu(AT, lhs_high, TMP);
4535 __ Sltu(TMP, TMP, lhs_high);
4536 if (dst != lhs_low) {
4537 __ LoadConst32(dst, imm_low);
4538 __ Sltu(dst, lhs_low, dst);
4539 }
4540 __ Slt(dst, TMP, dst);
4541 __ Or(dst, dst, AT);
4542 if (cond == kCondAE) {
4543 __ Xori(dst, dst, 1);
4544 }
4545 break;
4546 case kCondA:
4547 case kCondBE:
4548 if (dst == lhs_low) {
4549 __ LoadConst32(TMP, imm_low);
4550 __ Sltu(dst, TMP, lhs_low);
4551 }
4552 __ LoadConst32(TMP, imm_high);
4553 __ Sltu(AT, TMP, lhs_high);
4554 __ Sltu(TMP, lhs_high, TMP);
4555 if (dst != lhs_low) {
4556 __ LoadConst32(dst, imm_low);
4557 __ Sltu(dst, dst, lhs_low);
4558 }
4559 __ Slt(dst, TMP, dst);
4560 __ Or(dst, dst, AT);
4561 if (cond == kCondBE) {
4562 __ Xori(dst, dst, 1);
4563 }
4564 break;
4565 }
4566 } else {
4567 switch (cond) {
4568 case kCondEQ:
4569 __ Xor(TMP, lhs_high, rhs_high);
4570 __ Xor(AT, lhs_low, rhs_low);
4571 __ Or(dst, TMP, AT);
4572 __ Sltiu(dst, dst, 1);
4573 break;
4574 case kCondNE:
4575 __ Xor(TMP, lhs_high, rhs_high);
4576 __ Xor(AT, lhs_low, rhs_low);
4577 __ Or(dst, TMP, AT);
4578 __ Sltu(dst, ZERO, dst);
4579 break;
4580 case kCondLT:
4581 case kCondGE:
4582 __ Slt(TMP, rhs_high, lhs_high);
4583 __ Sltu(AT, lhs_low, rhs_low);
4584 __ Slt(TMP, TMP, AT);
4585 __ Slt(AT, lhs_high, rhs_high);
4586 __ Or(dst, AT, TMP);
4587 if (cond == kCondGE) {
4588 __ Xori(dst, dst, 1);
4589 }
4590 break;
4591 case kCondGT:
4592 case kCondLE:
4593 __ Slt(TMP, lhs_high, rhs_high);
4594 __ Sltu(AT, rhs_low, lhs_low);
4595 __ Slt(TMP, TMP, AT);
4596 __ Slt(AT, rhs_high, lhs_high);
4597 __ Or(dst, AT, TMP);
4598 if (cond == kCondLE) {
4599 __ Xori(dst, dst, 1);
4600 }
4601 break;
4602 case kCondB:
4603 case kCondAE:
4604 __ Sltu(TMP, rhs_high, lhs_high);
4605 __ Sltu(AT, lhs_low, rhs_low);
4606 __ Slt(TMP, TMP, AT);
4607 __ Sltu(AT, lhs_high, rhs_high);
4608 __ Or(dst, AT, TMP);
4609 if (cond == kCondAE) {
4610 __ Xori(dst, dst, 1);
4611 }
4612 break;
4613 case kCondA:
4614 case kCondBE:
4615 __ Sltu(TMP, lhs_high, rhs_high);
4616 __ Sltu(AT, rhs_low, lhs_low);
4617 __ Slt(TMP, TMP, AT);
4618 __ Sltu(AT, rhs_high, lhs_high);
4619 __ Or(dst, AT, TMP);
4620 if (cond == kCondBE) {
4621 __ Xori(dst, dst, 1);
4622 }
4623 break;
4624 }
4625 }
4626}
4627
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004628void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4629 LocationSummary* locations,
4630 MipsLabel* label) {
4631 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4632 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4633 Location rhs_location = locations->InAt(1);
4634 Register rhs_high = ZERO;
4635 Register rhs_low = ZERO;
4636 int64_t imm = 0;
4637 uint32_t imm_high = 0;
4638 uint32_t imm_low = 0;
4639 bool use_imm = rhs_location.IsConstant();
4640 if (use_imm) {
4641 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4642 imm_high = High32Bits(imm);
4643 imm_low = Low32Bits(imm);
4644 } else {
4645 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4646 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4647 }
4648
4649 if (use_imm && imm == 0) {
4650 switch (cond) {
4651 case kCondEQ:
4652 case kCondBE: // <= 0 if zero
4653 __ Or(TMP, lhs_high, lhs_low);
4654 __ Beqz(TMP, label);
4655 break;
4656 case kCondNE:
4657 case kCondA: // > 0 if non-zero
4658 __ Or(TMP, lhs_high, lhs_low);
4659 __ Bnez(TMP, label);
4660 break;
4661 case kCondLT:
4662 __ Bltz(lhs_high, label);
4663 break;
4664 case kCondGE:
4665 __ Bgez(lhs_high, label);
4666 break;
4667 case kCondLE:
4668 __ Or(TMP, lhs_high, lhs_low);
4669 __ Sra(AT, lhs_high, 31);
4670 __ Bgeu(AT, TMP, label);
4671 break;
4672 case kCondGT:
4673 __ Or(TMP, lhs_high, lhs_low);
4674 __ Sra(AT, lhs_high, 31);
4675 __ Bltu(AT, TMP, label);
4676 break;
4677 case kCondB: // always false
4678 break;
4679 case kCondAE: // always true
4680 __ B(label);
4681 break;
4682 }
4683 } else if (use_imm) {
4684 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4685 switch (cond) {
4686 case kCondEQ:
4687 __ LoadConst32(TMP, imm_high);
4688 __ Xor(TMP, TMP, lhs_high);
4689 __ LoadConst32(AT, imm_low);
4690 __ Xor(AT, AT, lhs_low);
4691 __ Or(TMP, TMP, AT);
4692 __ Beqz(TMP, label);
4693 break;
4694 case kCondNE:
4695 __ LoadConst32(TMP, imm_high);
4696 __ Xor(TMP, TMP, lhs_high);
4697 __ LoadConst32(AT, imm_low);
4698 __ Xor(AT, AT, lhs_low);
4699 __ Or(TMP, TMP, AT);
4700 __ Bnez(TMP, label);
4701 break;
4702 case kCondLT:
4703 __ LoadConst32(TMP, imm_high);
4704 __ Blt(lhs_high, TMP, label);
4705 __ Slt(TMP, TMP, lhs_high);
4706 __ LoadConst32(AT, imm_low);
4707 __ Sltu(AT, lhs_low, AT);
4708 __ Blt(TMP, AT, label);
4709 break;
4710 case kCondGE:
4711 __ LoadConst32(TMP, imm_high);
4712 __ Blt(TMP, lhs_high, label);
4713 __ Slt(TMP, lhs_high, TMP);
4714 __ LoadConst32(AT, imm_low);
4715 __ Sltu(AT, lhs_low, AT);
4716 __ Or(TMP, TMP, AT);
4717 __ Beqz(TMP, label);
4718 break;
4719 case kCondLE:
4720 __ LoadConst32(TMP, imm_high);
4721 __ Blt(lhs_high, TMP, label);
4722 __ Slt(TMP, TMP, lhs_high);
4723 __ LoadConst32(AT, imm_low);
4724 __ Sltu(AT, AT, lhs_low);
4725 __ Or(TMP, TMP, AT);
4726 __ Beqz(TMP, label);
4727 break;
4728 case kCondGT:
4729 __ LoadConst32(TMP, imm_high);
4730 __ Blt(TMP, lhs_high, label);
4731 __ Slt(TMP, lhs_high, TMP);
4732 __ LoadConst32(AT, imm_low);
4733 __ Sltu(AT, AT, lhs_low);
4734 __ Blt(TMP, AT, label);
4735 break;
4736 case kCondB:
4737 __ LoadConst32(TMP, imm_high);
4738 __ Bltu(lhs_high, TMP, label);
4739 __ Sltu(TMP, TMP, lhs_high);
4740 __ LoadConst32(AT, imm_low);
4741 __ Sltu(AT, lhs_low, AT);
4742 __ Blt(TMP, AT, label);
4743 break;
4744 case kCondAE:
4745 __ LoadConst32(TMP, imm_high);
4746 __ Bltu(TMP, lhs_high, label);
4747 __ Sltu(TMP, lhs_high, TMP);
4748 __ LoadConst32(AT, imm_low);
4749 __ Sltu(AT, lhs_low, AT);
4750 __ Or(TMP, TMP, AT);
4751 __ Beqz(TMP, label);
4752 break;
4753 case kCondBE:
4754 __ LoadConst32(TMP, imm_high);
4755 __ Bltu(lhs_high, TMP, label);
4756 __ Sltu(TMP, TMP, lhs_high);
4757 __ LoadConst32(AT, imm_low);
4758 __ Sltu(AT, AT, lhs_low);
4759 __ Or(TMP, TMP, AT);
4760 __ Beqz(TMP, label);
4761 break;
4762 case kCondA:
4763 __ LoadConst32(TMP, imm_high);
4764 __ Bltu(TMP, lhs_high, label);
4765 __ Sltu(TMP, lhs_high, TMP);
4766 __ LoadConst32(AT, imm_low);
4767 __ Sltu(AT, AT, lhs_low);
4768 __ Blt(TMP, AT, label);
4769 break;
4770 }
4771 } else {
4772 switch (cond) {
4773 case kCondEQ:
4774 __ Xor(TMP, lhs_high, rhs_high);
4775 __ Xor(AT, lhs_low, rhs_low);
4776 __ Or(TMP, TMP, AT);
4777 __ Beqz(TMP, label);
4778 break;
4779 case kCondNE:
4780 __ Xor(TMP, lhs_high, rhs_high);
4781 __ Xor(AT, lhs_low, rhs_low);
4782 __ Or(TMP, TMP, AT);
4783 __ Bnez(TMP, label);
4784 break;
4785 case kCondLT:
4786 __ Blt(lhs_high, rhs_high, label);
4787 __ Slt(TMP, rhs_high, lhs_high);
4788 __ Sltu(AT, lhs_low, rhs_low);
4789 __ Blt(TMP, AT, label);
4790 break;
4791 case kCondGE:
4792 __ Blt(rhs_high, lhs_high, label);
4793 __ Slt(TMP, lhs_high, rhs_high);
4794 __ Sltu(AT, lhs_low, rhs_low);
4795 __ Or(TMP, TMP, AT);
4796 __ Beqz(TMP, label);
4797 break;
4798 case kCondLE:
4799 __ Blt(lhs_high, rhs_high, label);
4800 __ Slt(TMP, rhs_high, lhs_high);
4801 __ Sltu(AT, rhs_low, lhs_low);
4802 __ Or(TMP, TMP, AT);
4803 __ Beqz(TMP, label);
4804 break;
4805 case kCondGT:
4806 __ Blt(rhs_high, lhs_high, label);
4807 __ Slt(TMP, lhs_high, rhs_high);
4808 __ Sltu(AT, rhs_low, lhs_low);
4809 __ Blt(TMP, AT, label);
4810 break;
4811 case kCondB:
4812 __ Bltu(lhs_high, rhs_high, label);
4813 __ Sltu(TMP, rhs_high, lhs_high);
4814 __ Sltu(AT, lhs_low, rhs_low);
4815 __ Blt(TMP, AT, label);
4816 break;
4817 case kCondAE:
4818 __ Bltu(rhs_high, lhs_high, label);
4819 __ Sltu(TMP, lhs_high, rhs_high);
4820 __ Sltu(AT, lhs_low, rhs_low);
4821 __ Or(TMP, TMP, AT);
4822 __ Beqz(TMP, label);
4823 break;
4824 case kCondBE:
4825 __ Bltu(lhs_high, rhs_high, label);
4826 __ Sltu(TMP, rhs_high, lhs_high);
4827 __ Sltu(AT, rhs_low, lhs_low);
4828 __ Or(TMP, TMP, AT);
4829 __ Beqz(TMP, label);
4830 break;
4831 case kCondA:
4832 __ Bltu(rhs_high, lhs_high, label);
4833 __ Sltu(TMP, lhs_high, rhs_high);
4834 __ Sltu(AT, rhs_low, lhs_low);
4835 __ Blt(TMP, AT, label);
4836 break;
4837 }
4838 }
4839}
4840
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004841void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4842 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004844 LocationSummary* locations) {
4845 Register dst = locations->Out().AsRegister<Register>();
4846 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4847 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4848 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004849 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004850 if (isR6) {
4851 switch (cond) {
4852 case kCondEQ:
4853 __ CmpEqS(FTMP, lhs, rhs);
4854 __ Mfc1(dst, FTMP);
4855 __ Andi(dst, dst, 1);
4856 break;
4857 case kCondNE:
4858 __ CmpEqS(FTMP, lhs, rhs);
4859 __ Mfc1(dst, FTMP);
4860 __ Addiu(dst, dst, 1);
4861 break;
4862 case kCondLT:
4863 if (gt_bias) {
4864 __ CmpLtS(FTMP, lhs, rhs);
4865 } else {
4866 __ CmpUltS(FTMP, lhs, rhs);
4867 }
4868 __ Mfc1(dst, FTMP);
4869 __ Andi(dst, dst, 1);
4870 break;
4871 case kCondLE:
4872 if (gt_bias) {
4873 __ CmpLeS(FTMP, lhs, rhs);
4874 } else {
4875 __ CmpUleS(FTMP, lhs, rhs);
4876 }
4877 __ Mfc1(dst, FTMP);
4878 __ Andi(dst, dst, 1);
4879 break;
4880 case kCondGT:
4881 if (gt_bias) {
4882 __ CmpUltS(FTMP, rhs, lhs);
4883 } else {
4884 __ CmpLtS(FTMP, rhs, lhs);
4885 }
4886 __ Mfc1(dst, FTMP);
4887 __ Andi(dst, dst, 1);
4888 break;
4889 case kCondGE:
4890 if (gt_bias) {
4891 __ CmpUleS(FTMP, rhs, lhs);
4892 } else {
4893 __ CmpLeS(FTMP, rhs, lhs);
4894 }
4895 __ Mfc1(dst, FTMP);
4896 __ Andi(dst, dst, 1);
4897 break;
4898 default:
4899 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4900 UNREACHABLE();
4901 }
4902 } else {
4903 switch (cond) {
4904 case kCondEQ:
4905 __ CeqS(0, lhs, rhs);
4906 __ LoadConst32(dst, 1);
4907 __ Movf(dst, ZERO, 0);
4908 break;
4909 case kCondNE:
4910 __ CeqS(0, lhs, rhs);
4911 __ LoadConst32(dst, 1);
4912 __ Movt(dst, ZERO, 0);
4913 break;
4914 case kCondLT:
4915 if (gt_bias) {
4916 __ ColtS(0, lhs, rhs);
4917 } else {
4918 __ CultS(0, lhs, rhs);
4919 }
4920 __ LoadConst32(dst, 1);
4921 __ Movf(dst, ZERO, 0);
4922 break;
4923 case kCondLE:
4924 if (gt_bias) {
4925 __ ColeS(0, lhs, rhs);
4926 } else {
4927 __ CuleS(0, lhs, rhs);
4928 }
4929 __ LoadConst32(dst, 1);
4930 __ Movf(dst, ZERO, 0);
4931 break;
4932 case kCondGT:
4933 if (gt_bias) {
4934 __ CultS(0, rhs, lhs);
4935 } else {
4936 __ ColtS(0, rhs, lhs);
4937 }
4938 __ LoadConst32(dst, 1);
4939 __ Movf(dst, ZERO, 0);
4940 break;
4941 case kCondGE:
4942 if (gt_bias) {
4943 __ CuleS(0, rhs, lhs);
4944 } else {
4945 __ ColeS(0, rhs, lhs);
4946 }
4947 __ LoadConst32(dst, 1);
4948 __ Movf(dst, ZERO, 0);
4949 break;
4950 default:
4951 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4952 UNREACHABLE();
4953 }
4954 }
4955 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004956 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004957 if (isR6) {
4958 switch (cond) {
4959 case kCondEQ:
4960 __ CmpEqD(FTMP, lhs, rhs);
4961 __ Mfc1(dst, FTMP);
4962 __ Andi(dst, dst, 1);
4963 break;
4964 case kCondNE:
4965 __ CmpEqD(FTMP, lhs, rhs);
4966 __ Mfc1(dst, FTMP);
4967 __ Addiu(dst, dst, 1);
4968 break;
4969 case kCondLT:
4970 if (gt_bias) {
4971 __ CmpLtD(FTMP, lhs, rhs);
4972 } else {
4973 __ CmpUltD(FTMP, lhs, rhs);
4974 }
4975 __ Mfc1(dst, FTMP);
4976 __ Andi(dst, dst, 1);
4977 break;
4978 case kCondLE:
4979 if (gt_bias) {
4980 __ CmpLeD(FTMP, lhs, rhs);
4981 } else {
4982 __ CmpUleD(FTMP, lhs, rhs);
4983 }
4984 __ Mfc1(dst, FTMP);
4985 __ Andi(dst, dst, 1);
4986 break;
4987 case kCondGT:
4988 if (gt_bias) {
4989 __ CmpUltD(FTMP, rhs, lhs);
4990 } else {
4991 __ CmpLtD(FTMP, rhs, lhs);
4992 }
4993 __ Mfc1(dst, FTMP);
4994 __ Andi(dst, dst, 1);
4995 break;
4996 case kCondGE:
4997 if (gt_bias) {
4998 __ CmpUleD(FTMP, rhs, lhs);
4999 } else {
5000 __ CmpLeD(FTMP, rhs, lhs);
5001 }
5002 __ Mfc1(dst, FTMP);
5003 __ Andi(dst, dst, 1);
5004 break;
5005 default:
5006 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5007 UNREACHABLE();
5008 }
5009 } else {
5010 switch (cond) {
5011 case kCondEQ:
5012 __ CeqD(0, lhs, rhs);
5013 __ LoadConst32(dst, 1);
5014 __ Movf(dst, ZERO, 0);
5015 break;
5016 case kCondNE:
5017 __ CeqD(0, lhs, rhs);
5018 __ LoadConst32(dst, 1);
5019 __ Movt(dst, ZERO, 0);
5020 break;
5021 case kCondLT:
5022 if (gt_bias) {
5023 __ ColtD(0, lhs, rhs);
5024 } else {
5025 __ CultD(0, lhs, rhs);
5026 }
5027 __ LoadConst32(dst, 1);
5028 __ Movf(dst, ZERO, 0);
5029 break;
5030 case kCondLE:
5031 if (gt_bias) {
5032 __ ColeD(0, lhs, rhs);
5033 } else {
5034 __ CuleD(0, lhs, rhs);
5035 }
5036 __ LoadConst32(dst, 1);
5037 __ Movf(dst, ZERO, 0);
5038 break;
5039 case kCondGT:
5040 if (gt_bias) {
5041 __ CultD(0, rhs, lhs);
5042 } else {
5043 __ ColtD(0, rhs, lhs);
5044 }
5045 __ LoadConst32(dst, 1);
5046 __ Movf(dst, ZERO, 0);
5047 break;
5048 case kCondGE:
5049 if (gt_bias) {
5050 __ CuleD(0, rhs, lhs);
5051 } else {
5052 __ ColeD(0, rhs, lhs);
5053 }
5054 __ LoadConst32(dst, 1);
5055 __ Movf(dst, ZERO, 0);
5056 break;
5057 default:
5058 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5059 UNREACHABLE();
5060 }
5061 }
5062 }
5063}
5064
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005065bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5066 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005067 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005068 LocationSummary* input_locations,
5069 int cc) {
5070 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5071 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5072 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005073 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005074 switch (cond) {
5075 case kCondEQ:
5076 __ CeqS(cc, lhs, rhs);
5077 return false;
5078 case kCondNE:
5079 __ CeqS(cc, lhs, rhs);
5080 return true;
5081 case kCondLT:
5082 if (gt_bias) {
5083 __ ColtS(cc, lhs, rhs);
5084 } else {
5085 __ CultS(cc, lhs, rhs);
5086 }
5087 return false;
5088 case kCondLE:
5089 if (gt_bias) {
5090 __ ColeS(cc, lhs, rhs);
5091 } else {
5092 __ CuleS(cc, lhs, rhs);
5093 }
5094 return false;
5095 case kCondGT:
5096 if (gt_bias) {
5097 __ CultS(cc, rhs, lhs);
5098 } else {
5099 __ ColtS(cc, rhs, lhs);
5100 }
5101 return false;
5102 case kCondGE:
5103 if (gt_bias) {
5104 __ CuleS(cc, rhs, lhs);
5105 } else {
5106 __ ColeS(cc, rhs, lhs);
5107 }
5108 return false;
5109 default:
5110 LOG(FATAL) << "Unexpected non-floating-point condition";
5111 UNREACHABLE();
5112 }
5113 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005114 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005115 switch (cond) {
5116 case kCondEQ:
5117 __ CeqD(cc, lhs, rhs);
5118 return false;
5119 case kCondNE:
5120 __ CeqD(cc, lhs, rhs);
5121 return true;
5122 case kCondLT:
5123 if (gt_bias) {
5124 __ ColtD(cc, lhs, rhs);
5125 } else {
5126 __ CultD(cc, lhs, rhs);
5127 }
5128 return false;
5129 case kCondLE:
5130 if (gt_bias) {
5131 __ ColeD(cc, lhs, rhs);
5132 } else {
5133 __ CuleD(cc, lhs, rhs);
5134 }
5135 return false;
5136 case kCondGT:
5137 if (gt_bias) {
5138 __ CultD(cc, rhs, lhs);
5139 } else {
5140 __ ColtD(cc, rhs, lhs);
5141 }
5142 return false;
5143 case kCondGE:
5144 if (gt_bias) {
5145 __ CuleD(cc, rhs, lhs);
5146 } else {
5147 __ ColeD(cc, rhs, lhs);
5148 }
5149 return false;
5150 default:
5151 LOG(FATAL) << "Unexpected non-floating-point condition";
5152 UNREACHABLE();
5153 }
5154 }
5155}
5156
5157bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5158 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005159 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005160 LocationSummary* input_locations,
5161 FRegister dst) {
5162 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5163 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5164 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005165 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005166 switch (cond) {
5167 case kCondEQ:
5168 __ CmpEqS(dst, lhs, rhs);
5169 return false;
5170 case kCondNE:
5171 __ CmpEqS(dst, lhs, rhs);
5172 return true;
5173 case kCondLT:
5174 if (gt_bias) {
5175 __ CmpLtS(dst, lhs, rhs);
5176 } else {
5177 __ CmpUltS(dst, lhs, rhs);
5178 }
5179 return false;
5180 case kCondLE:
5181 if (gt_bias) {
5182 __ CmpLeS(dst, lhs, rhs);
5183 } else {
5184 __ CmpUleS(dst, lhs, rhs);
5185 }
5186 return false;
5187 case kCondGT:
5188 if (gt_bias) {
5189 __ CmpUltS(dst, rhs, lhs);
5190 } else {
5191 __ CmpLtS(dst, rhs, lhs);
5192 }
5193 return false;
5194 case kCondGE:
5195 if (gt_bias) {
5196 __ CmpUleS(dst, rhs, lhs);
5197 } else {
5198 __ CmpLeS(dst, rhs, lhs);
5199 }
5200 return false;
5201 default:
5202 LOG(FATAL) << "Unexpected non-floating-point condition";
5203 UNREACHABLE();
5204 }
5205 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005206 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005207 switch (cond) {
5208 case kCondEQ:
5209 __ CmpEqD(dst, lhs, rhs);
5210 return false;
5211 case kCondNE:
5212 __ CmpEqD(dst, lhs, rhs);
5213 return true;
5214 case kCondLT:
5215 if (gt_bias) {
5216 __ CmpLtD(dst, lhs, rhs);
5217 } else {
5218 __ CmpUltD(dst, lhs, rhs);
5219 }
5220 return false;
5221 case kCondLE:
5222 if (gt_bias) {
5223 __ CmpLeD(dst, lhs, rhs);
5224 } else {
5225 __ CmpUleD(dst, lhs, rhs);
5226 }
5227 return false;
5228 case kCondGT:
5229 if (gt_bias) {
5230 __ CmpUltD(dst, rhs, lhs);
5231 } else {
5232 __ CmpLtD(dst, rhs, lhs);
5233 }
5234 return false;
5235 case kCondGE:
5236 if (gt_bias) {
5237 __ CmpUleD(dst, rhs, lhs);
5238 } else {
5239 __ CmpLeD(dst, rhs, lhs);
5240 }
5241 return false;
5242 default:
5243 LOG(FATAL) << "Unexpected non-floating-point condition";
5244 UNREACHABLE();
5245 }
5246 }
5247}
5248
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005249void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5250 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005251 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005252 LocationSummary* locations,
5253 MipsLabel* label) {
5254 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5255 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5256 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005257 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005258 if (isR6) {
5259 switch (cond) {
5260 case kCondEQ:
5261 __ CmpEqS(FTMP, lhs, rhs);
5262 __ Bc1nez(FTMP, label);
5263 break;
5264 case kCondNE:
5265 __ CmpEqS(FTMP, lhs, rhs);
5266 __ Bc1eqz(FTMP, label);
5267 break;
5268 case kCondLT:
5269 if (gt_bias) {
5270 __ CmpLtS(FTMP, lhs, rhs);
5271 } else {
5272 __ CmpUltS(FTMP, lhs, rhs);
5273 }
5274 __ Bc1nez(FTMP, label);
5275 break;
5276 case kCondLE:
5277 if (gt_bias) {
5278 __ CmpLeS(FTMP, lhs, rhs);
5279 } else {
5280 __ CmpUleS(FTMP, lhs, rhs);
5281 }
5282 __ Bc1nez(FTMP, label);
5283 break;
5284 case kCondGT:
5285 if (gt_bias) {
5286 __ CmpUltS(FTMP, rhs, lhs);
5287 } else {
5288 __ CmpLtS(FTMP, rhs, lhs);
5289 }
5290 __ Bc1nez(FTMP, label);
5291 break;
5292 case kCondGE:
5293 if (gt_bias) {
5294 __ CmpUleS(FTMP, rhs, lhs);
5295 } else {
5296 __ CmpLeS(FTMP, rhs, lhs);
5297 }
5298 __ Bc1nez(FTMP, label);
5299 break;
5300 default:
5301 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005302 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005303 }
5304 } else {
5305 switch (cond) {
5306 case kCondEQ:
5307 __ CeqS(0, lhs, rhs);
5308 __ Bc1t(0, label);
5309 break;
5310 case kCondNE:
5311 __ CeqS(0, lhs, rhs);
5312 __ Bc1f(0, label);
5313 break;
5314 case kCondLT:
5315 if (gt_bias) {
5316 __ ColtS(0, lhs, rhs);
5317 } else {
5318 __ CultS(0, lhs, rhs);
5319 }
5320 __ Bc1t(0, label);
5321 break;
5322 case kCondLE:
5323 if (gt_bias) {
5324 __ ColeS(0, lhs, rhs);
5325 } else {
5326 __ CuleS(0, lhs, rhs);
5327 }
5328 __ Bc1t(0, label);
5329 break;
5330 case kCondGT:
5331 if (gt_bias) {
5332 __ CultS(0, rhs, lhs);
5333 } else {
5334 __ ColtS(0, rhs, lhs);
5335 }
5336 __ Bc1t(0, label);
5337 break;
5338 case kCondGE:
5339 if (gt_bias) {
5340 __ CuleS(0, rhs, lhs);
5341 } else {
5342 __ ColeS(0, rhs, lhs);
5343 }
5344 __ Bc1t(0, label);
5345 break;
5346 default:
5347 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005348 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005349 }
5350 }
5351 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005352 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005353 if (isR6) {
5354 switch (cond) {
5355 case kCondEQ:
5356 __ CmpEqD(FTMP, lhs, rhs);
5357 __ Bc1nez(FTMP, label);
5358 break;
5359 case kCondNE:
5360 __ CmpEqD(FTMP, lhs, rhs);
5361 __ Bc1eqz(FTMP, label);
5362 break;
5363 case kCondLT:
5364 if (gt_bias) {
5365 __ CmpLtD(FTMP, lhs, rhs);
5366 } else {
5367 __ CmpUltD(FTMP, lhs, rhs);
5368 }
5369 __ Bc1nez(FTMP, label);
5370 break;
5371 case kCondLE:
5372 if (gt_bias) {
5373 __ CmpLeD(FTMP, lhs, rhs);
5374 } else {
5375 __ CmpUleD(FTMP, lhs, rhs);
5376 }
5377 __ Bc1nez(FTMP, label);
5378 break;
5379 case kCondGT:
5380 if (gt_bias) {
5381 __ CmpUltD(FTMP, rhs, lhs);
5382 } else {
5383 __ CmpLtD(FTMP, rhs, lhs);
5384 }
5385 __ Bc1nez(FTMP, label);
5386 break;
5387 case kCondGE:
5388 if (gt_bias) {
5389 __ CmpUleD(FTMP, rhs, lhs);
5390 } else {
5391 __ CmpLeD(FTMP, rhs, lhs);
5392 }
5393 __ Bc1nez(FTMP, label);
5394 break;
5395 default:
5396 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005397 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005398 }
5399 } else {
5400 switch (cond) {
5401 case kCondEQ:
5402 __ CeqD(0, lhs, rhs);
5403 __ Bc1t(0, label);
5404 break;
5405 case kCondNE:
5406 __ CeqD(0, lhs, rhs);
5407 __ Bc1f(0, label);
5408 break;
5409 case kCondLT:
5410 if (gt_bias) {
5411 __ ColtD(0, lhs, rhs);
5412 } else {
5413 __ CultD(0, lhs, rhs);
5414 }
5415 __ Bc1t(0, label);
5416 break;
5417 case kCondLE:
5418 if (gt_bias) {
5419 __ ColeD(0, lhs, rhs);
5420 } else {
5421 __ CuleD(0, lhs, rhs);
5422 }
5423 __ Bc1t(0, label);
5424 break;
5425 case kCondGT:
5426 if (gt_bias) {
5427 __ CultD(0, rhs, lhs);
5428 } else {
5429 __ ColtD(0, rhs, lhs);
5430 }
5431 __ Bc1t(0, label);
5432 break;
5433 case kCondGE:
5434 if (gt_bias) {
5435 __ CuleD(0, rhs, lhs);
5436 } else {
5437 __ ColeD(0, rhs, lhs);
5438 }
5439 __ Bc1t(0, label);
5440 break;
5441 default:
5442 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005443 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005444 }
5445 }
5446 }
5447}
5448
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005449void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005450 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005451 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005452 MipsLabel* false_target) {
5453 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454
David Brazdil0debae72015-11-12 18:37:00 +00005455 if (true_target == nullptr && false_target == nullptr) {
5456 // Nothing to do. The code always falls through.
5457 return;
5458 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005459 // Constant condition, statically compared against "true" (integer value 1).
5460 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005461 if (true_target != nullptr) {
5462 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005463 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005464 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005465 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005466 if (false_target != nullptr) {
5467 __ B(false_target);
5468 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005469 }
David Brazdil0debae72015-11-12 18:37:00 +00005470 return;
5471 }
5472
5473 // The following code generates these patterns:
5474 // (1) true_target == nullptr && false_target != nullptr
5475 // - opposite condition true => branch to false_target
5476 // (2) true_target != nullptr && false_target == nullptr
5477 // - condition true => branch to true_target
5478 // (3) true_target != nullptr && false_target != nullptr
5479 // - condition true => branch to true_target
5480 // - branch to false_target
5481 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005482 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005483 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005484 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005485 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005486 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5487 } else {
5488 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005490 } else {
5491 // The condition instruction has not been materialized, use its inputs as
5492 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005493 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005494 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005495 LocationSummary* locations = cond->GetLocations();
5496 IfCondition if_cond = condition->GetCondition();
5497 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005498
David Brazdil0debae72015-11-12 18:37:00 +00005499 if (true_target == nullptr) {
5500 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005501 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005502 }
5503
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005504 switch (type) {
5505 default:
5506 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5507 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005508 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005509 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5510 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 case DataType::Type::kFloat32:
5512 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005513 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5514 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005515 }
5516 }
David Brazdil0debae72015-11-12 18:37:00 +00005517
5518 // If neither branch falls through (case 3), the conditional branch to `true_target`
5519 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5520 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005521 __ B(false_target);
5522 }
5523}
5524
5525void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005526 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005527 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005528 locations->SetInAt(0, Location::RequiresRegister());
5529 }
5530}
5531
5532void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005533 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5534 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5535 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5536 nullptr : codegen_->GetLabelOf(true_successor);
5537 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5538 nullptr : codegen_->GetLabelOf(false_successor);
5539 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005540}
5541
5542void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005543 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005544 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005545 InvokeRuntimeCallingConvention calling_convention;
5546 RegisterSet caller_saves = RegisterSet::Empty();
5547 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5548 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005549 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005550 locations->SetInAt(0, Location::RequiresRegister());
5551 }
5552}
5553
5554void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005555 SlowPathCodeMIPS* slow_path =
5556 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005557 GenerateTestAndBranch(deoptimize,
5558 /* condition_input_index */ 0,
5559 slow_path->GetEntryLabel(),
5560 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561}
5562
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005563// This function returns true if a conditional move can be generated for HSelect.
5564// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5565// branches and regular moves.
5566//
5567// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5568//
5569// While determining feasibility of a conditional move and setting inputs/outputs
5570// are two distinct tasks, this function does both because they share quite a bit
5571// of common logic.
5572static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5573 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5574 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5575 HCondition* condition = cond->AsCondition();
5576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005577 DataType::Type cond_type =
5578 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5579 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005580
5581 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5582 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5583 bool is_true_value_zero_constant =
5584 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5585 bool is_false_value_zero_constant =
5586 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5587
5588 bool can_move_conditionally = false;
5589 bool use_const_for_false_in = false;
5590 bool use_const_for_true_in = false;
5591
5592 if (!cond->IsConstant()) {
5593 switch (cond_type) {
5594 default:
5595 switch (dst_type) {
5596 default:
5597 // Moving int on int condition.
5598 if (is_r6) {
5599 if (is_true_value_zero_constant) {
5600 // seleqz out_reg, false_reg, cond_reg
5601 can_move_conditionally = true;
5602 use_const_for_true_in = true;
5603 } else if (is_false_value_zero_constant) {
5604 // selnez out_reg, true_reg, cond_reg
5605 can_move_conditionally = true;
5606 use_const_for_false_in = true;
5607 } else if (materialized) {
5608 // Not materializing unmaterialized int conditions
5609 // to keep the instruction count low.
5610 // selnez AT, true_reg, cond_reg
5611 // seleqz TMP, false_reg, cond_reg
5612 // or out_reg, AT, TMP
5613 can_move_conditionally = true;
5614 }
5615 } else {
5616 // movn out_reg, true_reg/ZERO, cond_reg
5617 can_move_conditionally = true;
5618 use_const_for_true_in = is_true_value_zero_constant;
5619 }
5620 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005622 // Moving long on int condition.
5623 if (is_r6) {
5624 if (is_true_value_zero_constant) {
5625 // seleqz out_reg_lo, false_reg_lo, cond_reg
5626 // seleqz out_reg_hi, false_reg_hi, cond_reg
5627 can_move_conditionally = true;
5628 use_const_for_true_in = true;
5629 } else if (is_false_value_zero_constant) {
5630 // selnez out_reg_lo, true_reg_lo, cond_reg
5631 // selnez out_reg_hi, true_reg_hi, cond_reg
5632 can_move_conditionally = true;
5633 use_const_for_false_in = true;
5634 }
5635 // Other long conditional moves would generate 6+ instructions,
5636 // which is too many.
5637 } else {
5638 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5639 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5640 can_move_conditionally = true;
5641 use_const_for_true_in = is_true_value_zero_constant;
5642 }
5643 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 case DataType::Type::kFloat32:
5645 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005646 // Moving float/double on int condition.
5647 if (is_r6) {
5648 if (materialized) {
5649 // Not materializing unmaterialized int conditions
5650 // to keep the instruction count low.
5651 can_move_conditionally = true;
5652 if (is_true_value_zero_constant) {
5653 // sltu TMP, ZERO, cond_reg
5654 // mtc1 TMP, temp_cond_reg
5655 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5656 use_const_for_true_in = true;
5657 } else if (is_false_value_zero_constant) {
5658 // sltu TMP, ZERO, cond_reg
5659 // mtc1 TMP, temp_cond_reg
5660 // selnez.fmt out_reg, true_reg, temp_cond_reg
5661 use_const_for_false_in = true;
5662 } else {
5663 // sltu TMP, ZERO, cond_reg
5664 // mtc1 TMP, temp_cond_reg
5665 // sel.fmt temp_cond_reg, false_reg, true_reg
5666 // mov.fmt out_reg, temp_cond_reg
5667 }
5668 }
5669 } else {
5670 // movn.fmt out_reg, true_reg, cond_reg
5671 can_move_conditionally = true;
5672 }
5673 break;
5674 }
5675 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005676 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005677 // We don't materialize long comparison now
5678 // and use conditional branches instead.
5679 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kFloat32:
5681 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005682 switch (dst_type) {
5683 default:
5684 // Moving int on float/double condition.
5685 if (is_r6) {
5686 if (is_true_value_zero_constant) {
5687 // mfc1 TMP, temp_cond_reg
5688 // seleqz out_reg, false_reg, TMP
5689 can_move_conditionally = true;
5690 use_const_for_true_in = true;
5691 } else if (is_false_value_zero_constant) {
5692 // mfc1 TMP, temp_cond_reg
5693 // selnez out_reg, true_reg, TMP
5694 can_move_conditionally = true;
5695 use_const_for_false_in = true;
5696 } else {
5697 // mfc1 TMP, temp_cond_reg
5698 // selnez AT, true_reg, TMP
5699 // seleqz TMP, false_reg, TMP
5700 // or out_reg, AT, TMP
5701 can_move_conditionally = true;
5702 }
5703 } else {
5704 // movt out_reg, true_reg/ZERO, cc
5705 can_move_conditionally = true;
5706 use_const_for_true_in = is_true_value_zero_constant;
5707 }
5708 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005709 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005710 // Moving long on float/double condition.
5711 if (is_r6) {
5712 if (is_true_value_zero_constant) {
5713 // mfc1 TMP, temp_cond_reg
5714 // seleqz out_reg_lo, false_reg_lo, TMP
5715 // seleqz out_reg_hi, false_reg_hi, TMP
5716 can_move_conditionally = true;
5717 use_const_for_true_in = true;
5718 } else if (is_false_value_zero_constant) {
5719 // mfc1 TMP, temp_cond_reg
5720 // selnez out_reg_lo, true_reg_lo, TMP
5721 // selnez out_reg_hi, true_reg_hi, TMP
5722 can_move_conditionally = true;
5723 use_const_for_false_in = true;
5724 }
5725 // Other long conditional moves would generate 6+ instructions,
5726 // which is too many.
5727 } else {
5728 // movt out_reg_lo, true_reg_lo/ZERO, cc
5729 // movt out_reg_hi, true_reg_hi/ZERO, cc
5730 can_move_conditionally = true;
5731 use_const_for_true_in = is_true_value_zero_constant;
5732 }
5733 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 case DataType::Type::kFloat32:
5735 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005736 // Moving float/double on float/double condition.
5737 if (is_r6) {
5738 can_move_conditionally = true;
5739 if (is_true_value_zero_constant) {
5740 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5741 use_const_for_true_in = true;
5742 } else if (is_false_value_zero_constant) {
5743 // selnez.fmt out_reg, true_reg, temp_cond_reg
5744 use_const_for_false_in = true;
5745 } else {
5746 // sel.fmt temp_cond_reg, false_reg, true_reg
5747 // mov.fmt out_reg, temp_cond_reg
5748 }
5749 } else {
5750 // movt.fmt out_reg, true_reg, cc
5751 can_move_conditionally = true;
5752 }
5753 break;
5754 }
5755 break;
5756 }
5757 }
5758
5759 if (can_move_conditionally) {
5760 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5761 } else {
5762 DCHECK(!use_const_for_false_in);
5763 DCHECK(!use_const_for_true_in);
5764 }
5765
5766 if (locations_to_set != nullptr) {
5767 if (use_const_for_false_in) {
5768 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5769 } else {
5770 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005771 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005772 ? Location::RequiresFpuRegister()
5773 : Location::RequiresRegister());
5774 }
5775 if (use_const_for_true_in) {
5776 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5777 } else {
5778 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005780 ? Location::RequiresFpuRegister()
5781 : Location::RequiresRegister());
5782 }
5783 if (materialized) {
5784 locations_to_set->SetInAt(2, Location::RequiresRegister());
5785 }
5786 // On R6 we don't require the output to be the same as the
5787 // first input for conditional moves unlike on R2.
5788 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5789 if (is_out_same_as_first_in) {
5790 locations_to_set->SetOut(Location::SameAsFirstInput());
5791 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005792 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005793 ? Location::RequiresFpuRegister()
5794 : Location::RequiresRegister());
5795 }
5796 }
5797
5798 return can_move_conditionally;
5799}
5800
5801void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5802 LocationSummary* locations = select->GetLocations();
5803 Location dst = locations->Out();
5804 Location src = locations->InAt(1);
5805 Register src_reg = ZERO;
5806 Register src_reg_high = ZERO;
5807 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5808 Register cond_reg = TMP;
5809 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005811 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005813
5814 if (IsBooleanValueOrMaterializedCondition(cond)) {
5815 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5816 } else {
5817 HCondition* condition = cond->AsCondition();
5818 LocationSummary* cond_locations = cond->GetLocations();
5819 IfCondition if_cond = condition->GetCondition();
5820 cond_type = condition->InputAt(0)->GetType();
5821 switch (cond_type) {
5822 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005823 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005824 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5825 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 case DataType::Type::kFloat32:
5827 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005828 cond_inverted = MaterializeFpCompareR2(if_cond,
5829 condition->IsGtBias(),
5830 cond_type,
5831 cond_locations,
5832 cond_cc);
5833 break;
5834 }
5835 }
5836
5837 DCHECK(dst.Equals(locations->InAt(0)));
5838 if (src.IsRegister()) {
5839 src_reg = src.AsRegister<Register>();
5840 } else if (src.IsRegisterPair()) {
5841 src_reg = src.AsRegisterPairLow<Register>();
5842 src_reg_high = src.AsRegisterPairHigh<Register>();
5843 } else if (src.IsConstant()) {
5844 DCHECK(src.GetConstant()->IsZeroBitPattern());
5845 }
5846
5847 switch (cond_type) {
5848 default:
5849 switch (dst_type) {
5850 default:
5851 if (cond_inverted) {
5852 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5853 } else {
5854 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5855 }
5856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005858 if (cond_inverted) {
5859 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5860 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5861 } else {
5862 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5863 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5864 }
5865 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005867 if (cond_inverted) {
5868 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5869 } else {
5870 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5871 }
5872 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005874 if (cond_inverted) {
5875 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5876 } else {
5877 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5878 }
5879 break;
5880 }
5881 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005883 LOG(FATAL) << "Unreachable";
5884 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 case DataType::Type::kFloat32:
5886 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005887 switch (dst_type) {
5888 default:
5889 if (cond_inverted) {
5890 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5891 } else {
5892 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5893 }
5894 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005896 if (cond_inverted) {
5897 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5898 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5899 } else {
5900 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5901 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5902 }
5903 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005904 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005905 if (cond_inverted) {
5906 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5907 } else {
5908 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5909 }
5910 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005912 if (cond_inverted) {
5913 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5914 } else {
5915 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5916 }
5917 break;
5918 }
5919 break;
5920 }
5921}
5922
5923void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5924 LocationSummary* locations = select->GetLocations();
5925 Location dst = locations->Out();
5926 Location false_src = locations->InAt(0);
5927 Location true_src = locations->InAt(1);
5928 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5929 Register cond_reg = TMP;
5930 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005932 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005933 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005934
5935 if (IsBooleanValueOrMaterializedCondition(cond)) {
5936 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5937 } else {
5938 HCondition* condition = cond->AsCondition();
5939 LocationSummary* cond_locations = cond->GetLocations();
5940 IfCondition if_cond = condition->GetCondition();
5941 cond_type = condition->InputAt(0)->GetType();
5942 switch (cond_type) {
5943 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005945 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5946 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 case DataType::Type::kFloat32:
5948 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005949 cond_inverted = MaterializeFpCompareR6(if_cond,
5950 condition->IsGtBias(),
5951 cond_type,
5952 cond_locations,
5953 fcond_reg);
5954 break;
5955 }
5956 }
5957
5958 if (true_src.IsConstant()) {
5959 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5960 }
5961 if (false_src.IsConstant()) {
5962 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5963 }
5964
5965 switch (dst_type) {
5966 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005968 __ Mfc1(cond_reg, fcond_reg);
5969 }
5970 if (true_src.IsConstant()) {
5971 if (cond_inverted) {
5972 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5973 } else {
5974 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5975 }
5976 } else if (false_src.IsConstant()) {
5977 if (cond_inverted) {
5978 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5979 } else {
5980 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5981 }
5982 } else {
5983 DCHECK_NE(cond_reg, AT);
5984 if (cond_inverted) {
5985 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5986 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5987 } else {
5988 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5989 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5990 }
5991 __ Or(dst.AsRegister<Register>(), AT, TMP);
5992 }
5993 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kInt64: {
5995 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005996 __ Mfc1(cond_reg, fcond_reg);
5997 }
5998 Register dst_lo = dst.AsRegisterPairLow<Register>();
5999 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6000 if (true_src.IsConstant()) {
6001 Register src_lo = false_src.AsRegisterPairLow<Register>();
6002 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6003 if (cond_inverted) {
6004 __ Selnez(dst_lo, src_lo, cond_reg);
6005 __ Selnez(dst_hi, src_hi, cond_reg);
6006 } else {
6007 __ Seleqz(dst_lo, src_lo, cond_reg);
6008 __ Seleqz(dst_hi, src_hi, cond_reg);
6009 }
6010 } else {
6011 DCHECK(false_src.IsConstant());
6012 Register src_lo = true_src.AsRegisterPairLow<Register>();
6013 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6014 if (cond_inverted) {
6015 __ Seleqz(dst_lo, src_lo, cond_reg);
6016 __ Seleqz(dst_hi, src_hi, cond_reg);
6017 } else {
6018 __ Selnez(dst_lo, src_lo, cond_reg);
6019 __ Selnez(dst_hi, src_hi, cond_reg);
6020 }
6021 }
6022 break;
6023 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 case DataType::Type::kFloat32: {
6025 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006026 // sel*.fmt tests bit 0 of the condition register, account for that.
6027 __ Sltu(TMP, ZERO, cond_reg);
6028 __ Mtc1(TMP, fcond_reg);
6029 }
6030 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6031 if (true_src.IsConstant()) {
6032 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6033 if (cond_inverted) {
6034 __ SelnezS(dst_reg, src_reg, fcond_reg);
6035 } else {
6036 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6037 }
6038 } else if (false_src.IsConstant()) {
6039 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6040 if (cond_inverted) {
6041 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6042 } else {
6043 __ SelnezS(dst_reg, src_reg, fcond_reg);
6044 }
6045 } else {
6046 if (cond_inverted) {
6047 __ SelS(fcond_reg,
6048 true_src.AsFpuRegister<FRegister>(),
6049 false_src.AsFpuRegister<FRegister>());
6050 } else {
6051 __ SelS(fcond_reg,
6052 false_src.AsFpuRegister<FRegister>(),
6053 true_src.AsFpuRegister<FRegister>());
6054 }
6055 __ MovS(dst_reg, fcond_reg);
6056 }
6057 break;
6058 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006059 case DataType::Type::kFloat64: {
6060 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006061 // sel*.fmt tests bit 0 of the condition register, account for that.
6062 __ Sltu(TMP, ZERO, cond_reg);
6063 __ Mtc1(TMP, fcond_reg);
6064 }
6065 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6066 if (true_src.IsConstant()) {
6067 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6068 if (cond_inverted) {
6069 __ SelnezD(dst_reg, src_reg, fcond_reg);
6070 } else {
6071 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6072 }
6073 } else if (false_src.IsConstant()) {
6074 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6075 if (cond_inverted) {
6076 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6077 } else {
6078 __ SelnezD(dst_reg, src_reg, fcond_reg);
6079 }
6080 } else {
6081 if (cond_inverted) {
6082 __ SelD(fcond_reg,
6083 true_src.AsFpuRegister<FRegister>(),
6084 false_src.AsFpuRegister<FRegister>());
6085 } else {
6086 __ SelD(fcond_reg,
6087 false_src.AsFpuRegister<FRegister>(),
6088 true_src.AsFpuRegister<FRegister>());
6089 }
6090 __ MovD(dst_reg, fcond_reg);
6091 }
6092 break;
6093 }
6094 }
6095}
6096
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006097void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006098 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006099 LocationSummary(flag, LocationSummary::kNoCall);
6100 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006101}
6102
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006103void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6104 __ LoadFromOffset(kLoadWord,
6105 flag->GetLocations()->Out().AsRegister<Register>(),
6106 SP,
6107 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006108}
6109
David Brazdil74eb1b22015-12-14 11:44:01 +00006110void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006111 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006112 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006113}
6114
6115void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006116 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6117 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6118 if (is_r6) {
6119 GenConditionalMoveR6(select);
6120 } else {
6121 GenConditionalMoveR2(select);
6122 }
6123 } else {
6124 LocationSummary* locations = select->GetLocations();
6125 MipsLabel false_target;
6126 GenerateTestAndBranch(select,
6127 /* condition_input_index */ 2,
6128 /* true_target */ nullptr,
6129 &false_target);
6130 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6131 __ Bind(&false_target);
6132 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006133}
6134
David Srbecky0cf44932015-12-09 14:09:59 +00006135void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006136 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006137}
6138
David Srbeckyd28f4a02016-03-14 17:14:24 +00006139void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6140 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006141}
6142
6143void CodeGeneratorMIPS::GenerateNop() {
6144 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006145}
6146
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006147void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006148 DataType::Type field_type = field_info.GetFieldType();
6149 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006150 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006151 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006153 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006154 instruction,
6155 generate_volatile
6156 ? LocationSummary::kCallOnMainOnly
6157 : (object_field_get_with_read_barrier
6158 ? LocationSummary::kCallOnSlowPath
6159 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006160
Alexey Frunzec61c0762017-04-10 13:54:23 -07006161 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6162 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6163 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006164 locations->SetInAt(0, Location::RequiresRegister());
6165 if (generate_volatile) {
6166 InvokeRuntimeCallingConvention calling_convention;
6167 // need A0 to hold base + offset
6168 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006169 if (field_type == DataType::Type::kInt64) {
6170 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006171 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006172 // Use Location::Any() to prevent situations when running out of available fp registers.
6173 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006174 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006176 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6177 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6178 }
6179 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006180 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006181 locations->SetOut(Location::RequiresFpuRegister());
6182 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006183 // The output overlaps in the case of an object field get with
6184 // read barriers enabled: we do not want the move to overwrite the
6185 // object's location, as we need it to emit the read barrier.
6186 locations->SetOut(Location::RequiresRegister(),
6187 object_field_get_with_read_barrier
6188 ? Location::kOutputOverlap
6189 : Location::kNoOutputOverlap);
6190 }
6191 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6192 // We need a temporary register for the read barrier marking slow
6193 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006194 if (!kBakerReadBarrierThunksEnableForFields) {
6195 locations->AddTemp(Location::RequiresRegister());
6196 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006197 }
6198 }
6199}
6200
6201void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6202 const FieldInfo& field_info,
6203 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006204 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006205 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006206 Location obj_loc = locations->InAt(0);
6207 Register obj = obj_loc.AsRegister<Register>();
6208 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006209 LoadOperandType load_type = kLoadUnsignedByte;
6210 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006211 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006212 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006213
6214 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006216 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006217 load_type = kLoadUnsignedByte;
6218 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006219 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006220 load_type = kLoadSignedByte;
6221 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006222 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006223 load_type = kLoadUnsignedHalfword;
6224 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006225 case DataType::Type::kInt16:
6226 load_type = kLoadSignedHalfword;
6227 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006228 case DataType::Type::kInt32:
6229 case DataType::Type::kFloat32:
6230 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006231 load_type = kLoadWord;
6232 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kInt64:
6234 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 load_type = kLoadDoubleword;
6236 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006238 LOG(FATAL) << "Unreachable type " << type;
6239 UNREACHABLE();
6240 }
6241
6242 if (is_volatile && load_type == kLoadDoubleword) {
6243 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006244 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006245 // Do implicit Null check
6246 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6247 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006248 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006250 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006251 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006252 if (dst_loc.IsFpuRegister()) {
6253 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006254 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006255 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006256 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006257 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006258 __ StoreToOffset(kStoreWord,
6259 locations->GetTemp(1).AsRegister<Register>(),
6260 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006261 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006262 __ StoreToOffset(kStoreWord,
6263 locations->GetTemp(2).AsRegister<Register>(),
6264 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006265 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006266 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006267 }
6268 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006270 // /* HeapReference<Object> */ dst = *(obj + offset)
6271 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006272 Location temp_loc =
6273 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006274 // Note that a potential implicit null check is handled in this
6275 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6276 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6277 dst_loc,
6278 obj,
6279 offset,
6280 temp_loc,
6281 /* needs_null_check */ true);
6282 if (is_volatile) {
6283 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6284 }
6285 } else {
6286 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6287 if (is_volatile) {
6288 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6289 }
6290 // If read barriers are enabled, emit read barriers other than
6291 // Baker's using a slow path (and also unpoison the loaded
6292 // reference, if heap poisoning is enabled).
6293 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6294 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006296 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006298 DCHECK(dst_loc.IsRegisterPair());
6299 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006300 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006301 DCHECK(dst_loc.IsRegister());
6302 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006304 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006305 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006306 DCHECK(dst_loc.IsFpuRegister());
6307 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006308 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006309 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006310 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006311 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006312 }
6313 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314 }
6315
Alexey Frunze15958152017-02-09 19:08:30 -08006316 // Memory barriers, in the case of references, are handled in the
6317 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006319 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6320 }
6321}
6322
6323void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006324 DataType::Type field_type = field_info.GetFieldType();
6325 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006326 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006327 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006328 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329
6330 locations->SetInAt(0, Location::RequiresRegister());
6331 if (generate_volatile) {
6332 InvokeRuntimeCallingConvention calling_convention;
6333 // need A0 to hold base + offset
6334 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006336 locations->SetInAt(1, Location::RegisterPairLocation(
6337 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6338 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006339 // Use Location::Any() to prevent situations when running out of available fp registers.
6340 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006341 // Pass FP parameters in core registers.
6342 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6343 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6344 }
6345 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006347 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006348 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006349 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006350 }
6351 }
6352}
6353
6354void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6355 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006356 uint32_t dex_pc,
6357 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006358 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006359 LocationSummary* locations = instruction->GetLocations();
6360 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006361 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006362 StoreOperandType store_type = kStoreByte;
6363 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006364 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006365 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006366 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006367
6368 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006370 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006371 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006372 store_type = kStoreByte;
6373 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006375 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 store_type = kStoreHalfword;
6377 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006378 case DataType::Type::kInt32:
6379 case DataType::Type::kFloat32:
6380 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 store_type = kStoreWord;
6382 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 case DataType::Type::kInt64:
6384 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 store_type = kStoreDoubleword;
6386 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006388 LOG(FATAL) << "Unreachable type " << type;
6389 UNREACHABLE();
6390 }
6391
6392 if (is_volatile) {
6393 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6394 }
6395
6396 if (is_volatile && store_type == kStoreDoubleword) {
6397 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006398 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006399 // Do implicit Null check.
6400 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6401 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006404 if (value_location.IsFpuRegister()) {
6405 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6406 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006407 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006408 value_location.AsFpuRegister<FRegister>());
6409 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006410 __ LoadFromOffset(kLoadWord,
6411 locations->GetTemp(1).AsRegister<Register>(),
6412 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006413 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006414 __ LoadFromOffset(kLoadWord,
6415 locations->GetTemp(2).AsRegister<Register>(),
6416 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006417 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006418 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006419 DCHECK(value_location.IsConstant());
6420 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6421 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006422 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6423 locations->GetTemp(1).AsRegister<Register>(),
6424 value);
6425 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006426 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006427 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006428 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6429 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006430 if (value_location.IsConstant()) {
6431 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6432 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006433 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006434 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006435 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006436 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006438 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006439 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006440 if (kPoisonHeapReferences && needs_write_barrier) {
6441 // Note that in the case where `value` is a null reference,
6442 // we do not enter this block, as a null reference does not
6443 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006444 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006445 __ PoisonHeapReference(TMP, src);
6446 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6447 } else {
6448 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006451 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006452 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006453 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006454 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006455 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006456 }
6457 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006458 }
6459
Alexey Frunzec061de12017-02-14 13:27:23 -08006460 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006461 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006462 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006463 }
6464
6465 if (is_volatile) {
6466 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6467 }
6468}
6469
6470void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6471 HandleFieldGet(instruction, instruction->GetFieldInfo());
6472}
6473
6474void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6475 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6476}
6477
6478void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6479 HandleFieldSet(instruction, instruction->GetFieldInfo());
6480}
6481
6482void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006483 HandleFieldSet(instruction,
6484 instruction->GetFieldInfo(),
6485 instruction->GetDexPc(),
6486 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006487}
6488
Alexey Frunze15958152017-02-09 19:08:30 -08006489void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6490 HInstruction* instruction,
6491 Location out,
6492 uint32_t offset,
6493 Location maybe_temp,
6494 ReadBarrierOption read_barrier_option) {
6495 Register out_reg = out.AsRegister<Register>();
6496 if (read_barrier_option == kWithReadBarrier) {
6497 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006498 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6499 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6500 }
Alexey Frunze15958152017-02-09 19:08:30 -08006501 if (kUseBakerReadBarrier) {
6502 // Load with fast path based Baker's read barrier.
6503 // /* HeapReference<Object> */ out = *(out + offset)
6504 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6505 out,
6506 out_reg,
6507 offset,
6508 maybe_temp,
6509 /* needs_null_check */ false);
6510 } else {
6511 // Load with slow path based read barrier.
6512 // Save the value of `out` into `maybe_temp` before overwriting it
6513 // in the following move operation, as we will need it for the
6514 // read barrier below.
6515 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6516 // /* HeapReference<Object> */ out = *(out + offset)
6517 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6518 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6519 }
6520 } else {
6521 // Plain load with no read barrier.
6522 // /* HeapReference<Object> */ out = *(out + offset)
6523 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6524 __ MaybeUnpoisonHeapReference(out_reg);
6525 }
6526}
6527
6528void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6529 HInstruction* instruction,
6530 Location out,
6531 Location obj,
6532 uint32_t offset,
6533 Location maybe_temp,
6534 ReadBarrierOption read_barrier_option) {
6535 Register out_reg = out.AsRegister<Register>();
6536 Register obj_reg = obj.AsRegister<Register>();
6537 if (read_barrier_option == kWithReadBarrier) {
6538 CHECK(kEmitCompilerReadBarrier);
6539 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006540 if (!kBakerReadBarrierThunksEnableForFields) {
6541 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6542 }
Alexey Frunze15958152017-02-09 19:08:30 -08006543 // Load with fast path based Baker's read barrier.
6544 // /* HeapReference<Object> */ out = *(obj + offset)
6545 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6546 out,
6547 obj_reg,
6548 offset,
6549 maybe_temp,
6550 /* needs_null_check */ false);
6551 } else {
6552 // Load with slow path based read barrier.
6553 // /* HeapReference<Object> */ out = *(obj + offset)
6554 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6555 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6556 }
6557 } else {
6558 // Plain load with no read barrier.
6559 // /* HeapReference<Object> */ out = *(obj + offset)
6560 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6561 __ MaybeUnpoisonHeapReference(out_reg);
6562 }
6563}
6564
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006565static inline int GetBakerMarkThunkNumber(Register reg) {
6566 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6567 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6568 return reg - V0;
6569 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6570 return 14 + (reg - S2);
6571 } else if (reg == FP) { // One more.
6572 return 20;
6573 }
6574 LOG(FATAL) << "Unexpected register " << reg;
6575 UNREACHABLE();
6576}
6577
6578static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6579 int num = GetBakerMarkThunkNumber(reg) +
6580 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6581 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6582}
6583
6584static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6585 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6586 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6587}
6588
Alexey Frunze15958152017-02-09 19:08:30 -08006589void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6590 Location root,
6591 Register obj,
6592 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006593 ReadBarrierOption read_barrier_option,
6594 MipsLabel* label_low) {
6595 bool reordering;
6596 if (label_low != nullptr) {
6597 DCHECK_EQ(offset, 0x5678u);
6598 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006599 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006600 if (read_barrier_option == kWithReadBarrier) {
6601 DCHECK(kEmitCompilerReadBarrier);
6602 if (kUseBakerReadBarrier) {
6603 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6604 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006605 if (kBakerReadBarrierThunksEnableForGcRoots) {
6606 // Note that we do not actually check the value of `GetIsGcMarking()`
6607 // to decide whether to mark the loaded GC root or not. Instead, we
6608 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6609 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6610 // vice versa.
6611 //
6612 // We use thunks for the slow path. That thunk checks the reference
6613 // and jumps to the entrypoint if needed.
6614 //
6615 // temp = Thread::Current()->pReadBarrierMarkReg00
6616 // // AKA &art_quick_read_barrier_mark_introspection.
6617 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6618 // if (temp != nullptr) {
6619 // temp = &gc_root_thunk<root_reg>
6620 // root = temp(root)
6621 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006622
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006623 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6624 const int32_t entry_point_offset =
6625 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6626 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6627 int16_t offset_low = Low16Bits(offset);
6628 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6629 // extension in lw.
6630 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6631 Register base = short_offset ? obj : TMP;
6632 // Loading the entrypoint does not require a load acquire since it is only changed when
6633 // threads are suspended or running a checkpoint.
6634 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6635 reordering = __ SetReorder(false);
6636 if (!short_offset) {
6637 DCHECK(!label_low);
6638 __ AddUpper(base, obj, offset_high);
6639 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006640 MipsLabel skip_call;
6641 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006642 if (label_low != nullptr) {
6643 DCHECK(short_offset);
6644 __ Bind(label_low);
6645 }
6646 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6647 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6648 // in delay slot.
6649 if (isR6) {
6650 __ Jialc(T9, thunk_disp);
6651 } else {
6652 __ Addiu(T9, T9, thunk_disp);
6653 __ Jalr(T9);
6654 __ Nop();
6655 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006656 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006657 __ SetReorder(reordering);
6658 } else {
6659 // Note that we do not actually check the value of `GetIsGcMarking()`
6660 // to decide whether to mark the loaded GC root or not. Instead, we
6661 // load into `temp` (T9) the read barrier mark entry point corresponding
6662 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6663 // is false, and vice versa.
6664 //
6665 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6666 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6667 // if (temp != null) {
6668 // root = temp(root)
6669 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006670
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006671 if (label_low != nullptr) {
6672 reordering = __ SetReorder(false);
6673 __ Bind(label_low);
6674 }
6675 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6676 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6677 if (label_low != nullptr) {
6678 __ SetReorder(reordering);
6679 }
6680 static_assert(
6681 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6682 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6683 "have different sizes.");
6684 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6685 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6686 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006687
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006688 // Slow path marking the GC root `root`.
6689 Location temp = Location::RegisterLocation(T9);
6690 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006691 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006692 instruction,
6693 root,
6694 /*entrypoint*/ temp);
6695 codegen_->AddSlowPath(slow_path);
6696
6697 const int32_t entry_point_offset =
6698 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6699 // Loading the entrypoint does not require a load acquire since it is only changed when
6700 // threads are suspended or running a checkpoint.
6701 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6702 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6703 __ Bind(slow_path->GetExitLabel());
6704 }
Alexey Frunze15958152017-02-09 19:08:30 -08006705 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006706 if (label_low != nullptr) {
6707 reordering = __ SetReorder(false);
6708 __ Bind(label_low);
6709 }
Alexey Frunze15958152017-02-09 19:08:30 -08006710 // GC root loaded through a slow path for read barriers other
6711 // than Baker's.
6712 // /* GcRoot<mirror::Object>* */ root = obj + offset
6713 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006714 if (label_low != nullptr) {
6715 __ SetReorder(reordering);
6716 }
Alexey Frunze15958152017-02-09 19:08:30 -08006717 // /* mirror::Object* */ root = root->Read()
6718 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6719 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006720 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006721 if (label_low != nullptr) {
6722 reordering = __ SetReorder(false);
6723 __ Bind(label_low);
6724 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006725 // Plain GC root load with no read barrier.
6726 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6727 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6728 // Note that GC roots are not affected by heap poisoning, thus we
6729 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006730 if (label_low != nullptr) {
6731 __ SetReorder(reordering);
6732 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006733 }
6734}
6735
Alexey Frunze15958152017-02-09 19:08:30 -08006736void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6737 Location ref,
6738 Register obj,
6739 uint32_t offset,
6740 Location temp,
6741 bool needs_null_check) {
6742 DCHECK(kEmitCompilerReadBarrier);
6743 DCHECK(kUseBakerReadBarrier);
6744
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006745 if (kBakerReadBarrierThunksEnableForFields) {
6746 // Note that we do not actually check the value of `GetIsGcMarking()`
6747 // to decide whether to mark the loaded reference or not. Instead, we
6748 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6749 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6750 // vice versa.
6751 //
6752 // We use thunks for the slow path. That thunk checks the reference
6753 // and jumps to the entrypoint if needed. If the holder is not gray,
6754 // it issues a load-load memory barrier and returns to the original
6755 // reference load.
6756 //
6757 // temp = Thread::Current()->pReadBarrierMarkReg00
6758 // // AKA &art_quick_read_barrier_mark_introspection.
6759 // if (temp != nullptr) {
6760 // temp = &field_array_thunk<holder_reg>
6761 // temp()
6762 // }
6763 // not_gray_return_address:
6764 // // If the offset is too large to fit into the lw instruction, we
6765 // // use an adjusted base register (TMP) here. This register
6766 // // receives bits 16 ... 31 of the offset before the thunk invocation
6767 // // and the thunk benefits from it.
6768 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6769 // gray_return_address:
6770
6771 DCHECK(temp.IsInvalid());
6772 bool isR6 = GetInstructionSetFeatures().IsR6();
6773 int16_t offset_low = Low16Bits(offset);
6774 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6775 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6776 bool reordering = __ SetReorder(false);
6777 const int32_t entry_point_offset =
6778 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6779 // There may have or may have not been a null check if the field offset is smaller than
6780 // the page size.
6781 // There must've been a null check in case it's actually a load from an array.
6782 // We will, however, perform an explicit null check in the thunk as it's easier to
6783 // do it than not.
6784 if (instruction->IsArrayGet()) {
6785 DCHECK(!needs_null_check);
6786 }
6787 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6788 // Loading the entrypoint does not require a load acquire since it is only changed when
6789 // threads are suspended or running a checkpoint.
6790 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6791 Register ref_reg = ref.AsRegister<Register>();
6792 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006793 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006794 if (short_offset) {
6795 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006796 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006797 __ Nop(); // In forbidden slot.
6798 __ Jialc(T9, thunk_disp);
6799 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006800 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006801 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6802 __ Jalr(T9);
6803 __ Nop(); // In delay slot.
6804 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006805 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006806 } else {
6807 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006808 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006809 __ Aui(base, obj, offset_high); // In delay slot.
6810 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006811 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006812 } else {
6813 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006814 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006815 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6816 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006817 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006818 __ Addu(base, base, obj); // In delay slot.
6819 }
6820 }
6821 // /* HeapReference<Object> */ ref = *(obj + offset)
6822 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6823 if (needs_null_check) {
6824 MaybeRecordImplicitNullCheck(instruction);
6825 }
6826 __ MaybeUnpoisonHeapReference(ref_reg);
6827 __ SetReorder(reordering);
6828 return;
6829 }
6830
Alexey Frunze15958152017-02-09 19:08:30 -08006831 // /* HeapReference<Object> */ ref = *(obj + offset)
6832 Location no_index = Location::NoLocation();
6833 ScaleFactor no_scale_factor = TIMES_1;
6834 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6835 ref,
6836 obj,
6837 offset,
6838 no_index,
6839 no_scale_factor,
6840 temp,
6841 needs_null_check);
6842}
6843
6844void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6845 Location ref,
6846 Register obj,
6847 uint32_t data_offset,
6848 Location index,
6849 Location temp,
6850 bool needs_null_check) {
6851 DCHECK(kEmitCompilerReadBarrier);
6852 DCHECK(kUseBakerReadBarrier);
6853
6854 static_assert(
6855 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6856 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006857 ScaleFactor scale_factor = TIMES_4;
6858
6859 if (kBakerReadBarrierThunksEnableForArrays) {
6860 // Note that we do not actually check the value of `GetIsGcMarking()`
6861 // to decide whether to mark the loaded reference or not. Instead, we
6862 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6863 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6864 // vice versa.
6865 //
6866 // We use thunks for the slow path. That thunk checks the reference
6867 // and jumps to the entrypoint if needed. If the holder is not gray,
6868 // it issues a load-load memory barrier and returns to the original
6869 // reference load.
6870 //
6871 // temp = Thread::Current()->pReadBarrierMarkReg00
6872 // // AKA &art_quick_read_barrier_mark_introspection.
6873 // if (temp != nullptr) {
6874 // temp = &field_array_thunk<holder_reg>
6875 // temp()
6876 // }
6877 // not_gray_return_address:
6878 // // The element address is pre-calculated in the TMP register before the
6879 // // thunk invocation and the thunk benefits from it.
6880 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6881 // gray_return_address:
6882
6883 DCHECK(temp.IsInvalid());
6884 DCHECK(index.IsValid());
6885 bool reordering = __ SetReorder(false);
6886 const int32_t entry_point_offset =
6887 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6888 // We will not do the explicit null check in the thunk as some form of a null check
6889 // must've been done earlier.
6890 DCHECK(!needs_null_check);
6891 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6892 // Loading the entrypoint does not require a load acquire since it is only changed when
6893 // threads are suspended or running a checkpoint.
6894 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6895 Register ref_reg = ref.AsRegister<Register>();
6896 Register index_reg = index.IsRegisterPair()
6897 ? index.AsRegisterPairLow<Register>()
6898 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006899 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006900 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006901 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006902 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6903 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006904 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006905 } else {
6906 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006907 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006908 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6909 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006910 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006911 __ Addu(TMP, TMP, obj); // In delay slot.
6912 }
6913 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6914 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6915 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6916 __ MaybeUnpoisonHeapReference(ref_reg);
6917 __ SetReorder(reordering);
6918 return;
6919 }
6920
Alexey Frunze15958152017-02-09 19:08:30 -08006921 // /* HeapReference<Object> */ ref =
6922 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006923 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6924 ref,
6925 obj,
6926 data_offset,
6927 index,
6928 scale_factor,
6929 temp,
6930 needs_null_check);
6931}
6932
6933void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6934 Location ref,
6935 Register obj,
6936 uint32_t offset,
6937 Location index,
6938 ScaleFactor scale_factor,
6939 Location temp,
6940 bool needs_null_check,
6941 bool always_update_field) {
6942 DCHECK(kEmitCompilerReadBarrier);
6943 DCHECK(kUseBakerReadBarrier);
6944
6945 // In slow path based read barriers, the read barrier call is
6946 // inserted after the original load. However, in fast path based
6947 // Baker's read barriers, we need to perform the load of
6948 // mirror::Object::monitor_ *before* the original reference load.
6949 // This load-load ordering is required by the read barrier.
6950 // The fast path/slow path (for Baker's algorithm) should look like:
6951 //
6952 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6953 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6954 // HeapReference<Object> ref = *src; // Original reference load.
6955 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6956 // if (is_gray) {
6957 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6958 // }
6959 //
6960 // Note: the original implementation in ReadBarrier::Barrier is
6961 // slightly more complex as it performs additional checks that we do
6962 // not do here for performance reasons.
6963
6964 Register ref_reg = ref.AsRegister<Register>();
6965 Register temp_reg = temp.AsRegister<Register>();
6966 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6967
6968 // /* int32_t */ monitor = obj->monitor_
6969 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6970 if (needs_null_check) {
6971 MaybeRecordImplicitNullCheck(instruction);
6972 }
6973 // /* LockWord */ lock_word = LockWord(monitor)
6974 static_assert(sizeof(LockWord) == sizeof(int32_t),
6975 "art::LockWord and int32_t have different sizes.");
6976
6977 __ Sync(0); // Barrier to prevent load-load reordering.
6978
6979 // The actual reference load.
6980 if (index.IsValid()) {
6981 // Load types involving an "index": ArrayGet,
6982 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6983 // intrinsics.
6984 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6985 if (index.IsConstant()) {
6986 size_t computed_offset =
6987 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6988 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6989 } else {
6990 // Handle the special case of the
6991 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6992 // intrinsics, which use a register pair as index ("long
6993 // offset"), of which only the low part contains data.
6994 Register index_reg = index.IsRegisterPair()
6995 ? index.AsRegisterPairLow<Register>()
6996 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006997 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006998 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6999 }
7000 } else {
7001 // /* HeapReference<Object> */ ref = *(obj + offset)
7002 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7003 }
7004
7005 // Object* ref = ref_addr->AsMirrorPtr()
7006 __ MaybeUnpoisonHeapReference(ref_reg);
7007
7008 // Slow path marking the object `ref` when it is gray.
7009 SlowPathCodeMIPS* slow_path;
7010 if (always_update_field) {
7011 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7012 // of the form `obj + field_offset`, where `obj` is a register and
7013 // `field_offset` is a register pair (of which only the lower half
7014 // is used). Thus `offset` and `scale_factor` above are expected
7015 // to be null in this code path.
7016 DCHECK_EQ(offset, 0u);
7017 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007018 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007019 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7020 ref,
7021 obj,
7022 /* field_offset */ index,
7023 temp_reg);
7024 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007025 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007026 }
7027 AddSlowPath(slow_path);
7028
7029 // if (rb_state == ReadBarrier::GrayState())
7030 // ref = ReadBarrier::Mark(ref);
7031 // Given the numeric representation, it's enough to check the low bit of the
7032 // rb_state. We do that by shifting the bit into the sign bit (31) and
7033 // performing a branch on less than zero.
7034 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7035 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7036 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7037 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7038 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7039 __ Bind(slow_path->GetExitLabel());
7040}
7041
7042void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7043 Location out,
7044 Location ref,
7045 Location obj,
7046 uint32_t offset,
7047 Location index) {
7048 DCHECK(kEmitCompilerReadBarrier);
7049
7050 // Insert a slow path based read barrier *after* the reference load.
7051 //
7052 // If heap poisoning is enabled, the unpoisoning of the loaded
7053 // reference will be carried out by the runtime within the slow
7054 // path.
7055 //
7056 // Note that `ref` currently does not get unpoisoned (when heap
7057 // poisoning is enabled), which is alright as the `ref` argument is
7058 // not used by the artReadBarrierSlow entry point.
7059 //
7060 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007061 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007062 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7063 AddSlowPath(slow_path);
7064
7065 __ B(slow_path->GetEntryLabel());
7066 __ Bind(slow_path->GetExitLabel());
7067}
7068
7069void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7070 Location out,
7071 Location ref,
7072 Location obj,
7073 uint32_t offset,
7074 Location index) {
7075 if (kEmitCompilerReadBarrier) {
7076 // Baker's read barriers shall be handled by the fast path
7077 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7078 DCHECK(!kUseBakerReadBarrier);
7079 // If heap poisoning is enabled, unpoisoning will be taken care of
7080 // by the runtime within the slow path.
7081 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7082 } else if (kPoisonHeapReferences) {
7083 __ UnpoisonHeapReference(out.AsRegister<Register>());
7084 }
7085}
7086
7087void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7088 Location out,
7089 Location root) {
7090 DCHECK(kEmitCompilerReadBarrier);
7091
7092 // Insert a slow path based read barrier *after* the GC root load.
7093 //
7094 // Note that GC roots are not affected by heap poisoning, so we do
7095 // not need to do anything special for this here.
7096 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007097 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007098 AddSlowPath(slow_path);
7099
7100 __ B(slow_path->GetEntryLabel());
7101 __ Bind(slow_path->GetExitLabel());
7102}
7103
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007104void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007105 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7106 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007107 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007108 switch (type_check_kind) {
7109 case TypeCheckKind::kExactCheck:
7110 case TypeCheckKind::kAbstractClassCheck:
7111 case TypeCheckKind::kClassHierarchyCheck:
7112 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007113 call_kind =
7114 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007115 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007116 break;
7117 case TypeCheckKind::kArrayCheck:
7118 case TypeCheckKind::kUnresolvedCheck:
7119 case TypeCheckKind::kInterfaceCheck:
7120 call_kind = LocationSummary::kCallOnSlowPath;
7121 break;
7122 }
7123
Vladimir Markoca6fff82017-10-03 14:49:14 +01007124 LocationSummary* locations =
7125 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007126 if (baker_read_barrier_slow_path) {
7127 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7128 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007129 locations->SetInAt(0, Location::RequiresRegister());
7130 locations->SetInAt(1, Location::RequiresRegister());
7131 // The output does overlap inputs.
7132 // Note that TypeCheckSlowPathMIPS uses this register too.
7133 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007134 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007135}
7136
7137void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007138 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007139 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007140 Location obj_loc = locations->InAt(0);
7141 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007142 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007143 Location out_loc = locations->Out();
7144 Register out = out_loc.AsRegister<Register>();
7145 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7146 DCHECK_LE(num_temps, 1u);
7147 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007148 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7149 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7150 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7151 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007152 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007153 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007154
7155 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007156 // Avoid this check if we know `obj` is not null.
7157 if (instruction->MustDoNullCheck()) {
7158 __ Move(out, ZERO);
7159 __ Beqz(obj, &done);
7160 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007161
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007162 switch (type_check_kind) {
7163 case TypeCheckKind::kExactCheck: {
7164 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007165 GenerateReferenceLoadTwoRegisters(instruction,
7166 out_loc,
7167 obj_loc,
7168 class_offset,
7169 maybe_temp_loc,
7170 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007171 // Classes must be equal for the instanceof to succeed.
7172 __ Xor(out, out, cls);
7173 __ Sltiu(out, out, 1);
7174 break;
7175 }
7176
7177 case TypeCheckKind::kAbstractClassCheck: {
7178 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007179 GenerateReferenceLoadTwoRegisters(instruction,
7180 out_loc,
7181 obj_loc,
7182 class_offset,
7183 maybe_temp_loc,
7184 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007185 // If the class is abstract, we eagerly fetch the super class of the
7186 // object to avoid doing a comparison we know will fail.
7187 MipsLabel loop;
7188 __ Bind(&loop);
7189 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007190 GenerateReferenceLoadOneRegister(instruction,
7191 out_loc,
7192 super_offset,
7193 maybe_temp_loc,
7194 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007195 // If `out` is null, we use it for the result, and jump to `done`.
7196 __ Beqz(out, &done);
7197 __ Bne(out, cls, &loop);
7198 __ LoadConst32(out, 1);
7199 break;
7200 }
7201
7202 case TypeCheckKind::kClassHierarchyCheck: {
7203 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007204 GenerateReferenceLoadTwoRegisters(instruction,
7205 out_loc,
7206 obj_loc,
7207 class_offset,
7208 maybe_temp_loc,
7209 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007210 // Walk over the class hierarchy to find a match.
7211 MipsLabel loop, success;
7212 __ Bind(&loop);
7213 __ Beq(out, cls, &success);
7214 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007215 GenerateReferenceLoadOneRegister(instruction,
7216 out_loc,
7217 super_offset,
7218 maybe_temp_loc,
7219 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007220 __ Bnez(out, &loop);
7221 // If `out` is null, we use it for the result, and jump to `done`.
7222 __ B(&done);
7223 __ Bind(&success);
7224 __ LoadConst32(out, 1);
7225 break;
7226 }
7227
7228 case TypeCheckKind::kArrayObjectCheck: {
7229 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007230 GenerateReferenceLoadTwoRegisters(instruction,
7231 out_loc,
7232 obj_loc,
7233 class_offset,
7234 maybe_temp_loc,
7235 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007236 // Do an exact check.
7237 MipsLabel success;
7238 __ Beq(out, cls, &success);
7239 // Otherwise, we need to check that the object's class is a non-primitive array.
7240 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007241 GenerateReferenceLoadOneRegister(instruction,
7242 out_loc,
7243 component_offset,
7244 maybe_temp_loc,
7245 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007246 // If `out` is null, we use it for the result, and jump to `done`.
7247 __ Beqz(out, &done);
7248 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7249 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7250 __ Sltiu(out, out, 1);
7251 __ B(&done);
7252 __ Bind(&success);
7253 __ LoadConst32(out, 1);
7254 break;
7255 }
7256
7257 case TypeCheckKind::kArrayCheck: {
7258 // No read barrier since the slow path will retry upon failure.
7259 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007260 GenerateReferenceLoadTwoRegisters(instruction,
7261 out_loc,
7262 obj_loc,
7263 class_offset,
7264 maybe_temp_loc,
7265 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007266 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007267 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7268 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007269 codegen_->AddSlowPath(slow_path);
7270 __ Bne(out, cls, slow_path->GetEntryLabel());
7271 __ LoadConst32(out, 1);
7272 break;
7273 }
7274
7275 case TypeCheckKind::kUnresolvedCheck:
7276 case TypeCheckKind::kInterfaceCheck: {
7277 // Note that we indeed only call on slow path, but we always go
7278 // into the slow path for the unresolved and interface check
7279 // cases.
7280 //
7281 // We cannot directly call the InstanceofNonTrivial runtime
7282 // entry point without resorting to a type checking slow path
7283 // here (i.e. by calling InvokeRuntime directly), as it would
7284 // require to assign fixed registers for the inputs of this
7285 // HInstanceOf instruction (following the runtime calling
7286 // convention), which might be cluttered by the potential first
7287 // read barrier emission at the beginning of this method.
7288 //
7289 // TODO: Introduce a new runtime entry point taking the object
7290 // to test (instead of its class) as argument, and let it deal
7291 // with the read barrier issues. This will let us refactor this
7292 // case of the `switch` code as it was previously (with a direct
7293 // call to the runtime not using a type checking slow path).
7294 // This should also be beneficial for the other cases above.
7295 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007296 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7297 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007298 codegen_->AddSlowPath(slow_path);
7299 __ B(slow_path->GetEntryLabel());
7300 break;
7301 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007302 }
7303
7304 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007305
7306 if (slow_path != nullptr) {
7307 __ Bind(slow_path->GetExitLabel());
7308 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007309}
7310
7311void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007312 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007313 locations->SetOut(Location::ConstantLocation(constant));
7314}
7315
7316void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7317 // Will be generated at use site.
7318}
7319
7320void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007321 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007322 locations->SetOut(Location::ConstantLocation(constant));
7323}
7324
7325void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7326 // Will be generated at use site.
7327}
7328
7329void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7330 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7331 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7332}
7333
7334void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7335 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007336 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007337 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007338 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007339}
7340
7341void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7342 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7343 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007344 Location receiver = invoke->GetLocations()->InAt(0);
7345 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007346 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007347
7348 // Set the hidden argument.
7349 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7350 invoke->GetDexMethodIndex());
7351
7352 // temp = object->GetClass();
7353 if (receiver.IsStackSlot()) {
7354 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7355 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7356 } else {
7357 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7358 }
7359 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007360 // Instead of simply (possibly) unpoisoning `temp` here, we should
7361 // emit a read barrier for the previous class reference load.
7362 // However this is not required in practice, as this is an
7363 // intermediate/temporary reference and because the current
7364 // concurrent copying collector keeps the from-space memory
7365 // intact/accessible until the end of the marking phase (the
7366 // concurrent copying collector may not in the future).
7367 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007368 __ LoadFromOffset(kLoadWord, temp, temp,
7369 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7370 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007371 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007372 // temp = temp->GetImtEntryAt(method_offset);
7373 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7374 // T9 = temp->GetEntryPoint();
7375 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7376 // T9();
7377 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007378 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007379 DCHECK(!codegen_->IsLeafMethod());
7380 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7381}
7382
7383void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007384 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7385 if (intrinsic.TryDispatch(invoke)) {
7386 return;
7387 }
7388
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007389 HandleInvoke(invoke);
7390}
7391
7392void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007393 // Explicit clinit checks triggered by static invokes must have been pruned by
7394 // art::PrepareForRegisterAllocation.
7395 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007396
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007397 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007398 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7399 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007400
Chris Larsen701566a2015-10-27 15:29:13 -07007401 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7402 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007403 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7404 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7405 }
Chris Larsen701566a2015-10-27 15:29:13 -07007406 return;
7407 }
7408
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007409 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007410
7411 // Add the extra input register if either the dex cache array base register
7412 // or the PC-relative base register for accessing literals is needed.
7413 if (has_extra_input) {
7414 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7415 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007416}
7417
Orion Hodsonac141392017-01-13 11:53:47 +00007418void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7419 HandleInvoke(invoke);
7420}
7421
7422void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7423 codegen_->GenerateInvokePolymorphicCall(invoke);
7424}
7425
Chris Larsen701566a2015-10-27 15:29:13 -07007426static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007427 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007428 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7429 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007430 return true;
7431 }
7432 return false;
7433}
7434
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007435HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007436 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007437 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007438 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007439 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007440 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007441 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007442 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007443 case HLoadString::LoadKind::kJitTableAddress:
7444 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007445 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007446 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007447 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007448 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007449 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007450 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007451}
7452
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007453HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7454 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007455 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007456 case HLoadClass::LoadKind::kInvalid:
7457 LOG(FATAL) << "UNREACHABLE";
7458 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007459 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007460 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007461 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007462 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007463 case HLoadClass::LoadKind::kBssEntry:
7464 DCHECK(!Runtime::Current()->UseJitCompilation());
7465 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007466 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007467 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007468 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007469 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007470 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007471 break;
7472 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007473 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007474}
7475
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007476Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7477 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007478 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007479 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007480 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7481 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7482 if (!invoke->GetLocations()->Intrinsified()) {
7483 return location.AsRegister<Register>();
7484 }
7485 // For intrinsics we allow any location, so it may be on the stack.
7486 if (!location.IsRegister()) {
7487 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7488 return temp;
7489 }
7490 // For register locations, check if the register was saved. If so, get it from the stack.
7491 // Note: There is a chance that the register was saved but not overwritten, so we could
7492 // save one load. However, since this is just an intrinsic slow path we prefer this
7493 // simple and more robust approach rather that trying to determine if that's the case.
7494 SlowPathCode* slow_path = GetCurrentSlowPath();
7495 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7496 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7497 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7498 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7499 return temp;
7500 }
7501 return location.AsRegister<Register>();
7502}
7503
Vladimir Markodc151b22015-10-15 18:02:30 +01007504HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7505 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007506 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007507 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007508}
7509
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007510void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7511 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007512 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007513 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007514 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7515 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007516 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007517 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7518 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007519 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7520 : ZERO;
7521
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007522 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007523 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007524 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007525 uint32_t offset =
7526 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007527 __ LoadFromOffset(kLoadWord,
7528 temp.AsRegister<Register>(),
7529 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007530 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007531 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007532 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007534 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007535 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007536 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7537 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007538 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7539 PcRelativePatchInfo* info_low =
7540 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007541 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007542 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7543 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007544 break;
7545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007546 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7547 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7548 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007549 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007550 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007551 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007552 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7553 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007554 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007555 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7556 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007557 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007558 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007559 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7560 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7561 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007562 }
7563 }
7564
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007565 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007566 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007567 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007568 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007569 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7570 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007571 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007572 T9,
7573 callee_method.AsRegister<Register>(),
7574 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007575 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007576 // T9()
7577 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007578 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007579 break;
7580 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007581 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7582
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007583 DCHECK(!IsLeafMethod());
7584}
7585
7586void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007587 // Explicit clinit checks triggered by static invokes must have been pruned by
7588 // art::PrepareForRegisterAllocation.
7589 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007590
7591 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7592 return;
7593 }
7594
7595 LocationSummary* locations = invoke->GetLocations();
7596 codegen_->GenerateStaticOrDirectCall(invoke,
7597 locations->HasTemps()
7598 ? locations->GetTemp(0)
7599 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007600}
7601
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007602void CodeGeneratorMIPS::GenerateVirtualCall(
7603 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007604 // Use the calling convention instead of the location of the receiver, as
7605 // intrinsics may have put the receiver in a different register. In the intrinsics
7606 // slow path, the arguments have been moved to the right place, so here we are
7607 // guaranteed that the receiver is the first register of the calling convention.
7608 InvokeDexCallingConvention calling_convention;
7609 Register receiver = calling_convention.GetRegisterAt(0);
7610
Chris Larsen3acee732015-11-18 13:31:08 -08007611 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007612 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7613 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7614 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007615 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007616
7617 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007618 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007619 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007620 // Instead of simply (possibly) unpoisoning `temp` here, we should
7621 // emit a read barrier for the previous class reference load.
7622 // However this is not required in practice, as this is an
7623 // intermediate/temporary reference and because the current
7624 // concurrent copying collector keeps the from-space memory
7625 // intact/accessible until the end of the marking phase (the
7626 // concurrent copying collector may not in the future).
7627 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007628 // temp = temp->GetMethodAt(method_offset);
7629 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7630 // T9 = temp->GetEntryPoint();
7631 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7632 // T9();
7633 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007634 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007635 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007636}
7637
7638void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7639 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7640 return;
7641 }
7642
7643 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007644 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007645}
7646
7647void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007648 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007649 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007650 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007651 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7652 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007653 return;
7654 }
Vladimir Marko41559982017-01-06 14:04:23 +00007655 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007656 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007657 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007658 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7659 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007660 ? LocationSummary::kCallOnSlowPath
7661 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007662 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007663 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7664 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7665 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007666 switch (load_kind) {
7667 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007668 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007669 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007670 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007671 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007672 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007673 break;
7674 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007675 if (has_irreducible_loops) {
7676 codegen_->ClobberRA();
7677 break;
7678 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007679 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007680 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007681 locations->SetInAt(0, Location::RequiresRegister());
7682 break;
7683 default:
7684 break;
7685 }
7686 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007687 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7688 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7689 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007690 // Request a temp to hold the BSS entry location for the slow path.
7691 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007692 RegisterSet caller_saves = RegisterSet::Empty();
7693 InvokeRuntimeCallingConvention calling_convention;
7694 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7695 locations->SetCustomSlowPathCallerSaves(caller_saves);
7696 } else {
7697 // For non-Baker read barriers we have a temp-clobbering call.
7698 }
7699 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007700}
7701
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007702// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7703// move.
7704void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007705 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007706 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007707 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007708 return;
7709 }
Vladimir Marko41559982017-01-06 14:04:23 +00007710 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007711
Vladimir Marko41559982017-01-06 14:04:23 +00007712 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007713 Location out_loc = locations->Out();
7714 Register out = out_loc.AsRegister<Register>();
7715 Register base_or_current_method_reg;
7716 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007717 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007718 switch (load_kind) {
7719 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007720 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007721 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007722 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007723 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007724 base_or_current_method_reg =
7725 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007726 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007728 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007729 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7730 break;
7731 default:
7732 base_or_current_method_reg = ZERO;
7733 break;
7734 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007735
Alexey Frunze15958152017-02-09 19:08:30 -08007736 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7737 ? kWithoutReadBarrier
7738 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007739 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007740 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007741 switch (load_kind) {
7742 case HLoadClass::LoadKind::kReferrersClass: {
7743 DCHECK(!cls->CanCallRuntime());
7744 DCHECK(!cls->MustGenerateClinitCheck());
7745 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7746 GenerateGcRootFieldLoad(cls,
7747 out_loc,
7748 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007749 ArtMethod::DeclaringClassOffset().Int32Value(),
7750 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007751 break;
7752 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007753 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007754 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007755 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007756 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007757 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007758 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7759 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007760 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7761 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007762 base_or_current_method_reg);
7763 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007764 break;
7765 }
7766 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007767 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007768 uint32_t address = dchecked_integral_cast<uint32_t>(
7769 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7770 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007771 if (isR6 || !has_irreducible_loops) {
7772 __ LoadLiteral(out,
7773 base_or_current_method_reg,
7774 codegen_->DeduplicateBootImageAddressLiteral(address));
7775 } else {
7776 __ LoadConst32(out, address);
7777 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007778 break;
7779 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007780 case HLoadClass::LoadKind::kBootImageClassTable: {
7781 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7782 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7783 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7784 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7785 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7786 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7787 out,
7788 base_or_current_method_reg);
7789 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7790 // Extract the reference from the slot data, i.e. clear the hash bits.
7791 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7792 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7793 if (masked_hash != 0) {
7794 __ Addiu(out, out, -masked_hash);
7795 }
7796 break;
7797 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007798 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007799 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7800 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7801 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007802 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007803 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007804 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7805 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007806 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007807 GenerateGcRootFieldLoad(cls,
7808 out_loc,
7809 temp,
7810 /* placeholder */ 0x5678,
7811 read_barrier_option,
7812 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007813 generate_null_check = true;
7814 break;
7815 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007816 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007817 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7818 cls->GetTypeIndex(),
7819 cls->GetClass());
7820 bool reordering = __ SetReorder(false);
7821 __ Bind(&info->high_label);
7822 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007823 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007824 GenerateGcRootFieldLoad(cls,
7825 out_loc,
7826 out,
7827 /* placeholder */ 0x5678,
7828 read_barrier_option,
7829 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007830 break;
7831 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007832 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007833 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007834 LOG(FATAL) << "UNREACHABLE";
7835 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007836 }
7837
7838 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7839 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007840 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007841 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 codegen_->AddSlowPath(slow_path);
7843 if (generate_null_check) {
7844 __ Beqz(out, slow_path->GetEntryLabel());
7845 }
7846 if (cls->MustGenerateClinitCheck()) {
7847 GenerateClassInitializationCheck(slow_path, out);
7848 } else {
7849 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007850 }
7851 }
7852}
7853
7854static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007855 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007856}
7857
7858void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7859 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007860 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007861 locations->SetOut(Location::RequiresRegister());
7862}
7863
7864void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7865 Register out = load->GetLocations()->Out().AsRegister<Register>();
7866 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7867}
7868
7869void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007870 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007871}
7872
7873void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7874 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7875}
7876
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007877void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007878 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007879 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007880 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007881 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007882 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007883 switch (load_kind) {
7884 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 case HLoadString::LoadKind::kBootImageAddress:
7886 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007887 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007888 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007889 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007890 break;
7891 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007892 if (has_irreducible_loops) {
7893 codegen_->ClobberRA();
7894 break;
7895 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007896 FALLTHROUGH_INTENDED;
7897 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007898 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007899 locations->SetInAt(0, Location::RequiresRegister());
7900 break;
7901 default:
7902 break;
7903 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007904 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007905 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007906 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007907 } else {
7908 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007909 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7910 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7911 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007912 // Request a temp to hold the BSS entry location for the slow path.
7913 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007914 RegisterSet caller_saves = RegisterSet::Empty();
7915 InvokeRuntimeCallingConvention calling_convention;
7916 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7917 locations->SetCustomSlowPathCallerSaves(caller_saves);
7918 } else {
7919 // For non-Baker read barriers we have a temp-clobbering call.
7920 }
7921 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007922 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007923}
7924
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007925// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7926// move.
7927void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007928 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007929 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007930 Location out_loc = locations->Out();
7931 Register out = out_loc.AsRegister<Register>();
7932 Register base_or_current_method_reg;
7933 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007934 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007935 switch (load_kind) {
7936 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007937 case HLoadString::LoadKind::kBootImageAddress:
7938 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007939 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007940 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007941 base_or_current_method_reg =
7942 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007943 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007944 default:
7945 base_or_current_method_reg = ZERO;
7946 break;
7947 }
7948
7949 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007950 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007951 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007952 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007953 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007954 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7955 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007956 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7957 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007958 base_or_current_method_reg);
7959 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007960 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007961 }
7962 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007963 uint32_t address = dchecked_integral_cast<uint32_t>(
7964 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7965 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007966 if (isR6 || !has_irreducible_loops) {
7967 __ LoadLiteral(out,
7968 base_or_current_method_reg,
7969 codegen_->DeduplicateBootImageAddressLiteral(address));
7970 } else {
7971 __ LoadConst32(out, address);
7972 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007973 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007974 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007975 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007976 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007977 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007978 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007979 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7980 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007981 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7982 out,
7983 base_or_current_method_reg);
7984 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7985 return;
7986 }
7987 case HLoadString::LoadKind::kBssEntry: {
7988 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7989 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7990 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7991 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7992 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007993 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007994 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007995 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7996 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007997 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007998 GenerateGcRootFieldLoad(load,
7999 out_loc,
8000 temp,
8001 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008002 kCompilerReadBarrierOption,
8003 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008004 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008005 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008006 codegen_->AddSlowPath(slow_path);
8007 __ Beqz(out, slow_path->GetEntryLabel());
8008 __ Bind(slow_path->GetExitLabel());
8009 return;
8010 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008011 case HLoadString::LoadKind::kJitTableAddress: {
8012 CodeGeneratorMIPS::JitPatchInfo* info =
8013 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8014 load->GetStringIndex(),
8015 load->GetString());
8016 bool reordering = __ SetReorder(false);
8017 __ Bind(&info->high_label);
8018 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008019 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008020 GenerateGcRootFieldLoad(load,
8021 out_loc,
8022 out,
8023 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008024 kCompilerReadBarrierOption,
8025 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008026 return;
8027 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008028 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008029 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008030 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008031
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008032 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008033 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008034 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008035 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008036 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008037 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8038 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008039}
8040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008041void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008042 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008043 locations->SetOut(Location::ConstantLocation(constant));
8044}
8045
8046void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8047 // Will be generated at use site.
8048}
8049
8050void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008051 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8052 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008053 InvokeRuntimeCallingConvention calling_convention;
8054 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8055}
8056
8057void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8058 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008059 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008060 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8061 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008062 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008063 }
8064 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8065}
8066
8067void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8068 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008069 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008070 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008071 case DataType::Type::kInt32:
8072 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008073 locations->SetInAt(0, Location::RequiresRegister());
8074 locations->SetInAt(1, Location::RequiresRegister());
8075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8076 break;
8077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008078 case DataType::Type::kFloat32:
8079 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008080 locations->SetInAt(0, Location::RequiresFpuRegister());
8081 locations->SetInAt(1, Location::RequiresFpuRegister());
8082 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8083 break;
8084
8085 default:
8086 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8087 }
8088}
8089
8090void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008091 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008092 LocationSummary* locations = instruction->GetLocations();
8093 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8094
8095 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008096 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008097 Register dst = locations->Out().AsRegister<Register>();
8098 Register lhs = locations->InAt(0).AsRegister<Register>();
8099 Register rhs = locations->InAt(1).AsRegister<Register>();
8100
8101 if (isR6) {
8102 __ MulR6(dst, lhs, rhs);
8103 } else {
8104 __ MulR2(dst, lhs, rhs);
8105 }
8106 break;
8107 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008108 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008109 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8110 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8111 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8112 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8113 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8114 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8115
8116 // Extra checks to protect caused by the existance of A1_A2.
8117 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8118 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8119 DCHECK_NE(dst_high, lhs_low);
8120 DCHECK_NE(dst_high, rhs_low);
8121
8122 // A_B * C_D
8123 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8124 // dst_lo: [ low(B*D) ]
8125 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8126
8127 if (isR6) {
8128 __ MulR6(TMP, lhs_high, rhs_low);
8129 __ MulR6(dst_high, lhs_low, rhs_high);
8130 __ Addu(dst_high, dst_high, TMP);
8131 __ MuhuR6(TMP, lhs_low, rhs_low);
8132 __ Addu(dst_high, dst_high, TMP);
8133 __ MulR6(dst_low, lhs_low, rhs_low);
8134 } else {
8135 __ MulR2(TMP, lhs_high, rhs_low);
8136 __ MulR2(dst_high, lhs_low, rhs_high);
8137 __ Addu(dst_high, dst_high, TMP);
8138 __ MultuR2(lhs_low, rhs_low);
8139 __ Mfhi(TMP);
8140 __ Addu(dst_high, dst_high, TMP);
8141 __ Mflo(dst_low);
8142 }
8143 break;
8144 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008145 case DataType::Type::kFloat32:
8146 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008147 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8148 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8149 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008150 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008151 __ MulS(dst, lhs, rhs);
8152 } else {
8153 __ MulD(dst, lhs, rhs);
8154 }
8155 break;
8156 }
8157 default:
8158 LOG(FATAL) << "Unexpected mul type " << type;
8159 }
8160}
8161
8162void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8163 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008164 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008165 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008166 case DataType::Type::kInt32:
8167 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008168 locations->SetInAt(0, Location::RequiresRegister());
8169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8170 break;
8171
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008172 case DataType::Type::kFloat32:
8173 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008174 locations->SetInAt(0, Location::RequiresFpuRegister());
8175 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8176 break;
8177
8178 default:
8179 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8180 }
8181}
8182
8183void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008184 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008185 LocationSummary* locations = instruction->GetLocations();
8186
8187 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008188 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008189 Register dst = locations->Out().AsRegister<Register>();
8190 Register src = locations->InAt(0).AsRegister<Register>();
8191 __ Subu(dst, ZERO, src);
8192 break;
8193 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008194 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008195 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8196 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8197 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8198 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8199 __ Subu(dst_low, ZERO, src_low);
8200 __ Sltu(TMP, ZERO, dst_low);
8201 __ Subu(dst_high, ZERO, src_high);
8202 __ Subu(dst_high, dst_high, TMP);
8203 break;
8204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008205 case DataType::Type::kFloat32:
8206 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008207 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8208 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008209 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008210 __ NegS(dst, src);
8211 } else {
8212 __ NegD(dst, src);
8213 }
8214 break;
8215 }
8216 default:
8217 LOG(FATAL) << "Unexpected neg type " << type;
8218 }
8219}
8220
8221void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008222 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8223 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008224 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008225 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008226 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8227 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008228}
8229
8230void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008231 // Note: if heap poisoning is enabled, the entry point takes care
8232 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008233 QuickEntrypointEnum entrypoint =
8234 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8235 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008236 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008237 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008238}
8239
8240void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008241 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8242 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008243 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008244 if (instruction->IsStringAlloc()) {
8245 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8246 } else {
8247 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008248 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008249 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008250}
8251
8252void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008253 // Note: if heap poisoning is enabled, the entry point takes care
8254 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008255 if (instruction->IsStringAlloc()) {
8256 // String is allocated through StringFactory. Call NewEmptyString entry point.
8257 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008258 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008259 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8260 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8261 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008262 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008263 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8264 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008265 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008266 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008267 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008268}
8269
8270void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008271 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008272 locations->SetInAt(0, Location::RequiresRegister());
8273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8274}
8275
8276void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008277 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008278 LocationSummary* locations = instruction->GetLocations();
8279
8280 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008281 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008282 Register dst = locations->Out().AsRegister<Register>();
8283 Register src = locations->InAt(0).AsRegister<Register>();
8284 __ Nor(dst, src, ZERO);
8285 break;
8286 }
8287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008288 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008289 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8290 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8291 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8292 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8293 __ Nor(dst_high, src_high, ZERO);
8294 __ Nor(dst_low, src_low, ZERO);
8295 break;
8296 }
8297
8298 default:
8299 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8300 }
8301}
8302
8303void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008304 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008305 locations->SetInAt(0, Location::RequiresRegister());
8306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8307}
8308
8309void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8310 LocationSummary* locations = instruction->GetLocations();
8311 __ Xori(locations->Out().AsRegister<Register>(),
8312 locations->InAt(0).AsRegister<Register>(),
8313 1);
8314}
8315
8316void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008317 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8318 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008319}
8320
Calin Juravle2ae48182016-03-16 14:05:09 +00008321void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8322 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008323 return;
8324 }
8325 Location obj = instruction->GetLocations()->InAt(0);
8326
8327 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008328 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008329}
8330
Calin Juravle2ae48182016-03-16 14:05:09 +00008331void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008332 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008333 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008334
8335 Location obj = instruction->GetLocations()->InAt(0);
8336
8337 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8338}
8339
8340void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008341 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342}
8343
8344void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8345 HandleBinaryOp(instruction);
8346}
8347
8348void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8349 HandleBinaryOp(instruction);
8350}
8351
8352void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8353 LOG(FATAL) << "Unreachable";
8354}
8355
8356void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008357 if (instruction->GetNext()->IsSuspendCheck() &&
8358 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8359 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8360 // The back edge will generate the suspend check.
8361 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8362 }
8363
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008364 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8365}
8366
8367void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008368 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008369 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8370 if (location.IsStackSlot()) {
8371 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8372 } else if (location.IsDoubleStackSlot()) {
8373 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8374 }
8375 locations->SetOut(location);
8376}
8377
8378void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8379 ATTRIBUTE_UNUSED) {
8380 // Nothing to do, the parameter is already at its location.
8381}
8382
8383void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8384 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008385 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008386 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8387}
8388
8389void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8390 ATTRIBUTE_UNUSED) {
8391 // Nothing to do, the method is already at its location.
8392}
8393
8394void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008395 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008396 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008397 locations->SetInAt(i, Location::Any());
8398 }
8399 locations->SetOut(Location::Any());
8400}
8401
8402void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8403 LOG(FATAL) << "Unreachable";
8404}
8405
8406void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008407 DataType::Type type = rem->GetResultType();
8408 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8409 ? LocationSummary::kNoCall
8410 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008411 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008412
8413 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008414 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008415 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008416 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8418 break;
8419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008420 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008421 InvokeRuntimeCallingConvention calling_convention;
8422 locations->SetInAt(0, Location::RegisterPairLocation(
8423 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8424 locations->SetInAt(1, Location::RegisterPairLocation(
8425 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8426 locations->SetOut(calling_convention.GetReturnLocation(type));
8427 break;
8428 }
8429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008430 case DataType::Type::kFloat32:
8431 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008432 InvokeRuntimeCallingConvention calling_convention;
8433 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8434 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8435 locations->SetOut(calling_convention.GetReturnLocation(type));
8436 break;
8437 }
8438
8439 default:
8440 LOG(FATAL) << "Unexpected rem type " << type;
8441 }
8442}
8443
8444void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008445 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008446
8447 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008448 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008449 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008450 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008451 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008452 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008453 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8454 break;
8455 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008456 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008457 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008458 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008459 break;
8460 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008461 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008462 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008463 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008464 break;
8465 }
8466 default:
8467 LOG(FATAL) << "Unexpected rem type " << type;
8468 }
8469}
8470
Igor Murashkind01745e2017-04-05 16:40:31 -07008471void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8472 constructor_fence->SetLocations(nullptr);
8473}
8474
8475void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8476 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8477 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8478}
8479
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008480void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8481 memory_barrier->SetLocations(nullptr);
8482}
8483
8484void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8485 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8486}
8487
8488void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008489 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008490 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491 locations->SetInAt(0, MipsReturnLocation(return_type));
8492}
8493
8494void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8495 codegen_->GenerateFrameExit();
8496}
8497
8498void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8499 ret->SetLocations(nullptr);
8500}
8501
8502void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8503 codegen_->GenerateFrameExit();
8504}
8505
Alexey Frunze92d90602015-12-18 18:16:36 -08008506void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8507 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008508}
8509
Alexey Frunze92d90602015-12-18 18:16:36 -08008510void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8511 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008512}
8513
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008514void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8515 HandleShift(shl);
8516}
8517
8518void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8519 HandleShift(shl);
8520}
8521
8522void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8523 HandleShift(shr);
8524}
8525
8526void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8527 HandleShift(shr);
8528}
8529
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008530void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8531 HandleBinaryOp(instruction);
8532}
8533
8534void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8535 HandleBinaryOp(instruction);
8536}
8537
8538void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8539 HandleFieldGet(instruction, instruction->GetFieldInfo());
8540}
8541
8542void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8543 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8544}
8545
8546void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8547 HandleFieldSet(instruction, instruction->GetFieldInfo());
8548}
8549
8550void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008551 HandleFieldSet(instruction,
8552 instruction->GetFieldInfo(),
8553 instruction->GetDexPc(),
8554 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008555}
8556
8557void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8558 HUnresolvedInstanceFieldGet* instruction) {
8559 FieldAccessCallingConventionMIPS calling_convention;
8560 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8561 instruction->GetFieldType(),
8562 calling_convention);
8563}
8564
8565void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8566 HUnresolvedInstanceFieldGet* instruction) {
8567 FieldAccessCallingConventionMIPS calling_convention;
8568 codegen_->GenerateUnresolvedFieldAccess(instruction,
8569 instruction->GetFieldType(),
8570 instruction->GetFieldIndex(),
8571 instruction->GetDexPc(),
8572 calling_convention);
8573}
8574
8575void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8576 HUnresolvedInstanceFieldSet* instruction) {
8577 FieldAccessCallingConventionMIPS calling_convention;
8578 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8579 instruction->GetFieldType(),
8580 calling_convention);
8581}
8582
8583void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8584 HUnresolvedInstanceFieldSet* instruction) {
8585 FieldAccessCallingConventionMIPS calling_convention;
8586 codegen_->GenerateUnresolvedFieldAccess(instruction,
8587 instruction->GetFieldType(),
8588 instruction->GetFieldIndex(),
8589 instruction->GetDexPc(),
8590 calling_convention);
8591}
8592
8593void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8594 HUnresolvedStaticFieldGet* instruction) {
8595 FieldAccessCallingConventionMIPS calling_convention;
8596 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8597 instruction->GetFieldType(),
8598 calling_convention);
8599}
8600
8601void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8602 HUnresolvedStaticFieldGet* instruction) {
8603 FieldAccessCallingConventionMIPS calling_convention;
8604 codegen_->GenerateUnresolvedFieldAccess(instruction,
8605 instruction->GetFieldType(),
8606 instruction->GetFieldIndex(),
8607 instruction->GetDexPc(),
8608 calling_convention);
8609}
8610
8611void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8612 HUnresolvedStaticFieldSet* instruction) {
8613 FieldAccessCallingConventionMIPS calling_convention;
8614 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8615 instruction->GetFieldType(),
8616 calling_convention);
8617}
8618
8619void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8620 HUnresolvedStaticFieldSet* instruction) {
8621 FieldAccessCallingConventionMIPS calling_convention;
8622 codegen_->GenerateUnresolvedFieldAccess(instruction,
8623 instruction->GetFieldType(),
8624 instruction->GetFieldIndex(),
8625 instruction->GetDexPc(),
8626 calling_convention);
8627}
8628
8629void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008630 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8631 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008632 // In suspend check slow path, usually there are no caller-save registers at all.
8633 // If SIMD instructions are present, however, we force spilling all live SIMD
8634 // registers in full width (since the runtime only saves/restores lower part).
8635 locations->SetCustomSlowPathCallerSaves(
8636 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008637}
8638
8639void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8640 HBasicBlock* block = instruction->GetBlock();
8641 if (block->GetLoopInformation() != nullptr) {
8642 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8643 // The back edge will generate the suspend check.
8644 return;
8645 }
8646 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8647 // The goto will generate the suspend check.
8648 return;
8649 }
8650 GenerateSuspendCheck(instruction, nullptr);
8651}
8652
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008653void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008654 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8655 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008656 InvokeRuntimeCallingConvention calling_convention;
8657 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8658}
8659
8660void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008661 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008662 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8663}
8664
8665void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008666 DataType::Type input_type = conversion->GetInputType();
8667 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008668 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8669 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008670 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008671
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008672 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8673 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008674 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8675 }
8676
8677 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008678 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008679 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8680 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008681 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682 }
8683
Vladimir Markoca6fff82017-10-03 14:49:14 +01008684 LocationSummary* locations =
8685 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008686
8687 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008688 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008689 locations->SetInAt(0, Location::RequiresFpuRegister());
8690 } else {
8691 locations->SetInAt(0, Location::RequiresRegister());
8692 }
8693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008694 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008695 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8696 } else {
8697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8698 }
8699 } else {
8700 InvokeRuntimeCallingConvention calling_convention;
8701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008702 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008703 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8704 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008705 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008706 locations->SetInAt(0, Location::RegisterPairLocation(
8707 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8708 }
8709
8710 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8711 }
8712}
8713
8714void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8715 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008716 DataType::Type result_type = conversion->GetResultType();
8717 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008718 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008719 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008720
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008721 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8722 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008724 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8726 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8727 Register src = locations->InAt(0).AsRegister<Register>();
8728
Alexey Frunzea871ef12016-06-27 15:20:11 -07008729 if (dst_low != src) {
8730 __ Move(dst_low, src);
8731 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008732 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008733 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008734 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008735 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008736 ? locations->InAt(0).AsRegisterPairLow<Register>()
8737 : locations->InAt(0).AsRegister<Register>();
8738
8739 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008740 case DataType::Type::kUint8:
8741 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008742 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008743 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 if (has_sign_extension) {
8745 __ Seb(dst, src);
8746 } else {
8747 __ Sll(dst, src, 24);
8748 __ Sra(dst, dst, 24);
8749 }
8750 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008751 case DataType::Type::kUint16:
8752 __ Andi(dst, src, 0xFFFF);
8753 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008754 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008755 if (has_sign_extension) {
8756 __ Seh(dst, src);
8757 } else {
8758 __ Sll(dst, src, 16);
8759 __ Sra(dst, dst, 16);
8760 }
8761 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008762 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008763 if (dst != src) {
8764 __ Move(dst, src);
8765 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008766 break;
8767
8768 default:
8769 LOG(FATAL) << "Unexpected type conversion from " << input_type
8770 << " to " << result_type;
8771 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008772 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8773 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008774 if (isR6) {
8775 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8776 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8777 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8778 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8779 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8780 __ Mtc1(src_low, FTMP);
8781 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008782 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008783 __ Cvtsl(dst, FTMP);
8784 } else {
8785 __ Cvtdl(dst, FTMP);
8786 }
8787 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008788 QuickEntrypointEnum entrypoint =
8789 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008790 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008791 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008792 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8793 } else {
8794 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8795 }
8796 }
8797 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008798 Register src = locations->InAt(0).AsRegister<Register>();
8799 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8800 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008801 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008802 __ Cvtsw(dst, FTMP);
8803 } else {
8804 __ Cvtdw(dst, FTMP);
8805 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008807 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8808 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008809
8810 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8811 // value of the output type if the input is outside of the range after the truncation or
8812 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8813 // results. This matches the desired float/double-to-int/long conversion exactly.
8814 //
8815 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8816 // value when the input is either a NaN or is outside of the range of the output type
8817 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8818 // the same result.
8819 //
8820 // The code takes care of the different behaviors by first comparing the input to the
8821 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8822 // If the input is greater than or equal to the minimum, it procedes to the truncate
8823 // instruction, which will handle such an input the same way irrespective of NAN2008.
8824 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8825 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008826 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008827 if (isR6) {
8828 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8829 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8830 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8831 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8832 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008834 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008835 __ TruncLS(FTMP, src);
8836 } else {
8837 __ TruncLD(FTMP, src);
8838 }
8839 __ Mfc1(dst_low, FTMP);
8840 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008841 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 QuickEntrypointEnum entrypoint =
8843 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008844 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008845 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008846 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8847 } else {
8848 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8849 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008850 }
8851 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008852 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8853 Register dst = locations->Out().AsRegister<Register>();
8854 MipsLabel truncate;
8855 MipsLabel done;
8856
Lena Djokicf4e23a82017-05-09 15:43:45 +02008857 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008858 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008859 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8860 __ LoadConst32(TMP, min_val);
8861 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008862 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008863 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8864 __ LoadConst32(TMP, High32Bits(min_val));
8865 __ Mtc1(ZERO, FTMP);
8866 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008867 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008869 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008870 __ ColeS(0, FTMP, src);
8871 } else {
8872 __ ColeD(0, FTMP, src);
8873 }
8874 __ Bc1t(0, &truncate);
8875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008876 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008877 __ CeqS(0, src, src);
8878 } else {
8879 __ CeqD(0, src, src);
8880 }
8881 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8882 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008883
8884 __ B(&done);
8885
8886 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008887 }
8888
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008889 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008890 __ TruncWS(FTMP, src);
8891 } else {
8892 __ TruncWD(FTMP, src);
8893 }
8894 __ Mfc1(dst, FTMP);
8895
Lena Djokicf4e23a82017-05-09 15:43:45 +02008896 if (!isR6) {
8897 __ Bind(&done);
8898 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008899 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008900 } else if (DataType::IsFloatingPointType(result_type) &&
8901 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008902 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8903 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008904 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008905 __ Cvtsd(dst, src);
8906 } else {
8907 __ Cvtds(dst, src);
8908 }
8909 } else {
8910 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8911 << " to " << result_type;
8912 }
8913}
8914
8915void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8916 HandleShift(ushr);
8917}
8918
8919void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8920 HandleShift(ushr);
8921}
8922
8923void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8924 HandleBinaryOp(instruction);
8925}
8926
8927void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8928 HandleBinaryOp(instruction);
8929}
8930
8931void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8932 // Nothing to do, this should be removed during prepare for register allocator.
8933 LOG(FATAL) << "Unreachable";
8934}
8935
8936void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8937 // Nothing to do, this should be removed during prepare for register allocator.
8938 LOG(FATAL) << "Unreachable";
8939}
8940
8941void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008942 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008943}
8944
8945void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008946 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008947}
8948
8949void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008950 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008951}
8952
8953void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008954 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008955}
8956
8957void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008958 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008959}
8960
8961void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008962 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008963}
8964
8965void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008966 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008967}
8968
8969void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008970 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008971}
8972
8973void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008974 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008975}
8976
8977void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008978 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008979}
8980
8981void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008982 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008983}
8984
8985void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008986 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008987}
8988
8989void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008990 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008991}
8992
8993void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008994 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008995}
8996
8997void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008998 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008999}
9000
9001void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009002 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009003}
9004
9005void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009006 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009007}
9008
9009void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009010 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009011}
9012
9013void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009014 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009015}
9016
9017void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009018 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009019}
9020
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009021void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9022 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009023 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009024 locations->SetInAt(0, Location::RequiresRegister());
9025}
9026
Alexey Frunze96b66822016-09-10 02:32:44 -07009027void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9028 int32_t lower_bound,
9029 uint32_t num_entries,
9030 HBasicBlock* switch_block,
9031 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009032 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009033 Register temp_reg = TMP;
9034 __ Addiu32(temp_reg, value_reg, -lower_bound);
9035 // Jump to default if index is negative
9036 // Note: We don't check the case that index is positive while value < lower_bound, because in
9037 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9038 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9039
Alexey Frunze96b66822016-09-10 02:32:44 -07009040 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009041 // Jump to successors[0] if value == lower_bound.
9042 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9043 int32_t last_index = 0;
9044 for (; num_entries - last_index > 2; last_index += 2) {
9045 __ Addiu(temp_reg, temp_reg, -2);
9046 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9047 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9048 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9049 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9050 }
9051 if (num_entries - last_index == 2) {
9052 // The last missing case_value.
9053 __ Addiu(temp_reg, temp_reg, -1);
9054 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009055 }
9056
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009057 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009058 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009059 __ B(codegen_->GetLabelOf(default_block));
9060 }
9061}
9062
Alexey Frunze96b66822016-09-10 02:32:44 -07009063void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9064 Register constant_area,
9065 int32_t lower_bound,
9066 uint32_t num_entries,
9067 HBasicBlock* switch_block,
9068 HBasicBlock* default_block) {
9069 // Create a jump table.
9070 std::vector<MipsLabel*> labels(num_entries);
9071 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9072 for (uint32_t i = 0; i < num_entries; i++) {
9073 labels[i] = codegen_->GetLabelOf(successors[i]);
9074 }
9075 JumpTable* table = __ CreateJumpTable(std::move(labels));
9076
9077 // Is the value in range?
9078 __ Addiu32(TMP, value_reg, -lower_bound);
9079 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9080 __ Sltiu(AT, TMP, num_entries);
9081 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9082 } else {
9083 __ LoadConst32(AT, num_entries);
9084 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9085 }
9086
9087 // We are in the range of the table.
9088 // Load the target address from the jump table, indexing by the value.
9089 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009090 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009091 __ Lw(TMP, TMP, 0);
9092 // Compute the absolute target address by adding the table start address
9093 // (the table contains offsets to targets relative to its start).
9094 __ Addu(TMP, TMP, AT);
9095 // And jump.
9096 __ Jr(TMP);
9097 __ NopIfNoReordering();
9098}
9099
9100void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9101 int32_t lower_bound = switch_instr->GetStartValue();
9102 uint32_t num_entries = switch_instr->GetNumEntries();
9103 LocationSummary* locations = switch_instr->GetLocations();
9104 Register value_reg = locations->InAt(0).AsRegister<Register>();
9105 HBasicBlock* switch_block = switch_instr->GetBlock();
9106 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9107
9108 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9109 num_entries > kPackedSwitchJumpTableThreshold) {
9110 // R6 uses PC-relative addressing to access the jump table.
9111 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9112 // the jump table and it is implemented by changing HPackedSwitch to
9113 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9114 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9115 GenTableBasedPackedSwitch(value_reg,
9116 ZERO,
9117 lower_bound,
9118 num_entries,
9119 switch_block,
9120 default_block);
9121 } else {
9122 GenPackedSwitchWithCompares(value_reg,
9123 lower_bound,
9124 num_entries,
9125 switch_block,
9126 default_block);
9127 }
9128}
9129
9130void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9131 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009132 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009133 locations->SetInAt(0, Location::RequiresRegister());
9134 // Constant area pointer (HMipsComputeBaseMethodAddress).
9135 locations->SetInAt(1, Location::RequiresRegister());
9136}
9137
9138void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9139 int32_t lower_bound = switch_instr->GetStartValue();
9140 uint32_t num_entries = switch_instr->GetNumEntries();
9141 LocationSummary* locations = switch_instr->GetLocations();
9142 Register value_reg = locations->InAt(0).AsRegister<Register>();
9143 Register constant_area = locations->InAt(1).AsRegister<Register>();
9144 HBasicBlock* switch_block = switch_instr->GetBlock();
9145 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9146
9147 // This is an R2-only path. HPackedSwitch has been changed to
9148 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9149 // required to address the jump table relative to PC.
9150 GenTableBasedPackedSwitch(value_reg,
9151 constant_area,
9152 lower_bound,
9153 num_entries,
9154 switch_block,
9155 default_block);
9156}
9157
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009158void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9159 HMipsComputeBaseMethodAddress* insn) {
9160 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009161 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009162 locations->SetOut(Location::RequiresRegister());
9163}
9164
9165void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9166 HMipsComputeBaseMethodAddress* insn) {
9167 LocationSummary* locations = insn->GetLocations();
9168 Register reg = locations->Out().AsRegister<Register>();
9169
9170 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9171
9172 // Generate a dummy PC-relative call to obtain PC.
9173 __ Nal();
9174 // Grab the return address off RA.
9175 __ Move(reg, RA);
9176
9177 // Remember this offset (the obtained PC value) for later use with constant area.
9178 __ BindPcRelBaseLabel();
9179}
9180
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009181void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9182 // The trampoline uses the same calling convention as dex calling conventions,
9183 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9184 // the method_idx.
9185 HandleInvoke(invoke);
9186}
9187
9188void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9189 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9190}
9191
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009192void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9193 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009194 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009195 locations->SetInAt(0, Location::RequiresRegister());
9196 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009197}
9198
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009199void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9200 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009201 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009202 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009203 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009204 __ LoadFromOffset(kLoadWord,
9205 locations->Out().AsRegister<Register>(),
9206 locations->InAt(0).AsRegister<Register>(),
9207 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009208 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009209 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009210 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009211 __ LoadFromOffset(kLoadWord,
9212 locations->Out().AsRegister<Register>(),
9213 locations->InAt(0).AsRegister<Register>(),
9214 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009215 __ LoadFromOffset(kLoadWord,
9216 locations->Out().AsRegister<Register>(),
9217 locations->Out().AsRegister<Register>(),
9218 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009219 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009220}
9221
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009222#undef __
9223#undef QUICK_ENTRY_POINT
9224
9225} // namespace mips
9226} // namespace art