blob: d2cfa4f1870dcc3766fc1a180deba73c1b184d41 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
61 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020062 return Location::RegisterLocation(V0);
63
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010064 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020065 return Location::RegisterPairLocation(V0, V1);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kFloat32:
68 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020069 return Location::FpuRegisterLocation(F0);
70
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010071 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location();
73 }
74 UNREACHABLE();
75}
76
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010077Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020078 return MipsReturnLocation(type);
79}
80
81Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
82 return Location::RegisterLocation(kMethodRegisterArgument);
83}
84
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020086 Location next_location;
87
88 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010089 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kInt8:
93 case DataType::Type::kUint16:
94 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010095 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020096 uint32_t gp_index = gp_index_++;
97 if (gp_index < calling_convention.GetNumberOfRegisters()) {
98 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
99 } else {
100 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
101 next_location = Location::StackSlot(stack_offset);
102 }
103 break;
104 }
105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200107 uint32_t gp_index = gp_index_;
108 gp_index_ += 2;
109 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800110 Register reg = calling_convention.GetRegisterAt(gp_index);
111 if (reg == A1 || reg == A3) {
112 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200113 gp_index++;
114 }
115 Register low_even = calling_convention.GetRegisterAt(gp_index);
116 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
117 DCHECK_EQ(low_even + 1, high_odd);
118 next_location = Location::RegisterPairLocation(low_even, high_odd);
119 } else {
120 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
121 next_location = Location::DoubleStackSlot(stack_offset);
122 }
123 break;
124 }
125
126 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
127 // will take up the even/odd pair, while floats are stored in even regs only.
128 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 case DataType::Type::kFloat32:
130 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200131 uint32_t float_index = float_index_++;
132 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
133 next_location = Location::FpuRegisterLocation(
134 calling_convention.GetFpuRegisterAt(float_index));
135 } else {
136 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
138 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200139 }
140 break;
141 }
142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200144 LOG(FATAL) << "Unexpected parameter type " << type;
145 break;
146 }
147
148 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 return next_location;
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155 return MipsReturnLocation(type);
156}
157
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100158// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
159#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700160#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
163 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000164 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
167 LocationSummary* locations = instruction_->GetLocations();
168 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
169 __ Bind(GetEntryLabel());
170 if (instruction_->CanThrowIntoCatchBlock()) {
171 // Live registers will be restored in the catch block if caught.
172 SaveLiveRegisters(codegen, instruction_->GetLocations());
173 }
174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
176 InvokeRuntimeCallingConvention calling_convention;
177 codegen->EmitParallelMoves(locations->InAt(0),
178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100179 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 locations->InAt(1),
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100183 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
184 ? kQuickThrowStringBounds
185 : kQuickThrowArrayBounds;
186 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100187 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
189 }
190
191 bool IsFatal() const OVERRIDE { return true; }
192
193 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
194
195 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
197};
198
199class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
200 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202
203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
204 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
205 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100206 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
208 }
209
210 bool IsFatal() const OVERRIDE { return true; }
211
212 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
213
214 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200215 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
216};
217
218class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
219 public:
220 LoadClassSlowPathMIPS(HLoadClass* cls,
221 HInstruction* at,
222 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700223 bool do_clinit,
224 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
225 : SlowPathCodeMIPS(at),
226 cls_(cls),
227 dex_pc_(dex_pc),
228 do_clinit_(do_clinit),
229 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
231 }
232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000234 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700237 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700238 InvokeRuntimeCallingConvention calling_convention;
239 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
240 const bool is_load_class_bss_entry =
241 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200242 __ Bind(GetEntryLabel());
243 SaveLiveRegisters(codegen, locations);
244
Alexey Frunzec61c0762017-04-10 13:54:23 -0700245 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
246 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700247 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700248 Register temp = locations->GetTemp(0).AsRegister<Register>();
249 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
250 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
251 // kSaveEverything call.
252 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
253 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
254 if (temp_is_a0) {
255 __ Move(entry_address, temp);
256 }
257 }
258
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000259 dex::TypeIndex type_index = cls_->GetTypeIndex();
260 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100261 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
262 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
269
Alexey Frunzec61c0762017-04-10 13:54:23 -0700270 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700271 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700272 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700273 DCHECK(bss_info_high_);
274 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
275 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700276 __ Sw(calling_convention.GetRegisterAt(0),
277 entry_address,
278 /* placeholder */ 0x5678,
279 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700280 }
281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283 if (out.IsValid()) {
284 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100285 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700286 mips_codegen->MoveLocation(out,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
288 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291
292 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700293 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
294 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700296 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200297 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
298 Register base =
299 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700300 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000301 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700302 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
303 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700304 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
305 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 __ B(GetExitLabel());
308 }
309
310 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
311
312 private:
313 // The class this slow path will load.
314 HLoadClass* const cls_;
315
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 // The dex PC of `at_`.
317 const uint32_t dex_pc_;
318
319 // Whether to initialize the class.
320 const bool do_clinit_;
321
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700322 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
323 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
324
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200325 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
326};
327
328class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
329 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700330 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
331 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
332 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333
334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700335 DCHECK(instruction_->IsLoadString());
336 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 LocationSummary* locations = instruction_->GetLocations();
338 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700339 HLoadString* load = instruction_->AsLoadString();
340 const dex::StringIndex string_index = load->GetStringIndex();
341 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700343 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700344 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 __ Bind(GetEntryLabel());
346 SaveLiveRegisters(codegen, locations);
347
Alexey Frunzec61c0762017-04-10 13:54:23 -0700348 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
349 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700350 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700351 Register temp = locations->GetTemp(0).AsRegister<Register>();
352 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
353 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
354 // kSaveEverything call.
355 entry_address = temp_is_a0 ? out : temp;
356 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
357 if (temp_is_a0) {
358 __ Move(entry_address, temp);
359 }
360 }
361
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000362 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100363 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200364 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700365
366 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700367 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700368 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700369 DCHECK(bss_info_high_);
370 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100371 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700372 __ Sw(calling_convention.GetRegisterAt(0),
373 entry_address,
374 /* placeholder */ 0x5678,
375 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700376 }
377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100378 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700380 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000383
Alexey Frunzec61c0762017-04-10 13:54:23 -0700384 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700385 if (!baker_or_no_read_barriers) {
386 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700387 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700388 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200389 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
390 Register base =
391 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700392 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100393 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700394 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100395 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700396 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
397 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700398 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 __ B(GetExitLabel());
400 }
401
402 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
403
404 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700405 // Pointer to the high half PC-relative patch info.
406 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
407
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200408 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
409};
410
411class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
412 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000413 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
417 __ Bind(GetEntryLabel());
418 if (instruction_->CanThrowIntoCatchBlock()) {
419 // Live registers will be restored in the catch block if caught.
420 SaveLiveRegisters(codegen, instruction_->GetLocations());
421 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100422 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 instruction_,
424 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100425 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200426 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
427 }
428
429 bool IsFatal() const OVERRIDE { return true; }
430
431 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
432
433 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
435};
436
437class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
438 public:
439 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000440 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441
442 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200443 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
445 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200446 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100447 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200449 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 if (successor_ == nullptr) {
451 __ B(GetReturnLabel());
452 } else {
453 __ B(mips_codegen->GetLabelOf(successor_));
454 }
455 }
456
457 MipsLabel* GetReturnLabel() {
458 DCHECK(successor_ == nullptr);
459 return &return_label_;
460 }
461
462 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
463
Chris Larsena2045912017-11-02 12:39:54 -0700464 HBasicBlock* GetSuccessor() const {
465 return successor_;
466 }
467
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200468 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200469 // If not null, the block to branch to after the suspend check.
470 HBasicBlock* const successor_;
471
472 // If `successor_` is null, the label to branch to after the suspend check.
473 MipsLabel return_label_;
474
475 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
476};
477
478class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
479 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800480 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
481 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482
483 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
484 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485 uint32_t dex_pc = instruction_->GetDexPc();
486 DCHECK(instruction_->IsCheckCast()
487 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
488 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
489
490 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800491 if (!is_fatal_) {
492 SaveLiveRegisters(codegen, locations);
493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494
495 // We're moving two locations to locations that could overlap, so we need a parallel
496 // move resolver.
497 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800498 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800501 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200502 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100503 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200504 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100505 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800506 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100507 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
509 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200510 } else {
511 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800512 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
513 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200514 }
515
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800516 if (!is_fatal_) {
517 RestoreLiveRegisters(codegen, locations);
518 __ B(GetExitLabel());
519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200520 }
521
522 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
523
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800524 bool IsFatal() const OVERRIDE { return is_fatal_; }
525
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200526 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800527 const bool is_fatal_;
528
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200529 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
530};
531
532class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
533 public:
Aart Bik42249c32016-01-07 15:33:50 -0800534 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000535 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200536
537 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800538 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200539 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100540 LocationSummary* locations = instruction_->GetLocations();
541 SaveLiveRegisters(codegen, locations);
542 InvokeRuntimeCallingConvention calling_convention;
543 __ LoadConst32(calling_convention.GetRegisterAt(0),
544 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100545 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100546 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200547 }
548
549 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
550
551 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200552 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
553};
554
Alexey Frunze15958152017-02-09 19:08:30 -0800555class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
556 public:
557 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
558
559 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
560 LocationSummary* locations = instruction_->GetLocations();
561 __ Bind(GetEntryLabel());
562 SaveLiveRegisters(codegen, locations);
563
564 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100565 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800566 parallel_move.AddMove(
567 locations->InAt(0),
568 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800570 nullptr);
571 parallel_move.AddMove(
572 locations->InAt(1),
573 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800575 nullptr);
576 parallel_move.AddMove(
577 locations->InAt(2),
578 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100579 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800580 nullptr);
581 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
582
583 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
584 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
585 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
586 RestoreLiveRegisters(codegen, locations);
587 __ B(GetExitLabel());
588 }
589
590 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
591
592 private:
593 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
594};
595
596// Slow path marking an object reference `ref` during a read
597// barrier. The field `obj.field` in the object `obj` holding this
598// reference does not get updated by this slow path after marking (see
599// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
600//
601// This means that after the execution of this slow path, `ref` will
602// always be up-to-date, but `obj.field` may not; i.e., after the
603// flip, `ref` will be a to-space reference, but `obj.field` will
604// probably still be a from-space reference (unless it gets updated by
605// another thread, or if another thread installed another object
606// reference (different from `ref`) in `obj.field`).
607//
608// If `entrypoint` is a valid location it is assumed to already be
609// holding the entrypoint. The case where the entrypoint is passed in
610// is for the GcRoot read barrier.
611class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
612 public:
613 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
614 Location ref,
615 Location entrypoint = Location::NoLocation())
616 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
617 DCHECK(kEmitCompilerReadBarrier);
618 }
619
620 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
621
622 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
623 LocationSummary* locations = instruction_->GetLocations();
624 Register ref_reg = ref_.AsRegister<Register>();
625 DCHECK(locations->CanCall());
626 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
627 DCHECK(instruction_->IsInstanceFieldGet() ||
628 instruction_->IsStaticFieldGet() ||
629 instruction_->IsArrayGet() ||
630 instruction_->IsArraySet() ||
631 instruction_->IsLoadClass() ||
632 instruction_->IsLoadString() ||
633 instruction_->IsInstanceOf() ||
634 instruction_->IsCheckCast() ||
635 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
636 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
637 << "Unexpected instruction in read barrier marking slow path: "
638 << instruction_->DebugName();
639
640 __ Bind(GetEntryLabel());
641 // No need to save live registers; it's taken care of by the
642 // entrypoint. Also, there is no need to update the stack mask,
643 // as this runtime call will not trigger a garbage collection.
644 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
645 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
646 (S2 <= ref_reg && ref_reg <= S7) ||
647 (ref_reg == FP)) << ref_reg;
648 // "Compact" slow path, saving two moves.
649 //
650 // Instead of using the standard runtime calling convention (input
651 // and output in A0 and V0 respectively):
652 //
653 // A0 <- ref
654 // V0 <- ReadBarrierMark(A0)
655 // ref <- V0
656 //
657 // we just use rX (the register containing `ref`) as input and output
658 // of a dedicated entrypoint:
659 //
660 // rX <- ReadBarrierMarkRegX(rX)
661 //
662 if (entrypoint_.IsValid()) {
663 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
664 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
665 __ Jalr(entrypoint_.AsRegister<Register>());
666 __ NopIfNoReordering();
667 } else {
668 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100669 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800670 // This runtime call does not require a stack map.
671 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
672 instruction_,
673 this,
674 /* direct */ false);
675 }
676 __ B(GetExitLabel());
677 }
678
679 private:
680 // The location (register) of the marked object reference.
681 const Location ref_;
682
683 // The location of the entrypoint if already loaded.
684 const Location entrypoint_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
687};
688
689// Slow path marking an object reference `ref` during a read barrier,
690// and if needed, atomically updating the field `obj.field` in the
691// object `obj` holding this reference after marking (contrary to
692// ReadBarrierMarkSlowPathMIPS above, which never tries to update
693// `obj.field`).
694//
695// This means that after the execution of this slow path, both `ref`
696// and `obj.field` will be up-to-date; i.e., after the flip, both will
697// hold the same to-space reference (unless another thread installed
698// another object reference (different from `ref`) in `obj.field`).
699class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
700 public:
701 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
702 Location ref,
703 Register obj,
704 Location field_offset,
705 Register temp1)
706 : SlowPathCodeMIPS(instruction),
707 ref_(ref),
708 obj_(obj),
709 field_offset_(field_offset),
710 temp1_(temp1) {
711 DCHECK(kEmitCompilerReadBarrier);
712 }
713
714 const char* GetDescription() const OVERRIDE {
715 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
716 }
717
718 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
719 LocationSummary* locations = instruction_->GetLocations();
720 Register ref_reg = ref_.AsRegister<Register>();
721 DCHECK(locations->CanCall());
722 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
723 // This slow path is only used by the UnsafeCASObject intrinsic.
724 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
725 << "Unexpected instruction in read barrier marking and field updating slow path: "
726 << instruction_->DebugName();
727 DCHECK(instruction_->GetLocations()->Intrinsified());
728 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
729 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
730
731 __ Bind(GetEntryLabel());
732
733 // Save the old reference.
734 // Note that we cannot use AT or TMP to save the old reference, as those
735 // are used by the code that follows, but we need the old reference after
736 // the call to the ReadBarrierMarkRegX entry point.
737 DCHECK_NE(temp1_, AT);
738 DCHECK_NE(temp1_, TMP);
739 __ Move(temp1_, ref_reg);
740
741 // No need to save live registers; it's taken care of by the
742 // entrypoint. Also, there is no need to update the stack mask,
743 // as this runtime call will not trigger a garbage collection.
744 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
745 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
746 (S2 <= ref_reg && ref_reg <= S7) ||
747 (ref_reg == FP)) << ref_reg;
748 // "Compact" slow path, saving two moves.
749 //
750 // Instead of using the standard runtime calling convention (input
751 // and output in A0 and V0 respectively):
752 //
753 // A0 <- ref
754 // V0 <- ReadBarrierMark(A0)
755 // ref <- V0
756 //
757 // we just use rX (the register containing `ref`) as input and output
758 // of a dedicated entrypoint:
759 //
760 // rX <- ReadBarrierMarkRegX(rX)
761 //
762 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100763 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800764 // This runtime call does not require a stack map.
765 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
766 instruction_,
767 this,
768 /* direct */ false);
769
770 // If the new reference is different from the old reference,
771 // update the field in the holder (`*(obj_ + field_offset_)`).
772 //
773 // Note that this field could also hold a different object, if
774 // another thread had concurrently changed it. In that case, the
775 // the compare-and-set (CAS) loop below would abort, leaving the
776 // field as-is.
777 MipsLabel done;
778 __ Beq(temp1_, ref_reg, &done);
779
780 // Update the the holder's field atomically. This may fail if
781 // mutator updates before us, but it's OK. This is achieved
782 // using a strong compare-and-set (CAS) operation with relaxed
783 // memory synchronization ordering, where the expected value is
784 // the old reference and the desired value is the new reference.
785
786 // Convenience aliases.
787 Register base = obj_;
788 // The UnsafeCASObject intrinsic uses a register pair as field
789 // offset ("long offset"), of which only the low part contains
790 // data.
791 Register offset = field_offset_.AsRegisterPairLow<Register>();
792 Register expected = temp1_;
793 Register value = ref_reg;
794 Register tmp_ptr = TMP; // Pointer to actual memory.
795 Register tmp = AT; // Value in memory.
796
797 __ Addu(tmp_ptr, base, offset);
798
799 if (kPoisonHeapReferences) {
800 __ PoisonHeapReference(expected);
801 // Do not poison `value` if it is the same register as
802 // `expected`, which has just been poisoned.
803 if (value != expected) {
804 __ PoisonHeapReference(value);
805 }
806 }
807
808 // do {
809 // tmp = [r_ptr] - expected;
810 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
811
812 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
813 MipsLabel loop_head, exit_loop;
814 __ Bind(&loop_head);
815 if (is_r6) {
816 __ LlR6(tmp, tmp_ptr);
817 } else {
818 __ LlR2(tmp, tmp_ptr);
819 }
820 __ Bne(tmp, expected, &exit_loop);
821 __ Move(tmp, value);
822 if (is_r6) {
823 __ ScR6(tmp, tmp_ptr);
824 } else {
825 __ ScR2(tmp, tmp_ptr);
826 }
827 __ Beqz(tmp, &loop_head);
828 __ Bind(&exit_loop);
829
830 if (kPoisonHeapReferences) {
831 __ UnpoisonHeapReference(expected);
832 // Do not unpoison `value` if it is the same register as
833 // `expected`, which has just been unpoisoned.
834 if (value != expected) {
835 __ UnpoisonHeapReference(value);
836 }
837 }
838
839 __ Bind(&done);
840 __ B(GetExitLabel());
841 }
842
843 private:
844 // The location (register) of the marked object reference.
845 const Location ref_;
846 // The register containing the object holding the marked object reference field.
847 const Register obj_;
848 // The location of the offset of the marked reference field within `obj_`.
849 Location field_offset_;
850
851 const Register temp1_;
852
853 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
854};
855
856// Slow path generating a read barrier for a heap reference.
857class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
858 public:
859 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
860 Location out,
861 Location ref,
862 Location obj,
863 uint32_t offset,
864 Location index)
865 : SlowPathCodeMIPS(instruction),
866 out_(out),
867 ref_(ref),
868 obj_(obj),
869 offset_(offset),
870 index_(index) {
871 DCHECK(kEmitCompilerReadBarrier);
872 // If `obj` is equal to `out` or `ref`, it means the initial object
873 // has been overwritten by (or after) the heap object reference load
874 // to be instrumented, e.g.:
875 //
876 // __ LoadFromOffset(kLoadWord, out, out, offset);
877 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
878 //
879 // In that case, we have lost the information about the original
880 // object, and the emitted read barrier cannot work properly.
881 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
882 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
883 }
884
885 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
886 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
887 LocationSummary* locations = instruction_->GetLocations();
888 Register reg_out = out_.AsRegister<Register>();
889 DCHECK(locations->CanCall());
890 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
891 DCHECK(instruction_->IsInstanceFieldGet() ||
892 instruction_->IsStaticFieldGet() ||
893 instruction_->IsArrayGet() ||
894 instruction_->IsInstanceOf() ||
895 instruction_->IsCheckCast() ||
896 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
897 << "Unexpected instruction in read barrier for heap reference slow path: "
898 << instruction_->DebugName();
899
900 __ Bind(GetEntryLabel());
901 SaveLiveRegisters(codegen, locations);
902
903 // We may have to change the index's value, but as `index_` is a
904 // constant member (like other "inputs" of this slow path),
905 // introduce a copy of it, `index`.
906 Location index = index_;
907 if (index_.IsValid()) {
908 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
909 if (instruction_->IsArrayGet()) {
910 // Compute the actual memory offset and store it in `index`.
911 Register index_reg = index_.AsRegister<Register>();
912 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
913 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
914 // We are about to change the value of `index_reg` (see the
915 // calls to art::mips::MipsAssembler::Sll and
916 // art::mips::MipsAssembler::Addiu32 below), but it has
917 // not been saved by the previous call to
918 // art::SlowPathCode::SaveLiveRegisters, as it is a
919 // callee-save register --
920 // art::SlowPathCode::SaveLiveRegisters does not consider
921 // callee-save registers, as it has been designed with the
922 // assumption that callee-save registers are supposed to be
923 // handled by the called function. So, as a callee-save
924 // register, `index_reg` _would_ eventually be saved onto
925 // the stack, but it would be too late: we would have
926 // changed its value earlier. Therefore, we manually save
927 // it here into another freely available register,
928 // `free_reg`, chosen of course among the caller-save
929 // registers (as a callee-save `free_reg` register would
930 // exhibit the same problem).
931 //
932 // Note we could have requested a temporary register from
933 // the register allocator instead; but we prefer not to, as
934 // this is a slow path, and we know we can find a
935 // caller-save register that is available.
936 Register free_reg = FindAvailableCallerSaveRegister(codegen);
937 __ Move(free_reg, index_reg);
938 index_reg = free_reg;
939 index = Location::RegisterLocation(index_reg);
940 } else {
941 // The initial register stored in `index_` has already been
942 // saved in the call to art::SlowPathCode::SaveLiveRegisters
943 // (as it is not a callee-save register), so we can freely
944 // use it.
945 }
946 // Shifting the index value contained in `index_reg` by the scale
947 // factor (2) cannot overflow in practice, as the runtime is
948 // unable to allocate object arrays with a size larger than
949 // 2^26 - 1 (that is, 2^28 - 4 bytes).
950 __ Sll(index_reg, index_reg, TIMES_4);
951 static_assert(
952 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
953 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
954 __ Addiu32(index_reg, index_reg, offset_);
955 } else {
956 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
957 // intrinsics, `index_` is not shifted by a scale factor of 2
958 // (as in the case of ArrayGet), as it is actually an offset
959 // to an object field within an object.
960 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
961 DCHECK(instruction_->GetLocations()->Intrinsified());
962 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
963 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
964 << instruction_->AsInvoke()->GetIntrinsic();
965 DCHECK_EQ(offset_, 0U);
966 DCHECK(index_.IsRegisterPair());
967 // UnsafeGet's offset location is a register pair, the low
968 // part contains the correct offset.
969 index = index_.ToLow();
970 }
971 }
972
973 // We're moving two or three locations to locations that could
974 // overlap, so we need a parallel move resolver.
975 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100976 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800977 parallel_move.AddMove(ref_,
978 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100979 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800980 nullptr);
981 parallel_move.AddMove(obj_,
982 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800984 nullptr);
985 if (index.IsValid()) {
986 parallel_move.AddMove(index,
987 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100988 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800989 nullptr);
990 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
991 } else {
992 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
993 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
994 }
995 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
996 instruction_,
997 instruction_->GetDexPc(),
998 this);
999 CheckEntrypointTypes<
1000 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +02001001 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001002 calling_convention.GetReturnLocation(DataType::Type::kReference),
1003 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001004
1005 RestoreLiveRegisters(codegen, locations);
1006 __ B(GetExitLabel());
1007 }
1008
1009 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1010
1011 private:
1012 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1013 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1014 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1015 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1016 if (i != ref &&
1017 i != obj &&
1018 !codegen->IsCoreCalleeSaveRegister(i) &&
1019 !codegen->IsBlockedCoreRegister(i)) {
1020 return static_cast<Register>(i);
1021 }
1022 }
1023 // We shall never fail to find a free caller-save register, as
1024 // there are more than two core caller-save registers on MIPS
1025 // (meaning it is possible to find one which is different from
1026 // `ref` and `obj`).
1027 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1028 LOG(FATAL) << "Could not find a free caller-save register";
1029 UNREACHABLE();
1030 }
1031
1032 const Location out_;
1033 const Location ref_;
1034 const Location obj_;
1035 const uint32_t offset_;
1036 // An additional location containing an index to an array.
1037 // Only used for HArrayGet and the UnsafeGetObject &
1038 // UnsafeGetObjectVolatile intrinsics.
1039 const Location index_;
1040
1041 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1042};
1043
1044// Slow path generating a read barrier for a GC root.
1045class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1046 public:
1047 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1048 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1049 DCHECK(kEmitCompilerReadBarrier);
1050 }
1051
1052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1053 LocationSummary* locations = instruction_->GetLocations();
1054 Register reg_out = out_.AsRegister<Register>();
1055 DCHECK(locations->CanCall());
1056 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1057 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1058 << "Unexpected instruction in read barrier for GC root slow path: "
1059 << instruction_->DebugName();
1060
1061 __ Bind(GetEntryLabel());
1062 SaveLiveRegisters(codegen, locations);
1063
1064 InvokeRuntimeCallingConvention calling_convention;
1065 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001066 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1067 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001068 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001069 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1070 instruction_,
1071 instruction_->GetDexPc(),
1072 this);
1073 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001074 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001075 calling_convention.GetReturnLocation(DataType::Type::kReference),
1076 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001077
1078 RestoreLiveRegisters(codegen, locations);
1079 __ B(GetExitLabel());
1080 }
1081
1082 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1083
1084 private:
1085 const Location out_;
1086 const Location root_;
1087
1088 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1089};
1090
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001091CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1092 const MipsInstructionSetFeatures& isa_features,
1093 const CompilerOptions& compiler_options,
1094 OptimizingCompilerStats* stats)
1095 : CodeGenerator(graph,
1096 kNumberOfCoreRegisters,
1097 kNumberOfFRegisters,
1098 kNumberOfRegisterPairs,
1099 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1100 arraysize(kCoreCalleeSaves)),
1101 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1102 arraysize(kFpuCalleeSaves)),
1103 compiler_options,
1104 stats),
1105 block_labels_(nullptr),
1106 location_builder_(graph, this),
1107 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 move_resolver_(graph->GetAllocator(), this),
1109 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001110 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001111 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001112 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1113 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1114 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1115 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1116 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1117 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1118 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1119 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1120 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001121 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001122 // Save RA (containing the return address) to mimic Quick.
1123 AddAllocatedRegister(Location::RegisterLocation(RA));
1124}
1125
1126#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001127// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1128#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001129#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001130
1131void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1132 // Ensure that we fix up branches.
1133 __ FinalizeCode();
1134
1135 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001136 StackMapStream* stack_map_stream = GetStackMapStream();
1137 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001138 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001139 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001140 uint32_t new_position = __ GetAdjustedPosition(old_position);
1141 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001142 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001143 }
1144
1145 // Adjust pc offsets for the disassembly information.
1146 if (disasm_info_ != nullptr) {
1147 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1148 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1149 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1150 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1151 it.second.start = __ GetAdjustedPosition(it.second.start);
1152 it.second.end = __ GetAdjustedPosition(it.second.end);
1153 }
1154 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1155 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1156 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1157 }
1158 }
1159
1160 CodeGenerator::Finalize(allocator);
1161}
1162
1163MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1164 return codegen_->GetAssembler();
1165}
1166
1167void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1168 DCHECK_LT(index, moves_.size());
1169 MoveOperands* move = moves_[index];
1170 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1171}
1172
1173void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1174 DCHECK_LT(index, moves_.size());
1175 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001176 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001177 Location loc1 = move->GetDestination();
1178 Location loc2 = move->GetSource();
1179
1180 DCHECK(!loc1.IsConstant());
1181 DCHECK(!loc2.IsConstant());
1182
1183 if (loc1.Equals(loc2)) {
1184 return;
1185 }
1186
1187 if (loc1.IsRegister() && loc2.IsRegister()) {
1188 // Swap 2 GPRs.
1189 Register r1 = loc1.AsRegister<Register>();
1190 Register r2 = loc2.AsRegister<Register>();
1191 __ Move(TMP, r2);
1192 __ Move(r2, r1);
1193 __ Move(r1, TMP);
1194 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1195 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1196 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001198 __ MovS(FTMP, f2);
1199 __ MovS(f2, f1);
1200 __ MovS(f1, FTMP);
1201 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001202 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001203 __ MovD(FTMP, f2);
1204 __ MovD(f2, f1);
1205 __ MovD(f1, FTMP);
1206 }
1207 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1208 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1209 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001210 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001211 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1212 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001213 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001214 __ Move(TMP, r2);
1215 __ Mfc1(r2, f1);
1216 __ Mtc1(TMP, f1);
1217 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1218 // Swap 2 GPR register pairs.
1219 Register r1 = loc1.AsRegisterPairLow<Register>();
1220 Register r2 = loc2.AsRegisterPairLow<Register>();
1221 __ Move(TMP, r2);
1222 __ Move(r2, r1);
1223 __ Move(r1, TMP);
1224 r1 = loc1.AsRegisterPairHigh<Register>();
1225 r2 = loc2.AsRegisterPairHigh<Register>();
1226 __ Move(TMP, r2);
1227 __ Move(r2, r1);
1228 __ Move(r1, TMP);
1229 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1230 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1231 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1234 : loc2.AsFpuRegister<FRegister>();
1235 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1236 : loc2.AsRegisterPairLow<Register>();
1237 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1238 : loc2.AsRegisterPairHigh<Register>();
1239 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1240 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1241 // unpredictable and the following mfch1 will fail.
1242 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001243 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001245 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246 __ Move(r2_l, TMP);
1247 __ Move(r2_h, AT);
1248 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1249 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1250 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1251 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001252 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1253 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001254 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1255 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001256 __ Move(TMP, reg);
1257 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1258 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1259 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1260 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1261 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1262 : loc2.AsRegisterPairLow<Register>();
1263 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1264 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001265 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001266 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1267 : loc2.GetHighStackIndex(kMipsWordSize);
1268 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001269 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001270 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001271 __ Move(TMP, reg_h);
1272 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1273 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001274 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1275 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1276 : loc2.AsFpuRegister<FRegister>();
1277 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001278 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001279 __ MovS(FTMP, reg);
1280 __ LoadSFromOffset(reg, SP, offset);
1281 __ StoreSToOffset(FTMP, SP, offset);
1282 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001284 __ MovD(FTMP, reg);
1285 __ LoadDFromOffset(reg, SP, offset);
1286 __ StoreDToOffset(FTMP, SP, offset);
1287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001288 } else {
1289 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1290 }
1291}
1292
1293void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1294 __ Pop(static_cast<Register>(reg));
1295}
1296
1297void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1298 __ Push(static_cast<Register>(reg));
1299}
1300
1301void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1302 // Allocate a scratch register other than TMP, if available.
1303 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1304 // automatically unspilled when the scratch scope object is destroyed).
1305 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1306 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001307 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1309 __ LoadFromOffset(kLoadWord,
1310 Register(ensure_scratch.GetRegister()),
1311 SP,
1312 index1 + stack_offset);
1313 __ LoadFromOffset(kLoadWord,
1314 TMP,
1315 SP,
1316 index2 + stack_offset);
1317 __ StoreToOffset(kStoreWord,
1318 Register(ensure_scratch.GetRegister()),
1319 SP,
1320 index2 + stack_offset);
1321 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1322 }
1323}
1324
Alexey Frunze73296a72016-06-03 22:51:46 -07001325void CodeGeneratorMIPS::ComputeSpillMask() {
1326 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1327 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1328 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1329 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1330 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1331 // within the stack frame.
1332 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1333 core_spill_mask_ |= (1 << ZERO);
1334 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001335}
1336
1337bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001338 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001339 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1340 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1341 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001342 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001343}
1344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345static dwarf::Reg DWARFReg(Register reg) {
1346 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1347}
1348
1349// TODO: mapping of floating-point registers to DWARF.
1350
1351void CodeGeneratorMIPS::GenerateFrameEntry() {
1352 __ Bind(&frame_entry_label_);
1353
Vladimir Marko33bff252017-11-01 14:35:42 +00001354 bool do_overflow_check =
1355 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001356
1357 if (do_overflow_check) {
1358 __ LoadFromOffset(kLoadWord,
1359 ZERO,
1360 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001361 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362 RecordPcInfo(nullptr, 0);
1363 }
1364
1365 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001366 CHECK_EQ(fpu_spill_mask_, 0u);
1367 CHECK_EQ(core_spill_mask_, 1u << RA);
1368 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001369 return;
1370 }
1371
1372 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001373 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1374 LOG(FATAL) << "Stack frame larger than "
1375 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001376 }
1377
1378 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379
Alexey Frunze73296a72016-06-03 22:51:46 -07001380 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001381 __ IncreaseFrameSize(ofs);
1382
Alexey Frunze73296a72016-06-03 22:51:46 -07001383 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1384 Register reg = static_cast<Register>(MostSignificantBit(mask));
1385 mask ^= 1u << reg;
1386 ofs -= kMipsWordSize;
1387 // The ZERO register is only included for alignment.
1388 if (reg != ZERO) {
1389 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001390 __ cfi().RelOffset(DWARFReg(reg), ofs);
1391 }
1392 }
1393
Alexey Frunze73296a72016-06-03 22:51:46 -07001394 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1395 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1396 mask ^= 1u << reg;
1397 ofs -= kMipsDoublewordSize;
1398 __ StoreDToOffset(reg, SP, ofs);
1399 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001400 }
1401
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001402 // Save the current method if we need it. Note that we do not
1403 // do this in HCurrentMethod, as the instruction might have been removed
1404 // in the SSA graph.
1405 if (RequiresCurrentMethod()) {
1406 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1407 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001408
1409 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1410 // Initialize should deoptimize flag to 0.
1411 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413}
1414
1415void CodeGeneratorMIPS::GenerateFrameExit() {
1416 __ cfi().RememberState();
1417
1418 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001420
Alexey Frunze73296a72016-06-03 22:51:46 -07001421 // For better instruction scheduling restore RA before other registers.
1422 uint32_t ofs = GetFrameSize();
1423 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1424 Register reg = static_cast<Register>(MostSignificantBit(mask));
1425 mask ^= 1u << reg;
1426 ofs -= kMipsWordSize;
1427 // The ZERO register is only included for alignment.
1428 if (reg != ZERO) {
1429 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001430 __ cfi().Restore(DWARFReg(reg));
1431 }
1432 }
1433
Alexey Frunze73296a72016-06-03 22:51:46 -07001434 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1435 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1436 mask ^= 1u << reg;
1437 ofs -= kMipsDoublewordSize;
1438 __ LoadDFromOffset(reg, SP, ofs);
1439 // TODO: __ cfi().Restore(DWARFReg(reg));
1440 }
1441
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001442 size_t frame_size = GetFrameSize();
1443 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1444 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1445 bool reordering = __ SetReorder(false);
1446 if (exchange) {
1447 __ Jr(RA);
1448 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1449 } else {
1450 __ DecreaseFrameSize(frame_size);
1451 __ Jr(RA);
1452 __ Nop(); // In delay slot.
1453 }
1454 __ SetReorder(reordering);
1455 } else {
1456 __ Jr(RA);
1457 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001458 }
1459
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001460 __ cfi().RestoreState();
1461 __ cfi().DefCFAOffset(GetFrameSize());
1462}
1463
1464void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1465 __ Bind(GetLabelOf(block));
1466}
1467
Lena Djokicca8c2952017-05-29 11:31:46 +02001468VectorRegister VectorRegisterFrom(Location location) {
1469 DCHECK(location.IsFpuRegister());
1470 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1471}
1472
Lena Djokic8098da92017-06-28 12:07:50 +02001473void CodeGeneratorMIPS::MoveLocation(Location destination,
1474 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001475 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001476 if (source.Equals(destination)) {
1477 return;
1478 }
1479
Lena Djokic8098da92017-06-28 12:07:50 +02001480 if (source.IsConstant()) {
1481 MoveConstant(destination, source.GetConstant());
1482 } else {
1483 if (destination.IsRegister()) {
1484 if (source.IsRegister()) {
1485 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1486 } else if (source.IsFpuRegister()) {
1487 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1488 } else {
1489 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001490 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001491 }
1492 } else if (destination.IsRegisterPair()) {
1493 if (source.IsRegisterPair()) {
1494 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1495 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1496 } else if (source.IsFpuRegister()) {
1497 Register dst_high = destination.AsRegisterPairHigh<Register>();
1498 Register dst_low = destination.AsRegisterPairLow<Register>();
1499 FRegister src = source.AsFpuRegister<FRegister>();
1500 __ Mfc1(dst_low, src);
1501 __ MoveFromFpuHigh(dst_high, src);
1502 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001503 DCHECK(source.IsDoubleStackSlot())
1504 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001505 int32_t off = source.GetStackIndex();
1506 Register r = destination.AsRegisterPairLow<Register>();
1507 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1508 }
1509 } else if (destination.IsFpuRegister()) {
1510 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001511 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001512 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1513 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001515 FRegister dst = destination.AsFpuRegister<FRegister>();
1516 Register src_high = source.AsRegisterPairHigh<Register>();
1517 Register src_low = source.AsRegisterPairLow<Register>();
1518 __ Mtc1(src_low, dst);
1519 __ MoveToFpuHigh(src_high, dst);
1520 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001521 if (GetGraph()->HasSIMD()) {
1522 __ MoveV(VectorRegisterFrom(destination),
1523 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001524 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001526 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1527 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001528 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001529 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1530 }
Lena Djokic8098da92017-06-28 12:07:50 +02001531 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001532 } else if (source.IsSIMDStackSlot()) {
1533 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001534 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001535 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001536 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1537 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001539 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1540 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1541 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001542 } else if (destination.IsSIMDStackSlot()) {
1543 if (source.IsFpuRegister()) {
1544 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1545 } else {
1546 DCHECK(source.IsSIMDStackSlot());
1547 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1548 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1549 }
Lena Djokic8098da92017-06-28 12:07:50 +02001550 } else if (destination.IsDoubleStackSlot()) {
1551 int32_t dst_offset = destination.GetStackIndex();
1552 if (source.IsRegisterPair()) {
1553 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1554 } else if (source.IsFpuRegister()) {
1555 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1556 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001557 DCHECK(source.IsDoubleStackSlot())
1558 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001559 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1560 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1561 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1562 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1563 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001564 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001565 DCHECK(destination.IsStackSlot()) << destination;
1566 int32_t dst_offset = destination.GetStackIndex();
1567 if (source.IsRegister()) {
1568 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1569 } else if (source.IsFpuRegister()) {
1570 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1571 } else {
1572 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1573 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1574 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1575 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 }
1577 }
1578}
1579
1580void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1581 if (c->IsIntConstant() || c->IsNullConstant()) {
1582 // Move 32 bit constant.
1583 int32_t value = GetInt32ValueOf(c);
1584 if (destination.IsRegister()) {
1585 Register dst = destination.AsRegister<Register>();
1586 __ LoadConst32(dst, value);
1587 } else {
1588 DCHECK(destination.IsStackSlot())
1589 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001590 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001591 }
1592 } else if (c->IsLongConstant()) {
1593 // Move 64 bit constant.
1594 int64_t value = GetInt64ValueOf(c);
1595 if (destination.IsRegisterPair()) {
1596 Register r_h = destination.AsRegisterPairHigh<Register>();
1597 Register r_l = destination.AsRegisterPairLow<Register>();
1598 __ LoadConst64(r_h, r_l, value);
1599 } else {
1600 DCHECK(destination.IsDoubleStackSlot())
1601 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001602 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 }
1604 } else if (c->IsFloatConstant()) {
1605 // Move 32 bit float constant.
1606 int32_t value = GetInt32ValueOf(c);
1607 if (destination.IsFpuRegister()) {
1608 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1609 } else {
1610 DCHECK(destination.IsStackSlot())
1611 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001612 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001613 }
1614 } else {
1615 // Move 64 bit double constant.
1616 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1617 int64_t value = GetInt64ValueOf(c);
1618 if (destination.IsFpuRegister()) {
1619 FRegister fd = destination.AsFpuRegister<FRegister>();
1620 __ LoadDConst64(fd, value, TMP);
1621 } else {
1622 DCHECK(destination.IsDoubleStackSlot())
1623 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001624 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 }
1626 }
1627}
1628
1629void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1630 DCHECK(destination.IsRegister());
1631 Register dst = destination.AsRegister<Register>();
1632 __ LoadConst32(dst, value);
1633}
1634
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001635void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1636 if (location.IsRegister()) {
1637 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001638 } else if (location.IsRegisterPair()) {
1639 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1640 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641 } else {
1642 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1643 }
1644}
1645
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001646template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001647inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1648 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001649 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001650 for (const PcRelativePatchInfo& info : infos) {
1651 const DexFile& dex_file = info.target_dex_file;
1652 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001653 DCHECK(info.label.IsBound());
1654 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001655 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1656 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001657 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1658 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1659 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001660 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001661 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001662 }
1663}
1664
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001665void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001666 DCHECK(linker_patches->empty());
1667 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001668 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001669 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001670 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001671 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001672 pc_relative_string_patches_.size() +
1673 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001674 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001675 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1677 pc_relative_method_patches_, linker_patches);
1678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1679 pc_relative_type_patches_, linker_patches);
1680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1681 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001682 } else {
1683 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001684 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1685 pc_relative_type_patches_, linker_patches);
1686 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1687 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001688 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001689 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1690 method_bss_entry_patches_, linker_patches);
1691 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1692 type_bss_entry_patches_, linker_patches);
1693 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1694 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001695 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001696}
1697
Vladimir Marko65979462017-05-19 17:25:12 +01001698CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001699 MethodReference target_method,
1700 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001701 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001702 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001703 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001704 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705}
1706
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001707CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001708 MethodReference target_method,
1709 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001710 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001711 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001713 &method_bss_entry_patches_);
1714}
1715
Alexey Frunze06a46c42016-07-19 15:00:40 -07001716CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001717 const DexFile& dex_file,
1718 dex::TypeIndex type_index,
1719 const PcRelativePatchInfo* info_high) {
1720 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001721}
1722
Vladimir Marko1998cd02017-01-13 13:02:58 +00001723CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001724 const DexFile& dex_file,
1725 dex::TypeIndex type_index,
1726 const PcRelativePatchInfo* info_high) {
1727 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001728}
1729
Vladimir Marko65979462017-05-19 17:25:12 +01001730CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001731 const DexFile& dex_file,
1732 dex::StringIndex string_index,
1733 const PcRelativePatchInfo* info_high) {
1734 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001735}
1736
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001737CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1738 const DexFile& dex_file,
1739 dex::StringIndex string_index,
1740 const PcRelativePatchInfo* info_high) {
1741 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1742}
1743
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001744CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001745 const DexFile& dex_file,
1746 uint32_t offset_or_index,
1747 const PcRelativePatchInfo* info_high,
1748 ArenaDeque<PcRelativePatchInfo>* patches) {
1749 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001750 return &patches->back();
1751}
1752
Alexey Frunze06a46c42016-07-19 15:00:40 -07001753Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1754 return map->GetOrCreate(
1755 value,
1756 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1757}
1758
Alexey Frunze06a46c42016-07-19 15:00:40 -07001759Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001760 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001761}
1762
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001763void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001764 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001765 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001766 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001767 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001768 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001769 if (GetInstructionSetFeatures().IsR6()) {
1770 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001771 __ Bind(&info_high->label);
1772 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001773 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001774 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001775 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001776 } else {
1777 // If base is ZERO, emit NAL to obtain the actual base.
1778 if (base == ZERO) {
1779 // Generate a dummy PC-relative call to obtain PC.
1780 __ Nal();
1781 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001782 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001783 __ Lui(out, /* placeholder */ 0x1234);
1784 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1785 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1786 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001787 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001788 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001789 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001790 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001791 __ Addu(out, out, (base == ZERO) ? RA : base);
1792 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001793 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001794 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001795}
1796
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1798 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001799 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001800 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001801 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1802 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001803 return &jit_string_patches_.back();
1804}
1805
1806CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1807 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001808 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001809 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001810 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1811 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812 return &jit_class_patches_.back();
1813}
1814
1815void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1816 const uint8_t* roots_data,
1817 const CodeGeneratorMIPS::JitPatchInfo& info,
1818 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1820 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001821 uintptr_t address =
1822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1823 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1824 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001825 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1826 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1827 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1828 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001829 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001830 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1831 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001832 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001834 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1835 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001836 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001837 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1838 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839}
1840
1841void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1842 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001843 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1844 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001845 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 }
1847 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001848 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1849 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001850 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001851 }
1852}
1853
Goran Jakovljevice114da22016-12-26 14:21:43 +01001854void CodeGeneratorMIPS::MarkGCCard(Register object,
1855 Register value,
1856 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 MipsLabel done;
1858 Register card = AT;
1859 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001860 if (value_can_be_null) {
1861 __ Beqz(value, &done);
1862 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 __ LoadFromOffset(kLoadWord,
1864 card,
1865 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001866 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1868 __ Addu(temp, card, temp);
1869 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001870 if (value_can_be_null) {
1871 __ Bind(&done);
1872 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873}
1874
David Brazdil58282f42016-01-14 12:45:10 +00001875void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1877 blocked_core_registers_[ZERO] = true;
1878 blocked_core_registers_[K0] = true;
1879 blocked_core_registers_[K1] = true;
1880 blocked_core_registers_[GP] = true;
1881 blocked_core_registers_[SP] = true;
1882 blocked_core_registers_[RA] = true;
1883
1884 // AT and TMP(T8) are used as temporary/scratch registers
1885 // (similar to how AT is used by MIPS assemblers).
1886 blocked_core_registers_[AT] = true;
1887 blocked_core_registers_[TMP] = true;
1888 blocked_fpu_registers_[FTMP] = true;
1889
1890 // Reserve suspend and thread registers.
1891 blocked_core_registers_[S0] = true;
1892 blocked_core_registers_[TR] = true;
1893
1894 // Reserve T9 for function calls
1895 blocked_core_registers_[T9] = true;
1896
1897 // Reserve odd-numbered FPU registers.
1898 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1899 blocked_fpu_registers_[i] = true;
1900 }
1901
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001902 if (GetGraph()->IsDebuggable()) {
1903 // Stubs do not save callee-save floating point registers. If the graph
1904 // is debuggable, we need to deal with these registers differently. For
1905 // now, just block them.
1906 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1907 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1908 }
1909 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910}
1911
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1913 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1914 return kMipsWordSize;
1915}
1916
1917size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1918 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1919 return kMipsWordSize;
1920}
1921
1922size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001923 if (GetGraph()->HasSIMD()) {
1924 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1925 } else {
1926 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1927 }
1928 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929}
1930
1931size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001932 if (GetGraph()->HasSIMD()) {
1933 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1934 } else {
1935 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1936 }
1937 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938}
1939
1940void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001941 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942}
1943
1944void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001945 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946}
1947
Serban Constantinescufca16662016-07-14 09:21:59 +01001948constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1949
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001950void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1951 HInstruction* instruction,
1952 uint32_t dex_pc,
1953 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001954 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001955 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1956 IsDirectEntrypoint(entrypoint));
1957 if (EntrypointRequiresStackMap(entrypoint)) {
1958 RecordPcInfo(instruction, dex_pc, slow_path);
1959 }
1960}
1961
1962void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1963 HInstruction* instruction,
1964 SlowPathCode* slow_path,
1965 bool direct) {
1966 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1967 GenerateInvokeRuntime(entry_point_offset, direct);
1968}
1969
1970void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001971 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001972 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001973 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001974 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975 // Reserve argument space on stack (for $a0-$a3) for
1976 // entrypoints that directly reference native implementations.
1977 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001978 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001980 } else {
1981 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001983 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984}
1985
1986void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1987 Register class_reg) {
1988 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1989 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1990 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1991 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1992 __ Sync(0);
1993 __ Bind(slow_path->GetExitLabel());
1994}
1995
1996void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1997 __ Sync(0); // Only stype 0 is supported.
1998}
1999
2000void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2001 HBasicBlock* successor) {
2002 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002003 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2004
2005 if (slow_path == nullptr) {
2006 slow_path =
2007 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2008 instruction->SetSlowPath(slow_path);
2009 codegen_->AddSlowPath(slow_path);
2010 if (successor != nullptr) {
2011 DCHECK(successor->IsLoopHeader());
2012 }
2013 } else {
2014 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2015 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016
2017 __ LoadFromOffset(kLoadUnsignedHalfword,
2018 TMP,
2019 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002020 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 if (successor == nullptr) {
2022 __ Bnez(TMP, slow_path->GetEntryLabel());
2023 __ Bind(slow_path->GetReturnLabel());
2024 } else {
2025 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2026 __ B(slow_path->GetEntryLabel());
2027 // slow_path will return to GetLabelOf(successor).
2028 }
2029}
2030
2031InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2032 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002033 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034 assembler_(codegen->GetAssembler()),
2035 codegen_(codegen) {}
2036
2037void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2038 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002039 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043 locations->SetInAt(0, Location::RequiresRegister());
2044 HInstruction* right = instruction->InputAt(1);
2045 bool can_use_imm = false;
2046 if (right->IsConstant()) {
2047 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2048 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2049 can_use_imm = IsUint<16>(imm);
2050 } else if (instruction->IsAdd()) {
2051 can_use_imm = IsInt<16>(imm);
2052 } else {
2053 DCHECK(instruction->IsSub());
2054 can_use_imm = IsInt<16>(-imm);
2055 }
2056 }
2057 if (can_use_imm)
2058 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2059 else
2060 locations->SetInAt(1, Location::RequiresRegister());
2061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2062 break;
2063 }
2064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002067 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2068 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002069 break;
2070 }
2071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 case DataType::Type::kFloat32:
2073 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 DCHECK(instruction->IsAdd() || instruction->IsSub());
2075 locations->SetInAt(0, Location::RequiresFpuRegister());
2076 locations->SetInAt(1, Location::RequiresFpuRegister());
2077 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2078 break;
2079
2080 default:
2081 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2082 }
2083}
2084
2085void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 LocationSummary* locations = instruction->GetLocations();
2088
2089 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002091 Register dst = locations->Out().AsRegister<Register>();
2092 Register lhs = locations->InAt(0).AsRegister<Register>();
2093 Location rhs_location = locations->InAt(1);
2094
2095 Register rhs_reg = ZERO;
2096 int32_t rhs_imm = 0;
2097 bool use_imm = rhs_location.IsConstant();
2098 if (use_imm) {
2099 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2100 } else {
2101 rhs_reg = rhs_location.AsRegister<Register>();
2102 }
2103
2104 if (instruction->IsAnd()) {
2105 if (use_imm)
2106 __ Andi(dst, lhs, rhs_imm);
2107 else
2108 __ And(dst, lhs, rhs_reg);
2109 } else if (instruction->IsOr()) {
2110 if (use_imm)
2111 __ Ori(dst, lhs, rhs_imm);
2112 else
2113 __ Or(dst, lhs, rhs_reg);
2114 } else if (instruction->IsXor()) {
2115 if (use_imm)
2116 __ Xori(dst, lhs, rhs_imm);
2117 else
2118 __ Xor(dst, lhs, rhs_reg);
2119 } else if (instruction->IsAdd()) {
2120 if (use_imm)
2121 __ Addiu(dst, lhs, rhs_imm);
2122 else
2123 __ Addu(dst, lhs, rhs_reg);
2124 } else {
2125 DCHECK(instruction->IsSub());
2126 if (use_imm)
2127 __ Addiu(dst, lhs, -rhs_imm);
2128 else
2129 __ Subu(dst, lhs, rhs_reg);
2130 }
2131 break;
2132 }
2133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002134 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002135 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2136 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2137 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2138 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002139 Location rhs_location = locations->InAt(1);
2140 bool use_imm = rhs_location.IsConstant();
2141 if (!use_imm) {
2142 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2143 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2144 if (instruction->IsAnd()) {
2145 __ And(dst_low, lhs_low, rhs_low);
2146 __ And(dst_high, lhs_high, rhs_high);
2147 } else if (instruction->IsOr()) {
2148 __ Or(dst_low, lhs_low, rhs_low);
2149 __ Or(dst_high, lhs_high, rhs_high);
2150 } else if (instruction->IsXor()) {
2151 __ Xor(dst_low, lhs_low, rhs_low);
2152 __ Xor(dst_high, lhs_high, rhs_high);
2153 } else if (instruction->IsAdd()) {
2154 if (lhs_low == rhs_low) {
2155 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2156 __ Slt(TMP, lhs_low, ZERO);
2157 __ Addu(dst_low, lhs_low, rhs_low);
2158 } else {
2159 __ Addu(dst_low, lhs_low, rhs_low);
2160 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2161 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2162 }
2163 __ Addu(dst_high, lhs_high, rhs_high);
2164 __ Addu(dst_high, dst_high, TMP);
2165 } else {
2166 DCHECK(instruction->IsSub());
2167 __ Sltu(TMP, lhs_low, rhs_low);
2168 __ Subu(dst_low, lhs_low, rhs_low);
2169 __ Subu(dst_high, lhs_high, rhs_high);
2170 __ Subu(dst_high, dst_high, TMP);
2171 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002172 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002173 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2174 if (instruction->IsOr()) {
2175 uint32_t low = Low32Bits(value);
2176 uint32_t high = High32Bits(value);
2177 if (IsUint<16>(low)) {
2178 if (dst_low != lhs_low || low != 0) {
2179 __ Ori(dst_low, lhs_low, low);
2180 }
2181 } else {
2182 __ LoadConst32(TMP, low);
2183 __ Or(dst_low, lhs_low, TMP);
2184 }
2185 if (IsUint<16>(high)) {
2186 if (dst_high != lhs_high || high != 0) {
2187 __ Ori(dst_high, lhs_high, high);
2188 }
2189 } else {
2190 if (high != low) {
2191 __ LoadConst32(TMP, high);
2192 }
2193 __ Or(dst_high, lhs_high, TMP);
2194 }
2195 } else if (instruction->IsXor()) {
2196 uint32_t low = Low32Bits(value);
2197 uint32_t high = High32Bits(value);
2198 if (IsUint<16>(low)) {
2199 if (dst_low != lhs_low || low != 0) {
2200 __ Xori(dst_low, lhs_low, low);
2201 }
2202 } else {
2203 __ LoadConst32(TMP, low);
2204 __ Xor(dst_low, lhs_low, TMP);
2205 }
2206 if (IsUint<16>(high)) {
2207 if (dst_high != lhs_high || high != 0) {
2208 __ Xori(dst_high, lhs_high, high);
2209 }
2210 } else {
2211 if (high != low) {
2212 __ LoadConst32(TMP, high);
2213 }
2214 __ Xor(dst_high, lhs_high, TMP);
2215 }
2216 } else if (instruction->IsAnd()) {
2217 uint32_t low = Low32Bits(value);
2218 uint32_t high = High32Bits(value);
2219 if (IsUint<16>(low)) {
2220 __ Andi(dst_low, lhs_low, low);
2221 } else if (low != 0xFFFFFFFF) {
2222 __ LoadConst32(TMP, low);
2223 __ And(dst_low, lhs_low, TMP);
2224 } else if (dst_low != lhs_low) {
2225 __ Move(dst_low, lhs_low);
2226 }
2227 if (IsUint<16>(high)) {
2228 __ Andi(dst_high, lhs_high, high);
2229 } else if (high != 0xFFFFFFFF) {
2230 if (high != low) {
2231 __ LoadConst32(TMP, high);
2232 }
2233 __ And(dst_high, lhs_high, TMP);
2234 } else if (dst_high != lhs_high) {
2235 __ Move(dst_high, lhs_high);
2236 }
2237 } else {
2238 if (instruction->IsSub()) {
2239 value = -value;
2240 } else {
2241 DCHECK(instruction->IsAdd());
2242 }
2243 int32_t low = Low32Bits(value);
2244 int32_t high = High32Bits(value);
2245 if (IsInt<16>(low)) {
2246 if (dst_low != lhs_low || low != 0) {
2247 __ Addiu(dst_low, lhs_low, low);
2248 }
2249 if (low != 0) {
2250 __ Sltiu(AT, dst_low, low);
2251 }
2252 } else {
2253 __ LoadConst32(TMP, low);
2254 __ Addu(dst_low, lhs_low, TMP);
2255 __ Sltu(AT, dst_low, TMP);
2256 }
2257 if (IsInt<16>(high)) {
2258 if (dst_high != lhs_high || high != 0) {
2259 __ Addiu(dst_high, lhs_high, high);
2260 }
2261 } else {
2262 if (high != low) {
2263 __ LoadConst32(TMP, high);
2264 }
2265 __ Addu(dst_high, lhs_high, TMP);
2266 }
2267 if (low != 0) {
2268 __ Addu(dst_high, dst_high, AT);
2269 }
2270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 }
2272 break;
2273 }
2274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 case DataType::Type::kFloat32:
2276 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2278 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2279 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2280 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 __ AddS(dst, lhs, rhs);
2283 } else {
2284 __ AddD(dst, lhs, rhs);
2285 }
2286 } else {
2287 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289 __ SubS(dst, lhs, rhs);
2290 } else {
2291 __ SubD(dst, lhs, rhs);
2292 }
2293 }
2294 break;
2295 }
2296
2297 default:
2298 LOG(FATAL) << "Unexpected binary operation type " << type;
2299 }
2300}
2301
2302void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002303 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002304
Vladimir Markoca6fff82017-10-03 14:49:14 +01002305 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002307 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002309 locations->SetInAt(0, Location::RequiresRegister());
2310 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2312 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 locations->SetInAt(0, Location::RequiresRegister());
2315 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2316 locations->SetOut(Location::RequiresRegister());
2317 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002318 default:
2319 LOG(FATAL) << "Unexpected shift type " << type;
2320 }
2321}
2322
2323static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2324
2325void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002326 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329
2330 Location rhs_location = locations->InAt(1);
2331 bool use_imm = rhs_location.IsConstant();
2332 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2333 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002334 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002336 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2338 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002339
2340 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002342 Register dst = locations->Out().AsRegister<Register>();
2343 Register lhs = locations->InAt(0).AsRegister<Register>();
2344 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 if (shift_value == 0) {
2346 if (dst != lhs) {
2347 __ Move(dst, lhs);
2348 }
2349 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 __ Sll(dst, lhs, shift_value);
2351 } else if (instr->IsShr()) {
2352 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002353 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002355 } else {
2356 if (has_ins_rotr) {
2357 __ Rotr(dst, lhs, shift_value);
2358 } else {
2359 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2360 __ Srl(dst, lhs, shift_value);
2361 __ Or(dst, dst, TMP);
2362 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002363 }
2364 } else {
2365 if (instr->IsShl()) {
2366 __ Sllv(dst, lhs, rhs_reg);
2367 } else if (instr->IsShr()) {
2368 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002369 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002371 } else {
2372 if (has_ins_rotr) {
2373 __ Rotrv(dst, lhs, rhs_reg);
2374 } else {
2375 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002376 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2377 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2378 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2379 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2380 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002381 __ Sllv(TMP, lhs, TMP);
2382 __ Srlv(dst, lhs, rhs_reg);
2383 __ Or(dst, dst, TMP);
2384 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 }
2386 }
2387 break;
2388 }
2389
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002390 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002391 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2392 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2393 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2394 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2395 if (use_imm) {
2396 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002397 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002399 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002400 if (instr->IsShl()) {
2401 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2402 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2403 __ Sll(dst_low, lhs_low, shift_value);
2404 } else if (instr->IsShr()) {
2405 __ Srl(dst_low, lhs_low, shift_value);
2406 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2407 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002408 } else if (instr->IsUShr()) {
2409 __ Srl(dst_low, lhs_low, shift_value);
2410 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2411 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002412 } else {
2413 __ Srl(dst_low, lhs_low, shift_value);
2414 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2415 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002417 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002419 if (instr->IsShl()) {
2420 __ Sll(dst_low, lhs_low, shift_value);
2421 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2422 __ Sll(dst_high, lhs_high, shift_value);
2423 __ Or(dst_high, dst_high, TMP);
2424 } else if (instr->IsShr()) {
2425 __ Sra(dst_high, lhs_high, shift_value);
2426 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2427 __ Srl(dst_low, lhs_low, shift_value);
2428 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002430 __ Srl(dst_high, lhs_high, shift_value);
2431 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2432 __ Srl(dst_low, lhs_low, shift_value);
2433 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else {
2435 __ Srl(TMP, lhs_low, shift_value);
2436 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2437 __ Or(dst_low, dst_low, TMP);
2438 __ Srl(TMP, lhs_high, shift_value);
2439 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2440 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002441 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 }
2443 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002444 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002446 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002447 __ Move(dst_low, ZERO);
2448 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002449 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002451 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002452 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002453 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002454 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002455 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002456 // 64-bit rotation by 32 is just a swap.
2457 __ Move(dst_low, lhs_high);
2458 __ Move(dst_high, lhs_low);
2459 } else {
2460 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002461 __ Srl(dst_low, lhs_high, shift_value_high);
2462 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2463 __ Srl(dst_high, lhs_low, shift_value_high);
2464 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002465 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002466 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2467 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002468 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002469 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2470 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002471 __ Or(dst_high, dst_high, TMP);
2472 }
2473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002474 }
2475 }
2476 } else {
2477 MipsLabel done;
2478 if (instr->IsShl()) {
2479 __ Sllv(dst_low, lhs_low, rhs_reg);
2480 __ Nor(AT, ZERO, rhs_reg);
2481 __ Srl(TMP, lhs_low, 1);
2482 __ Srlv(TMP, TMP, AT);
2483 __ Sllv(dst_high, lhs_high, rhs_reg);
2484 __ Or(dst_high, dst_high, TMP);
2485 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2486 __ Beqz(TMP, &done);
2487 __ Move(dst_high, dst_low);
2488 __ Move(dst_low, ZERO);
2489 } else if (instr->IsShr()) {
2490 __ Srav(dst_high, lhs_high, rhs_reg);
2491 __ Nor(AT, ZERO, rhs_reg);
2492 __ Sll(TMP, lhs_high, 1);
2493 __ Sllv(TMP, TMP, AT);
2494 __ Srlv(dst_low, lhs_low, rhs_reg);
2495 __ Or(dst_low, dst_low, TMP);
2496 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2497 __ Beqz(TMP, &done);
2498 __ Move(dst_low, dst_high);
2499 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002500 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002501 __ Srlv(dst_high, lhs_high, rhs_reg);
2502 __ Nor(AT, ZERO, rhs_reg);
2503 __ Sll(TMP, lhs_high, 1);
2504 __ Sllv(TMP, TMP, AT);
2505 __ Srlv(dst_low, lhs_low, rhs_reg);
2506 __ Or(dst_low, dst_low, TMP);
2507 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2508 __ Beqz(TMP, &done);
2509 __ Move(dst_low, dst_high);
2510 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002511 } else {
2512 __ Nor(AT, ZERO, rhs_reg);
2513 __ Srlv(TMP, lhs_low, rhs_reg);
2514 __ Sll(dst_low, lhs_high, 1);
2515 __ Sllv(dst_low, dst_low, AT);
2516 __ Or(dst_low, dst_low, TMP);
2517 __ Srlv(TMP, lhs_high, rhs_reg);
2518 __ Sll(dst_high, lhs_low, 1);
2519 __ Sllv(dst_high, dst_high, AT);
2520 __ Or(dst_high, dst_high, TMP);
2521 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2522 __ Beqz(TMP, &done);
2523 __ Move(TMP, dst_high);
2524 __ Move(dst_high, dst_low);
2525 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002526 }
2527 __ Bind(&done);
2528 }
2529 break;
2530 }
2531
2532 default:
2533 LOG(FATAL) << "Unexpected shift operation type " << type;
2534 }
2535}
2536
2537void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2538 HandleBinaryOp(instruction);
2539}
2540
2541void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2542 HandleBinaryOp(instruction);
2543}
2544
2545void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2546 HandleBinaryOp(instruction);
2547}
2548
2549void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2550 HandleBinaryOp(instruction);
2551}
2552
2553void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002555 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002557 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002558 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2559 object_array_get_with_read_barrier
2560 ? LocationSummary::kCallOnSlowPath
2561 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002562 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2563 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2564 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002568 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2569 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002570 // The output overlaps in the case of an object array get with
2571 // read barriers enabled: we do not want the move to overwrite the
2572 // array's location, as we need it to emit the read barrier.
2573 locations->SetOut(Location::RequiresRegister(),
2574 object_array_get_with_read_barrier
2575 ? Location::kOutputOverlap
2576 : Location::kNoOutputOverlap);
2577 }
2578 // We need a temporary register for the read barrier marking slow
2579 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2580 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002581 bool temp_needed = instruction->GetIndex()->IsConstant()
2582 ? !kBakerReadBarrierThunksEnableForFields
2583 : !kBakerReadBarrierThunksEnableForArrays;
2584 if (temp_needed) {
2585 locations->AddTemp(Location::RequiresRegister());
2586 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002587 }
2588}
2589
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002590static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2591 auto null_checker = [codegen, instruction]() {
2592 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002593 };
2594 return null_checker;
2595}
2596
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2598 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002599 Location obj_loc = locations->InAt(0);
2600 Register obj = obj_loc.AsRegister<Register>();
2601 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002602 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002603 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002604 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002606 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002607 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2608 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002610 case DataType::Type::kBool:
2611 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002612 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 if (index.IsConstant()) {
2614 size_t offset =
2615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002616 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 } else {
2618 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002619 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 }
2621 break;
2622 }
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002625 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002626 if (index.IsConstant()) {
2627 size_t offset =
2628 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002629 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002630 } else {
2631 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002632 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 }
2634 break;
2635 }
2636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002638 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002639 if (maybe_compressed_char_at) {
2640 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2641 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2642 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2643 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2644 "Expecting 0=compressed, 1=uncompressed");
2645 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002646 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002647 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2648 if (maybe_compressed_char_at) {
2649 MipsLabel uncompressed_load, done;
2650 __ Bnez(TMP, &uncompressed_load);
2651 __ LoadFromOffset(kLoadUnsignedByte,
2652 out,
2653 obj,
2654 data_offset + (const_index << TIMES_1));
2655 __ B(&done);
2656 __ Bind(&uncompressed_load);
2657 __ LoadFromOffset(kLoadUnsignedHalfword,
2658 out,
2659 obj,
2660 data_offset + (const_index << TIMES_2));
2661 __ Bind(&done);
2662 } else {
2663 __ LoadFromOffset(kLoadUnsignedHalfword,
2664 out,
2665 obj,
2666 data_offset + (const_index << TIMES_2),
2667 null_checker);
2668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002670 Register index_reg = index.AsRegister<Register>();
2671 if (maybe_compressed_char_at) {
2672 MipsLabel uncompressed_load, done;
2673 __ Bnez(TMP, &uncompressed_load);
2674 __ Addu(TMP, obj, index_reg);
2675 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2676 __ B(&done);
2677 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002678 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002679 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2680 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002681 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2682 __ Addu(TMP, index_reg, obj);
2683 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002684 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002685 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002686 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2687 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 }
2689 break;
2690 }
2691
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002692 case DataType::Type::kInt16: {
2693 Register out = out_loc.AsRegister<Register>();
2694 if (index.IsConstant()) {
2695 size_t offset =
2696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2697 __ LoadFromOffset(kLoadSignedHalfword, 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(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002701 } else {
2702 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2703 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2704 }
2705 break;
2706 }
2707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002710 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 if (index.IsConstant()) {
2712 size_t offset =
2713 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002714 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002715 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2716 __ Addu(TMP, index.AsRegister<Register>(), obj);
2717 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002718 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002719 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002720 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002721 }
2722 break;
2723 }
2724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002726 static_assert(
2727 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2728 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2729 // /* HeapReference<Object> */ out =
2730 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2731 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002732 bool temp_needed = index.IsConstant()
2733 ? !kBakerReadBarrierThunksEnableForFields
2734 : !kBakerReadBarrierThunksEnableForArrays;
2735 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002736 // Note that a potential implicit null check is handled in this
2737 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002738 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2739 if (index.IsConstant()) {
2740 // Array load with a constant index can be treated as a field load.
2741 size_t offset =
2742 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2743 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2744 out_loc,
2745 obj,
2746 offset,
2747 temp,
2748 /* needs_null_check */ false);
2749 } else {
2750 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2751 out_loc,
2752 obj,
2753 data_offset,
2754 index,
2755 temp,
2756 /* needs_null_check */ false);
2757 }
Alexey Frunze15958152017-02-09 19:08:30 -08002758 } else {
2759 Register out = out_loc.AsRegister<Register>();
2760 if (index.IsConstant()) {
2761 size_t offset =
2762 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2763 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2764 // If read barriers are enabled, emit read barriers other than
2765 // Baker's using a slow path (and also unpoison the loaded
2766 // reference, if heap poisoning is enabled).
2767 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2768 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002769 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002770 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2771 // If read barriers are enabled, emit read barriers other than
2772 // Baker's using a slow path (and also unpoison the loaded
2773 // reference, if heap poisoning is enabled).
2774 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2775 out_loc,
2776 out_loc,
2777 obj_loc,
2778 data_offset,
2779 index);
2780 }
2781 }
2782 break;
2783 }
2784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002786 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002787 if (index.IsConstant()) {
2788 size_t offset =
2789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002790 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002791 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2792 __ Addu(TMP, index.AsRegister<Register>(), obj);
2793 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002794 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002795 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002796 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 }
2798 break;
2799 }
2800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002802 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803 if (index.IsConstant()) {
2804 size_t offset =
2805 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002806 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002807 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2808 __ Addu(TMP, index.AsRegister<Register>(), obj);
2809 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002811 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002812 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 }
2814 break;
2815 }
2816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002818 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002819 if (index.IsConstant()) {
2820 size_t offset =
2821 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002822 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002823 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2824 __ Addu(TMP, index.AsRegister<Register>(), obj);
2825 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002827 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002828 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002829 }
2830 break;
2831 }
2832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002834 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2835 UNREACHABLE();
2836 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837}
2838
2839void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002840 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002841 locations->SetInAt(0, Location::RequiresRegister());
2842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2843}
2844
2845void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2846 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002847 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002848 Register obj = locations->InAt(0).AsRegister<Register>();
2849 Register out = locations->Out().AsRegister<Register>();
2850 __ LoadFromOffset(kLoadWord, out, obj, offset);
2851 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002852 // Mask out compression flag from String's array length.
2853 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2854 __ Srl(out, out, 1u);
2855 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002856}
2857
Alexey Frunzef58b2482016-09-02 22:14:06 -07002858Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2859 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2860 ? Location::ConstantLocation(instruction->AsConstant())
2861 : Location::RequiresRegister();
2862}
2863
2864Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2865 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2866 // We can store a non-zero float or double constant without first loading it into the FPU,
2867 // but we should only prefer this if the constant has a single use.
2868 if (instruction->IsConstant() &&
2869 (instruction->AsConstant()->IsZeroBitPattern() ||
2870 instruction->GetUses().HasExactlyOneElement())) {
2871 return Location::ConstantLocation(instruction->AsConstant());
2872 // Otherwise fall through and require an FPU register for the constant.
2873 }
2874 return Location::RequiresFpuRegister();
2875}
2876
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002877void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002879
2880 bool needs_write_barrier =
2881 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2882 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2883
Vladimir Markoca6fff82017-10-03 14:49:14 +01002884 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002885 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002886 may_need_runtime_call_for_type_check ?
2887 LocationSummary::kCallOnSlowPath :
2888 LocationSummary::kNoCall);
2889
2890 locations->SetInAt(0, Location::RequiresRegister());
2891 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002893 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002894 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002895 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2896 }
2897 if (needs_write_barrier) {
2898 // Temporary register for the write barrier.
2899 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900 }
2901}
2902
2903void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2904 LocationSummary* locations = instruction->GetLocations();
2905 Register obj = locations->InAt(0).AsRegister<Register>();
2906 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002907 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002909 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002910 bool needs_write_barrier =
2911 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002912 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002913 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914
2915 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002917 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002921 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002922 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002923 __ Addu(base_reg, obj, index.AsRegister<Register>());
2924 }
2925 if (value_location.IsConstant()) {
2926 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2927 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2928 } else {
2929 Register value = value_location.AsRegister<Register>();
2930 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 }
2932 break;
2933 }
2934
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002935 case DataType::Type::kUint16:
2936 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002939 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002940 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2941 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002942 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002943 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002944 }
2945 if (value_location.IsConstant()) {
2946 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2947 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2948 } else {
2949 Register value = value_location.AsRegister<Register>();
2950 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002951 }
2952 break;
2953 }
2954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002956 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2957 if (index.IsConstant()) {
2958 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002959 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2960 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002961 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002962 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002963 }
2964 if (value_location.IsConstant()) {
2965 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2966 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2967 } else {
2968 Register value = value_location.AsRegister<Register>();
2969 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2970 }
2971 break;
2972 }
2973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002975 if (value_location.IsConstant()) {
2976 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002977 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002978 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002979 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002980 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002981 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002982 }
Alexey Frunze15958152017-02-09 19:08:30 -08002983 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2984 DCHECK_EQ(value, 0);
2985 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2986 DCHECK(!needs_write_barrier);
2987 DCHECK(!may_need_runtime_call_for_type_check);
2988 break;
2989 }
2990
2991 DCHECK(needs_write_barrier);
2992 Register value = value_location.AsRegister<Register>();
2993 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2994 Register temp2 = TMP; // Doesn't need to survive slow path.
2995 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2996 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2997 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2998 MipsLabel done;
2999 SlowPathCodeMIPS* slow_path = nullptr;
3000
3001 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003002 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003003 codegen_->AddSlowPath(slow_path);
3004 if (instruction->GetValueCanBeNull()) {
3005 MipsLabel non_zero;
3006 __ Bnez(value, &non_zero);
3007 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3008 if (index.IsConstant()) {
3009 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003010 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3011 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003012 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003013 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003014 }
Alexey Frunze15958152017-02-09 19:08:30 -08003015 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3016 __ B(&done);
3017 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003018 }
Alexey Frunze15958152017-02-09 19:08:30 -08003019
3020 // Note that when read barriers are enabled, the type checks
3021 // are performed without read barriers. This is fine, even in
3022 // the case where a class object is in the from-space after
3023 // the flip, as a comparison involving such a type would not
3024 // produce a false positive; it may of course produce a false
3025 // negative, in which case we would take the ArraySet slow
3026 // path.
3027
3028 // /* HeapReference<Class> */ temp1 = obj->klass_
3029 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3030 __ MaybeUnpoisonHeapReference(temp1);
3031
3032 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3033 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3034 // /* HeapReference<Class> */ temp2 = value->klass_
3035 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3036 // If heap poisoning is enabled, no need to unpoison `temp1`
3037 // nor `temp2`, as we are comparing two poisoned references.
3038
3039 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3040 MipsLabel do_put;
3041 __ Beq(temp1, temp2, &do_put);
3042 // If heap poisoning is enabled, the `temp1` reference has
3043 // not been unpoisoned yet; unpoison it now.
3044 __ MaybeUnpoisonHeapReference(temp1);
3045
3046 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3047 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3048 // If heap poisoning is enabled, no need to unpoison
3049 // `temp1`, as we are comparing against null below.
3050 __ Bnez(temp1, slow_path->GetEntryLabel());
3051 __ Bind(&do_put);
3052 } else {
3053 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3054 }
3055 }
3056
3057 Register source = value;
3058 if (kPoisonHeapReferences) {
3059 // Note that in the case where `value` is a null reference,
3060 // we do not enter this block, as a null reference does not
3061 // need poisoning.
3062 __ Move(temp1, value);
3063 __ PoisonHeapReference(temp1);
3064 source = temp1;
3065 }
3066
3067 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3068 if (index.IsConstant()) {
3069 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003070 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003071 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003072 }
3073 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3074
3075 if (!may_need_runtime_call_for_type_check) {
3076 codegen_->MaybeRecordImplicitNullCheck(instruction);
3077 }
3078
3079 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3080
3081 if (done.IsLinked()) {
3082 __ Bind(&done);
3083 }
3084
3085 if (slow_path != nullptr) {
3086 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003087 }
3088 break;
3089 }
3090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003092 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003094 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003095 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3096 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003098 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003099 }
3100 if (value_location.IsConstant()) {
3101 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3102 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3103 } else {
3104 Register value = value_location.AsRegisterPairLow<Register>();
3105 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003106 }
3107 break;
3108 }
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003114 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3115 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003116 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003117 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003118 }
3119 if (value_location.IsConstant()) {
3120 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3121 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3122 } else {
3123 FRegister value = value_location.AsFpuRegister<FRegister>();
3124 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003125 }
3126 break;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003132 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003133 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3134 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003136 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003137 }
3138 if (value_location.IsConstant()) {
3139 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3140 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3141 } else {
3142 FRegister value = value_location.AsFpuRegister<FRegister>();
3143 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003144 }
3145 break;
3146 }
3147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003149 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3150 UNREACHABLE();
3151 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003152}
3153
Lena Djokica2901602017-09-21 13:50:52 +02003154void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3155 HIntermediateArrayAddressIndex* instruction) {
3156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003157 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003158
3159 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3160
3161 locations->SetInAt(0, Location::RequiresRegister());
3162 locations->SetInAt(1, Location::ConstantLocation(shift));
3163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3164}
3165
3166void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3167 HIntermediateArrayAddressIndex* instruction) {
3168 LocationSummary* locations = instruction->GetLocations();
3169 Register index_reg = locations->InAt(0).AsRegister<Register>();
3170 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3171 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3172}
3173
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003174void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003175 RegisterSet caller_saves = RegisterSet::Empty();
3176 InvokeRuntimeCallingConvention calling_convention;
3177 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3178 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3179 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003180 locations->SetInAt(0, Location::RequiresRegister());
3181 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003182}
3183
3184void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3185 LocationSummary* locations = instruction->GetLocations();
3186 BoundsCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003187 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003188 codegen_->AddSlowPath(slow_path);
3189
3190 Register index = locations->InAt(0).AsRegister<Register>();
3191 Register length = locations->InAt(1).AsRegister<Register>();
3192
3193 // length is limited by the maximum positive signed 32-bit integer.
3194 // Unsigned comparison of length and index checks for index < 0
3195 // and for length <= index simultaneously.
3196 __ Bgeu(index, length, slow_path->GetEntryLabel());
3197}
3198
Alexey Frunze15958152017-02-09 19:08:30 -08003199// Temp is used for read barrier.
3200static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3201 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003202 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003203 (kUseBakerReadBarrier ||
3204 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3205 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3206 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3207 return 1;
3208 }
3209 return 0;
3210}
3211
3212// Extra temp is used for read barrier.
3213static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3214 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3215}
3216
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003217void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003218 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3219 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3220
3221 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3222 switch (type_check_kind) {
3223 case TypeCheckKind::kExactCheck:
3224 case TypeCheckKind::kAbstractClassCheck:
3225 case TypeCheckKind::kClassHierarchyCheck:
3226 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003227 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003228 ? LocationSummary::kCallOnSlowPath
3229 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3230 break;
3231 case TypeCheckKind::kArrayCheck:
3232 case TypeCheckKind::kUnresolvedCheck:
3233 case TypeCheckKind::kInterfaceCheck:
3234 call_kind = LocationSummary::kCallOnSlowPath;
3235 break;
3236 }
3237
Vladimir Markoca6fff82017-10-03 14:49:14 +01003238 LocationSummary* locations =
3239 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240 locations->SetInAt(0, Location::RequiresRegister());
3241 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003242 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003243}
3244
3245void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003246 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003248 Location obj_loc = locations->InAt(0);
3249 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003250 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003251 Location temp_loc = locations->GetTemp(0);
3252 Register temp = temp_loc.AsRegister<Register>();
3253 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3254 DCHECK_LE(num_temps, 2u);
3255 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003256 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3257 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3258 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3259 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3260 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3261 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3262 const uint32_t object_array_data_offset =
3263 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3264 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003265
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003266 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3267 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3268 // read barriers is done for performance and code size reasons.
3269 bool is_type_check_slow_path_fatal = false;
3270 if (!kEmitCompilerReadBarrier) {
3271 is_type_check_slow_path_fatal =
3272 (type_check_kind == TypeCheckKind::kExactCheck ||
3273 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3274 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3275 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3276 !instruction->CanThrowIntoCatchBlock();
3277 }
3278 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003279 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3280 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003281 codegen_->AddSlowPath(slow_path);
3282
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003283 // Avoid this check if we know `obj` is not null.
3284 if (instruction->MustDoNullCheck()) {
3285 __ Beqz(obj, &done);
3286 }
3287
3288 switch (type_check_kind) {
3289 case TypeCheckKind::kExactCheck:
3290 case TypeCheckKind::kArrayCheck: {
3291 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003292 GenerateReferenceLoadTwoRegisters(instruction,
3293 temp_loc,
3294 obj_loc,
3295 class_offset,
3296 maybe_temp2_loc,
3297 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003298 // Jump to slow path for throwing the exception or doing a
3299 // more involved array check.
3300 __ Bne(temp, cls, slow_path->GetEntryLabel());
3301 break;
3302 }
3303
3304 case TypeCheckKind::kAbstractClassCheck: {
3305 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003306 GenerateReferenceLoadTwoRegisters(instruction,
3307 temp_loc,
3308 obj_loc,
3309 class_offset,
3310 maybe_temp2_loc,
3311 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003312 // If the class is abstract, we eagerly fetch the super class of the
3313 // object to avoid doing a comparison we know will fail.
3314 MipsLabel loop;
3315 __ Bind(&loop);
3316 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003317 GenerateReferenceLoadOneRegister(instruction,
3318 temp_loc,
3319 super_offset,
3320 maybe_temp2_loc,
3321 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003322 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3323 // exception.
3324 __ Beqz(temp, slow_path->GetEntryLabel());
3325 // Otherwise, compare the classes.
3326 __ Bne(temp, cls, &loop);
3327 break;
3328 }
3329
3330 case TypeCheckKind::kClassHierarchyCheck: {
3331 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003332 GenerateReferenceLoadTwoRegisters(instruction,
3333 temp_loc,
3334 obj_loc,
3335 class_offset,
3336 maybe_temp2_loc,
3337 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003338 // Walk over the class hierarchy to find a match.
3339 MipsLabel loop;
3340 __ Bind(&loop);
3341 __ Beq(temp, cls, &done);
3342 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003343 GenerateReferenceLoadOneRegister(instruction,
3344 temp_loc,
3345 super_offset,
3346 maybe_temp2_loc,
3347 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003348 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3349 // exception. Otherwise, jump to the beginning of the loop.
3350 __ Bnez(temp, &loop);
3351 __ B(slow_path->GetEntryLabel());
3352 break;
3353 }
3354
3355 case TypeCheckKind::kArrayObjectCheck: {
3356 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003357 GenerateReferenceLoadTwoRegisters(instruction,
3358 temp_loc,
3359 obj_loc,
3360 class_offset,
3361 maybe_temp2_loc,
3362 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003363 // Do an exact check.
3364 __ Beq(temp, cls, &done);
3365 // Otherwise, we need to check that the object's class is a non-primitive array.
3366 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003367 GenerateReferenceLoadOneRegister(instruction,
3368 temp_loc,
3369 component_offset,
3370 maybe_temp2_loc,
3371 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003372 // If the component type is null, jump to the slow path to throw the exception.
3373 __ Beqz(temp, slow_path->GetEntryLabel());
3374 // Otherwise, the object is indeed an array, further check that this component
3375 // type is not a primitive type.
3376 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3377 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3378 __ Bnez(temp, slow_path->GetEntryLabel());
3379 break;
3380 }
3381
3382 case TypeCheckKind::kUnresolvedCheck:
3383 // We always go into the type check slow path for the unresolved check case.
3384 // We cannot directly call the CheckCast runtime entry point
3385 // without resorting to a type checking slow path here (i.e. by
3386 // calling InvokeRuntime directly), as it would require to
3387 // assign fixed registers for the inputs of this HInstanceOf
3388 // instruction (following the runtime calling convention), which
3389 // might be cluttered by the potential first read barrier
3390 // emission at the beginning of this method.
3391 __ B(slow_path->GetEntryLabel());
3392 break;
3393
3394 case TypeCheckKind::kInterfaceCheck: {
3395 // Avoid read barriers to improve performance of the fast path. We can not get false
3396 // positives by doing this.
3397 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003398 GenerateReferenceLoadTwoRegisters(instruction,
3399 temp_loc,
3400 obj_loc,
3401 class_offset,
3402 maybe_temp2_loc,
3403 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003404 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003405 GenerateReferenceLoadTwoRegisters(instruction,
3406 temp_loc,
3407 temp_loc,
3408 iftable_offset,
3409 maybe_temp2_loc,
3410 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003411 // Iftable is never null.
3412 __ Lw(TMP, temp, array_length_offset);
3413 // Loop through the iftable and check if any class matches.
3414 MipsLabel loop;
3415 __ Bind(&loop);
3416 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3417 __ Beqz(TMP, slow_path->GetEntryLabel());
3418 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3419 __ MaybeUnpoisonHeapReference(AT);
3420 // Go to next interface.
3421 __ Addiu(TMP, TMP, -2);
3422 // Compare the classes and continue the loop if they do not match.
3423 __ Bne(AT, cls, &loop);
3424 break;
3425 }
3426 }
3427
3428 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003429 __ Bind(slow_path->GetExitLabel());
3430}
3431
3432void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3433 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003434 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003435 locations->SetInAt(0, Location::RequiresRegister());
3436 if (check->HasUses()) {
3437 locations->SetOut(Location::SameAsFirstInput());
3438 }
3439}
3440
3441void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3442 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003443 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444 check->GetLoadClass(),
3445 check,
3446 check->GetDexPc(),
3447 true);
3448 codegen_->AddSlowPath(slow_path);
3449 GenerateClassInitializationCheck(slow_path,
3450 check->GetLocations()->InAt(0).AsRegister<Register>());
3451}
3452
3453void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003454 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003455
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003456 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003457 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003458
3459 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003460 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003461 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003464 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003466 locations->SetInAt(0, Location::RequiresRegister());
3467 locations->SetInAt(1, Location::RequiresRegister());
3468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3469 break;
3470
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003471 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003472 locations->SetInAt(0, Location::RequiresRegister());
3473 locations->SetInAt(1, Location::RequiresRegister());
3474 // Output overlaps because it is written before doing the low comparison.
3475 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3476 break;
3477
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003478 case DataType::Type::kFloat32:
3479 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003480 locations->SetInAt(0, Location::RequiresFpuRegister());
3481 locations->SetInAt(1, Location::RequiresFpuRegister());
3482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003483 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003484
3485 default:
3486 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3487 }
3488}
3489
3490void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3491 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003492 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003493 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003494 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003495
3496 // 0 if: left == right
3497 // 1 if: left > right
3498 // -1 if: left < right
3499 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003501 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003504 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003505 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003506 Register lhs = locations->InAt(0).AsRegister<Register>();
3507 Register rhs = locations->InAt(1).AsRegister<Register>();
3508 __ Slt(TMP, lhs, rhs);
3509 __ Slt(res, rhs, lhs);
3510 __ Subu(res, res, TMP);
3511 break;
3512 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003513 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003514 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003515 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3516 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3517 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3518 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3519 // TODO: more efficient (direct) comparison with a constant.
3520 __ Slt(TMP, lhs_high, rhs_high);
3521 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3522 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3523 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3524 __ Sltu(TMP, lhs_low, rhs_low);
3525 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3526 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3527 __ Bind(&done);
3528 break;
3529 }
3530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003531 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003532 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003533 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3534 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3535 MipsLabel done;
3536 if (isR6) {
3537 __ CmpEqS(FTMP, lhs, rhs);
3538 __ LoadConst32(res, 0);
3539 __ Bc1nez(FTMP, &done);
3540 if (gt_bias) {
3541 __ CmpLtS(FTMP, lhs, rhs);
3542 __ LoadConst32(res, -1);
3543 __ Bc1nez(FTMP, &done);
3544 __ LoadConst32(res, 1);
3545 } else {
3546 __ CmpLtS(FTMP, rhs, lhs);
3547 __ LoadConst32(res, 1);
3548 __ Bc1nez(FTMP, &done);
3549 __ LoadConst32(res, -1);
3550 }
3551 } else {
3552 if (gt_bias) {
3553 __ ColtS(0, lhs, rhs);
3554 __ LoadConst32(res, -1);
3555 __ Bc1t(0, &done);
3556 __ CeqS(0, lhs, rhs);
3557 __ LoadConst32(res, 1);
3558 __ Movt(res, ZERO, 0);
3559 } else {
3560 __ ColtS(0, rhs, lhs);
3561 __ LoadConst32(res, 1);
3562 __ Bc1t(0, &done);
3563 __ CeqS(0, lhs, rhs);
3564 __ LoadConst32(res, -1);
3565 __ Movt(res, ZERO, 0);
3566 }
3567 }
3568 __ Bind(&done);
3569 break;
3570 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003571 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003572 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003573 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3574 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3575 MipsLabel done;
3576 if (isR6) {
3577 __ CmpEqD(FTMP, lhs, rhs);
3578 __ LoadConst32(res, 0);
3579 __ Bc1nez(FTMP, &done);
3580 if (gt_bias) {
3581 __ CmpLtD(FTMP, lhs, rhs);
3582 __ LoadConst32(res, -1);
3583 __ Bc1nez(FTMP, &done);
3584 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003585 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003586 __ CmpLtD(FTMP, rhs, lhs);
3587 __ LoadConst32(res, 1);
3588 __ Bc1nez(FTMP, &done);
3589 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003590 }
3591 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003592 if (gt_bias) {
3593 __ ColtD(0, lhs, rhs);
3594 __ LoadConst32(res, -1);
3595 __ Bc1t(0, &done);
3596 __ CeqD(0, lhs, rhs);
3597 __ LoadConst32(res, 1);
3598 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003599 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003600 __ ColtD(0, rhs, lhs);
3601 __ LoadConst32(res, 1);
3602 __ Bc1t(0, &done);
3603 __ CeqD(0, lhs, rhs);
3604 __ LoadConst32(res, -1);
3605 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003606 }
3607 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003608 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003609 break;
3610 }
3611
3612 default:
3613 LOG(FATAL) << "Unimplemented compare type " << in_type;
3614 }
3615}
3616
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003617void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003618 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003619 switch (instruction->InputAt(0)->GetType()) {
3620 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003622 locations->SetInAt(0, Location::RequiresRegister());
3623 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3624 break;
3625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003626 case DataType::Type::kFloat32:
3627 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003628 locations->SetInAt(0, Location::RequiresFpuRegister());
3629 locations->SetInAt(1, Location::RequiresFpuRegister());
3630 break;
3631 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003632 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3634 }
3635}
3636
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003637void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003638 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003639 return;
3640 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003642 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003643 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003645 switch (type) {
3646 default:
3647 // Integer case.
3648 GenerateIntCompare(instruction->GetCondition(), locations);
3649 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003652 GenerateLongCompare(instruction->GetCondition(), locations);
3653 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 case DataType::Type::kFloat32:
3656 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003657 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3658 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003659 }
3660}
3661
Alexey Frunze7e99e052015-11-24 19:28:01 -08003662void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3663 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003664 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003665
3666 LocationSummary* locations = instruction->GetLocations();
3667 Location second = locations->InAt(1);
3668 DCHECK(second.IsConstant());
3669
3670 Register out = locations->Out().AsRegister<Register>();
3671 Register dividend = locations->InAt(0).AsRegister<Register>();
3672 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3673 DCHECK(imm == 1 || imm == -1);
3674
3675 if (instruction->IsRem()) {
3676 __ Move(out, ZERO);
3677 } else {
3678 if (imm == -1) {
3679 __ Subu(out, ZERO, dividend);
3680 } else if (out != dividend) {
3681 __ Move(out, dividend);
3682 }
3683 }
3684}
3685
3686void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3687 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003688 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003689
3690 LocationSummary* locations = instruction->GetLocations();
3691 Location second = locations->InAt(1);
3692 DCHECK(second.IsConstant());
3693
3694 Register out = locations->Out().AsRegister<Register>();
3695 Register dividend = locations->InAt(0).AsRegister<Register>();
3696 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003697 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003698 int ctz_imm = CTZ(abs_imm);
3699
3700 if (instruction->IsDiv()) {
3701 if (ctz_imm == 1) {
3702 // Fast path for division by +/-2, which is very common.
3703 __ Srl(TMP, dividend, 31);
3704 } else {
3705 __ Sra(TMP, dividend, 31);
3706 __ Srl(TMP, TMP, 32 - ctz_imm);
3707 }
3708 __ Addu(out, dividend, TMP);
3709 __ Sra(out, out, ctz_imm);
3710 if (imm < 0) {
3711 __ Subu(out, ZERO, out);
3712 }
3713 } else {
3714 if (ctz_imm == 1) {
3715 // Fast path for modulo +/-2, which is very common.
3716 __ Sra(TMP, dividend, 31);
3717 __ Subu(out, dividend, TMP);
3718 __ Andi(out, out, 1);
3719 __ Addu(out, out, TMP);
3720 } else {
3721 __ Sra(TMP, dividend, 31);
3722 __ Srl(TMP, TMP, 32 - ctz_imm);
3723 __ Addu(out, dividend, TMP);
3724 if (IsUint<16>(abs_imm - 1)) {
3725 __ Andi(out, out, abs_imm - 1);
3726 } else {
3727 __ Sll(out, out, 32 - ctz_imm);
3728 __ Srl(out, out, 32 - ctz_imm);
3729 }
3730 __ Subu(out, out, TMP);
3731 }
3732 }
3733}
3734
3735void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3736 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003738
3739 LocationSummary* locations = instruction->GetLocations();
3740 Location second = locations->InAt(1);
3741 DCHECK(second.IsConstant());
3742
3743 Register out = locations->Out().AsRegister<Register>();
3744 Register dividend = locations->InAt(0).AsRegister<Register>();
3745 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3746
3747 int64_t magic;
3748 int shift;
3749 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3750
3751 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3752
3753 __ LoadConst32(TMP, magic);
3754 if (isR6) {
3755 __ MuhR6(TMP, dividend, TMP);
3756 } else {
3757 __ MultR2(dividend, TMP);
3758 __ Mfhi(TMP);
3759 }
3760 if (imm > 0 && magic < 0) {
3761 __ Addu(TMP, TMP, dividend);
3762 } else if (imm < 0 && magic > 0) {
3763 __ Subu(TMP, TMP, dividend);
3764 }
3765
3766 if (shift != 0) {
3767 __ Sra(TMP, TMP, shift);
3768 }
3769
3770 if (instruction->IsDiv()) {
3771 __ Sra(out, TMP, 31);
3772 __ Subu(out, TMP, out);
3773 } else {
3774 __ Sra(AT, TMP, 31);
3775 __ Subu(AT, TMP, AT);
3776 __ LoadConst32(TMP, imm);
3777 if (isR6) {
3778 __ MulR6(TMP, AT, TMP);
3779 } else {
3780 __ MulR2(TMP, AT, TMP);
3781 }
3782 __ Subu(out, dividend, TMP);
3783 }
3784}
3785
3786void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3787 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003788 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003789
3790 LocationSummary* locations = instruction->GetLocations();
3791 Register out = locations->Out().AsRegister<Register>();
3792 Location second = locations->InAt(1);
3793
3794 if (second.IsConstant()) {
3795 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3796 if (imm == 0) {
3797 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3798 } else if (imm == 1 || imm == -1) {
3799 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003800 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003801 DivRemByPowerOfTwo(instruction);
3802 } else {
3803 DCHECK(imm <= -2 || imm >= 2);
3804 GenerateDivRemWithAnyConstant(instruction);
3805 }
3806 } else {
3807 Register dividend = locations->InAt(0).AsRegister<Register>();
3808 Register divisor = second.AsRegister<Register>();
3809 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3810 if (instruction->IsDiv()) {
3811 if (isR6) {
3812 __ DivR6(out, dividend, divisor);
3813 } else {
3814 __ DivR2(out, dividend, divisor);
3815 }
3816 } else {
3817 if (isR6) {
3818 __ ModR6(out, dividend, divisor);
3819 } else {
3820 __ ModR2(out, dividend, divisor);
3821 }
3822 }
3823 }
3824}
3825
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003827 DataType::Type type = div->GetResultType();
3828 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003829 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003830 : LocationSummary::kNoCall;
3831
Vladimir Markoca6fff82017-10-03 14:49:14 +01003832 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833
3834 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003836 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003837 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3839 break;
3840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003842 InvokeRuntimeCallingConvention calling_convention;
3843 locations->SetInAt(0, Location::RegisterPairLocation(
3844 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3845 locations->SetInAt(1, Location::RegisterPairLocation(
3846 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3847 locations->SetOut(calling_convention.GetReturnLocation(type));
3848 break;
3849 }
3850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kFloat32:
3852 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 locations->SetInAt(0, Location::RequiresFpuRegister());
3854 locations->SetInAt(1, Location::RequiresFpuRegister());
3855 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3856 break;
3857
3858 default:
3859 LOG(FATAL) << "Unexpected div type " << type;
3860 }
3861}
3862
3863void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003866
3867 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003869 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003870 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003871 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003872 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003873 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3874 break;
3875 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003876 case DataType::Type::kFloat32:
3877 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003878 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3879 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3880 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003881 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003882 __ DivS(dst, lhs, rhs);
3883 } else {
3884 __ DivD(dst, lhs, rhs);
3885 }
3886 break;
3887 }
3888 default:
3889 LOG(FATAL) << "Unexpected div type " << type;
3890 }
3891}
3892
3893void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003894 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003895 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896}
3897
3898void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003899 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003900 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003901 codegen_->AddSlowPath(slow_path);
3902 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003903 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003904
3905 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003906 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003907 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003908 case DataType::Type::kInt8:
3909 case DataType::Type::kUint16:
3910 case DataType::Type::kInt16:
3911 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003912 if (value.IsConstant()) {
3913 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3914 __ B(slow_path->GetEntryLabel());
3915 } else {
3916 // A division by a non-null constant is valid. We don't need to perform
3917 // any check, so simply fall through.
3918 }
3919 } else {
3920 DCHECK(value.IsRegister()) << value;
3921 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3922 }
3923 break;
3924 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003925 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003926 if (value.IsConstant()) {
3927 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3928 __ B(slow_path->GetEntryLabel());
3929 } else {
3930 // A division by a non-null constant is valid. We don't need to perform
3931 // any check, so simply fall through.
3932 }
3933 } else {
3934 DCHECK(value.IsRegisterPair()) << value;
3935 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3936 __ Beqz(TMP, slow_path->GetEntryLabel());
3937 }
3938 break;
3939 }
3940 default:
3941 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3942 }
3943}
3944
3945void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* 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::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3952 // Will be generated at use site.
3953}
3954
3955void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3956 exit->SetLocations(nullptr);
3957}
3958
3959void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3960}
3961
3962void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3963 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003964 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003965 locations->SetOut(Location::ConstantLocation(constant));
3966}
3967
3968void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3969 // Will be generated at use site.
3970}
3971
3972void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3973 got->SetLocations(nullptr);
3974}
3975
3976void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3977 DCHECK(!successor->IsExitBlock());
3978 HBasicBlock* block = got->GetBlock();
3979 HInstruction* previous = got->GetPrevious();
3980 HLoopInformation* info = block->GetLoopInformation();
3981
3982 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003983 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3984 return;
3985 }
3986 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3987 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3988 }
3989 if (!codegen_->GoesToNextBlock(block, successor)) {
3990 __ B(codegen_->GetLabelOf(successor));
3991 }
3992}
3993
3994void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3995 HandleGoto(got, got->GetSuccessor());
3996}
3997
3998void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3999 try_boundary->SetLocations(nullptr);
4000}
4001
4002void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4003 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4004 if (!successor->IsExitBlock()) {
4005 HandleGoto(try_boundary, successor);
4006 }
4007}
4008
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004009void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4010 LocationSummary* locations) {
4011 Register dst = locations->Out().AsRegister<Register>();
4012 Register lhs = locations->InAt(0).AsRegister<Register>();
4013 Location rhs_location = locations->InAt(1);
4014 Register rhs_reg = ZERO;
4015 int64_t rhs_imm = 0;
4016 bool use_imm = rhs_location.IsConstant();
4017 if (use_imm) {
4018 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4019 } else {
4020 rhs_reg = rhs_location.AsRegister<Register>();
4021 }
4022
4023 switch (cond) {
4024 case kCondEQ:
4025 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004026 if (use_imm && IsInt<16>(-rhs_imm)) {
4027 if (rhs_imm == 0) {
4028 if (cond == kCondEQ) {
4029 __ Sltiu(dst, lhs, 1);
4030 } else {
4031 __ Sltu(dst, ZERO, lhs);
4032 }
4033 } else {
4034 __ Addiu(dst, lhs, -rhs_imm);
4035 if (cond == kCondEQ) {
4036 __ Sltiu(dst, dst, 1);
4037 } else {
4038 __ Sltu(dst, ZERO, dst);
4039 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004040 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004041 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004042 if (use_imm && IsUint<16>(rhs_imm)) {
4043 __ Xori(dst, lhs, rhs_imm);
4044 } else {
4045 if (use_imm) {
4046 rhs_reg = TMP;
4047 __ LoadConst32(rhs_reg, rhs_imm);
4048 }
4049 __ Xor(dst, lhs, rhs_reg);
4050 }
4051 if (cond == kCondEQ) {
4052 __ Sltiu(dst, dst, 1);
4053 } else {
4054 __ Sltu(dst, ZERO, dst);
4055 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004056 }
4057 break;
4058
4059 case kCondLT:
4060 case kCondGE:
4061 if (use_imm && IsInt<16>(rhs_imm)) {
4062 __ Slti(dst, lhs, rhs_imm);
4063 } else {
4064 if (use_imm) {
4065 rhs_reg = TMP;
4066 __ LoadConst32(rhs_reg, rhs_imm);
4067 }
4068 __ Slt(dst, lhs, rhs_reg);
4069 }
4070 if (cond == kCondGE) {
4071 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4072 // only the slt instruction but no sge.
4073 __ Xori(dst, dst, 1);
4074 }
4075 break;
4076
4077 case kCondLE:
4078 case kCondGT:
4079 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4080 // Simulate lhs <= rhs via lhs < rhs + 1.
4081 __ Slti(dst, lhs, rhs_imm + 1);
4082 if (cond == kCondGT) {
4083 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4084 // only the slti instruction but no sgti.
4085 __ Xori(dst, dst, 1);
4086 }
4087 } else {
4088 if (use_imm) {
4089 rhs_reg = TMP;
4090 __ LoadConst32(rhs_reg, rhs_imm);
4091 }
4092 __ Slt(dst, rhs_reg, lhs);
4093 if (cond == kCondLE) {
4094 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4095 // only the slt instruction but no sle.
4096 __ Xori(dst, dst, 1);
4097 }
4098 }
4099 break;
4100
4101 case kCondB:
4102 case kCondAE:
4103 if (use_imm && IsInt<16>(rhs_imm)) {
4104 // Sltiu sign-extends its 16-bit immediate operand before
4105 // the comparison and thus lets us compare directly with
4106 // unsigned values in the ranges [0, 0x7fff] and
4107 // [0xffff8000, 0xffffffff].
4108 __ Sltiu(dst, lhs, rhs_imm);
4109 } else {
4110 if (use_imm) {
4111 rhs_reg = TMP;
4112 __ LoadConst32(rhs_reg, rhs_imm);
4113 }
4114 __ Sltu(dst, lhs, rhs_reg);
4115 }
4116 if (cond == kCondAE) {
4117 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4118 // only the sltu instruction but no sgeu.
4119 __ Xori(dst, dst, 1);
4120 }
4121 break;
4122
4123 case kCondBE:
4124 case kCondA:
4125 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4126 // Simulate lhs <= rhs via lhs < rhs + 1.
4127 // Note that this only works if rhs + 1 does not overflow
4128 // to 0, hence the check above.
4129 // Sltiu sign-extends its 16-bit immediate operand before
4130 // the comparison and thus lets us compare directly with
4131 // unsigned values in the ranges [0, 0x7fff] and
4132 // [0xffff8000, 0xffffffff].
4133 __ Sltiu(dst, lhs, rhs_imm + 1);
4134 if (cond == kCondA) {
4135 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4136 // only the sltiu instruction but no sgtiu.
4137 __ Xori(dst, dst, 1);
4138 }
4139 } else {
4140 if (use_imm) {
4141 rhs_reg = TMP;
4142 __ LoadConst32(rhs_reg, rhs_imm);
4143 }
4144 __ Sltu(dst, rhs_reg, lhs);
4145 if (cond == kCondBE) {
4146 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4147 // only the sltu instruction but no sleu.
4148 __ Xori(dst, dst, 1);
4149 }
4150 }
4151 break;
4152 }
4153}
4154
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004155bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4156 LocationSummary* input_locations,
4157 Register dst) {
4158 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4159 Location rhs_location = input_locations->InAt(1);
4160 Register rhs_reg = ZERO;
4161 int64_t rhs_imm = 0;
4162 bool use_imm = rhs_location.IsConstant();
4163 if (use_imm) {
4164 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4165 } else {
4166 rhs_reg = rhs_location.AsRegister<Register>();
4167 }
4168
4169 switch (cond) {
4170 case kCondEQ:
4171 case kCondNE:
4172 if (use_imm && IsInt<16>(-rhs_imm)) {
4173 __ Addiu(dst, lhs, -rhs_imm);
4174 } else if (use_imm && IsUint<16>(rhs_imm)) {
4175 __ Xori(dst, lhs, rhs_imm);
4176 } else {
4177 if (use_imm) {
4178 rhs_reg = TMP;
4179 __ LoadConst32(rhs_reg, rhs_imm);
4180 }
4181 __ Xor(dst, lhs, rhs_reg);
4182 }
4183 return (cond == kCondEQ);
4184
4185 case kCondLT:
4186 case kCondGE:
4187 if (use_imm && IsInt<16>(rhs_imm)) {
4188 __ Slti(dst, lhs, rhs_imm);
4189 } else {
4190 if (use_imm) {
4191 rhs_reg = TMP;
4192 __ LoadConst32(rhs_reg, rhs_imm);
4193 }
4194 __ Slt(dst, lhs, rhs_reg);
4195 }
4196 return (cond == kCondGE);
4197
4198 case kCondLE:
4199 case kCondGT:
4200 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4201 // Simulate lhs <= rhs via lhs < rhs + 1.
4202 __ Slti(dst, lhs, rhs_imm + 1);
4203 return (cond == kCondGT);
4204 } else {
4205 if (use_imm) {
4206 rhs_reg = TMP;
4207 __ LoadConst32(rhs_reg, rhs_imm);
4208 }
4209 __ Slt(dst, rhs_reg, lhs);
4210 return (cond == kCondLE);
4211 }
4212
4213 case kCondB:
4214 case kCondAE:
4215 if (use_imm && IsInt<16>(rhs_imm)) {
4216 // Sltiu sign-extends its 16-bit immediate operand before
4217 // the comparison and thus lets us compare directly with
4218 // unsigned values in the ranges [0, 0x7fff] and
4219 // [0xffff8000, 0xffffffff].
4220 __ Sltiu(dst, lhs, rhs_imm);
4221 } else {
4222 if (use_imm) {
4223 rhs_reg = TMP;
4224 __ LoadConst32(rhs_reg, rhs_imm);
4225 }
4226 __ Sltu(dst, lhs, rhs_reg);
4227 }
4228 return (cond == kCondAE);
4229
4230 case kCondBE:
4231 case kCondA:
4232 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4233 // Simulate lhs <= rhs via lhs < rhs + 1.
4234 // Note that this only works if rhs + 1 does not overflow
4235 // to 0, hence the check above.
4236 // Sltiu sign-extends its 16-bit immediate operand before
4237 // the comparison and thus lets us compare directly with
4238 // unsigned values in the ranges [0, 0x7fff] and
4239 // [0xffff8000, 0xffffffff].
4240 __ Sltiu(dst, lhs, rhs_imm + 1);
4241 return (cond == kCondA);
4242 } else {
4243 if (use_imm) {
4244 rhs_reg = TMP;
4245 __ LoadConst32(rhs_reg, rhs_imm);
4246 }
4247 __ Sltu(dst, rhs_reg, lhs);
4248 return (cond == kCondBE);
4249 }
4250 }
4251}
4252
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004253void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4254 LocationSummary* locations,
4255 MipsLabel* label) {
4256 Register lhs = locations->InAt(0).AsRegister<Register>();
4257 Location rhs_location = locations->InAt(1);
4258 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004259 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004260 bool use_imm = rhs_location.IsConstant();
4261 if (use_imm) {
4262 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4263 } else {
4264 rhs_reg = rhs_location.AsRegister<Register>();
4265 }
4266
4267 if (use_imm && rhs_imm == 0) {
4268 switch (cond) {
4269 case kCondEQ:
4270 case kCondBE: // <= 0 if zero
4271 __ Beqz(lhs, label);
4272 break;
4273 case kCondNE:
4274 case kCondA: // > 0 if non-zero
4275 __ Bnez(lhs, label);
4276 break;
4277 case kCondLT:
4278 __ Bltz(lhs, label);
4279 break;
4280 case kCondGE:
4281 __ Bgez(lhs, label);
4282 break;
4283 case kCondLE:
4284 __ Blez(lhs, label);
4285 break;
4286 case kCondGT:
4287 __ Bgtz(lhs, label);
4288 break;
4289 case kCondB: // always false
4290 break;
4291 case kCondAE: // always true
4292 __ B(label);
4293 break;
4294 }
4295 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004296 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4297 if (isR6 || !use_imm) {
4298 if (use_imm) {
4299 rhs_reg = TMP;
4300 __ LoadConst32(rhs_reg, rhs_imm);
4301 }
4302 switch (cond) {
4303 case kCondEQ:
4304 __ Beq(lhs, rhs_reg, label);
4305 break;
4306 case kCondNE:
4307 __ Bne(lhs, rhs_reg, label);
4308 break;
4309 case kCondLT:
4310 __ Blt(lhs, rhs_reg, label);
4311 break;
4312 case kCondGE:
4313 __ Bge(lhs, rhs_reg, label);
4314 break;
4315 case kCondLE:
4316 __ Bge(rhs_reg, lhs, label);
4317 break;
4318 case kCondGT:
4319 __ Blt(rhs_reg, lhs, label);
4320 break;
4321 case kCondB:
4322 __ Bltu(lhs, rhs_reg, label);
4323 break;
4324 case kCondAE:
4325 __ Bgeu(lhs, rhs_reg, label);
4326 break;
4327 case kCondBE:
4328 __ Bgeu(rhs_reg, lhs, label);
4329 break;
4330 case kCondA:
4331 __ Bltu(rhs_reg, lhs, label);
4332 break;
4333 }
4334 } else {
4335 // Special cases for more efficient comparison with constants on R2.
4336 switch (cond) {
4337 case kCondEQ:
4338 __ LoadConst32(TMP, rhs_imm);
4339 __ Beq(lhs, TMP, label);
4340 break;
4341 case kCondNE:
4342 __ LoadConst32(TMP, rhs_imm);
4343 __ Bne(lhs, TMP, label);
4344 break;
4345 case kCondLT:
4346 if (IsInt<16>(rhs_imm)) {
4347 __ Slti(TMP, lhs, rhs_imm);
4348 __ Bnez(TMP, label);
4349 } else {
4350 __ LoadConst32(TMP, rhs_imm);
4351 __ Blt(lhs, TMP, label);
4352 }
4353 break;
4354 case kCondGE:
4355 if (IsInt<16>(rhs_imm)) {
4356 __ Slti(TMP, lhs, rhs_imm);
4357 __ Beqz(TMP, label);
4358 } else {
4359 __ LoadConst32(TMP, rhs_imm);
4360 __ Bge(lhs, TMP, label);
4361 }
4362 break;
4363 case kCondLE:
4364 if (IsInt<16>(rhs_imm + 1)) {
4365 // Simulate lhs <= rhs via lhs < rhs + 1.
4366 __ Slti(TMP, lhs, rhs_imm + 1);
4367 __ Bnez(TMP, label);
4368 } else {
4369 __ LoadConst32(TMP, rhs_imm);
4370 __ Bge(TMP, lhs, label);
4371 }
4372 break;
4373 case kCondGT:
4374 if (IsInt<16>(rhs_imm + 1)) {
4375 // Simulate lhs > rhs via !(lhs < rhs + 1).
4376 __ Slti(TMP, lhs, rhs_imm + 1);
4377 __ Beqz(TMP, label);
4378 } else {
4379 __ LoadConst32(TMP, rhs_imm);
4380 __ Blt(TMP, lhs, label);
4381 }
4382 break;
4383 case kCondB:
4384 if (IsInt<16>(rhs_imm)) {
4385 __ Sltiu(TMP, lhs, rhs_imm);
4386 __ Bnez(TMP, label);
4387 } else {
4388 __ LoadConst32(TMP, rhs_imm);
4389 __ Bltu(lhs, TMP, label);
4390 }
4391 break;
4392 case kCondAE:
4393 if (IsInt<16>(rhs_imm)) {
4394 __ Sltiu(TMP, lhs, rhs_imm);
4395 __ Beqz(TMP, label);
4396 } else {
4397 __ LoadConst32(TMP, rhs_imm);
4398 __ Bgeu(lhs, TMP, label);
4399 }
4400 break;
4401 case kCondBE:
4402 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4403 // Simulate lhs <= rhs via lhs < rhs + 1.
4404 // Note that this only works if rhs + 1 does not overflow
4405 // to 0, hence the check above.
4406 __ Sltiu(TMP, lhs, rhs_imm + 1);
4407 __ Bnez(TMP, label);
4408 } else {
4409 __ LoadConst32(TMP, rhs_imm);
4410 __ Bgeu(TMP, lhs, label);
4411 }
4412 break;
4413 case kCondA:
4414 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4415 // Simulate lhs > rhs via !(lhs < rhs + 1).
4416 // Note that this only works if rhs + 1 does not overflow
4417 // to 0, hence the check above.
4418 __ Sltiu(TMP, lhs, rhs_imm + 1);
4419 __ Beqz(TMP, label);
4420 } else {
4421 __ LoadConst32(TMP, rhs_imm);
4422 __ Bltu(TMP, lhs, label);
4423 }
4424 break;
4425 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004426 }
4427 }
4428}
4429
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004430void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4431 LocationSummary* locations) {
4432 Register dst = locations->Out().AsRegister<Register>();
4433 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4434 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4435 Location rhs_location = locations->InAt(1);
4436 Register rhs_high = ZERO;
4437 Register rhs_low = ZERO;
4438 int64_t imm = 0;
4439 uint32_t imm_high = 0;
4440 uint32_t imm_low = 0;
4441 bool use_imm = rhs_location.IsConstant();
4442 if (use_imm) {
4443 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4444 imm_high = High32Bits(imm);
4445 imm_low = Low32Bits(imm);
4446 } else {
4447 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4448 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4449 }
4450 if (use_imm && imm == 0) {
4451 switch (cond) {
4452 case kCondEQ:
4453 case kCondBE: // <= 0 if zero
4454 __ Or(dst, lhs_high, lhs_low);
4455 __ Sltiu(dst, dst, 1);
4456 break;
4457 case kCondNE:
4458 case kCondA: // > 0 if non-zero
4459 __ Or(dst, lhs_high, lhs_low);
4460 __ Sltu(dst, ZERO, dst);
4461 break;
4462 case kCondLT:
4463 __ Slt(dst, lhs_high, ZERO);
4464 break;
4465 case kCondGE:
4466 __ Slt(dst, lhs_high, ZERO);
4467 __ Xori(dst, dst, 1);
4468 break;
4469 case kCondLE:
4470 __ Or(TMP, lhs_high, lhs_low);
4471 __ Sra(AT, lhs_high, 31);
4472 __ Sltu(dst, AT, TMP);
4473 __ Xori(dst, dst, 1);
4474 break;
4475 case kCondGT:
4476 __ Or(TMP, lhs_high, lhs_low);
4477 __ Sra(AT, lhs_high, 31);
4478 __ Sltu(dst, AT, TMP);
4479 break;
4480 case kCondB: // always false
4481 __ Andi(dst, dst, 0);
4482 break;
4483 case kCondAE: // always true
4484 __ Ori(dst, ZERO, 1);
4485 break;
4486 }
4487 } else if (use_imm) {
4488 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4489 switch (cond) {
4490 case kCondEQ:
4491 __ LoadConst32(TMP, imm_high);
4492 __ Xor(TMP, TMP, lhs_high);
4493 __ LoadConst32(AT, imm_low);
4494 __ Xor(AT, AT, lhs_low);
4495 __ Or(dst, TMP, AT);
4496 __ Sltiu(dst, dst, 1);
4497 break;
4498 case kCondNE:
4499 __ LoadConst32(TMP, imm_high);
4500 __ Xor(TMP, TMP, lhs_high);
4501 __ LoadConst32(AT, imm_low);
4502 __ Xor(AT, AT, lhs_low);
4503 __ Or(dst, TMP, AT);
4504 __ Sltu(dst, ZERO, dst);
4505 break;
4506 case kCondLT:
4507 case kCondGE:
4508 if (dst == lhs_low) {
4509 __ LoadConst32(TMP, imm_low);
4510 __ Sltu(dst, lhs_low, TMP);
4511 }
4512 __ LoadConst32(TMP, imm_high);
4513 __ Slt(AT, lhs_high, TMP);
4514 __ Slt(TMP, TMP, lhs_high);
4515 if (dst != lhs_low) {
4516 __ LoadConst32(dst, imm_low);
4517 __ Sltu(dst, lhs_low, dst);
4518 }
4519 __ Slt(dst, TMP, dst);
4520 __ Or(dst, dst, AT);
4521 if (cond == kCondGE) {
4522 __ Xori(dst, dst, 1);
4523 }
4524 break;
4525 case kCondGT:
4526 case kCondLE:
4527 if (dst == lhs_low) {
4528 __ LoadConst32(TMP, imm_low);
4529 __ Sltu(dst, TMP, lhs_low);
4530 }
4531 __ LoadConst32(TMP, imm_high);
4532 __ Slt(AT, TMP, lhs_high);
4533 __ Slt(TMP, lhs_high, TMP);
4534 if (dst != lhs_low) {
4535 __ LoadConst32(dst, imm_low);
4536 __ Sltu(dst, dst, lhs_low);
4537 }
4538 __ Slt(dst, TMP, dst);
4539 __ Or(dst, dst, AT);
4540 if (cond == kCondLE) {
4541 __ Xori(dst, dst, 1);
4542 }
4543 break;
4544 case kCondB:
4545 case kCondAE:
4546 if (dst == lhs_low) {
4547 __ LoadConst32(TMP, imm_low);
4548 __ Sltu(dst, lhs_low, TMP);
4549 }
4550 __ LoadConst32(TMP, imm_high);
4551 __ Sltu(AT, lhs_high, TMP);
4552 __ Sltu(TMP, TMP, lhs_high);
4553 if (dst != lhs_low) {
4554 __ LoadConst32(dst, imm_low);
4555 __ Sltu(dst, lhs_low, dst);
4556 }
4557 __ Slt(dst, TMP, dst);
4558 __ Or(dst, dst, AT);
4559 if (cond == kCondAE) {
4560 __ Xori(dst, dst, 1);
4561 }
4562 break;
4563 case kCondA:
4564 case kCondBE:
4565 if (dst == lhs_low) {
4566 __ LoadConst32(TMP, imm_low);
4567 __ Sltu(dst, TMP, lhs_low);
4568 }
4569 __ LoadConst32(TMP, imm_high);
4570 __ Sltu(AT, TMP, lhs_high);
4571 __ Sltu(TMP, lhs_high, TMP);
4572 if (dst != lhs_low) {
4573 __ LoadConst32(dst, imm_low);
4574 __ Sltu(dst, dst, lhs_low);
4575 }
4576 __ Slt(dst, TMP, dst);
4577 __ Or(dst, dst, AT);
4578 if (cond == kCondBE) {
4579 __ Xori(dst, dst, 1);
4580 }
4581 break;
4582 }
4583 } else {
4584 switch (cond) {
4585 case kCondEQ:
4586 __ Xor(TMP, lhs_high, rhs_high);
4587 __ Xor(AT, lhs_low, rhs_low);
4588 __ Or(dst, TMP, AT);
4589 __ Sltiu(dst, dst, 1);
4590 break;
4591 case kCondNE:
4592 __ Xor(TMP, lhs_high, rhs_high);
4593 __ Xor(AT, lhs_low, rhs_low);
4594 __ Or(dst, TMP, AT);
4595 __ Sltu(dst, ZERO, dst);
4596 break;
4597 case kCondLT:
4598 case kCondGE:
4599 __ Slt(TMP, rhs_high, lhs_high);
4600 __ Sltu(AT, lhs_low, rhs_low);
4601 __ Slt(TMP, TMP, AT);
4602 __ Slt(AT, lhs_high, rhs_high);
4603 __ Or(dst, AT, TMP);
4604 if (cond == kCondGE) {
4605 __ Xori(dst, dst, 1);
4606 }
4607 break;
4608 case kCondGT:
4609 case kCondLE:
4610 __ Slt(TMP, lhs_high, rhs_high);
4611 __ Sltu(AT, rhs_low, lhs_low);
4612 __ Slt(TMP, TMP, AT);
4613 __ Slt(AT, rhs_high, lhs_high);
4614 __ Or(dst, AT, TMP);
4615 if (cond == kCondLE) {
4616 __ Xori(dst, dst, 1);
4617 }
4618 break;
4619 case kCondB:
4620 case kCondAE:
4621 __ Sltu(TMP, rhs_high, lhs_high);
4622 __ Sltu(AT, lhs_low, rhs_low);
4623 __ Slt(TMP, TMP, AT);
4624 __ Sltu(AT, lhs_high, rhs_high);
4625 __ Or(dst, AT, TMP);
4626 if (cond == kCondAE) {
4627 __ Xori(dst, dst, 1);
4628 }
4629 break;
4630 case kCondA:
4631 case kCondBE:
4632 __ Sltu(TMP, lhs_high, rhs_high);
4633 __ Sltu(AT, rhs_low, lhs_low);
4634 __ Slt(TMP, TMP, AT);
4635 __ Sltu(AT, rhs_high, lhs_high);
4636 __ Or(dst, AT, TMP);
4637 if (cond == kCondBE) {
4638 __ Xori(dst, dst, 1);
4639 }
4640 break;
4641 }
4642 }
4643}
4644
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004645void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4646 LocationSummary* locations,
4647 MipsLabel* label) {
4648 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4649 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4650 Location rhs_location = locations->InAt(1);
4651 Register rhs_high = ZERO;
4652 Register rhs_low = ZERO;
4653 int64_t imm = 0;
4654 uint32_t imm_high = 0;
4655 uint32_t imm_low = 0;
4656 bool use_imm = rhs_location.IsConstant();
4657 if (use_imm) {
4658 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4659 imm_high = High32Bits(imm);
4660 imm_low = Low32Bits(imm);
4661 } else {
4662 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4663 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4664 }
4665
4666 if (use_imm && imm == 0) {
4667 switch (cond) {
4668 case kCondEQ:
4669 case kCondBE: // <= 0 if zero
4670 __ Or(TMP, lhs_high, lhs_low);
4671 __ Beqz(TMP, label);
4672 break;
4673 case kCondNE:
4674 case kCondA: // > 0 if non-zero
4675 __ Or(TMP, lhs_high, lhs_low);
4676 __ Bnez(TMP, label);
4677 break;
4678 case kCondLT:
4679 __ Bltz(lhs_high, label);
4680 break;
4681 case kCondGE:
4682 __ Bgez(lhs_high, label);
4683 break;
4684 case kCondLE:
4685 __ Or(TMP, lhs_high, lhs_low);
4686 __ Sra(AT, lhs_high, 31);
4687 __ Bgeu(AT, TMP, label);
4688 break;
4689 case kCondGT:
4690 __ Or(TMP, lhs_high, lhs_low);
4691 __ Sra(AT, lhs_high, 31);
4692 __ Bltu(AT, TMP, label);
4693 break;
4694 case kCondB: // always false
4695 break;
4696 case kCondAE: // always true
4697 __ B(label);
4698 break;
4699 }
4700 } else if (use_imm) {
4701 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4702 switch (cond) {
4703 case kCondEQ:
4704 __ LoadConst32(TMP, imm_high);
4705 __ Xor(TMP, TMP, lhs_high);
4706 __ LoadConst32(AT, imm_low);
4707 __ Xor(AT, AT, lhs_low);
4708 __ Or(TMP, TMP, AT);
4709 __ Beqz(TMP, label);
4710 break;
4711 case kCondNE:
4712 __ LoadConst32(TMP, imm_high);
4713 __ Xor(TMP, TMP, lhs_high);
4714 __ LoadConst32(AT, imm_low);
4715 __ Xor(AT, AT, lhs_low);
4716 __ Or(TMP, TMP, AT);
4717 __ Bnez(TMP, label);
4718 break;
4719 case kCondLT:
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, lhs_low, AT);
4725 __ Blt(TMP, AT, label);
4726 break;
4727 case kCondGE:
4728 __ LoadConst32(TMP, imm_high);
4729 __ Blt(TMP, lhs_high, label);
4730 __ Slt(TMP, lhs_high, TMP);
4731 __ LoadConst32(AT, imm_low);
4732 __ Sltu(AT, lhs_low, AT);
4733 __ Or(TMP, TMP, AT);
4734 __ Beqz(TMP, label);
4735 break;
4736 case kCondLE:
4737 __ LoadConst32(TMP, imm_high);
4738 __ Blt(lhs_high, TMP, label);
4739 __ Slt(TMP, TMP, lhs_high);
4740 __ LoadConst32(AT, imm_low);
4741 __ Sltu(AT, AT, lhs_low);
4742 __ Or(TMP, TMP, AT);
4743 __ Beqz(TMP, label);
4744 break;
4745 case kCondGT:
4746 __ LoadConst32(TMP, imm_high);
4747 __ Blt(TMP, lhs_high, label);
4748 __ Slt(TMP, lhs_high, TMP);
4749 __ LoadConst32(AT, imm_low);
4750 __ Sltu(AT, AT, lhs_low);
4751 __ Blt(TMP, AT, label);
4752 break;
4753 case kCondB:
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, lhs_low, AT);
4759 __ Blt(TMP, AT, label);
4760 break;
4761 case kCondAE:
4762 __ LoadConst32(TMP, imm_high);
4763 __ Bltu(TMP, lhs_high, label);
4764 __ Sltu(TMP, lhs_high, TMP);
4765 __ LoadConst32(AT, imm_low);
4766 __ Sltu(AT, lhs_low, AT);
4767 __ Or(TMP, TMP, AT);
4768 __ Beqz(TMP, label);
4769 break;
4770 case kCondBE:
4771 __ LoadConst32(TMP, imm_high);
4772 __ Bltu(lhs_high, TMP, label);
4773 __ Sltu(TMP, TMP, lhs_high);
4774 __ LoadConst32(AT, imm_low);
4775 __ Sltu(AT, AT, lhs_low);
4776 __ Or(TMP, TMP, AT);
4777 __ Beqz(TMP, label);
4778 break;
4779 case kCondA:
4780 __ LoadConst32(TMP, imm_high);
4781 __ Bltu(TMP, lhs_high, label);
4782 __ Sltu(TMP, lhs_high, TMP);
4783 __ LoadConst32(AT, imm_low);
4784 __ Sltu(AT, AT, lhs_low);
4785 __ Blt(TMP, AT, label);
4786 break;
4787 }
4788 } else {
4789 switch (cond) {
4790 case kCondEQ:
4791 __ Xor(TMP, lhs_high, rhs_high);
4792 __ Xor(AT, lhs_low, rhs_low);
4793 __ Or(TMP, TMP, AT);
4794 __ Beqz(TMP, label);
4795 break;
4796 case kCondNE:
4797 __ Xor(TMP, lhs_high, rhs_high);
4798 __ Xor(AT, lhs_low, rhs_low);
4799 __ Or(TMP, TMP, AT);
4800 __ Bnez(TMP, label);
4801 break;
4802 case kCondLT:
4803 __ Blt(lhs_high, rhs_high, label);
4804 __ Slt(TMP, rhs_high, lhs_high);
4805 __ Sltu(AT, lhs_low, rhs_low);
4806 __ Blt(TMP, AT, label);
4807 break;
4808 case kCondGE:
4809 __ Blt(rhs_high, lhs_high, label);
4810 __ Slt(TMP, lhs_high, rhs_high);
4811 __ Sltu(AT, lhs_low, rhs_low);
4812 __ Or(TMP, TMP, AT);
4813 __ Beqz(TMP, label);
4814 break;
4815 case kCondLE:
4816 __ Blt(lhs_high, rhs_high, label);
4817 __ Slt(TMP, rhs_high, lhs_high);
4818 __ Sltu(AT, rhs_low, lhs_low);
4819 __ Or(TMP, TMP, AT);
4820 __ Beqz(TMP, label);
4821 break;
4822 case kCondGT:
4823 __ Blt(rhs_high, lhs_high, label);
4824 __ Slt(TMP, lhs_high, rhs_high);
4825 __ Sltu(AT, rhs_low, lhs_low);
4826 __ Blt(TMP, AT, label);
4827 break;
4828 case kCondB:
4829 __ Bltu(lhs_high, rhs_high, label);
4830 __ Sltu(TMP, rhs_high, lhs_high);
4831 __ Sltu(AT, lhs_low, rhs_low);
4832 __ Blt(TMP, AT, label);
4833 break;
4834 case kCondAE:
4835 __ Bltu(rhs_high, lhs_high, label);
4836 __ Sltu(TMP, lhs_high, rhs_high);
4837 __ Sltu(AT, lhs_low, rhs_low);
4838 __ Or(TMP, TMP, AT);
4839 __ Beqz(TMP, label);
4840 break;
4841 case kCondBE:
4842 __ Bltu(lhs_high, rhs_high, label);
4843 __ Sltu(TMP, rhs_high, lhs_high);
4844 __ Sltu(AT, rhs_low, lhs_low);
4845 __ Or(TMP, TMP, AT);
4846 __ Beqz(TMP, label);
4847 break;
4848 case kCondA:
4849 __ Bltu(rhs_high, lhs_high, label);
4850 __ Sltu(TMP, lhs_high, rhs_high);
4851 __ Sltu(AT, rhs_low, lhs_low);
4852 __ Blt(TMP, AT, label);
4853 break;
4854 }
4855 }
4856}
4857
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004858void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4859 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004861 LocationSummary* locations) {
4862 Register dst = locations->Out().AsRegister<Register>();
4863 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4864 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4865 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004867 if (isR6) {
4868 switch (cond) {
4869 case kCondEQ:
4870 __ CmpEqS(FTMP, lhs, rhs);
4871 __ Mfc1(dst, FTMP);
4872 __ Andi(dst, dst, 1);
4873 break;
4874 case kCondNE:
4875 __ CmpEqS(FTMP, lhs, rhs);
4876 __ Mfc1(dst, FTMP);
4877 __ Addiu(dst, dst, 1);
4878 break;
4879 case kCondLT:
4880 if (gt_bias) {
4881 __ CmpLtS(FTMP, lhs, rhs);
4882 } else {
4883 __ CmpUltS(FTMP, lhs, rhs);
4884 }
4885 __ Mfc1(dst, FTMP);
4886 __ Andi(dst, dst, 1);
4887 break;
4888 case kCondLE:
4889 if (gt_bias) {
4890 __ CmpLeS(FTMP, lhs, rhs);
4891 } else {
4892 __ CmpUleS(FTMP, lhs, rhs);
4893 }
4894 __ Mfc1(dst, FTMP);
4895 __ Andi(dst, dst, 1);
4896 break;
4897 case kCondGT:
4898 if (gt_bias) {
4899 __ CmpUltS(FTMP, rhs, lhs);
4900 } else {
4901 __ CmpLtS(FTMP, rhs, lhs);
4902 }
4903 __ Mfc1(dst, FTMP);
4904 __ Andi(dst, dst, 1);
4905 break;
4906 case kCondGE:
4907 if (gt_bias) {
4908 __ CmpUleS(FTMP, rhs, lhs);
4909 } else {
4910 __ CmpLeS(FTMP, rhs, lhs);
4911 }
4912 __ Mfc1(dst, FTMP);
4913 __ Andi(dst, dst, 1);
4914 break;
4915 default:
4916 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4917 UNREACHABLE();
4918 }
4919 } else {
4920 switch (cond) {
4921 case kCondEQ:
4922 __ CeqS(0, lhs, rhs);
4923 __ LoadConst32(dst, 1);
4924 __ Movf(dst, ZERO, 0);
4925 break;
4926 case kCondNE:
4927 __ CeqS(0, lhs, rhs);
4928 __ LoadConst32(dst, 1);
4929 __ Movt(dst, ZERO, 0);
4930 break;
4931 case kCondLT:
4932 if (gt_bias) {
4933 __ ColtS(0, lhs, rhs);
4934 } else {
4935 __ CultS(0, lhs, rhs);
4936 }
4937 __ LoadConst32(dst, 1);
4938 __ Movf(dst, ZERO, 0);
4939 break;
4940 case kCondLE:
4941 if (gt_bias) {
4942 __ ColeS(0, lhs, rhs);
4943 } else {
4944 __ CuleS(0, lhs, rhs);
4945 }
4946 __ LoadConst32(dst, 1);
4947 __ Movf(dst, ZERO, 0);
4948 break;
4949 case kCondGT:
4950 if (gt_bias) {
4951 __ CultS(0, rhs, lhs);
4952 } else {
4953 __ ColtS(0, rhs, lhs);
4954 }
4955 __ LoadConst32(dst, 1);
4956 __ Movf(dst, ZERO, 0);
4957 break;
4958 case kCondGE:
4959 if (gt_bias) {
4960 __ CuleS(0, rhs, lhs);
4961 } else {
4962 __ ColeS(0, rhs, lhs);
4963 }
4964 __ LoadConst32(dst, 1);
4965 __ Movf(dst, ZERO, 0);
4966 break;
4967 default:
4968 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4969 UNREACHABLE();
4970 }
4971 }
4972 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004974 if (isR6) {
4975 switch (cond) {
4976 case kCondEQ:
4977 __ CmpEqD(FTMP, lhs, rhs);
4978 __ Mfc1(dst, FTMP);
4979 __ Andi(dst, dst, 1);
4980 break;
4981 case kCondNE:
4982 __ CmpEqD(FTMP, lhs, rhs);
4983 __ Mfc1(dst, FTMP);
4984 __ Addiu(dst, dst, 1);
4985 break;
4986 case kCondLT:
4987 if (gt_bias) {
4988 __ CmpLtD(FTMP, lhs, rhs);
4989 } else {
4990 __ CmpUltD(FTMP, lhs, rhs);
4991 }
4992 __ Mfc1(dst, FTMP);
4993 __ Andi(dst, dst, 1);
4994 break;
4995 case kCondLE:
4996 if (gt_bias) {
4997 __ CmpLeD(FTMP, lhs, rhs);
4998 } else {
4999 __ CmpUleD(FTMP, lhs, rhs);
5000 }
5001 __ Mfc1(dst, FTMP);
5002 __ Andi(dst, dst, 1);
5003 break;
5004 case kCondGT:
5005 if (gt_bias) {
5006 __ CmpUltD(FTMP, rhs, lhs);
5007 } else {
5008 __ CmpLtD(FTMP, rhs, lhs);
5009 }
5010 __ Mfc1(dst, FTMP);
5011 __ Andi(dst, dst, 1);
5012 break;
5013 case kCondGE:
5014 if (gt_bias) {
5015 __ CmpUleD(FTMP, rhs, lhs);
5016 } else {
5017 __ CmpLeD(FTMP, rhs, lhs);
5018 }
5019 __ Mfc1(dst, FTMP);
5020 __ Andi(dst, dst, 1);
5021 break;
5022 default:
5023 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5024 UNREACHABLE();
5025 }
5026 } else {
5027 switch (cond) {
5028 case kCondEQ:
5029 __ CeqD(0, lhs, rhs);
5030 __ LoadConst32(dst, 1);
5031 __ Movf(dst, ZERO, 0);
5032 break;
5033 case kCondNE:
5034 __ CeqD(0, lhs, rhs);
5035 __ LoadConst32(dst, 1);
5036 __ Movt(dst, ZERO, 0);
5037 break;
5038 case kCondLT:
5039 if (gt_bias) {
5040 __ ColtD(0, lhs, rhs);
5041 } else {
5042 __ CultD(0, lhs, rhs);
5043 }
5044 __ LoadConst32(dst, 1);
5045 __ Movf(dst, ZERO, 0);
5046 break;
5047 case kCondLE:
5048 if (gt_bias) {
5049 __ ColeD(0, lhs, rhs);
5050 } else {
5051 __ CuleD(0, lhs, rhs);
5052 }
5053 __ LoadConst32(dst, 1);
5054 __ Movf(dst, ZERO, 0);
5055 break;
5056 case kCondGT:
5057 if (gt_bias) {
5058 __ CultD(0, rhs, lhs);
5059 } else {
5060 __ ColtD(0, rhs, lhs);
5061 }
5062 __ LoadConst32(dst, 1);
5063 __ Movf(dst, ZERO, 0);
5064 break;
5065 case kCondGE:
5066 if (gt_bias) {
5067 __ CuleD(0, rhs, lhs);
5068 } else {
5069 __ ColeD(0, rhs, lhs);
5070 }
5071 __ LoadConst32(dst, 1);
5072 __ Movf(dst, ZERO, 0);
5073 break;
5074 default:
5075 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5076 UNREACHABLE();
5077 }
5078 }
5079 }
5080}
5081
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005082bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5083 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005084 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005085 LocationSummary* input_locations,
5086 int cc) {
5087 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5088 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5089 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005090 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005091 switch (cond) {
5092 case kCondEQ:
5093 __ CeqS(cc, lhs, rhs);
5094 return false;
5095 case kCondNE:
5096 __ CeqS(cc, lhs, rhs);
5097 return true;
5098 case kCondLT:
5099 if (gt_bias) {
5100 __ ColtS(cc, lhs, rhs);
5101 } else {
5102 __ CultS(cc, lhs, rhs);
5103 }
5104 return false;
5105 case kCondLE:
5106 if (gt_bias) {
5107 __ ColeS(cc, lhs, rhs);
5108 } else {
5109 __ CuleS(cc, lhs, rhs);
5110 }
5111 return false;
5112 case kCondGT:
5113 if (gt_bias) {
5114 __ CultS(cc, rhs, lhs);
5115 } else {
5116 __ ColtS(cc, rhs, lhs);
5117 }
5118 return false;
5119 case kCondGE:
5120 if (gt_bias) {
5121 __ CuleS(cc, rhs, lhs);
5122 } else {
5123 __ ColeS(cc, rhs, lhs);
5124 }
5125 return false;
5126 default:
5127 LOG(FATAL) << "Unexpected non-floating-point condition";
5128 UNREACHABLE();
5129 }
5130 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005131 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005132 switch (cond) {
5133 case kCondEQ:
5134 __ CeqD(cc, lhs, rhs);
5135 return false;
5136 case kCondNE:
5137 __ CeqD(cc, lhs, rhs);
5138 return true;
5139 case kCondLT:
5140 if (gt_bias) {
5141 __ ColtD(cc, lhs, rhs);
5142 } else {
5143 __ CultD(cc, lhs, rhs);
5144 }
5145 return false;
5146 case kCondLE:
5147 if (gt_bias) {
5148 __ ColeD(cc, lhs, rhs);
5149 } else {
5150 __ CuleD(cc, lhs, rhs);
5151 }
5152 return false;
5153 case kCondGT:
5154 if (gt_bias) {
5155 __ CultD(cc, rhs, lhs);
5156 } else {
5157 __ ColtD(cc, rhs, lhs);
5158 }
5159 return false;
5160 case kCondGE:
5161 if (gt_bias) {
5162 __ CuleD(cc, rhs, lhs);
5163 } else {
5164 __ ColeD(cc, rhs, lhs);
5165 }
5166 return false;
5167 default:
5168 LOG(FATAL) << "Unexpected non-floating-point condition";
5169 UNREACHABLE();
5170 }
5171 }
5172}
5173
5174bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5175 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005177 LocationSummary* input_locations,
5178 FRegister dst) {
5179 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5180 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5181 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005183 switch (cond) {
5184 case kCondEQ:
5185 __ CmpEqS(dst, lhs, rhs);
5186 return false;
5187 case kCondNE:
5188 __ CmpEqS(dst, lhs, rhs);
5189 return true;
5190 case kCondLT:
5191 if (gt_bias) {
5192 __ CmpLtS(dst, lhs, rhs);
5193 } else {
5194 __ CmpUltS(dst, lhs, rhs);
5195 }
5196 return false;
5197 case kCondLE:
5198 if (gt_bias) {
5199 __ CmpLeS(dst, lhs, rhs);
5200 } else {
5201 __ CmpUleS(dst, lhs, rhs);
5202 }
5203 return false;
5204 case kCondGT:
5205 if (gt_bias) {
5206 __ CmpUltS(dst, rhs, lhs);
5207 } else {
5208 __ CmpLtS(dst, rhs, lhs);
5209 }
5210 return false;
5211 case kCondGE:
5212 if (gt_bias) {
5213 __ CmpUleS(dst, rhs, lhs);
5214 } else {
5215 __ CmpLeS(dst, rhs, lhs);
5216 }
5217 return false;
5218 default:
5219 LOG(FATAL) << "Unexpected non-floating-point condition";
5220 UNREACHABLE();
5221 }
5222 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005224 switch (cond) {
5225 case kCondEQ:
5226 __ CmpEqD(dst, lhs, rhs);
5227 return false;
5228 case kCondNE:
5229 __ CmpEqD(dst, lhs, rhs);
5230 return true;
5231 case kCondLT:
5232 if (gt_bias) {
5233 __ CmpLtD(dst, lhs, rhs);
5234 } else {
5235 __ CmpUltD(dst, lhs, rhs);
5236 }
5237 return false;
5238 case kCondLE:
5239 if (gt_bias) {
5240 __ CmpLeD(dst, lhs, rhs);
5241 } else {
5242 __ CmpUleD(dst, lhs, rhs);
5243 }
5244 return false;
5245 case kCondGT:
5246 if (gt_bias) {
5247 __ CmpUltD(dst, rhs, lhs);
5248 } else {
5249 __ CmpLtD(dst, rhs, lhs);
5250 }
5251 return false;
5252 case kCondGE:
5253 if (gt_bias) {
5254 __ CmpUleD(dst, rhs, lhs);
5255 } else {
5256 __ CmpLeD(dst, rhs, lhs);
5257 }
5258 return false;
5259 default:
5260 LOG(FATAL) << "Unexpected non-floating-point condition";
5261 UNREACHABLE();
5262 }
5263 }
5264}
5265
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005266void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5267 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005268 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005269 LocationSummary* locations,
5270 MipsLabel* label) {
5271 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5272 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5273 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005274 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005275 if (isR6) {
5276 switch (cond) {
5277 case kCondEQ:
5278 __ CmpEqS(FTMP, lhs, rhs);
5279 __ Bc1nez(FTMP, label);
5280 break;
5281 case kCondNE:
5282 __ CmpEqS(FTMP, lhs, rhs);
5283 __ Bc1eqz(FTMP, label);
5284 break;
5285 case kCondLT:
5286 if (gt_bias) {
5287 __ CmpLtS(FTMP, lhs, rhs);
5288 } else {
5289 __ CmpUltS(FTMP, lhs, rhs);
5290 }
5291 __ Bc1nez(FTMP, label);
5292 break;
5293 case kCondLE:
5294 if (gt_bias) {
5295 __ CmpLeS(FTMP, lhs, rhs);
5296 } else {
5297 __ CmpUleS(FTMP, lhs, rhs);
5298 }
5299 __ Bc1nez(FTMP, label);
5300 break;
5301 case kCondGT:
5302 if (gt_bias) {
5303 __ CmpUltS(FTMP, rhs, lhs);
5304 } else {
5305 __ CmpLtS(FTMP, rhs, lhs);
5306 }
5307 __ Bc1nez(FTMP, label);
5308 break;
5309 case kCondGE:
5310 if (gt_bias) {
5311 __ CmpUleS(FTMP, rhs, lhs);
5312 } else {
5313 __ CmpLeS(FTMP, rhs, lhs);
5314 }
5315 __ Bc1nez(FTMP, label);
5316 break;
5317 default:
5318 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005319 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005320 }
5321 } else {
5322 switch (cond) {
5323 case kCondEQ:
5324 __ CeqS(0, lhs, rhs);
5325 __ Bc1t(0, label);
5326 break;
5327 case kCondNE:
5328 __ CeqS(0, lhs, rhs);
5329 __ Bc1f(0, label);
5330 break;
5331 case kCondLT:
5332 if (gt_bias) {
5333 __ ColtS(0, lhs, rhs);
5334 } else {
5335 __ CultS(0, lhs, rhs);
5336 }
5337 __ Bc1t(0, label);
5338 break;
5339 case kCondLE:
5340 if (gt_bias) {
5341 __ ColeS(0, lhs, rhs);
5342 } else {
5343 __ CuleS(0, lhs, rhs);
5344 }
5345 __ Bc1t(0, label);
5346 break;
5347 case kCondGT:
5348 if (gt_bias) {
5349 __ CultS(0, rhs, lhs);
5350 } else {
5351 __ ColtS(0, rhs, lhs);
5352 }
5353 __ Bc1t(0, label);
5354 break;
5355 case kCondGE:
5356 if (gt_bias) {
5357 __ CuleS(0, rhs, lhs);
5358 } else {
5359 __ ColeS(0, rhs, lhs);
5360 }
5361 __ Bc1t(0, label);
5362 break;
5363 default:
5364 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005365 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005366 }
5367 }
5368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005369 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005370 if (isR6) {
5371 switch (cond) {
5372 case kCondEQ:
5373 __ CmpEqD(FTMP, lhs, rhs);
5374 __ Bc1nez(FTMP, label);
5375 break;
5376 case kCondNE:
5377 __ CmpEqD(FTMP, lhs, rhs);
5378 __ Bc1eqz(FTMP, label);
5379 break;
5380 case kCondLT:
5381 if (gt_bias) {
5382 __ CmpLtD(FTMP, lhs, rhs);
5383 } else {
5384 __ CmpUltD(FTMP, lhs, rhs);
5385 }
5386 __ Bc1nez(FTMP, label);
5387 break;
5388 case kCondLE:
5389 if (gt_bias) {
5390 __ CmpLeD(FTMP, lhs, rhs);
5391 } else {
5392 __ CmpUleD(FTMP, lhs, rhs);
5393 }
5394 __ Bc1nez(FTMP, label);
5395 break;
5396 case kCondGT:
5397 if (gt_bias) {
5398 __ CmpUltD(FTMP, rhs, lhs);
5399 } else {
5400 __ CmpLtD(FTMP, rhs, lhs);
5401 }
5402 __ Bc1nez(FTMP, label);
5403 break;
5404 case kCondGE:
5405 if (gt_bias) {
5406 __ CmpUleD(FTMP, rhs, lhs);
5407 } else {
5408 __ CmpLeD(FTMP, rhs, lhs);
5409 }
5410 __ Bc1nez(FTMP, label);
5411 break;
5412 default:
5413 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005414 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005415 }
5416 } else {
5417 switch (cond) {
5418 case kCondEQ:
5419 __ CeqD(0, lhs, rhs);
5420 __ Bc1t(0, label);
5421 break;
5422 case kCondNE:
5423 __ CeqD(0, lhs, rhs);
5424 __ Bc1f(0, label);
5425 break;
5426 case kCondLT:
5427 if (gt_bias) {
5428 __ ColtD(0, lhs, rhs);
5429 } else {
5430 __ CultD(0, lhs, rhs);
5431 }
5432 __ Bc1t(0, label);
5433 break;
5434 case kCondLE:
5435 if (gt_bias) {
5436 __ ColeD(0, lhs, rhs);
5437 } else {
5438 __ CuleD(0, lhs, rhs);
5439 }
5440 __ Bc1t(0, label);
5441 break;
5442 case kCondGT:
5443 if (gt_bias) {
5444 __ CultD(0, rhs, lhs);
5445 } else {
5446 __ ColtD(0, rhs, lhs);
5447 }
5448 __ Bc1t(0, label);
5449 break;
5450 case kCondGE:
5451 if (gt_bias) {
5452 __ CuleD(0, rhs, lhs);
5453 } else {
5454 __ ColeD(0, rhs, lhs);
5455 }
5456 __ Bc1t(0, label);
5457 break;
5458 default:
5459 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005460 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005461 }
5462 }
5463 }
5464}
5465
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005466void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005467 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005468 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005469 MipsLabel* false_target) {
5470 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005471
David Brazdil0debae72015-11-12 18:37:00 +00005472 if (true_target == nullptr && false_target == nullptr) {
5473 // Nothing to do. The code always falls through.
5474 return;
5475 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005476 // Constant condition, statically compared against "true" (integer value 1).
5477 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005478 if (true_target != nullptr) {
5479 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005480 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005481 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005482 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005483 if (false_target != nullptr) {
5484 __ B(false_target);
5485 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005486 }
David Brazdil0debae72015-11-12 18:37:00 +00005487 return;
5488 }
5489
5490 // The following code generates these patterns:
5491 // (1) true_target == nullptr && false_target != nullptr
5492 // - opposite condition true => branch to false_target
5493 // (2) true_target != nullptr && false_target == nullptr
5494 // - condition true => branch to true_target
5495 // (3) true_target != nullptr && false_target != nullptr
5496 // - condition true => branch to true_target
5497 // - branch to false_target
5498 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005499 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005500 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005501 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005502 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005503 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5504 } else {
5505 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005507 } else {
5508 // The condition instruction has not been materialized, use its inputs as
5509 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005510 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005512 LocationSummary* locations = cond->GetLocations();
5513 IfCondition if_cond = condition->GetCondition();
5514 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005515
David Brazdil0debae72015-11-12 18:37:00 +00005516 if (true_target == nullptr) {
5517 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005518 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005519 }
5520
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005521 switch (type) {
5522 default:
5523 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5524 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005525 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005526 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5527 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005528 case DataType::Type::kFloat32:
5529 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005530 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5531 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005532 }
5533 }
David Brazdil0debae72015-11-12 18:37:00 +00005534
5535 // If neither branch falls through (case 3), the conditional branch to `true_target`
5536 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5537 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005538 __ B(false_target);
5539 }
5540}
5541
5542void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005543 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005544 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005545 locations->SetInAt(0, Location::RequiresRegister());
5546 }
5547}
5548
5549void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005550 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5551 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5552 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5553 nullptr : codegen_->GetLabelOf(true_successor);
5554 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5555 nullptr : codegen_->GetLabelOf(false_successor);
5556 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557}
5558
5559void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005560 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005562 InvokeRuntimeCallingConvention calling_convention;
5563 RegisterSet caller_saves = RegisterSet::Empty();
5564 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5565 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005566 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005567 locations->SetInAt(0, Location::RequiresRegister());
5568 }
5569}
5570
5571void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005572 SlowPathCodeMIPS* slow_path =
5573 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005574 GenerateTestAndBranch(deoptimize,
5575 /* condition_input_index */ 0,
5576 slow_path->GetEntryLabel(),
5577 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005578}
5579
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005580// This function returns true if a conditional move can be generated for HSelect.
5581// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5582// branches and regular moves.
5583//
5584// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5585//
5586// While determining feasibility of a conditional move and setting inputs/outputs
5587// are two distinct tasks, this function does both because they share quite a bit
5588// of common logic.
5589static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5590 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5591 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5592 HCondition* condition = cond->AsCondition();
5593
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005594 DataType::Type cond_type =
5595 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5596 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005597
5598 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5599 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5600 bool is_true_value_zero_constant =
5601 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5602 bool is_false_value_zero_constant =
5603 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5604
5605 bool can_move_conditionally = false;
5606 bool use_const_for_false_in = false;
5607 bool use_const_for_true_in = false;
5608
5609 if (!cond->IsConstant()) {
5610 switch (cond_type) {
5611 default:
5612 switch (dst_type) {
5613 default:
5614 // Moving int on int condition.
5615 if (is_r6) {
5616 if (is_true_value_zero_constant) {
5617 // seleqz out_reg, false_reg, cond_reg
5618 can_move_conditionally = true;
5619 use_const_for_true_in = true;
5620 } else if (is_false_value_zero_constant) {
5621 // selnez out_reg, true_reg, cond_reg
5622 can_move_conditionally = true;
5623 use_const_for_false_in = true;
5624 } else if (materialized) {
5625 // Not materializing unmaterialized int conditions
5626 // to keep the instruction count low.
5627 // selnez AT, true_reg, cond_reg
5628 // seleqz TMP, false_reg, cond_reg
5629 // or out_reg, AT, TMP
5630 can_move_conditionally = true;
5631 }
5632 } else {
5633 // movn out_reg, true_reg/ZERO, cond_reg
5634 can_move_conditionally = true;
5635 use_const_for_true_in = is_true_value_zero_constant;
5636 }
5637 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005639 // Moving long on int condition.
5640 if (is_r6) {
5641 if (is_true_value_zero_constant) {
5642 // seleqz out_reg_lo, false_reg_lo, cond_reg
5643 // seleqz out_reg_hi, false_reg_hi, cond_reg
5644 can_move_conditionally = true;
5645 use_const_for_true_in = true;
5646 } else if (is_false_value_zero_constant) {
5647 // selnez out_reg_lo, true_reg_lo, cond_reg
5648 // selnez out_reg_hi, true_reg_hi, cond_reg
5649 can_move_conditionally = true;
5650 use_const_for_false_in = true;
5651 }
5652 // Other long conditional moves would generate 6+ instructions,
5653 // which is too many.
5654 } else {
5655 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5656 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5657 can_move_conditionally = true;
5658 use_const_for_true_in = is_true_value_zero_constant;
5659 }
5660 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 case DataType::Type::kFloat32:
5662 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005663 // Moving float/double on int condition.
5664 if (is_r6) {
5665 if (materialized) {
5666 // Not materializing unmaterialized int conditions
5667 // to keep the instruction count low.
5668 can_move_conditionally = true;
5669 if (is_true_value_zero_constant) {
5670 // sltu TMP, ZERO, cond_reg
5671 // mtc1 TMP, temp_cond_reg
5672 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5673 use_const_for_true_in = true;
5674 } else if (is_false_value_zero_constant) {
5675 // sltu TMP, ZERO, cond_reg
5676 // mtc1 TMP, temp_cond_reg
5677 // selnez.fmt out_reg, true_reg, temp_cond_reg
5678 use_const_for_false_in = true;
5679 } else {
5680 // sltu TMP, ZERO, cond_reg
5681 // mtc1 TMP, temp_cond_reg
5682 // sel.fmt temp_cond_reg, false_reg, true_reg
5683 // mov.fmt out_reg, temp_cond_reg
5684 }
5685 }
5686 } else {
5687 // movn.fmt out_reg, true_reg, cond_reg
5688 can_move_conditionally = true;
5689 }
5690 break;
5691 }
5692 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005693 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005694 // We don't materialize long comparison now
5695 // and use conditional branches instead.
5696 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 case DataType::Type::kFloat32:
5698 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005699 switch (dst_type) {
5700 default:
5701 // Moving int on float/double condition.
5702 if (is_r6) {
5703 if (is_true_value_zero_constant) {
5704 // mfc1 TMP, temp_cond_reg
5705 // seleqz out_reg, false_reg, TMP
5706 can_move_conditionally = true;
5707 use_const_for_true_in = true;
5708 } else if (is_false_value_zero_constant) {
5709 // mfc1 TMP, temp_cond_reg
5710 // selnez out_reg, true_reg, TMP
5711 can_move_conditionally = true;
5712 use_const_for_false_in = true;
5713 } else {
5714 // mfc1 TMP, temp_cond_reg
5715 // selnez AT, true_reg, TMP
5716 // seleqz TMP, false_reg, TMP
5717 // or out_reg, AT, TMP
5718 can_move_conditionally = true;
5719 }
5720 } else {
5721 // movt out_reg, true_reg/ZERO, cc
5722 can_move_conditionally = true;
5723 use_const_for_true_in = is_true_value_zero_constant;
5724 }
5725 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005727 // Moving long on float/double condition.
5728 if (is_r6) {
5729 if (is_true_value_zero_constant) {
5730 // mfc1 TMP, temp_cond_reg
5731 // seleqz out_reg_lo, false_reg_lo, TMP
5732 // seleqz out_reg_hi, false_reg_hi, TMP
5733 can_move_conditionally = true;
5734 use_const_for_true_in = true;
5735 } else if (is_false_value_zero_constant) {
5736 // mfc1 TMP, temp_cond_reg
5737 // selnez out_reg_lo, true_reg_lo, TMP
5738 // selnez out_reg_hi, true_reg_hi, TMP
5739 can_move_conditionally = true;
5740 use_const_for_false_in = true;
5741 }
5742 // Other long conditional moves would generate 6+ instructions,
5743 // which is too many.
5744 } else {
5745 // movt out_reg_lo, true_reg_lo/ZERO, cc
5746 // movt out_reg_hi, true_reg_hi/ZERO, cc
5747 can_move_conditionally = true;
5748 use_const_for_true_in = is_true_value_zero_constant;
5749 }
5750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 case DataType::Type::kFloat32:
5752 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005753 // Moving float/double on float/double condition.
5754 if (is_r6) {
5755 can_move_conditionally = true;
5756 if (is_true_value_zero_constant) {
5757 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5758 use_const_for_true_in = true;
5759 } else if (is_false_value_zero_constant) {
5760 // selnez.fmt out_reg, true_reg, temp_cond_reg
5761 use_const_for_false_in = true;
5762 } else {
5763 // sel.fmt temp_cond_reg, false_reg, true_reg
5764 // mov.fmt out_reg, temp_cond_reg
5765 }
5766 } else {
5767 // movt.fmt out_reg, true_reg, cc
5768 can_move_conditionally = true;
5769 }
5770 break;
5771 }
5772 break;
5773 }
5774 }
5775
5776 if (can_move_conditionally) {
5777 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5778 } else {
5779 DCHECK(!use_const_for_false_in);
5780 DCHECK(!use_const_for_true_in);
5781 }
5782
5783 if (locations_to_set != nullptr) {
5784 if (use_const_for_false_in) {
5785 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5786 } else {
5787 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005788 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005789 ? Location::RequiresFpuRegister()
5790 : Location::RequiresRegister());
5791 }
5792 if (use_const_for_true_in) {
5793 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5794 } else {
5795 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005797 ? Location::RequiresFpuRegister()
5798 : Location::RequiresRegister());
5799 }
5800 if (materialized) {
5801 locations_to_set->SetInAt(2, Location::RequiresRegister());
5802 }
5803 // On R6 we don't require the output to be the same as the
5804 // first input for conditional moves unlike on R2.
5805 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5806 if (is_out_same_as_first_in) {
5807 locations_to_set->SetOut(Location::SameAsFirstInput());
5808 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005810 ? Location::RequiresFpuRegister()
5811 : Location::RequiresRegister());
5812 }
5813 }
5814
5815 return can_move_conditionally;
5816}
5817
5818void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5819 LocationSummary* locations = select->GetLocations();
5820 Location dst = locations->Out();
5821 Location src = locations->InAt(1);
5822 Register src_reg = ZERO;
5823 Register src_reg_high = ZERO;
5824 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5825 Register cond_reg = TMP;
5826 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005828 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005830
5831 if (IsBooleanValueOrMaterializedCondition(cond)) {
5832 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5833 } else {
5834 HCondition* condition = cond->AsCondition();
5835 LocationSummary* cond_locations = cond->GetLocations();
5836 IfCondition if_cond = condition->GetCondition();
5837 cond_type = condition->InputAt(0)->GetType();
5838 switch (cond_type) {
5839 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005841 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5842 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 case DataType::Type::kFloat32:
5844 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005845 cond_inverted = MaterializeFpCompareR2(if_cond,
5846 condition->IsGtBias(),
5847 cond_type,
5848 cond_locations,
5849 cond_cc);
5850 break;
5851 }
5852 }
5853
5854 DCHECK(dst.Equals(locations->InAt(0)));
5855 if (src.IsRegister()) {
5856 src_reg = src.AsRegister<Register>();
5857 } else if (src.IsRegisterPair()) {
5858 src_reg = src.AsRegisterPairLow<Register>();
5859 src_reg_high = src.AsRegisterPairHigh<Register>();
5860 } else if (src.IsConstant()) {
5861 DCHECK(src.GetConstant()->IsZeroBitPattern());
5862 }
5863
5864 switch (cond_type) {
5865 default:
5866 switch (dst_type) {
5867 default:
5868 if (cond_inverted) {
5869 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5870 } else {
5871 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5872 }
5873 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005875 if (cond_inverted) {
5876 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5877 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5878 } else {
5879 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5880 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5881 }
5882 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005884 if (cond_inverted) {
5885 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5886 } else {
5887 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5888 }
5889 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005891 if (cond_inverted) {
5892 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5893 } else {
5894 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5895 }
5896 break;
5897 }
5898 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005899 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005900 LOG(FATAL) << "Unreachable";
5901 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005902 case DataType::Type::kFloat32:
5903 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005904 switch (dst_type) {
5905 default:
5906 if (cond_inverted) {
5907 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5908 } else {
5909 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5910 }
5911 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005912 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005913 if (cond_inverted) {
5914 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5915 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5916 } else {
5917 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5918 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5919 }
5920 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005921 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005922 if (cond_inverted) {
5923 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5924 } else {
5925 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5926 }
5927 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005929 if (cond_inverted) {
5930 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5931 } else {
5932 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5933 }
5934 break;
5935 }
5936 break;
5937 }
5938}
5939
5940void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5941 LocationSummary* locations = select->GetLocations();
5942 Location dst = locations->Out();
5943 Location false_src = locations->InAt(0);
5944 Location true_src = locations->InAt(1);
5945 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5946 Register cond_reg = TMP;
5947 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005949 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005951
5952 if (IsBooleanValueOrMaterializedCondition(cond)) {
5953 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5954 } else {
5955 HCondition* condition = cond->AsCondition();
5956 LocationSummary* cond_locations = cond->GetLocations();
5957 IfCondition if_cond = condition->GetCondition();
5958 cond_type = condition->InputAt(0)->GetType();
5959 switch (cond_type) {
5960 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005961 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005962 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5963 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 case DataType::Type::kFloat32:
5965 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005966 cond_inverted = MaterializeFpCompareR6(if_cond,
5967 condition->IsGtBias(),
5968 cond_type,
5969 cond_locations,
5970 fcond_reg);
5971 break;
5972 }
5973 }
5974
5975 if (true_src.IsConstant()) {
5976 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5977 }
5978 if (false_src.IsConstant()) {
5979 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5980 }
5981
5982 switch (dst_type) {
5983 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005985 __ Mfc1(cond_reg, fcond_reg);
5986 }
5987 if (true_src.IsConstant()) {
5988 if (cond_inverted) {
5989 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5990 } else {
5991 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5992 }
5993 } else if (false_src.IsConstant()) {
5994 if (cond_inverted) {
5995 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5996 } else {
5997 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5998 }
5999 } else {
6000 DCHECK_NE(cond_reg, AT);
6001 if (cond_inverted) {
6002 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6003 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6004 } else {
6005 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6006 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6007 }
6008 __ Or(dst.AsRegister<Register>(), AT, TMP);
6009 }
6010 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 case DataType::Type::kInt64: {
6012 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006013 __ Mfc1(cond_reg, fcond_reg);
6014 }
6015 Register dst_lo = dst.AsRegisterPairLow<Register>();
6016 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6017 if (true_src.IsConstant()) {
6018 Register src_lo = false_src.AsRegisterPairLow<Register>();
6019 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6020 if (cond_inverted) {
6021 __ Selnez(dst_lo, src_lo, cond_reg);
6022 __ Selnez(dst_hi, src_hi, cond_reg);
6023 } else {
6024 __ Seleqz(dst_lo, src_lo, cond_reg);
6025 __ Seleqz(dst_hi, src_hi, cond_reg);
6026 }
6027 } else {
6028 DCHECK(false_src.IsConstant());
6029 Register src_lo = true_src.AsRegisterPairLow<Register>();
6030 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6031 if (cond_inverted) {
6032 __ Seleqz(dst_lo, src_lo, cond_reg);
6033 __ Seleqz(dst_hi, src_hi, cond_reg);
6034 } else {
6035 __ Selnez(dst_lo, src_lo, cond_reg);
6036 __ Selnez(dst_hi, src_hi, cond_reg);
6037 }
6038 }
6039 break;
6040 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 case DataType::Type::kFloat32: {
6042 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006043 // sel*.fmt tests bit 0 of the condition register, account for that.
6044 __ Sltu(TMP, ZERO, cond_reg);
6045 __ Mtc1(TMP, fcond_reg);
6046 }
6047 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6048 if (true_src.IsConstant()) {
6049 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6050 if (cond_inverted) {
6051 __ SelnezS(dst_reg, src_reg, fcond_reg);
6052 } else {
6053 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6054 }
6055 } else if (false_src.IsConstant()) {
6056 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6057 if (cond_inverted) {
6058 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6059 } else {
6060 __ SelnezS(dst_reg, src_reg, fcond_reg);
6061 }
6062 } else {
6063 if (cond_inverted) {
6064 __ SelS(fcond_reg,
6065 true_src.AsFpuRegister<FRegister>(),
6066 false_src.AsFpuRegister<FRegister>());
6067 } else {
6068 __ SelS(fcond_reg,
6069 false_src.AsFpuRegister<FRegister>(),
6070 true_src.AsFpuRegister<FRegister>());
6071 }
6072 __ MovS(dst_reg, fcond_reg);
6073 }
6074 break;
6075 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 case DataType::Type::kFloat64: {
6077 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006078 // sel*.fmt tests bit 0 of the condition register, account for that.
6079 __ Sltu(TMP, ZERO, cond_reg);
6080 __ Mtc1(TMP, fcond_reg);
6081 }
6082 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6083 if (true_src.IsConstant()) {
6084 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6085 if (cond_inverted) {
6086 __ SelnezD(dst_reg, src_reg, fcond_reg);
6087 } else {
6088 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6089 }
6090 } else if (false_src.IsConstant()) {
6091 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6092 if (cond_inverted) {
6093 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6094 } else {
6095 __ SelnezD(dst_reg, src_reg, fcond_reg);
6096 }
6097 } else {
6098 if (cond_inverted) {
6099 __ SelD(fcond_reg,
6100 true_src.AsFpuRegister<FRegister>(),
6101 false_src.AsFpuRegister<FRegister>());
6102 } else {
6103 __ SelD(fcond_reg,
6104 false_src.AsFpuRegister<FRegister>(),
6105 true_src.AsFpuRegister<FRegister>());
6106 }
6107 __ MovD(dst_reg, fcond_reg);
6108 }
6109 break;
6110 }
6111 }
6112}
6113
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006114void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006115 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006116 LocationSummary(flag, LocationSummary::kNoCall);
6117 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006118}
6119
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006120void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6121 __ LoadFromOffset(kLoadWord,
6122 flag->GetLocations()->Out().AsRegister<Register>(),
6123 SP,
6124 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006125}
6126
David Brazdil74eb1b22015-12-14 11:44:01 +00006127void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006128 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006129 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006130}
6131
6132void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006133 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6134 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6135 if (is_r6) {
6136 GenConditionalMoveR6(select);
6137 } else {
6138 GenConditionalMoveR2(select);
6139 }
6140 } else {
6141 LocationSummary* locations = select->GetLocations();
6142 MipsLabel false_target;
6143 GenerateTestAndBranch(select,
6144 /* condition_input_index */ 2,
6145 /* true_target */ nullptr,
6146 &false_target);
6147 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6148 __ Bind(&false_target);
6149 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006150}
6151
David Srbecky0cf44932015-12-09 14:09:59 +00006152void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006153 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006154}
6155
David Srbeckyd28f4a02016-03-14 17:14:24 +00006156void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6157 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006158}
6159
6160void CodeGeneratorMIPS::GenerateNop() {
6161 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006162}
6163
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006164void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006165 DataType::Type field_type = field_info.GetFieldType();
6166 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006168 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006169 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006170 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006171 instruction,
6172 generate_volatile
6173 ? LocationSummary::kCallOnMainOnly
6174 : (object_field_get_with_read_barrier
6175 ? LocationSummary::kCallOnSlowPath
6176 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006177
Alexey Frunzec61c0762017-04-10 13:54:23 -07006178 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6179 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6180 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006181 locations->SetInAt(0, Location::RequiresRegister());
6182 if (generate_volatile) {
6183 InvokeRuntimeCallingConvention calling_convention;
6184 // need A0 to hold base + offset
6185 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 if (field_type == DataType::Type::kInt64) {
6187 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006189 // Use Location::Any() to prevent situations when running out of available fp registers.
6190 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006191 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006193 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6194 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6195 }
6196 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006198 locations->SetOut(Location::RequiresFpuRegister());
6199 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006200 // The output overlaps in the case of an object field get with
6201 // read barriers enabled: we do not want the move to overwrite the
6202 // object's location, as we need it to emit the read barrier.
6203 locations->SetOut(Location::RequiresRegister(),
6204 object_field_get_with_read_barrier
6205 ? Location::kOutputOverlap
6206 : Location::kNoOutputOverlap);
6207 }
6208 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6209 // We need a temporary register for the read barrier marking slow
6210 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006211 if (!kBakerReadBarrierThunksEnableForFields) {
6212 locations->AddTemp(Location::RequiresRegister());
6213 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214 }
6215 }
6216}
6217
6218void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6219 const FieldInfo& field_info,
6220 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006221 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6222 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006223 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006224 Location obj_loc = locations->InAt(0);
6225 Register obj = obj_loc.AsRegister<Register>();
6226 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006227 LoadOperandType load_type = kLoadUnsignedByte;
6228 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006229 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006230 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006231
6232 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006234 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 load_type = kLoadUnsignedByte;
6236 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006238 load_type = kLoadSignedByte;
6239 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006241 load_type = kLoadUnsignedHalfword;
6242 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006243 case DataType::Type::kInt16:
6244 load_type = kLoadSignedHalfword;
6245 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 case DataType::Type::kInt32:
6247 case DataType::Type::kFloat32:
6248 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 load_type = kLoadWord;
6250 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006251 case DataType::Type::kInt64:
6252 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 load_type = kLoadDoubleword;
6254 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006255 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006256 LOG(FATAL) << "Unreachable type " << type;
6257 UNREACHABLE();
6258 }
6259
6260 if (is_volatile && load_type == kLoadDoubleword) {
6261 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006262 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006263 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006264 __ LoadFromOffset(kLoadWord,
6265 ZERO,
6266 locations->GetTemp(0).AsRegister<Register>(),
6267 0,
6268 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006269 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006270 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006271 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006272 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006273 if (dst_loc.IsFpuRegister()) {
6274 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006275 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006276 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006277 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006278 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006279 __ StoreToOffset(kStoreWord,
6280 locations->GetTemp(1).AsRegister<Register>(),
6281 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006282 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006283 __ StoreToOffset(kStoreWord,
6284 locations->GetTemp(2).AsRegister<Register>(),
6285 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006286 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006288 }
6289 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006290 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006291 // /* HeapReference<Object> */ dst = *(obj + offset)
6292 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006293 Location temp_loc =
6294 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006295 // Note that a potential implicit null check is handled in this
6296 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6297 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6298 dst_loc,
6299 obj,
6300 offset,
6301 temp_loc,
6302 /* needs_null_check */ true);
6303 if (is_volatile) {
6304 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6305 }
6306 } else {
6307 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6308 if (is_volatile) {
6309 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6310 }
6311 // If read barriers are enabled, emit read barriers other than
6312 // Baker's using a slow path (and also unpoison the loaded
6313 // reference, if heap poisoning is enabled).
6314 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6315 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006316 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006317 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006319 DCHECK(dst_loc.IsRegisterPair());
6320 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006321 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006322 DCHECK(dst_loc.IsRegister());
6323 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006324 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006325 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006326 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006327 DCHECK(dst_loc.IsFpuRegister());
6328 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006329 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006330 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006331 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006332 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006333 }
6334 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006335 }
6336
Alexey Frunze15958152017-02-09 19:08:30 -08006337 // Memory barriers, in the case of references, are handled in the
6338 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006340 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6341 }
6342}
6343
6344void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 DataType::Type field_type = field_info.GetFieldType();
6346 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006347 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006348 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006349 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006350
6351 locations->SetInAt(0, Location::RequiresRegister());
6352 if (generate_volatile) {
6353 InvokeRuntimeCallingConvention calling_convention;
6354 // need A0 to hold base + offset
6355 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006357 locations->SetInAt(1, Location::RegisterPairLocation(
6358 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6359 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006360 // Use Location::Any() to prevent situations when running out of available fp registers.
6361 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006362 // Pass FP parameters in core registers.
6363 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6364 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6365 }
6366 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006367 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006368 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006369 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006370 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006371 }
6372 }
6373}
6374
6375void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6376 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006377 uint32_t dex_pc,
6378 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 LocationSummary* locations = instruction->GetLocations();
6381 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006382 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006383 StoreOperandType store_type = kStoreByte;
6384 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006385 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006386 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006387 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006388
6389 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006391 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006392 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006393 store_type = kStoreByte;
6394 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006396 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006397 store_type = kStoreHalfword;
6398 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 case DataType::Type::kInt32:
6400 case DataType::Type::kFloat32:
6401 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006402 store_type = kStoreWord;
6403 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006404 case DataType::Type::kInt64:
6405 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006406 store_type = kStoreDoubleword;
6407 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006408 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006409 LOG(FATAL) << "Unreachable type " << type;
6410 UNREACHABLE();
6411 }
6412
6413 if (is_volatile) {
6414 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6415 }
6416
6417 if (is_volatile && store_type == kStoreDoubleword) {
6418 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006419 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006421 __ LoadFromOffset(kLoadWord,
6422 ZERO,
6423 locations->GetTemp(0).AsRegister<Register>(),
6424 0,
6425 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006426 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006427 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006428 if (value_location.IsFpuRegister()) {
6429 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6430 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006431 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006432 value_location.AsFpuRegister<FRegister>());
6433 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006434 __ LoadFromOffset(kLoadWord,
6435 locations->GetTemp(1).AsRegister<Register>(),
6436 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006437 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006438 __ LoadFromOffset(kLoadWord,
6439 locations->GetTemp(2).AsRegister<Register>(),
6440 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006441 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006442 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006443 DCHECK(value_location.IsConstant());
6444 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6445 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006446 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6447 locations->GetTemp(1).AsRegister<Register>(),
6448 value);
6449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006451 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006452 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6453 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006454 if (value_location.IsConstant()) {
6455 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6456 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006457 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006458 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006460 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006461 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006462 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006463 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006464 if (kPoisonHeapReferences && needs_write_barrier) {
6465 // Note that in the case where `value` is a null reference,
6466 // we do not enter this block, as a null reference does not
6467 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006468 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006469 __ PoisonHeapReference(TMP, src);
6470 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6471 } else {
6472 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006474 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006475 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006476 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006477 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006478 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006479 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 }
6481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006482 }
6483
Alexey Frunzec061de12017-02-14 13:27:23 -08006484 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006485 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006486 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006487 }
6488
6489 if (is_volatile) {
6490 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6491 }
6492}
6493
6494void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6495 HandleFieldGet(instruction, instruction->GetFieldInfo());
6496}
6497
6498void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6499 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6500}
6501
6502void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6503 HandleFieldSet(instruction, instruction->GetFieldInfo());
6504}
6505
6506void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006507 HandleFieldSet(instruction,
6508 instruction->GetFieldInfo(),
6509 instruction->GetDexPc(),
6510 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511}
6512
Alexey Frunze15958152017-02-09 19:08:30 -08006513void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6514 HInstruction* instruction,
6515 Location out,
6516 uint32_t offset,
6517 Location maybe_temp,
6518 ReadBarrierOption read_barrier_option) {
6519 Register out_reg = out.AsRegister<Register>();
6520 if (read_barrier_option == kWithReadBarrier) {
6521 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006522 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6523 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6524 }
Alexey Frunze15958152017-02-09 19:08:30 -08006525 if (kUseBakerReadBarrier) {
6526 // Load with fast path based Baker's read barrier.
6527 // /* HeapReference<Object> */ out = *(out + offset)
6528 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6529 out,
6530 out_reg,
6531 offset,
6532 maybe_temp,
6533 /* needs_null_check */ false);
6534 } else {
6535 // Load with slow path based read barrier.
6536 // Save the value of `out` into `maybe_temp` before overwriting it
6537 // in the following move operation, as we will need it for the
6538 // read barrier below.
6539 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6540 // /* HeapReference<Object> */ out = *(out + offset)
6541 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6542 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6543 }
6544 } else {
6545 // Plain load with no read barrier.
6546 // /* HeapReference<Object> */ out = *(out + offset)
6547 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6548 __ MaybeUnpoisonHeapReference(out_reg);
6549 }
6550}
6551
6552void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6553 HInstruction* instruction,
6554 Location out,
6555 Location obj,
6556 uint32_t offset,
6557 Location maybe_temp,
6558 ReadBarrierOption read_barrier_option) {
6559 Register out_reg = out.AsRegister<Register>();
6560 Register obj_reg = obj.AsRegister<Register>();
6561 if (read_barrier_option == kWithReadBarrier) {
6562 CHECK(kEmitCompilerReadBarrier);
6563 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006564 if (!kBakerReadBarrierThunksEnableForFields) {
6565 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6566 }
Alexey Frunze15958152017-02-09 19:08:30 -08006567 // Load with fast path based Baker's read barrier.
6568 // /* HeapReference<Object> */ out = *(obj + offset)
6569 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6570 out,
6571 obj_reg,
6572 offset,
6573 maybe_temp,
6574 /* needs_null_check */ false);
6575 } else {
6576 // Load with slow path based read barrier.
6577 // /* HeapReference<Object> */ out = *(obj + offset)
6578 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6579 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6580 }
6581 } else {
6582 // Plain load with no read barrier.
6583 // /* HeapReference<Object> */ out = *(obj + offset)
6584 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6585 __ MaybeUnpoisonHeapReference(out_reg);
6586 }
6587}
6588
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006589static inline int GetBakerMarkThunkNumber(Register reg) {
6590 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6591 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6592 return reg - V0;
6593 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6594 return 14 + (reg - S2);
6595 } else if (reg == FP) { // One more.
6596 return 20;
6597 }
6598 LOG(FATAL) << "Unexpected register " << reg;
6599 UNREACHABLE();
6600}
6601
6602static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6603 int num = GetBakerMarkThunkNumber(reg) +
6604 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6605 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6606}
6607
6608static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6609 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6610 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6611}
6612
Alexey Frunze15958152017-02-09 19:08:30 -08006613void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6614 Location root,
6615 Register obj,
6616 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006617 ReadBarrierOption read_barrier_option,
6618 MipsLabel* label_low) {
6619 bool reordering;
6620 if (label_low != nullptr) {
6621 DCHECK_EQ(offset, 0x5678u);
6622 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006623 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006624 if (read_barrier_option == kWithReadBarrier) {
6625 DCHECK(kEmitCompilerReadBarrier);
6626 if (kUseBakerReadBarrier) {
6627 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6628 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006629 if (kBakerReadBarrierThunksEnableForGcRoots) {
6630 // Note that we do not actually check the value of `GetIsGcMarking()`
6631 // to decide whether to mark the loaded GC root or not. Instead, we
6632 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6633 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6634 // vice versa.
6635 //
6636 // We use thunks for the slow path. That thunk checks the reference
6637 // and jumps to the entrypoint if needed.
6638 //
6639 // temp = Thread::Current()->pReadBarrierMarkReg00
6640 // // AKA &art_quick_read_barrier_mark_introspection.
6641 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6642 // if (temp != nullptr) {
6643 // temp = &gc_root_thunk<root_reg>
6644 // root = temp(root)
6645 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006646
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006647 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6648 const int32_t entry_point_offset =
6649 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6650 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6651 int16_t offset_low = Low16Bits(offset);
6652 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6653 // extension in lw.
6654 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6655 Register base = short_offset ? obj : TMP;
6656 // Loading the entrypoint does not require a load acquire since it is only changed when
6657 // threads are suspended or running a checkpoint.
6658 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6659 reordering = __ SetReorder(false);
6660 if (!short_offset) {
6661 DCHECK(!label_low);
6662 __ AddUpper(base, obj, offset_high);
6663 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006664 MipsLabel skip_call;
6665 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006666 if (label_low != nullptr) {
6667 DCHECK(short_offset);
6668 __ Bind(label_low);
6669 }
6670 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6671 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6672 // in delay slot.
6673 if (isR6) {
6674 __ Jialc(T9, thunk_disp);
6675 } else {
6676 __ Addiu(T9, T9, thunk_disp);
6677 __ Jalr(T9);
6678 __ Nop();
6679 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006680 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006681 __ SetReorder(reordering);
6682 } else {
6683 // Note that we do not actually check the value of `GetIsGcMarking()`
6684 // to decide whether to mark the loaded GC root or not. Instead, we
6685 // load into `temp` (T9) the read barrier mark entry point corresponding
6686 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6687 // is false, and vice versa.
6688 //
6689 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6690 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6691 // if (temp != null) {
6692 // root = temp(root)
6693 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006694
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006695 if (label_low != nullptr) {
6696 reordering = __ SetReorder(false);
6697 __ Bind(label_low);
6698 }
6699 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6700 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6701 if (label_low != nullptr) {
6702 __ SetReorder(reordering);
6703 }
6704 static_assert(
6705 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6706 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6707 "have different sizes.");
6708 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6709 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6710 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006711
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006712 // Slow path marking the GC root `root`.
6713 Location temp = Location::RegisterLocation(T9);
6714 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006715 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006716 instruction,
6717 root,
6718 /*entrypoint*/ temp);
6719 codegen_->AddSlowPath(slow_path);
6720
6721 const int32_t entry_point_offset =
6722 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6723 // Loading the entrypoint does not require a load acquire since it is only changed when
6724 // threads are suspended or running a checkpoint.
6725 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6726 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6727 __ Bind(slow_path->GetExitLabel());
6728 }
Alexey Frunze15958152017-02-09 19:08:30 -08006729 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006730 if (label_low != nullptr) {
6731 reordering = __ SetReorder(false);
6732 __ Bind(label_low);
6733 }
Alexey Frunze15958152017-02-09 19:08:30 -08006734 // GC root loaded through a slow path for read barriers other
6735 // than Baker's.
6736 // /* GcRoot<mirror::Object>* */ root = obj + offset
6737 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006738 if (label_low != nullptr) {
6739 __ SetReorder(reordering);
6740 }
Alexey Frunze15958152017-02-09 19:08:30 -08006741 // /* mirror::Object* */ root = root->Read()
6742 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6743 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006744 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006745 if (label_low != nullptr) {
6746 reordering = __ SetReorder(false);
6747 __ Bind(label_low);
6748 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006749 // Plain GC root load with no read barrier.
6750 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6751 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6752 // Note that GC roots are not affected by heap poisoning, thus we
6753 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006754 if (label_low != nullptr) {
6755 __ SetReorder(reordering);
6756 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006757 }
6758}
6759
Alexey Frunze15958152017-02-09 19:08:30 -08006760void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6761 Location ref,
6762 Register obj,
6763 uint32_t offset,
6764 Location temp,
6765 bool needs_null_check) {
6766 DCHECK(kEmitCompilerReadBarrier);
6767 DCHECK(kUseBakerReadBarrier);
6768
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006769 if (kBakerReadBarrierThunksEnableForFields) {
6770 // Note that we do not actually check the value of `GetIsGcMarking()`
6771 // to decide whether to mark the loaded reference or not. Instead, we
6772 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6773 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6774 // vice versa.
6775 //
6776 // We use thunks for the slow path. That thunk checks the reference
6777 // and jumps to the entrypoint if needed. If the holder is not gray,
6778 // it issues a load-load memory barrier and returns to the original
6779 // reference load.
6780 //
6781 // temp = Thread::Current()->pReadBarrierMarkReg00
6782 // // AKA &art_quick_read_barrier_mark_introspection.
6783 // if (temp != nullptr) {
6784 // temp = &field_array_thunk<holder_reg>
6785 // temp()
6786 // }
6787 // not_gray_return_address:
6788 // // If the offset is too large to fit into the lw instruction, we
6789 // // use an adjusted base register (TMP) here. This register
6790 // // receives bits 16 ... 31 of the offset before the thunk invocation
6791 // // and the thunk benefits from it.
6792 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6793 // gray_return_address:
6794
6795 DCHECK(temp.IsInvalid());
6796 bool isR6 = GetInstructionSetFeatures().IsR6();
6797 int16_t offset_low = Low16Bits(offset);
6798 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6799 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6800 bool reordering = __ SetReorder(false);
6801 const int32_t entry_point_offset =
6802 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6803 // There may have or may have not been a null check if the field offset is smaller than
6804 // the page size.
6805 // There must've been a null check in case it's actually a load from an array.
6806 // We will, however, perform an explicit null check in the thunk as it's easier to
6807 // do it than not.
6808 if (instruction->IsArrayGet()) {
6809 DCHECK(!needs_null_check);
6810 }
6811 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6812 // Loading the entrypoint does not require a load acquire since it is only changed when
6813 // threads are suspended or running a checkpoint.
6814 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6815 Register ref_reg = ref.AsRegister<Register>();
6816 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006817 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006818 if (short_offset) {
6819 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006820 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006821 __ Nop(); // In forbidden slot.
6822 __ Jialc(T9, thunk_disp);
6823 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006824 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006825 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6826 __ Jalr(T9);
6827 __ Nop(); // In delay slot.
6828 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006829 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006830 } else {
6831 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006832 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006833 __ Aui(base, obj, offset_high); // In delay slot.
6834 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006835 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006836 } else {
6837 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006838 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006839 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6840 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006841 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006842 __ Addu(base, base, obj); // In delay slot.
6843 }
6844 }
6845 // /* HeapReference<Object> */ ref = *(obj + offset)
6846 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6847 if (needs_null_check) {
6848 MaybeRecordImplicitNullCheck(instruction);
6849 }
6850 __ MaybeUnpoisonHeapReference(ref_reg);
6851 __ SetReorder(reordering);
6852 return;
6853 }
6854
Alexey Frunze15958152017-02-09 19:08:30 -08006855 // /* HeapReference<Object> */ ref = *(obj + offset)
6856 Location no_index = Location::NoLocation();
6857 ScaleFactor no_scale_factor = TIMES_1;
6858 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6859 ref,
6860 obj,
6861 offset,
6862 no_index,
6863 no_scale_factor,
6864 temp,
6865 needs_null_check);
6866}
6867
6868void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6869 Location ref,
6870 Register obj,
6871 uint32_t data_offset,
6872 Location index,
6873 Location temp,
6874 bool needs_null_check) {
6875 DCHECK(kEmitCompilerReadBarrier);
6876 DCHECK(kUseBakerReadBarrier);
6877
6878 static_assert(
6879 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6880 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006881 ScaleFactor scale_factor = TIMES_4;
6882
6883 if (kBakerReadBarrierThunksEnableForArrays) {
6884 // Note that we do not actually check the value of `GetIsGcMarking()`
6885 // to decide whether to mark the loaded reference or not. Instead, we
6886 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6887 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6888 // vice versa.
6889 //
6890 // We use thunks for the slow path. That thunk checks the reference
6891 // and jumps to the entrypoint if needed. If the holder is not gray,
6892 // it issues a load-load memory barrier and returns to the original
6893 // reference load.
6894 //
6895 // temp = Thread::Current()->pReadBarrierMarkReg00
6896 // // AKA &art_quick_read_barrier_mark_introspection.
6897 // if (temp != nullptr) {
6898 // temp = &field_array_thunk<holder_reg>
6899 // temp()
6900 // }
6901 // not_gray_return_address:
6902 // // The element address is pre-calculated in the TMP register before the
6903 // // thunk invocation and the thunk benefits from it.
6904 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6905 // gray_return_address:
6906
6907 DCHECK(temp.IsInvalid());
6908 DCHECK(index.IsValid());
6909 bool reordering = __ SetReorder(false);
6910 const int32_t entry_point_offset =
6911 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6912 // We will not do the explicit null check in the thunk as some form of a null check
6913 // must've been done earlier.
6914 DCHECK(!needs_null_check);
6915 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6916 // Loading the entrypoint does not require a load acquire since it is only changed when
6917 // threads are suspended or running a checkpoint.
6918 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6919 Register ref_reg = ref.AsRegister<Register>();
6920 Register index_reg = index.IsRegisterPair()
6921 ? index.AsRegisterPairLow<Register>()
6922 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006923 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006924 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006925 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006926 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6927 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006928 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006929 } else {
6930 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006931 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006932 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6933 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006934 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006935 __ Addu(TMP, TMP, obj); // In delay slot.
6936 }
6937 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6938 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6939 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6940 __ MaybeUnpoisonHeapReference(ref_reg);
6941 __ SetReorder(reordering);
6942 return;
6943 }
6944
Alexey Frunze15958152017-02-09 19:08:30 -08006945 // /* HeapReference<Object> */ ref =
6946 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006947 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6948 ref,
6949 obj,
6950 data_offset,
6951 index,
6952 scale_factor,
6953 temp,
6954 needs_null_check);
6955}
6956
6957void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6958 Location ref,
6959 Register obj,
6960 uint32_t offset,
6961 Location index,
6962 ScaleFactor scale_factor,
6963 Location temp,
6964 bool needs_null_check,
6965 bool always_update_field) {
6966 DCHECK(kEmitCompilerReadBarrier);
6967 DCHECK(kUseBakerReadBarrier);
6968
6969 // In slow path based read barriers, the read barrier call is
6970 // inserted after the original load. However, in fast path based
6971 // Baker's read barriers, we need to perform the load of
6972 // mirror::Object::monitor_ *before* the original reference load.
6973 // This load-load ordering is required by the read barrier.
6974 // The fast path/slow path (for Baker's algorithm) should look like:
6975 //
6976 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6977 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6978 // HeapReference<Object> ref = *src; // Original reference load.
6979 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6980 // if (is_gray) {
6981 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6982 // }
6983 //
6984 // Note: the original implementation in ReadBarrier::Barrier is
6985 // slightly more complex as it performs additional checks that we do
6986 // not do here for performance reasons.
6987
6988 Register ref_reg = ref.AsRegister<Register>();
6989 Register temp_reg = temp.AsRegister<Register>();
6990 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6991
6992 // /* int32_t */ monitor = obj->monitor_
6993 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6994 if (needs_null_check) {
6995 MaybeRecordImplicitNullCheck(instruction);
6996 }
6997 // /* LockWord */ lock_word = LockWord(monitor)
6998 static_assert(sizeof(LockWord) == sizeof(int32_t),
6999 "art::LockWord and int32_t have different sizes.");
7000
7001 __ Sync(0); // Barrier to prevent load-load reordering.
7002
7003 // The actual reference load.
7004 if (index.IsValid()) {
7005 // Load types involving an "index": ArrayGet,
7006 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7007 // intrinsics.
7008 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7009 if (index.IsConstant()) {
7010 size_t computed_offset =
7011 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7012 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7013 } else {
7014 // Handle the special case of the
7015 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7016 // intrinsics, which use a register pair as index ("long
7017 // offset"), of which only the low part contains data.
7018 Register index_reg = index.IsRegisterPair()
7019 ? index.AsRegisterPairLow<Register>()
7020 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007021 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007022 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7023 }
7024 } else {
7025 // /* HeapReference<Object> */ ref = *(obj + offset)
7026 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7027 }
7028
7029 // Object* ref = ref_addr->AsMirrorPtr()
7030 __ MaybeUnpoisonHeapReference(ref_reg);
7031
7032 // Slow path marking the object `ref` when it is gray.
7033 SlowPathCodeMIPS* slow_path;
7034 if (always_update_field) {
7035 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7036 // of the form `obj + field_offset`, where `obj` is a register and
7037 // `field_offset` is a register pair (of which only the lower half
7038 // is used). Thus `offset` and `scale_factor` above are expected
7039 // to be null in this code path.
7040 DCHECK_EQ(offset, 0u);
7041 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007042 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007043 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7044 ref,
7045 obj,
7046 /* field_offset */ index,
7047 temp_reg);
7048 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007049 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007050 }
7051 AddSlowPath(slow_path);
7052
7053 // if (rb_state == ReadBarrier::GrayState())
7054 // ref = ReadBarrier::Mark(ref);
7055 // Given the numeric representation, it's enough to check the low bit of the
7056 // rb_state. We do that by shifting the bit into the sign bit (31) and
7057 // performing a branch on less than zero.
7058 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7059 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7060 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7061 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7062 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7063 __ Bind(slow_path->GetExitLabel());
7064}
7065
7066void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7067 Location out,
7068 Location ref,
7069 Location obj,
7070 uint32_t offset,
7071 Location index) {
7072 DCHECK(kEmitCompilerReadBarrier);
7073
7074 // Insert a slow path based read barrier *after* the reference load.
7075 //
7076 // If heap poisoning is enabled, the unpoisoning of the loaded
7077 // reference will be carried out by the runtime within the slow
7078 // path.
7079 //
7080 // Note that `ref` currently does not get unpoisoned (when heap
7081 // poisoning is enabled), which is alright as the `ref` argument is
7082 // not used by the artReadBarrierSlow entry point.
7083 //
7084 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007085 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007086 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7087 AddSlowPath(slow_path);
7088
7089 __ B(slow_path->GetEntryLabel());
7090 __ Bind(slow_path->GetExitLabel());
7091}
7092
7093void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7094 Location out,
7095 Location ref,
7096 Location obj,
7097 uint32_t offset,
7098 Location index) {
7099 if (kEmitCompilerReadBarrier) {
7100 // Baker's read barriers shall be handled by the fast path
7101 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7102 DCHECK(!kUseBakerReadBarrier);
7103 // If heap poisoning is enabled, unpoisoning will be taken care of
7104 // by the runtime within the slow path.
7105 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7106 } else if (kPoisonHeapReferences) {
7107 __ UnpoisonHeapReference(out.AsRegister<Register>());
7108 }
7109}
7110
7111void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7112 Location out,
7113 Location root) {
7114 DCHECK(kEmitCompilerReadBarrier);
7115
7116 // Insert a slow path based read barrier *after* the GC root load.
7117 //
7118 // Note that GC roots are not affected by heap poisoning, so we do
7119 // not need to do anything special for this here.
7120 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007121 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007122 AddSlowPath(slow_path);
7123
7124 __ B(slow_path->GetEntryLabel());
7125 __ Bind(slow_path->GetExitLabel());
7126}
7127
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007128void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007129 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7130 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007131 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007132 switch (type_check_kind) {
7133 case TypeCheckKind::kExactCheck:
7134 case TypeCheckKind::kAbstractClassCheck:
7135 case TypeCheckKind::kClassHierarchyCheck:
7136 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007137 call_kind =
7138 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007139 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007140 break;
7141 case TypeCheckKind::kArrayCheck:
7142 case TypeCheckKind::kUnresolvedCheck:
7143 case TypeCheckKind::kInterfaceCheck:
7144 call_kind = LocationSummary::kCallOnSlowPath;
7145 break;
7146 }
7147
Vladimir Markoca6fff82017-10-03 14:49:14 +01007148 LocationSummary* locations =
7149 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007150 if (baker_read_barrier_slow_path) {
7151 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7152 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007153 locations->SetInAt(0, Location::RequiresRegister());
7154 locations->SetInAt(1, Location::RequiresRegister());
7155 // The output does overlap inputs.
7156 // Note that TypeCheckSlowPathMIPS uses this register too.
7157 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007158 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007159}
7160
7161void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007162 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007163 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007164 Location obj_loc = locations->InAt(0);
7165 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007166 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007167 Location out_loc = locations->Out();
7168 Register out = out_loc.AsRegister<Register>();
7169 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7170 DCHECK_LE(num_temps, 1u);
7171 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007172 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7173 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7174 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7175 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007177 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007178
7179 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007180 // Avoid this check if we know `obj` is not null.
7181 if (instruction->MustDoNullCheck()) {
7182 __ Move(out, ZERO);
7183 __ Beqz(obj, &done);
7184 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007185
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007186 switch (type_check_kind) {
7187 case TypeCheckKind::kExactCheck: {
7188 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007189 GenerateReferenceLoadTwoRegisters(instruction,
7190 out_loc,
7191 obj_loc,
7192 class_offset,
7193 maybe_temp_loc,
7194 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007195 // Classes must be equal for the instanceof to succeed.
7196 __ Xor(out, out, cls);
7197 __ Sltiu(out, out, 1);
7198 break;
7199 }
7200
7201 case TypeCheckKind::kAbstractClassCheck: {
7202 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007203 GenerateReferenceLoadTwoRegisters(instruction,
7204 out_loc,
7205 obj_loc,
7206 class_offset,
7207 maybe_temp_loc,
7208 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007209 // If the class is abstract, we eagerly fetch the super class of the
7210 // object to avoid doing a comparison we know will fail.
7211 MipsLabel loop;
7212 __ Bind(&loop);
7213 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007214 GenerateReferenceLoadOneRegister(instruction,
7215 out_loc,
7216 super_offset,
7217 maybe_temp_loc,
7218 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007219 // If `out` is null, we use it for the result, and jump to `done`.
7220 __ Beqz(out, &done);
7221 __ Bne(out, cls, &loop);
7222 __ LoadConst32(out, 1);
7223 break;
7224 }
7225
7226 case TypeCheckKind::kClassHierarchyCheck: {
7227 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007228 GenerateReferenceLoadTwoRegisters(instruction,
7229 out_loc,
7230 obj_loc,
7231 class_offset,
7232 maybe_temp_loc,
7233 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007234 // Walk over the class hierarchy to find a match.
7235 MipsLabel loop, success;
7236 __ Bind(&loop);
7237 __ Beq(out, cls, &success);
7238 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007239 GenerateReferenceLoadOneRegister(instruction,
7240 out_loc,
7241 super_offset,
7242 maybe_temp_loc,
7243 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007244 __ Bnez(out, &loop);
7245 // If `out` is null, we use it for the result, and jump to `done`.
7246 __ B(&done);
7247 __ Bind(&success);
7248 __ LoadConst32(out, 1);
7249 break;
7250 }
7251
7252 case TypeCheckKind::kArrayObjectCheck: {
7253 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007254 GenerateReferenceLoadTwoRegisters(instruction,
7255 out_loc,
7256 obj_loc,
7257 class_offset,
7258 maybe_temp_loc,
7259 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007260 // Do an exact check.
7261 MipsLabel success;
7262 __ Beq(out, cls, &success);
7263 // Otherwise, we need to check that the object's class is a non-primitive array.
7264 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007265 GenerateReferenceLoadOneRegister(instruction,
7266 out_loc,
7267 component_offset,
7268 maybe_temp_loc,
7269 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007270 // If `out` is null, we use it for the result, and jump to `done`.
7271 __ Beqz(out, &done);
7272 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7273 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7274 __ Sltiu(out, out, 1);
7275 __ B(&done);
7276 __ Bind(&success);
7277 __ LoadConst32(out, 1);
7278 break;
7279 }
7280
7281 case TypeCheckKind::kArrayCheck: {
7282 // No read barrier since the slow path will retry upon failure.
7283 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007284 GenerateReferenceLoadTwoRegisters(instruction,
7285 out_loc,
7286 obj_loc,
7287 class_offset,
7288 maybe_temp_loc,
7289 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007290 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007291 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7292 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007293 codegen_->AddSlowPath(slow_path);
7294 __ Bne(out, cls, slow_path->GetEntryLabel());
7295 __ LoadConst32(out, 1);
7296 break;
7297 }
7298
7299 case TypeCheckKind::kUnresolvedCheck:
7300 case TypeCheckKind::kInterfaceCheck: {
7301 // Note that we indeed only call on slow path, but we always go
7302 // into the slow path for the unresolved and interface check
7303 // cases.
7304 //
7305 // We cannot directly call the InstanceofNonTrivial runtime
7306 // entry point without resorting to a type checking slow path
7307 // here (i.e. by calling InvokeRuntime directly), as it would
7308 // require to assign fixed registers for the inputs of this
7309 // HInstanceOf instruction (following the runtime calling
7310 // convention), which might be cluttered by the potential first
7311 // read barrier emission at the beginning of this method.
7312 //
7313 // TODO: Introduce a new runtime entry point taking the object
7314 // to test (instead of its class) as argument, and let it deal
7315 // with the read barrier issues. This will let us refactor this
7316 // case of the `switch` code as it was previously (with a direct
7317 // call to the runtime not using a type checking slow path).
7318 // This should also be beneficial for the other cases above.
7319 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007320 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7321 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007322 codegen_->AddSlowPath(slow_path);
7323 __ B(slow_path->GetEntryLabel());
7324 break;
7325 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007326 }
7327
7328 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007329
7330 if (slow_path != nullptr) {
7331 __ Bind(slow_path->GetExitLabel());
7332 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007333}
7334
7335void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007337 locations->SetOut(Location::ConstantLocation(constant));
7338}
7339
7340void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7341 // Will be generated at use site.
7342}
7343
7344void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007345 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007346 locations->SetOut(Location::ConstantLocation(constant));
7347}
7348
7349void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7350 // Will be generated at use site.
7351}
7352
7353void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7354 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7355 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7356}
7357
7358void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7359 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007360 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007361 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007362 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007363}
7364
7365void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7366 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7367 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007368 Location receiver = invoke->GetLocations()->InAt(0);
7369 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007370 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007371
7372 // Set the hidden argument.
7373 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7374 invoke->GetDexMethodIndex());
7375
7376 // temp = object->GetClass();
7377 if (receiver.IsStackSlot()) {
7378 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7379 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7380 } else {
7381 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7382 }
7383 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007384 // Instead of simply (possibly) unpoisoning `temp` here, we should
7385 // emit a read barrier for the previous class reference load.
7386 // However this is not required in practice, as this is an
7387 // intermediate/temporary reference and because the current
7388 // concurrent copying collector keeps the from-space memory
7389 // intact/accessible until the end of the marking phase (the
7390 // concurrent copying collector may not in the future).
7391 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007392 __ LoadFromOffset(kLoadWord, temp, temp,
7393 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7394 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007395 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007396 // temp = temp->GetImtEntryAt(method_offset);
7397 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7398 // T9 = temp->GetEntryPoint();
7399 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7400 // T9();
7401 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007402 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007403 DCHECK(!codegen_->IsLeafMethod());
7404 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7405}
7406
7407void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007408 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7409 if (intrinsic.TryDispatch(invoke)) {
7410 return;
7411 }
7412
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007413 HandleInvoke(invoke);
7414}
7415
7416void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007417 // Explicit clinit checks triggered by static invokes must have been pruned by
7418 // art::PrepareForRegisterAllocation.
7419 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007420
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007421 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007422 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7423 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007424
Chris Larsen701566a2015-10-27 15:29:13 -07007425 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7426 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007427 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7428 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7429 }
Chris Larsen701566a2015-10-27 15:29:13 -07007430 return;
7431 }
7432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007433 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007434
7435 // Add the extra input register if either the dex cache array base register
7436 // or the PC-relative base register for accessing literals is needed.
7437 if (has_extra_input) {
7438 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007440}
7441
Orion Hodsonac141392017-01-13 11:53:47 +00007442void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7443 HandleInvoke(invoke);
7444}
7445
7446void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7447 codegen_->GenerateInvokePolymorphicCall(invoke);
7448}
7449
Chris Larsen701566a2015-10-27 15:29:13 -07007450static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007451 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007452 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7453 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007454 return true;
7455 }
7456 return false;
7457}
7458
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007459HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007460 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007461 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007462 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007463 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007464 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007465 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007466 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007467 case HLoadString::LoadKind::kJitTableAddress:
7468 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007469 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007470 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007471 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007472 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007473 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007474 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007475}
7476
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007477HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7478 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007479 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007480 case HLoadClass::LoadKind::kInvalid:
7481 LOG(FATAL) << "UNREACHABLE";
7482 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007483 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007484 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007485 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007486 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007487 case HLoadClass::LoadKind::kBssEntry:
7488 DCHECK(!Runtime::Current()->UseJitCompilation());
7489 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007490 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007491 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007492 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007493 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007494 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007495 break;
7496 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007497 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007498}
7499
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007500Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7501 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007502 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007503 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007504 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7505 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7506 if (!invoke->GetLocations()->Intrinsified()) {
7507 return location.AsRegister<Register>();
7508 }
7509 // For intrinsics we allow any location, so it may be on the stack.
7510 if (!location.IsRegister()) {
7511 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7512 return temp;
7513 }
7514 // For register locations, check if the register was saved. If so, get it from the stack.
7515 // Note: There is a chance that the register was saved but not overwritten, so we could
7516 // save one load. However, since this is just an intrinsic slow path we prefer this
7517 // simple and more robust approach rather that trying to determine if that's the case.
7518 SlowPathCode* slow_path = GetCurrentSlowPath();
7519 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7520 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7521 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7522 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7523 return temp;
7524 }
7525 return location.AsRegister<Register>();
7526}
7527
Vladimir Markodc151b22015-10-15 18:02:30 +01007528HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7529 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007530 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007531 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007532}
7533
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007534void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7535 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007536 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007537 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007538 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7539 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007540 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007541 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7542 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007543 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7544 : ZERO;
7545
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007546 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007547 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007548 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007549 uint32_t offset =
7550 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007551 __ LoadFromOffset(kLoadWord,
7552 temp.AsRegister<Register>(),
7553 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007554 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007555 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007556 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007557 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007558 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007559 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007560 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7561 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007562 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7563 PcRelativePatchInfo* info_low =
7564 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007565 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007566 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7567 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007568 break;
7569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007570 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7571 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7572 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007573 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007574 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007575 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007576 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7577 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007578 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007579 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7580 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007581 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007582 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007583 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7584 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7585 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007586 }
7587 }
7588
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007589 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007590 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007591 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007592 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007593 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7594 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007595 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007596 T9,
7597 callee_method.AsRegister<Register>(),
7598 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007599 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007600 // T9()
7601 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007602 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007603 break;
7604 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007605 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7606
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007607 DCHECK(!IsLeafMethod());
7608}
7609
7610void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007611 // Explicit clinit checks triggered by static invokes must have been pruned by
7612 // art::PrepareForRegisterAllocation.
7613 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007614
7615 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7616 return;
7617 }
7618
7619 LocationSummary* locations = invoke->GetLocations();
7620 codegen_->GenerateStaticOrDirectCall(invoke,
7621 locations->HasTemps()
7622 ? locations->GetTemp(0)
7623 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007624}
7625
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007626void CodeGeneratorMIPS::GenerateVirtualCall(
7627 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007628 // Use the calling convention instead of the location of the receiver, as
7629 // intrinsics may have put the receiver in a different register. In the intrinsics
7630 // slow path, the arguments have been moved to the right place, so here we are
7631 // guaranteed that the receiver is the first register of the calling convention.
7632 InvokeDexCallingConvention calling_convention;
7633 Register receiver = calling_convention.GetRegisterAt(0);
7634
Chris Larsen3acee732015-11-18 13:31:08 -08007635 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007636 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7637 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7638 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007639 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640
7641 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007642 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007643 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007644 // Instead of simply (possibly) unpoisoning `temp` here, we should
7645 // emit a read barrier for the previous class reference load.
7646 // However this is not required in practice, as this is an
7647 // intermediate/temporary reference and because the current
7648 // concurrent copying collector keeps the from-space memory
7649 // intact/accessible until the end of the marking phase (the
7650 // concurrent copying collector may not in the future).
7651 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007652 // temp = temp->GetMethodAt(method_offset);
7653 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7654 // T9 = temp->GetEntryPoint();
7655 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7656 // T9();
7657 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007658 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007659 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007660}
7661
7662void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7663 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7664 return;
7665 }
7666
7667 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007669}
7670
7671void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007672 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007673 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007674 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007675 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7676 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007677 return;
7678 }
Vladimir Marko41559982017-01-06 14:04:23 +00007679 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007680 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007681 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007682 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7683 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007684 ? LocationSummary::kCallOnSlowPath
7685 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007686 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007687 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7688 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7689 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007690 switch (load_kind) {
7691 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007692 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007693 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007694 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007695 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007696 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007697 break;
7698 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007699 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007700 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7701 codegen_->ClobberRA();
7702 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007703 break;
7704 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007705 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007706 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007707 locations->SetInAt(0, Location::RequiresRegister());
7708 break;
7709 default:
7710 break;
7711 }
7712 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007713 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7714 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7715 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007716 // Request a temp to hold the BSS entry location for the slow path.
7717 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007718 RegisterSet caller_saves = RegisterSet::Empty();
7719 InvokeRuntimeCallingConvention calling_convention;
7720 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7721 locations->SetCustomSlowPathCallerSaves(caller_saves);
7722 } else {
7723 // For non-Baker read barriers we have a temp-clobbering call.
7724 }
7725 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007726}
7727
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007728// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7729// move.
7730void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007731 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007732 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007733 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007734 return;
7735 }
Vladimir Marko41559982017-01-06 14:04:23 +00007736 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007737
Vladimir Marko41559982017-01-06 14:04:23 +00007738 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007739 Location out_loc = locations->Out();
7740 Register out = out_loc.AsRegister<Register>();
7741 Register base_or_current_method_reg;
7742 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007743 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007744 switch (load_kind) {
7745 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007746 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007747 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007748 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007749 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007750 base_or_current_method_reg =
7751 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007752 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007753 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007754 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007755 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7756 break;
7757 default:
7758 base_or_current_method_reg = ZERO;
7759 break;
7760 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007761
Alexey Frunze15958152017-02-09 19:08:30 -08007762 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7763 ? kWithoutReadBarrier
7764 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007765 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007766 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007767 switch (load_kind) {
7768 case HLoadClass::LoadKind::kReferrersClass: {
7769 DCHECK(!cls->CanCallRuntime());
7770 DCHECK(!cls->MustGenerateClinitCheck());
7771 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7772 GenerateGcRootFieldLoad(cls,
7773 out_loc,
7774 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007775 ArtMethod::DeclaringClassOffset().Int32Value(),
7776 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007777 break;
7778 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007779 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007780 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007781 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007782 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007783 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007784 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7785 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007786 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7787 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007788 base_or_current_method_reg);
7789 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007790 break;
7791 }
7792 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007793 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007794 uint32_t address = dchecked_integral_cast<uint32_t>(
7795 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7796 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007797 if (isR6 || !has_irreducible_loops) {
7798 __ LoadLiteral(out,
7799 base_or_current_method_reg,
7800 codegen_->DeduplicateBootImageAddressLiteral(address));
7801 } else {
7802 __ LoadConst32(out, address);
7803 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007804 break;
7805 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007806 case HLoadClass::LoadKind::kBootImageClassTable: {
7807 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7808 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7809 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7810 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7811 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7812 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7813 out,
7814 base_or_current_method_reg);
7815 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7816 // Extract the reference from the slot data, i.e. clear the hash bits.
7817 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7818 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7819 if (masked_hash != 0) {
7820 __ Addiu(out, out, -masked_hash);
7821 }
7822 break;
7823 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007824 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007825 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7826 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7827 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007828 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007829 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007830 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7831 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007832 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007833 GenerateGcRootFieldLoad(cls,
7834 out_loc,
7835 temp,
7836 /* placeholder */ 0x5678,
7837 read_barrier_option,
7838 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007839 generate_null_check = true;
7840 break;
7841 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007842 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007843 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7844 cls->GetTypeIndex(),
7845 cls->GetClass());
7846 bool reordering = __ SetReorder(false);
7847 __ Bind(&info->high_label);
7848 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007849 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007850 GenerateGcRootFieldLoad(cls,
7851 out_loc,
7852 out,
7853 /* placeholder */ 0x5678,
7854 read_barrier_option,
7855 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007856 break;
7857 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007858 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007859 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007860 LOG(FATAL) << "UNREACHABLE";
7861 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007862 }
7863
7864 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7865 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007866 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007867 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007868 codegen_->AddSlowPath(slow_path);
7869 if (generate_null_check) {
7870 __ Beqz(out, slow_path->GetEntryLabel());
7871 }
7872 if (cls->MustGenerateClinitCheck()) {
7873 GenerateClassInitializationCheck(slow_path, out);
7874 } else {
7875 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007876 }
7877 }
7878}
7879
7880static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007881 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007882}
7883
7884void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7885 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007886 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007887 locations->SetOut(Location::RequiresRegister());
7888}
7889
7890void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7891 Register out = load->GetLocations()->Out().AsRegister<Register>();
7892 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7893}
7894
7895void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007896 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007897}
7898
7899void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7900 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7901}
7902
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007903void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007904 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007905 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007906 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007907 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007908 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007909 switch (load_kind) {
7910 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007911 case HLoadString::LoadKind::kBootImageAddress:
7912 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007913 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007914 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007915 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007916 break;
7917 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007918 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007919 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7920 codegen_->ClobberRA();
7921 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007922 break;
7923 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007924 FALLTHROUGH_INTENDED;
7925 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007926 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007927 locations->SetInAt(0, Location::RequiresRegister());
7928 break;
7929 default:
7930 break;
7931 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007932 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007933 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007934 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007935 } else {
7936 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007937 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7938 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7939 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007940 // Request a temp to hold the BSS entry location for the slow path.
7941 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007942 RegisterSet caller_saves = RegisterSet::Empty();
7943 InvokeRuntimeCallingConvention calling_convention;
7944 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7945 locations->SetCustomSlowPathCallerSaves(caller_saves);
7946 } else {
7947 // For non-Baker read barriers we have a temp-clobbering call.
7948 }
7949 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007950 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007951}
7952
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007953// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7954// move.
7955void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007956 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007957 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007958 Location out_loc = locations->Out();
7959 Register out = out_loc.AsRegister<Register>();
7960 Register base_or_current_method_reg;
7961 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007962 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007963 switch (load_kind) {
7964 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007965 case HLoadString::LoadKind::kBootImageAddress:
7966 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007967 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007968 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007969 base_or_current_method_reg =
7970 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007971 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007972 default:
7973 base_or_current_method_reg = ZERO;
7974 break;
7975 }
7976
7977 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007978 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007979 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007980 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007981 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007982 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7983 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007984 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7985 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007986 base_or_current_method_reg);
7987 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007988 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007989 }
7990 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007991 uint32_t address = dchecked_integral_cast<uint32_t>(
7992 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7993 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007994 if (isR6 || !has_irreducible_loops) {
7995 __ LoadLiteral(out,
7996 base_or_current_method_reg,
7997 codegen_->DeduplicateBootImageAddressLiteral(address));
7998 } else {
7999 __ LoadConst32(out, address);
8000 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008001 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008002 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008003 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008004 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008005 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008006 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008007 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8008 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008009 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8010 out,
8011 base_or_current_method_reg);
8012 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8013 return;
8014 }
8015 case HLoadString::LoadKind::kBssEntry: {
8016 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8017 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8018 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8019 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8020 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008021 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008022 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008023 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8024 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008025 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008026 GenerateGcRootFieldLoad(load,
8027 out_loc,
8028 temp,
8029 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008030 kCompilerReadBarrierOption,
8031 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008032 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008033 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008034 codegen_->AddSlowPath(slow_path);
8035 __ Beqz(out, slow_path->GetEntryLabel());
8036 __ Bind(slow_path->GetExitLabel());
8037 return;
8038 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008039 case HLoadString::LoadKind::kJitTableAddress: {
8040 CodeGeneratorMIPS::JitPatchInfo* info =
8041 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8042 load->GetStringIndex(),
8043 load->GetString());
8044 bool reordering = __ SetReorder(false);
8045 __ Bind(&info->high_label);
8046 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008047 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008048 GenerateGcRootFieldLoad(load,
8049 out_loc,
8050 out,
8051 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008052 kCompilerReadBarrierOption,
8053 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008054 return;
8055 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008056 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008057 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008058 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008059
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008060 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008061 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008062 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008063 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008064 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008065 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8066 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008067}
8068
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008069void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008070 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008071 locations->SetOut(Location::ConstantLocation(constant));
8072}
8073
8074void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8075 // Will be generated at use site.
8076}
8077
8078void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008079 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8080 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008081 InvokeRuntimeCallingConvention calling_convention;
8082 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8083}
8084
8085void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8086 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008087 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008088 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8089 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008090 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008091 }
8092 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8093}
8094
8095void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8096 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008097 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008098 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008099 case DataType::Type::kInt32:
8100 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008101 locations->SetInAt(0, Location::RequiresRegister());
8102 locations->SetInAt(1, Location::RequiresRegister());
8103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8104 break;
8105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008106 case DataType::Type::kFloat32:
8107 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008108 locations->SetInAt(0, Location::RequiresFpuRegister());
8109 locations->SetInAt(1, Location::RequiresFpuRegister());
8110 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8111 break;
8112
8113 default:
8114 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8115 }
8116}
8117
8118void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008119 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008120 LocationSummary* locations = instruction->GetLocations();
8121 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8122
8123 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008124 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008125 Register dst = locations->Out().AsRegister<Register>();
8126 Register lhs = locations->InAt(0).AsRegister<Register>();
8127 Register rhs = locations->InAt(1).AsRegister<Register>();
8128
8129 if (isR6) {
8130 __ MulR6(dst, lhs, rhs);
8131 } else {
8132 __ MulR2(dst, lhs, rhs);
8133 }
8134 break;
8135 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008136 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008137 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8138 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8139 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8140 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8141 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8142 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8143
8144 // Extra checks to protect caused by the existance of A1_A2.
8145 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8146 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8147 DCHECK_NE(dst_high, lhs_low);
8148 DCHECK_NE(dst_high, rhs_low);
8149
8150 // A_B * C_D
8151 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8152 // dst_lo: [ low(B*D) ]
8153 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8154
8155 if (isR6) {
8156 __ MulR6(TMP, lhs_high, rhs_low);
8157 __ MulR6(dst_high, lhs_low, rhs_high);
8158 __ Addu(dst_high, dst_high, TMP);
8159 __ MuhuR6(TMP, lhs_low, rhs_low);
8160 __ Addu(dst_high, dst_high, TMP);
8161 __ MulR6(dst_low, lhs_low, rhs_low);
8162 } else {
8163 __ MulR2(TMP, lhs_high, rhs_low);
8164 __ MulR2(dst_high, lhs_low, rhs_high);
8165 __ Addu(dst_high, dst_high, TMP);
8166 __ MultuR2(lhs_low, rhs_low);
8167 __ Mfhi(TMP);
8168 __ Addu(dst_high, dst_high, TMP);
8169 __ Mflo(dst_low);
8170 }
8171 break;
8172 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008173 case DataType::Type::kFloat32:
8174 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008175 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8176 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8177 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008178 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008179 __ MulS(dst, lhs, rhs);
8180 } else {
8181 __ MulD(dst, lhs, rhs);
8182 }
8183 break;
8184 }
8185 default:
8186 LOG(FATAL) << "Unexpected mul type " << type;
8187 }
8188}
8189
8190void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8191 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008192 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008193 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008194 case DataType::Type::kInt32:
8195 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008196 locations->SetInAt(0, Location::RequiresRegister());
8197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8198 break;
8199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008200 case DataType::Type::kFloat32:
8201 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008202 locations->SetInAt(0, Location::RequiresFpuRegister());
8203 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8204 break;
8205
8206 default:
8207 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8208 }
8209}
8210
8211void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008212 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008213 LocationSummary* locations = instruction->GetLocations();
8214
8215 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008216 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008217 Register dst = locations->Out().AsRegister<Register>();
8218 Register src = locations->InAt(0).AsRegister<Register>();
8219 __ Subu(dst, ZERO, src);
8220 break;
8221 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008222 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008223 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8224 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8225 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8226 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8227 __ Subu(dst_low, ZERO, src_low);
8228 __ Sltu(TMP, ZERO, dst_low);
8229 __ Subu(dst_high, ZERO, src_high);
8230 __ Subu(dst_high, dst_high, TMP);
8231 break;
8232 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008233 case DataType::Type::kFloat32:
8234 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8236 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008237 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008238 __ NegS(dst, src);
8239 } else {
8240 __ NegD(dst, src);
8241 }
8242 break;
8243 }
8244 default:
8245 LOG(FATAL) << "Unexpected neg type " << type;
8246 }
8247}
8248
8249void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008250 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8251 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008252 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008253 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008254 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8255 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008256}
8257
8258void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008259 // Note: if heap poisoning is enabled, the entry point takes care
8260 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008261 QuickEntrypointEnum entrypoint =
8262 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8263 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008264 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008265 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008266}
8267
8268void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008269 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8270 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008271 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008272 if (instruction->IsStringAlloc()) {
8273 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8274 } else {
8275 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008276 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008277 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008278}
8279
8280void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008281 // Note: if heap poisoning is enabled, the entry point takes care
8282 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008283 if (instruction->IsStringAlloc()) {
8284 // String is allocated through StringFactory. Call NewEmptyString entry point.
8285 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008286 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008287 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8288 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8289 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008290 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008291 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8292 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008293 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008294 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008295 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008296}
8297
8298void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008299 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008300 locations->SetInAt(0, Location::RequiresRegister());
8301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8302}
8303
8304void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008305 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008306 LocationSummary* locations = instruction->GetLocations();
8307
8308 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008309 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008310 Register dst = locations->Out().AsRegister<Register>();
8311 Register src = locations->InAt(0).AsRegister<Register>();
8312 __ Nor(dst, src, ZERO);
8313 break;
8314 }
8315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008316 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008317 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8318 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8319 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8320 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8321 __ Nor(dst_high, src_high, ZERO);
8322 __ Nor(dst_low, src_low, ZERO);
8323 break;
8324 }
8325
8326 default:
8327 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8328 }
8329}
8330
8331void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008332 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008333 locations->SetInAt(0, Location::RequiresRegister());
8334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8335}
8336
8337void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8338 LocationSummary* locations = instruction->GetLocations();
8339 __ Xori(locations->Out().AsRegister<Register>(),
8340 locations->InAt(0).AsRegister<Register>(),
8341 1);
8342}
8343
8344void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008345 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8346 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008347}
8348
Calin Juravle2ae48182016-03-16 14:05:09 +00008349void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8350 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008351 return;
8352 }
8353 Location obj = instruction->GetLocations()->InAt(0);
8354
8355 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008356 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008357}
8358
Calin Juravle2ae48182016-03-16 14:05:09 +00008359void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008360 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008361 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008362
8363 Location obj = instruction->GetLocations()->InAt(0);
8364
8365 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8366}
8367
8368void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008369 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008370}
8371
8372void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8373 HandleBinaryOp(instruction);
8374}
8375
8376void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8377 HandleBinaryOp(instruction);
8378}
8379
8380void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8381 LOG(FATAL) << "Unreachable";
8382}
8383
8384void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008385 if (instruction->GetNext()->IsSuspendCheck() &&
8386 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8387 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8388 // The back edge will generate the suspend check.
8389 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8390 }
8391
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008392 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8393}
8394
8395void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008396 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008397 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8398 if (location.IsStackSlot()) {
8399 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8400 } else if (location.IsDoubleStackSlot()) {
8401 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8402 }
8403 locations->SetOut(location);
8404}
8405
8406void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8407 ATTRIBUTE_UNUSED) {
8408 // Nothing to do, the parameter is already at its location.
8409}
8410
8411void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8412 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008413 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008414 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8415}
8416
8417void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8418 ATTRIBUTE_UNUSED) {
8419 // Nothing to do, the method is already at its location.
8420}
8421
8422void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008423 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008424 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008425 locations->SetInAt(i, Location::Any());
8426 }
8427 locations->SetOut(Location::Any());
8428}
8429
8430void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8431 LOG(FATAL) << "Unreachable";
8432}
8433
8434void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008435 DataType::Type type = rem->GetResultType();
8436 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8437 ? LocationSummary::kNoCall
8438 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008439 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008440
8441 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008442 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008443 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008444 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008445 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8446 break;
8447
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008448 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008449 InvokeRuntimeCallingConvention calling_convention;
8450 locations->SetInAt(0, Location::RegisterPairLocation(
8451 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8452 locations->SetInAt(1, Location::RegisterPairLocation(
8453 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8454 locations->SetOut(calling_convention.GetReturnLocation(type));
8455 break;
8456 }
8457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008458 case DataType::Type::kFloat32:
8459 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460 InvokeRuntimeCallingConvention calling_convention;
8461 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8462 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8463 locations->SetOut(calling_convention.GetReturnLocation(type));
8464 break;
8465 }
8466
8467 default:
8468 LOG(FATAL) << "Unexpected rem type " << type;
8469 }
8470}
8471
8472void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008473 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474
8475 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008476 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008477 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008478 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008479 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008480 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008481 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8482 break;
8483 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008484 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008485 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008486 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008487 break;
8488 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008489 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008490 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008491 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008492 break;
8493 }
8494 default:
8495 LOG(FATAL) << "Unexpected rem type " << type;
8496 }
8497}
8498
Igor Murashkind01745e2017-04-05 16:40:31 -07008499void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8500 constructor_fence->SetLocations(nullptr);
8501}
8502
8503void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8504 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8505 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8506}
8507
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8509 memory_barrier->SetLocations(nullptr);
8510}
8511
8512void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8513 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8514}
8515
8516void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008517 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008518 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008519 locations->SetInAt(0, MipsReturnLocation(return_type));
8520}
8521
8522void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8523 codegen_->GenerateFrameExit();
8524}
8525
8526void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8527 ret->SetLocations(nullptr);
8528}
8529
8530void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8531 codegen_->GenerateFrameExit();
8532}
8533
Alexey Frunze92d90602015-12-18 18:16:36 -08008534void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8535 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008536}
8537
Alexey Frunze92d90602015-12-18 18:16:36 -08008538void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8539 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008540}
8541
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8543 HandleShift(shl);
8544}
8545
8546void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8547 HandleShift(shl);
8548}
8549
8550void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8551 HandleShift(shr);
8552}
8553
8554void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8555 HandleShift(shr);
8556}
8557
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008558void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8559 HandleBinaryOp(instruction);
8560}
8561
8562void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8563 HandleBinaryOp(instruction);
8564}
8565
8566void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8567 HandleFieldGet(instruction, instruction->GetFieldInfo());
8568}
8569
8570void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8571 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8572}
8573
8574void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8575 HandleFieldSet(instruction, instruction->GetFieldInfo());
8576}
8577
8578void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008579 HandleFieldSet(instruction,
8580 instruction->GetFieldInfo(),
8581 instruction->GetDexPc(),
8582 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008583}
8584
8585void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8586 HUnresolvedInstanceFieldGet* instruction) {
8587 FieldAccessCallingConventionMIPS calling_convention;
8588 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8589 instruction->GetFieldType(),
8590 calling_convention);
8591}
8592
8593void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8594 HUnresolvedInstanceFieldGet* instruction) {
8595 FieldAccessCallingConventionMIPS calling_convention;
8596 codegen_->GenerateUnresolvedFieldAccess(instruction,
8597 instruction->GetFieldType(),
8598 instruction->GetFieldIndex(),
8599 instruction->GetDexPc(),
8600 calling_convention);
8601}
8602
8603void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8604 HUnresolvedInstanceFieldSet* instruction) {
8605 FieldAccessCallingConventionMIPS calling_convention;
8606 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8607 instruction->GetFieldType(),
8608 calling_convention);
8609}
8610
8611void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8612 HUnresolvedInstanceFieldSet* instruction) {
8613 FieldAccessCallingConventionMIPS calling_convention;
8614 codegen_->GenerateUnresolvedFieldAccess(instruction,
8615 instruction->GetFieldType(),
8616 instruction->GetFieldIndex(),
8617 instruction->GetDexPc(),
8618 calling_convention);
8619}
8620
8621void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8622 HUnresolvedStaticFieldGet* instruction) {
8623 FieldAccessCallingConventionMIPS calling_convention;
8624 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8625 instruction->GetFieldType(),
8626 calling_convention);
8627}
8628
8629void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8630 HUnresolvedStaticFieldGet* instruction) {
8631 FieldAccessCallingConventionMIPS calling_convention;
8632 codegen_->GenerateUnresolvedFieldAccess(instruction,
8633 instruction->GetFieldType(),
8634 instruction->GetFieldIndex(),
8635 instruction->GetDexPc(),
8636 calling_convention);
8637}
8638
8639void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8640 HUnresolvedStaticFieldSet* instruction) {
8641 FieldAccessCallingConventionMIPS calling_convention;
8642 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8643 instruction->GetFieldType(),
8644 calling_convention);
8645}
8646
8647void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8648 HUnresolvedStaticFieldSet* instruction) {
8649 FieldAccessCallingConventionMIPS calling_convention;
8650 codegen_->GenerateUnresolvedFieldAccess(instruction,
8651 instruction->GetFieldType(),
8652 instruction->GetFieldIndex(),
8653 instruction->GetDexPc(),
8654 calling_convention);
8655}
8656
8657void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008658 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8659 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008660 // In suspend check slow path, usually there are no caller-save registers at all.
8661 // If SIMD instructions are present, however, we force spilling all live SIMD
8662 // registers in full width (since the runtime only saves/restores lower part).
8663 locations->SetCustomSlowPathCallerSaves(
8664 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008665}
8666
8667void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8668 HBasicBlock* block = instruction->GetBlock();
8669 if (block->GetLoopInformation() != nullptr) {
8670 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8671 // The back edge will generate the suspend check.
8672 return;
8673 }
8674 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8675 // The goto will generate the suspend check.
8676 return;
8677 }
8678 GenerateSuspendCheck(instruction, nullptr);
8679}
8680
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008681void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008682 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8683 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008684 InvokeRuntimeCallingConvention calling_convention;
8685 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8686}
8687
8688void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008689 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008690 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8691}
8692
8693void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008694 DataType::Type input_type = conversion->GetInputType();
8695 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008696 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8697 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008698 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008700 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8701 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008702 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8703 }
8704
8705 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008706 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008707 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8708 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008709 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710 }
8711
Vladimir Markoca6fff82017-10-03 14:49:14 +01008712 LocationSummary* locations =
8713 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714
8715 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008716 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008717 locations->SetInAt(0, Location::RequiresFpuRegister());
8718 } else {
8719 locations->SetInAt(0, Location::RequiresRegister());
8720 }
8721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008722 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008723 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8724 } else {
8725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8726 }
8727 } else {
8728 InvokeRuntimeCallingConvention calling_convention;
8729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008730 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008731 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8732 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008733 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008734 locations->SetInAt(0, Location::RegisterPairLocation(
8735 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8736 }
8737
8738 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8739 }
8740}
8741
8742void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8743 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008744 DataType::Type result_type = conversion->GetResultType();
8745 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008747 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008748
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008749 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8750 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008752 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008753 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8754 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8755 Register src = locations->InAt(0).AsRegister<Register>();
8756
Alexey Frunzea871ef12016-06-27 15:20:11 -07008757 if (dst_low != src) {
8758 __ Move(dst_low, src);
8759 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008760 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008761 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008762 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008763 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008764 ? locations->InAt(0).AsRegisterPairLow<Register>()
8765 : locations->InAt(0).AsRegister<Register>();
8766
8767 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008768 case DataType::Type::kUint8:
8769 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008771 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008772 if (has_sign_extension) {
8773 __ Seb(dst, src);
8774 } else {
8775 __ Sll(dst, src, 24);
8776 __ Sra(dst, dst, 24);
8777 }
8778 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008779 case DataType::Type::kUint16:
8780 __ Andi(dst, src, 0xFFFF);
8781 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008782 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008783 if (has_sign_extension) {
8784 __ Seh(dst, src);
8785 } else {
8786 __ Sll(dst, src, 16);
8787 __ Sra(dst, dst, 16);
8788 }
8789 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008790 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008791 if (dst != src) {
8792 __ Move(dst, src);
8793 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 break;
8795
8796 default:
8797 LOG(FATAL) << "Unexpected type conversion from " << input_type
8798 << " to " << result_type;
8799 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008800 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8801 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008802 if (isR6) {
8803 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8804 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8805 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8806 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8807 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8808 __ Mtc1(src_low, FTMP);
8809 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008810 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008811 __ Cvtsl(dst, FTMP);
8812 } else {
8813 __ Cvtdl(dst, FTMP);
8814 }
8815 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008816 QuickEntrypointEnum entrypoint =
8817 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008818 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008819 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008820 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8821 } else {
8822 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8823 }
8824 }
8825 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008826 Register src = locations->InAt(0).AsRegister<Register>();
8827 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8828 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008829 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008830 __ Cvtsw(dst, FTMP);
8831 } else {
8832 __ Cvtdw(dst, FTMP);
8833 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008834 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008835 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8836 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008837
8838 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8839 // value of the output type if the input is outside of the range after the truncation or
8840 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8841 // results. This matches the desired float/double-to-int/long conversion exactly.
8842 //
8843 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8844 // value when the input is either a NaN or is outside of the range of the output type
8845 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8846 // the same result.
8847 //
8848 // The code takes care of the different behaviors by first comparing the input to the
8849 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8850 // If the input is greater than or equal to the minimum, it procedes to the truncate
8851 // instruction, which will handle such an input the same way irrespective of NAN2008.
8852 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8853 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008854 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008855 if (isR6) {
8856 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8857 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8858 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8859 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8860 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008862 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008863 __ TruncLS(FTMP, src);
8864 } else {
8865 __ TruncLD(FTMP, src);
8866 }
8867 __ Mfc1(dst_low, FTMP);
8868 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008869 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008870 QuickEntrypointEnum entrypoint =
8871 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008872 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008873 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008874 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8875 } else {
8876 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8877 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008878 }
8879 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008880 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8881 Register dst = locations->Out().AsRegister<Register>();
8882 MipsLabel truncate;
8883 MipsLabel done;
8884
Lena Djokicf4e23a82017-05-09 15:43:45 +02008885 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008886 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008887 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8888 __ LoadConst32(TMP, min_val);
8889 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008890 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008891 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8892 __ LoadConst32(TMP, High32Bits(min_val));
8893 __ Mtc1(ZERO, FTMP);
8894 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008895 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008897 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008898 __ ColeS(0, FTMP, src);
8899 } else {
8900 __ ColeD(0, FTMP, src);
8901 }
8902 __ Bc1t(0, &truncate);
8903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008904 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008905 __ CeqS(0, src, src);
8906 } else {
8907 __ CeqD(0, src, src);
8908 }
8909 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8910 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008911
8912 __ B(&done);
8913
8914 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008915 }
8916
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008917 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008918 __ TruncWS(FTMP, src);
8919 } else {
8920 __ TruncWD(FTMP, src);
8921 }
8922 __ Mfc1(dst, FTMP);
8923
Lena Djokicf4e23a82017-05-09 15:43:45 +02008924 if (!isR6) {
8925 __ Bind(&done);
8926 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008927 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008928 } else if (DataType::IsFloatingPointType(result_type) &&
8929 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008930 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8931 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008932 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008933 __ Cvtsd(dst, src);
8934 } else {
8935 __ Cvtds(dst, src);
8936 }
8937 } else {
8938 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8939 << " to " << result_type;
8940 }
8941}
8942
8943void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8944 HandleShift(ushr);
8945}
8946
8947void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8948 HandleShift(ushr);
8949}
8950
8951void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8952 HandleBinaryOp(instruction);
8953}
8954
8955void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8956 HandleBinaryOp(instruction);
8957}
8958
8959void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8960 // Nothing to do, this should be removed during prepare for register allocator.
8961 LOG(FATAL) << "Unreachable";
8962}
8963
8964void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8965 // Nothing to do, this should be removed during prepare for register allocator.
8966 LOG(FATAL) << "Unreachable";
8967}
8968
8969void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008970 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008971}
8972
8973void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008974 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008975}
8976
8977void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008978 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008979}
8980
8981void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008982 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008983}
8984
8985void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008986 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008987}
8988
8989void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008990 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008991}
8992
8993void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008994 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008995}
8996
8997void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008998 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008999}
9000
9001void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009002 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009003}
9004
9005void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009006 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009007}
9008
9009void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009010 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009011}
9012
9013void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009014 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009015}
9016
9017void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009018 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009019}
9020
9021void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009022 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009023}
9024
9025void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009026 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009027}
9028
9029void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009030 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009031}
9032
9033void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009034 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009035}
9036
9037void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009038 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009039}
9040
9041void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009042 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009043}
9044
9045void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009046 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009047}
9048
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009049void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9050 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009051 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009052 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009053 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9054 uint32_t num_entries = switch_instr->GetNumEntries();
9055 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9056 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9057 // instruction to simulate PC-relative addressing when accessing the jump table.
9058 // NAL clobbers RA. Make sure RA is preserved.
9059 codegen_->ClobberRA();
9060 }
9061 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009062}
9063
Alexey Frunze96b66822016-09-10 02:32:44 -07009064void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9065 int32_t lower_bound,
9066 uint32_t num_entries,
9067 HBasicBlock* switch_block,
9068 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009069 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009070 Register temp_reg = TMP;
9071 __ Addiu32(temp_reg, value_reg, -lower_bound);
9072 // Jump to default if index is negative
9073 // Note: We don't check the case that index is positive while value < lower_bound, because in
9074 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9075 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9076
Alexey Frunze96b66822016-09-10 02:32:44 -07009077 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009078 // Jump to successors[0] if value == lower_bound.
9079 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9080 int32_t last_index = 0;
9081 for (; num_entries - last_index > 2; last_index += 2) {
9082 __ Addiu(temp_reg, temp_reg, -2);
9083 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9084 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9085 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9086 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9087 }
9088 if (num_entries - last_index == 2) {
9089 // The last missing case_value.
9090 __ Addiu(temp_reg, temp_reg, -1);
9091 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009092 }
9093
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009094 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009095 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009096 __ B(codegen_->GetLabelOf(default_block));
9097 }
9098}
9099
Alexey Frunze96b66822016-09-10 02:32:44 -07009100void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9101 Register constant_area,
9102 int32_t lower_bound,
9103 uint32_t num_entries,
9104 HBasicBlock* switch_block,
9105 HBasicBlock* default_block) {
9106 // Create a jump table.
9107 std::vector<MipsLabel*> labels(num_entries);
9108 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9109 for (uint32_t i = 0; i < num_entries; i++) {
9110 labels[i] = codegen_->GetLabelOf(successors[i]);
9111 }
9112 JumpTable* table = __ CreateJumpTable(std::move(labels));
9113
9114 // Is the value in range?
9115 __ Addiu32(TMP, value_reg, -lower_bound);
9116 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9117 __ Sltiu(AT, TMP, num_entries);
9118 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9119 } else {
9120 __ LoadConst32(AT, num_entries);
9121 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9122 }
9123
9124 // We are in the range of the table.
9125 // Load the target address from the jump table, indexing by the value.
9126 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009127 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009128 __ Lw(TMP, TMP, 0);
9129 // Compute the absolute target address by adding the table start address
9130 // (the table contains offsets to targets relative to its start).
9131 __ Addu(TMP, TMP, AT);
9132 // And jump.
9133 __ Jr(TMP);
9134 __ NopIfNoReordering();
9135}
9136
9137void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9138 int32_t lower_bound = switch_instr->GetStartValue();
9139 uint32_t num_entries = switch_instr->GetNumEntries();
9140 LocationSummary* locations = switch_instr->GetLocations();
9141 Register value_reg = locations->InAt(0).AsRegister<Register>();
9142 HBasicBlock* switch_block = switch_instr->GetBlock();
9143 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9144
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009145 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009146 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009147 //
9148 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9149 // to access the jump table and it is implemented by changing HPackedSwitch to
9150 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9151 // VisitMipsPackedSwitch()).
9152 //
9153 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9154 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9155 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009156 GenTableBasedPackedSwitch(value_reg,
9157 ZERO,
9158 lower_bound,
9159 num_entries,
9160 switch_block,
9161 default_block);
9162 } else {
9163 GenPackedSwitchWithCompares(value_reg,
9164 lower_bound,
9165 num_entries,
9166 switch_block,
9167 default_block);
9168 }
9169}
9170
9171void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9172 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009173 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009174 locations->SetInAt(0, Location::RequiresRegister());
9175 // Constant area pointer (HMipsComputeBaseMethodAddress).
9176 locations->SetInAt(1, Location::RequiresRegister());
9177}
9178
9179void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9180 int32_t lower_bound = switch_instr->GetStartValue();
9181 uint32_t num_entries = switch_instr->GetNumEntries();
9182 LocationSummary* locations = switch_instr->GetLocations();
9183 Register value_reg = locations->InAt(0).AsRegister<Register>();
9184 Register constant_area = locations->InAt(1).AsRegister<Register>();
9185 HBasicBlock* switch_block = switch_instr->GetBlock();
9186 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9187
9188 // This is an R2-only path. HPackedSwitch has been changed to
9189 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9190 // required to address the jump table relative to PC.
9191 GenTableBasedPackedSwitch(value_reg,
9192 constant_area,
9193 lower_bound,
9194 num_entries,
9195 switch_block,
9196 default_block);
9197}
9198
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009199void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9200 HMipsComputeBaseMethodAddress* insn) {
9201 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009202 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009203 locations->SetOut(Location::RequiresRegister());
9204}
9205
9206void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9207 HMipsComputeBaseMethodAddress* insn) {
9208 LocationSummary* locations = insn->GetLocations();
9209 Register reg = locations->Out().AsRegister<Register>();
9210
9211 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9212
9213 // Generate a dummy PC-relative call to obtain PC.
9214 __ Nal();
9215 // Grab the return address off RA.
9216 __ Move(reg, RA);
9217
9218 // Remember this offset (the obtained PC value) for later use with constant area.
9219 __ BindPcRelBaseLabel();
9220}
9221
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009222void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9223 // The trampoline uses the same calling convention as dex calling conventions,
9224 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9225 // the method_idx.
9226 HandleInvoke(invoke);
9227}
9228
9229void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9230 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9231}
9232
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009233void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9234 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009235 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009236 locations->SetInAt(0, Location::RequiresRegister());
9237 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009238}
9239
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009240void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9241 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009242 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009243 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009244 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009245 __ LoadFromOffset(kLoadWord,
9246 locations->Out().AsRegister<Register>(),
9247 locations->InAt(0).AsRegister<Register>(),
9248 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009249 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009250 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009251 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009252 __ LoadFromOffset(kLoadWord,
9253 locations->Out().AsRegister<Register>(),
9254 locations->InAt(0).AsRegister<Register>(),
9255 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009256 __ LoadFromOffset(kLoadWord,
9257 locations->Out().AsRegister<Register>(),
9258 locations->Out().AsRegister<Register>(),
9259 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009260 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009261}
9262
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009263void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9264 ATTRIBUTE_UNUSED) {
9265 LOG(FATAL) << "Unreachable";
9266}
9267
9268void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9269 ATTRIBUTE_UNUSED) {
9270 LOG(FATAL) << "Unreachable";
9271}
9272
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009273#undef __
9274#undef QUICK_ENTRY_POINT
9275
9276} // namespace mips
9277} // namespace art