blob: 51fb4dacfd6199d03d077b5348d8da5fa83e5a97 [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,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000223 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700224 : SlowPathCodeMIPS(at),
225 cls_(cls),
226 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000232 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700233 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 InvokeRuntimeCallingConvention calling_convention;
236 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200237 __ Bind(GetEntryLabel());
238 SaveLiveRegisters(codegen, locations);
239
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 dex::TypeIndex type_index = cls_->GetTypeIndex();
241 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100242 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
243 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200245 if (do_clinit_) {
246 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
247 } else {
248 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
249 }
250
251 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200252 if (out.IsValid()) {
253 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100254 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700255 mips_codegen->MoveLocation(out,
256 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
257 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200270 // The dex PC of `at_`.
271 const uint32_t dex_pc_;
272
273 // Whether to initialize the class.
274 const bool do_clinit_;
275
276 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
277};
278
279class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
280 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000281 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
282 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283
284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700285 DCHECK(instruction_->IsLoadString());
286 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287 LocationSummary* locations = instruction_->GetLocations();
288 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000289 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100296 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200297 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100299 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200300 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700301 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000304
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200305 __ B(GetExitLabel());
306 }
307
308 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
309
310 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200311 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
312};
313
314class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
315 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000316 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
319 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
320 __ Bind(GetEntryLabel());
321 if (instruction_->CanThrowIntoCatchBlock()) {
322 // Live registers will be restored in the catch block if caught.
323 SaveLiveRegisters(codegen, instruction_->GetLocations());
324 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100325 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200326 instruction_,
327 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100328 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
330 }
331
332 bool IsFatal() const OVERRIDE { return true; }
333
334 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
335
336 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
338};
339
340class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
341 public:
342 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000343 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344
345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200346 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200349 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100350 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200352 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200353 if (successor_ == nullptr) {
354 __ B(GetReturnLabel());
355 } else {
356 __ B(mips_codegen->GetLabelOf(successor_));
357 }
358 }
359
360 MipsLabel* GetReturnLabel() {
361 DCHECK(successor_ == nullptr);
362 return &return_label_;
363 }
364
365 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
366
Chris Larsena2045912017-11-02 12:39:54 -0700367 HBasicBlock* GetSuccessor() const {
368 return successor_;
369 }
370
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200371 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200372 // If not null, the block to branch to after the suspend check.
373 HBasicBlock* const successor_;
374
375 // If `successor_` is null, the label to branch to after the suspend check.
376 MipsLabel return_label_;
377
378 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
379};
380
381class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
382 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
384 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200388 uint32_t dex_pc = instruction_->GetDexPc();
389 DCHECK(instruction_->IsCheckCast()
390 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
391 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
392
393 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800394 if (!is_fatal_) {
395 SaveLiveRegisters(codegen, locations);
396 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397
398 // We're moving two locations to locations that could overlap, so we need a parallel
399 // move resolver.
400 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800401 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100403 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800404 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100406 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100408 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
412 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 } else {
414 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800415 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
416 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 }
418
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800419 if (!is_fatal_) {
420 RestoreLiveRegisters(codegen, locations);
421 __ B(GetExitLabel());
422 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 }
424
425 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
426
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800427 bool IsFatal() const OVERRIDE { return is_fatal_; }
428
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200429 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800430 const bool is_fatal_;
431
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200432 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
433};
434
435class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
436 public:
Aart Bik42249c32016-01-07 15:33:50 -0800437 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800441 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100443 LocationSummary* locations = instruction_->GetLocations();
444 SaveLiveRegisters(codegen, locations);
445 InvokeRuntimeCallingConvention calling_convention;
446 __ LoadConst32(calling_convention.GetRegisterAt(0),
447 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100448 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100449 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 }
451
452 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
453
454 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
456};
457
Alexey Frunze15958152017-02-09 19:08:30 -0800458class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
459 public:
460 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
461
462 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
463 LocationSummary* locations = instruction_->GetLocations();
464 __ Bind(GetEntryLabel());
465 SaveLiveRegisters(codegen, locations);
466
467 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100468 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800469 parallel_move.AddMove(
470 locations->InAt(0),
471 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100472 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800473 nullptr);
474 parallel_move.AddMove(
475 locations->InAt(1),
476 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100477 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800478 nullptr);
479 parallel_move.AddMove(
480 locations->InAt(2),
481 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800483 nullptr);
484 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
485
486 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
487 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
488 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
489 RestoreLiveRegisters(codegen, locations);
490 __ B(GetExitLabel());
491 }
492
493 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
494
495 private:
496 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
497};
498
499// Slow path marking an object reference `ref` during a read
500// barrier. The field `obj.field` in the object `obj` holding this
501// reference does not get updated by this slow path after marking (see
502// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
503//
504// This means that after the execution of this slow path, `ref` will
505// always be up-to-date, but `obj.field` may not; i.e., after the
506// flip, `ref` will be a to-space reference, but `obj.field` will
507// probably still be a from-space reference (unless it gets updated by
508// another thread, or if another thread installed another object
509// reference (different from `ref`) in `obj.field`).
510//
511// If `entrypoint` is a valid location it is assumed to already be
512// holding the entrypoint. The case where the entrypoint is passed in
513// is for the GcRoot read barrier.
514class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
515 public:
516 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
517 Location ref,
518 Location entrypoint = Location::NoLocation())
519 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
520 DCHECK(kEmitCompilerReadBarrier);
521 }
522
523 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
526 LocationSummary* locations = instruction_->GetLocations();
527 Register ref_reg = ref_.AsRegister<Register>();
528 DCHECK(locations->CanCall());
529 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
530 DCHECK(instruction_->IsInstanceFieldGet() ||
531 instruction_->IsStaticFieldGet() ||
532 instruction_->IsArrayGet() ||
533 instruction_->IsArraySet() ||
534 instruction_->IsLoadClass() ||
535 instruction_->IsLoadString() ||
536 instruction_->IsInstanceOf() ||
537 instruction_->IsCheckCast() ||
538 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
539 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
540 << "Unexpected instruction in read barrier marking slow path: "
541 << instruction_->DebugName();
542
543 __ Bind(GetEntryLabel());
544 // No need to save live registers; it's taken care of by the
545 // entrypoint. Also, there is no need to update the stack mask,
546 // as this runtime call will not trigger a garbage collection.
547 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
548 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
549 (S2 <= ref_reg && ref_reg <= S7) ||
550 (ref_reg == FP)) << ref_reg;
551 // "Compact" slow path, saving two moves.
552 //
553 // Instead of using the standard runtime calling convention (input
554 // and output in A0 and V0 respectively):
555 //
556 // A0 <- ref
557 // V0 <- ReadBarrierMark(A0)
558 // ref <- V0
559 //
560 // we just use rX (the register containing `ref`) as input and output
561 // of a dedicated entrypoint:
562 //
563 // rX <- ReadBarrierMarkRegX(rX)
564 //
565 if (entrypoint_.IsValid()) {
566 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
567 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
568 __ Jalr(entrypoint_.AsRegister<Register>());
569 __ NopIfNoReordering();
570 } else {
571 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100572 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800573 // This runtime call does not require a stack map.
574 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
575 instruction_,
576 this,
577 /* direct */ false);
578 }
579 __ B(GetExitLabel());
580 }
581
582 private:
583 // The location (register) of the marked object reference.
584 const Location ref_;
585
586 // The location of the entrypoint if already loaded.
587 const Location entrypoint_;
588
589 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
590};
591
592// Slow path marking an object reference `ref` during a read barrier,
593// and if needed, atomically updating the field `obj.field` in the
594// object `obj` holding this reference after marking (contrary to
595// ReadBarrierMarkSlowPathMIPS above, which never tries to update
596// `obj.field`).
597//
598// This means that after the execution of this slow path, both `ref`
599// and `obj.field` will be up-to-date; i.e., after the flip, both will
600// hold the same to-space reference (unless another thread installed
601// another object reference (different from `ref`) in `obj.field`).
602class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
603 public:
604 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
605 Location ref,
606 Register obj,
607 Location field_offset,
608 Register temp1)
609 : SlowPathCodeMIPS(instruction),
610 ref_(ref),
611 obj_(obj),
612 field_offset_(field_offset),
613 temp1_(temp1) {
614 DCHECK(kEmitCompilerReadBarrier);
615 }
616
617 const char* GetDescription() const OVERRIDE {
618 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
619 }
620
621 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
622 LocationSummary* locations = instruction_->GetLocations();
623 Register ref_reg = ref_.AsRegister<Register>();
624 DCHECK(locations->CanCall());
625 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
626 // This slow path is only used by the UnsafeCASObject intrinsic.
627 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
628 << "Unexpected instruction in read barrier marking and field updating slow path: "
629 << instruction_->DebugName();
630 DCHECK(instruction_->GetLocations()->Intrinsified());
631 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
632 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
633
634 __ Bind(GetEntryLabel());
635
636 // Save the old reference.
637 // Note that we cannot use AT or TMP to save the old reference, as those
638 // are used by the code that follows, but we need the old reference after
639 // the call to the ReadBarrierMarkRegX entry point.
640 DCHECK_NE(temp1_, AT);
641 DCHECK_NE(temp1_, TMP);
642 __ Move(temp1_, ref_reg);
643
644 // No need to save live registers; it's taken care of by the
645 // entrypoint. Also, there is no need to update the stack mask,
646 // as this runtime call will not trigger a garbage collection.
647 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
648 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
649 (S2 <= ref_reg && ref_reg <= S7) ||
650 (ref_reg == FP)) << ref_reg;
651 // "Compact" slow path, saving two moves.
652 //
653 // Instead of using the standard runtime calling convention (input
654 // and output in A0 and V0 respectively):
655 //
656 // A0 <- ref
657 // V0 <- ReadBarrierMark(A0)
658 // ref <- V0
659 //
660 // we just use rX (the register containing `ref`) as input and output
661 // of a dedicated entrypoint:
662 //
663 // rX <- ReadBarrierMarkRegX(rX)
664 //
665 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100666 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800667 // This runtime call does not require a stack map.
668 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
669 instruction_,
670 this,
671 /* direct */ false);
672
673 // If the new reference is different from the old reference,
674 // update the field in the holder (`*(obj_ + field_offset_)`).
675 //
676 // Note that this field could also hold a different object, if
677 // another thread had concurrently changed it. In that case, the
678 // the compare-and-set (CAS) loop below would abort, leaving the
679 // field as-is.
680 MipsLabel done;
681 __ Beq(temp1_, ref_reg, &done);
682
683 // Update the the holder's field atomically. This may fail if
684 // mutator updates before us, but it's OK. This is achieved
685 // using a strong compare-and-set (CAS) operation with relaxed
686 // memory synchronization ordering, where the expected value is
687 // the old reference and the desired value is the new reference.
688
689 // Convenience aliases.
690 Register base = obj_;
691 // The UnsafeCASObject intrinsic uses a register pair as field
692 // offset ("long offset"), of which only the low part contains
693 // data.
694 Register offset = field_offset_.AsRegisterPairLow<Register>();
695 Register expected = temp1_;
696 Register value = ref_reg;
697 Register tmp_ptr = TMP; // Pointer to actual memory.
698 Register tmp = AT; // Value in memory.
699
700 __ Addu(tmp_ptr, base, offset);
701
702 if (kPoisonHeapReferences) {
703 __ PoisonHeapReference(expected);
704 // Do not poison `value` if it is the same register as
705 // `expected`, which has just been poisoned.
706 if (value != expected) {
707 __ PoisonHeapReference(value);
708 }
709 }
710
711 // do {
712 // tmp = [r_ptr] - expected;
713 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
714
715 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
716 MipsLabel loop_head, exit_loop;
717 __ Bind(&loop_head);
718 if (is_r6) {
719 __ LlR6(tmp, tmp_ptr);
720 } else {
721 __ LlR2(tmp, tmp_ptr);
722 }
723 __ Bne(tmp, expected, &exit_loop);
724 __ Move(tmp, value);
725 if (is_r6) {
726 __ ScR6(tmp, tmp_ptr);
727 } else {
728 __ ScR2(tmp, tmp_ptr);
729 }
730 __ Beqz(tmp, &loop_head);
731 __ Bind(&exit_loop);
732
733 if (kPoisonHeapReferences) {
734 __ UnpoisonHeapReference(expected);
735 // Do not unpoison `value` if it is the same register as
736 // `expected`, which has just been unpoisoned.
737 if (value != expected) {
738 __ UnpoisonHeapReference(value);
739 }
740 }
741
742 __ Bind(&done);
743 __ B(GetExitLabel());
744 }
745
746 private:
747 // The location (register) of the marked object reference.
748 const Location ref_;
749 // The register containing the object holding the marked object reference field.
750 const Register obj_;
751 // The location of the offset of the marked reference field within `obj_`.
752 Location field_offset_;
753
754 const Register temp1_;
755
756 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
757};
758
759// Slow path generating a read barrier for a heap reference.
760class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
761 public:
762 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
763 Location out,
764 Location ref,
765 Location obj,
766 uint32_t offset,
767 Location index)
768 : SlowPathCodeMIPS(instruction),
769 out_(out),
770 ref_(ref),
771 obj_(obj),
772 offset_(offset),
773 index_(index) {
774 DCHECK(kEmitCompilerReadBarrier);
775 // If `obj` is equal to `out` or `ref`, it means the initial object
776 // has been overwritten by (or after) the heap object reference load
777 // to be instrumented, e.g.:
778 //
779 // __ LoadFromOffset(kLoadWord, out, out, offset);
780 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
781 //
782 // In that case, we have lost the information about the original
783 // object, and the emitted read barrier cannot work properly.
784 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
785 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
786 }
787
788 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
789 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
790 LocationSummary* locations = instruction_->GetLocations();
791 Register reg_out = out_.AsRegister<Register>();
792 DCHECK(locations->CanCall());
793 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
794 DCHECK(instruction_->IsInstanceFieldGet() ||
795 instruction_->IsStaticFieldGet() ||
796 instruction_->IsArrayGet() ||
797 instruction_->IsInstanceOf() ||
798 instruction_->IsCheckCast() ||
799 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
800 << "Unexpected instruction in read barrier for heap reference slow path: "
801 << instruction_->DebugName();
802
803 __ Bind(GetEntryLabel());
804 SaveLiveRegisters(codegen, locations);
805
806 // We may have to change the index's value, but as `index_` is a
807 // constant member (like other "inputs" of this slow path),
808 // introduce a copy of it, `index`.
809 Location index = index_;
810 if (index_.IsValid()) {
811 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
812 if (instruction_->IsArrayGet()) {
813 // Compute the actual memory offset and store it in `index`.
814 Register index_reg = index_.AsRegister<Register>();
815 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
816 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
817 // We are about to change the value of `index_reg` (see the
818 // calls to art::mips::MipsAssembler::Sll and
819 // art::mips::MipsAssembler::Addiu32 below), but it has
820 // not been saved by the previous call to
821 // art::SlowPathCode::SaveLiveRegisters, as it is a
822 // callee-save register --
823 // art::SlowPathCode::SaveLiveRegisters does not consider
824 // callee-save registers, as it has been designed with the
825 // assumption that callee-save registers are supposed to be
826 // handled by the called function. So, as a callee-save
827 // register, `index_reg` _would_ eventually be saved onto
828 // the stack, but it would be too late: we would have
829 // changed its value earlier. Therefore, we manually save
830 // it here into another freely available register,
831 // `free_reg`, chosen of course among the caller-save
832 // registers (as a callee-save `free_reg` register would
833 // exhibit the same problem).
834 //
835 // Note we could have requested a temporary register from
836 // the register allocator instead; but we prefer not to, as
837 // this is a slow path, and we know we can find a
838 // caller-save register that is available.
839 Register free_reg = FindAvailableCallerSaveRegister(codegen);
840 __ Move(free_reg, index_reg);
841 index_reg = free_reg;
842 index = Location::RegisterLocation(index_reg);
843 } else {
844 // The initial register stored in `index_` has already been
845 // saved in the call to art::SlowPathCode::SaveLiveRegisters
846 // (as it is not a callee-save register), so we can freely
847 // use it.
848 }
849 // Shifting the index value contained in `index_reg` by the scale
850 // factor (2) cannot overflow in practice, as the runtime is
851 // unable to allocate object arrays with a size larger than
852 // 2^26 - 1 (that is, 2^28 - 4 bytes).
853 __ Sll(index_reg, index_reg, TIMES_4);
854 static_assert(
855 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
856 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
857 __ Addiu32(index_reg, index_reg, offset_);
858 } else {
859 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
860 // intrinsics, `index_` is not shifted by a scale factor of 2
861 // (as in the case of ArrayGet), as it is actually an offset
862 // to an object field within an object.
863 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
864 DCHECK(instruction_->GetLocations()->Intrinsified());
865 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
866 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
867 << instruction_->AsInvoke()->GetIntrinsic();
868 DCHECK_EQ(offset_, 0U);
869 DCHECK(index_.IsRegisterPair());
870 // UnsafeGet's offset location is a register pair, the low
871 // part contains the correct offset.
872 index = index_.ToLow();
873 }
874 }
875
876 // We're moving two or three locations to locations that could
877 // overlap, so we need a parallel move resolver.
878 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100879 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800880 parallel_move.AddMove(ref_,
881 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100882 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800883 nullptr);
884 parallel_move.AddMove(obj_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 if (index.IsValid()) {
889 parallel_move.AddMove(index,
890 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100891 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800892 nullptr);
893 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
894 } else {
895 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
896 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
897 }
898 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
899 instruction_,
900 instruction_->GetDexPc(),
901 this);
902 CheckEntrypointTypes<
903 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200904 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 calling_convention.GetReturnLocation(DataType::Type::kReference),
906 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800907
908 RestoreLiveRegisters(codegen, locations);
909 __ B(GetExitLabel());
910 }
911
912 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
913
914 private:
915 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
916 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
917 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
918 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
919 if (i != ref &&
920 i != obj &&
921 !codegen->IsCoreCalleeSaveRegister(i) &&
922 !codegen->IsBlockedCoreRegister(i)) {
923 return static_cast<Register>(i);
924 }
925 }
926 // We shall never fail to find a free caller-save register, as
927 // there are more than two core caller-save registers on MIPS
928 // (meaning it is possible to find one which is different from
929 // `ref` and `obj`).
930 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
931 LOG(FATAL) << "Could not find a free caller-save register";
932 UNREACHABLE();
933 }
934
935 const Location out_;
936 const Location ref_;
937 const Location obj_;
938 const uint32_t offset_;
939 // An additional location containing an index to an array.
940 // Only used for HArrayGet and the UnsafeGetObject &
941 // UnsafeGetObjectVolatile intrinsics.
942 const Location index_;
943
944 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
945};
946
947// Slow path generating a read barrier for a GC root.
948class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
949 public:
950 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
951 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
952 DCHECK(kEmitCompilerReadBarrier);
953 }
954
955 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
956 LocationSummary* locations = instruction_->GetLocations();
957 Register reg_out = out_.AsRegister<Register>();
958 DCHECK(locations->CanCall());
959 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
960 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
961 << "Unexpected instruction in read barrier for GC root slow path: "
962 << instruction_->DebugName();
963
964 __ Bind(GetEntryLabel());
965 SaveLiveRegisters(codegen, locations);
966
967 InvokeRuntimeCallingConvention calling_convention;
968 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200969 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
970 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100971 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800972 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
973 instruction_,
974 instruction_->GetDexPc(),
975 this);
976 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200977 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100978 calling_convention.GetReturnLocation(DataType::Type::kReference),
979 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800980
981 RestoreLiveRegisters(codegen, locations);
982 __ B(GetExitLabel());
983 }
984
985 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
986
987 private:
988 const Location out_;
989 const Location root_;
990
991 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
992};
993
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200994CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
995 const MipsInstructionSetFeatures& isa_features,
996 const CompilerOptions& compiler_options,
997 OptimizingCompilerStats* stats)
998 : CodeGenerator(graph,
999 kNumberOfCoreRegisters,
1000 kNumberOfFRegisters,
1001 kNumberOfRegisterPairs,
1002 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1003 arraysize(kCoreCalleeSaves)),
1004 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1005 arraysize(kFpuCalleeSaves)),
1006 compiler_options,
1007 stats),
1008 block_labels_(nullptr),
1009 location_builder_(graph, this),
1010 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001011 move_resolver_(graph->GetAllocator(), this),
1012 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001014 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1016 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1017 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1018 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1019 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1020 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1021 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1022 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1023 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001024 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001025 // Save RA (containing the return address) to mimic Quick.
1026 AddAllocatedRegister(Location::RegisterLocation(RA));
1027}
1028
1029#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001030// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1031#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001032#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001033
1034void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1035 // Ensure that we fix up branches.
1036 __ FinalizeCode();
1037
1038 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001039 StackMapStream* stack_map_stream = GetStackMapStream();
1040 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001041 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001042 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001043 uint32_t new_position = __ GetAdjustedPosition(old_position);
1044 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001045 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001046 }
1047
1048 // Adjust pc offsets for the disassembly information.
1049 if (disasm_info_ != nullptr) {
1050 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1051 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1052 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1053 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1054 it.second.start = __ GetAdjustedPosition(it.second.start);
1055 it.second.end = __ GetAdjustedPosition(it.second.end);
1056 }
1057 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1058 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1059 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1060 }
1061 }
1062
1063 CodeGenerator::Finalize(allocator);
1064}
1065
1066MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1067 return codegen_->GetAssembler();
1068}
1069
1070void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1071 DCHECK_LT(index, moves_.size());
1072 MoveOperands* move = moves_[index];
1073 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1074}
1075
1076void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1077 DCHECK_LT(index, moves_.size());
1078 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001079 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001080 Location loc1 = move->GetDestination();
1081 Location loc2 = move->GetSource();
1082
1083 DCHECK(!loc1.IsConstant());
1084 DCHECK(!loc2.IsConstant());
1085
1086 if (loc1.Equals(loc2)) {
1087 return;
1088 }
1089
1090 if (loc1.IsRegister() && loc2.IsRegister()) {
1091 // Swap 2 GPRs.
1092 Register r1 = loc1.AsRegister<Register>();
1093 Register r2 = loc2.AsRegister<Register>();
1094 __ Move(TMP, r2);
1095 __ Move(r2, r1);
1096 __ Move(r1, TMP);
1097 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001098 if (codegen_->GetGraph()->HasSIMD()) {
1099 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1100 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1101 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001102 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001103 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1104 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1105 if (type == DataType::Type::kFloat32) {
1106 __ MovS(FTMP, f2);
1107 __ MovS(f2, f1);
1108 __ MovS(f1, FTMP);
1109 } else {
1110 DCHECK_EQ(type, DataType::Type::kFloat64);
1111 __ MovD(FTMP, f2);
1112 __ MovD(f2, f1);
1113 __ MovD(f1, FTMP);
1114 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115 }
1116 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1117 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1118 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001119 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001120 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1121 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001122 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123 __ Move(TMP, r2);
1124 __ Mfc1(r2, f1);
1125 __ Mtc1(TMP, f1);
1126 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1127 // Swap 2 GPR register pairs.
1128 Register r1 = loc1.AsRegisterPairLow<Register>();
1129 Register r2 = loc2.AsRegisterPairLow<Register>();
1130 __ Move(TMP, r2);
1131 __ Move(r2, r1);
1132 __ Move(r1, TMP);
1133 r1 = loc1.AsRegisterPairHigh<Register>();
1134 r2 = loc2.AsRegisterPairHigh<Register>();
1135 __ Move(TMP, r2);
1136 __ Move(r2, r1);
1137 __ Move(r1, TMP);
1138 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1139 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1140 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1143 : loc2.AsFpuRegister<FRegister>();
1144 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1145 : loc2.AsRegisterPairLow<Register>();
1146 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1147 : loc2.AsRegisterPairHigh<Register>();
1148 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1149 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1150 // unpredictable and the following mfch1 will fail.
1151 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001152 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001153 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001154 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001155 __ Move(r2_l, TMP);
1156 __ Move(r2_h, AT);
1157 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1158 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1159 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1160 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001161 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1162 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001163 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1164 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001165 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1166 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 __ Move(TMP, reg);
1168 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1169 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1170 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1171 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1172 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1173 : loc2.AsRegisterPairLow<Register>();
1174 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1175 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001176 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001177 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1178 : loc2.GetHighStackIndex(kMipsWordSize);
1179 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001180 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001182 __ Move(TMP, reg_h);
1183 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1184 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001185 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1186 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1187 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1188 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1189 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1190 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1191 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001192 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1193 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1194 : loc2.AsFpuRegister<FRegister>();
1195 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001197 __ MovS(FTMP, reg);
1198 __ LoadSFromOffset(reg, SP, offset);
1199 __ StoreSToOffset(FTMP, SP, offset);
1200 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001201 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001202 __ MovD(FTMP, reg);
1203 __ LoadDFromOffset(reg, SP, offset);
1204 __ StoreDToOffset(FTMP, SP, offset);
1205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 } else {
1207 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1208 }
1209}
1210
1211void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1212 __ Pop(static_cast<Register>(reg));
1213}
1214
1215void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1216 __ Push(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1220 // Allocate a scratch register other than TMP, if available.
1221 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1222 // automatically unspilled when the scratch scope object is destroyed).
1223 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1224 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001225 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001226 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1227 __ LoadFromOffset(kLoadWord,
1228 Register(ensure_scratch.GetRegister()),
1229 SP,
1230 index1 + stack_offset);
1231 __ LoadFromOffset(kLoadWord,
1232 TMP,
1233 SP,
1234 index2 + stack_offset);
1235 __ StoreToOffset(kStoreWord,
1236 Register(ensure_scratch.GetRegister()),
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1240 }
1241}
1242
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001243void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1244 __ LoadQFromOffset(FTMP, SP, index1);
1245 __ LoadQFromOffset(FTMP2, SP, index2);
1246 __ StoreQToOffset(FTMP, SP, index2);
1247 __ StoreQToOffset(FTMP2, SP, index1);
1248}
1249
Alexey Frunze73296a72016-06-03 22:51:46 -07001250void CodeGeneratorMIPS::ComputeSpillMask() {
1251 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1252 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1253 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1254 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1255 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1256 // within the stack frame.
1257 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1258 core_spill_mask_ |= (1 << ZERO);
1259 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001260}
1261
1262bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001263 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1265 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1266 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001267 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001268}
1269
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001270static dwarf::Reg DWARFReg(Register reg) {
1271 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1272}
1273
1274// TODO: mapping of floating-point registers to DWARF.
1275
1276void CodeGeneratorMIPS::GenerateFrameEntry() {
1277 __ Bind(&frame_entry_label_);
1278
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001279 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1280 LOG(WARNING) << "Unimplemented hotness update in mips backend";
1281 }
1282
Vladimir Marko33bff252017-11-01 14:35:42 +00001283 bool do_overflow_check =
1284 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001285
1286 if (do_overflow_check) {
1287 __ LoadFromOffset(kLoadWord,
1288 ZERO,
1289 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001290 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291 RecordPcInfo(nullptr, 0);
1292 }
1293
1294 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001295 CHECK_EQ(fpu_spill_mask_, 0u);
1296 CHECK_EQ(core_spill_mask_, 1u << RA);
1297 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001298 return;
1299 }
1300
1301 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001302 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1303 LOG(FATAL) << "Stack frame larger than "
1304 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 }
1306
1307 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308
Alexey Frunze73296a72016-06-03 22:51:46 -07001309 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 __ IncreaseFrameSize(ofs);
1311
Alexey Frunze73296a72016-06-03 22:51:46 -07001312 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1313 Register reg = static_cast<Register>(MostSignificantBit(mask));
1314 mask ^= 1u << reg;
1315 ofs -= kMipsWordSize;
1316 // The ZERO register is only included for alignment.
1317 if (reg != ZERO) {
1318 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319 __ cfi().RelOffset(DWARFReg(reg), ofs);
1320 }
1321 }
1322
Alexey Frunze73296a72016-06-03 22:51:46 -07001323 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1324 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1325 mask ^= 1u << reg;
1326 ofs -= kMipsDoublewordSize;
1327 __ StoreDToOffset(reg, SP, ofs);
1328 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001329 }
1330
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001331 // Save the current method if we need it. Note that we do not
1332 // do this in HCurrentMethod, as the instruction might have been removed
1333 // in the SSA graph.
1334 if (RequiresCurrentMethod()) {
1335 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1336 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001337
1338 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1339 // Initialize should deoptimize flag to 0.
1340 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1341 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342}
1343
1344void CodeGeneratorMIPS::GenerateFrameExit() {
1345 __ cfi().RememberState();
1346
1347 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001349
Alexey Frunze73296a72016-06-03 22:51:46 -07001350 // For better instruction scheduling restore RA before other registers.
1351 uint32_t ofs = GetFrameSize();
1352 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1353 Register reg = static_cast<Register>(MostSignificantBit(mask));
1354 mask ^= 1u << reg;
1355 ofs -= kMipsWordSize;
1356 // The ZERO register is only included for alignment.
1357 if (reg != ZERO) {
1358 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001359 __ cfi().Restore(DWARFReg(reg));
1360 }
1361 }
1362
Alexey Frunze73296a72016-06-03 22:51:46 -07001363 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1364 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1365 mask ^= 1u << reg;
1366 ofs -= kMipsDoublewordSize;
1367 __ LoadDFromOffset(reg, SP, ofs);
1368 // TODO: __ cfi().Restore(DWARFReg(reg));
1369 }
1370
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001371 size_t frame_size = GetFrameSize();
1372 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1373 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1374 bool reordering = __ SetReorder(false);
1375 if (exchange) {
1376 __ Jr(RA);
1377 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1378 } else {
1379 __ DecreaseFrameSize(frame_size);
1380 __ Jr(RA);
1381 __ Nop(); // In delay slot.
1382 }
1383 __ SetReorder(reordering);
1384 } else {
1385 __ Jr(RA);
1386 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001387 }
1388
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001389 __ cfi().RestoreState();
1390 __ cfi().DefCFAOffset(GetFrameSize());
1391}
1392
1393void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1394 __ Bind(GetLabelOf(block));
1395}
1396
Lena Djokicca8c2952017-05-29 11:31:46 +02001397VectorRegister VectorRegisterFrom(Location location) {
1398 DCHECK(location.IsFpuRegister());
1399 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1400}
1401
Lena Djokic8098da92017-06-28 12:07:50 +02001402void CodeGeneratorMIPS::MoveLocation(Location destination,
1403 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001404 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001405 if (source.Equals(destination)) {
1406 return;
1407 }
1408
Lena Djokic8098da92017-06-28 12:07:50 +02001409 if (source.IsConstant()) {
1410 MoveConstant(destination, source.GetConstant());
1411 } else {
1412 if (destination.IsRegister()) {
1413 if (source.IsRegister()) {
1414 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1415 } else if (source.IsFpuRegister()) {
1416 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1417 } else {
1418 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001420 }
1421 } else if (destination.IsRegisterPair()) {
1422 if (source.IsRegisterPair()) {
1423 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1424 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1425 } else if (source.IsFpuRegister()) {
1426 Register dst_high = destination.AsRegisterPairHigh<Register>();
1427 Register dst_low = destination.AsRegisterPairLow<Register>();
1428 FRegister src = source.AsFpuRegister<FRegister>();
1429 __ Mfc1(dst_low, src);
1430 __ MoveFromFpuHigh(dst_high, src);
1431 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001432 DCHECK(source.IsDoubleStackSlot())
1433 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001434 int32_t off = source.GetStackIndex();
1435 Register r = destination.AsRegisterPairLow<Register>();
1436 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1437 }
1438 } else if (destination.IsFpuRegister()) {
1439 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001440 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001441 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1442 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001443 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001444 FRegister dst = destination.AsFpuRegister<FRegister>();
1445 Register src_high = source.AsRegisterPairHigh<Register>();
1446 Register src_low = source.AsRegisterPairLow<Register>();
1447 __ Mtc1(src_low, dst);
1448 __ MoveToFpuHigh(src_high, dst);
1449 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001450 if (GetGraph()->HasSIMD()) {
1451 __ MoveV(VectorRegisterFrom(destination),
1452 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001453 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001454 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001455 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1456 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001457 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001458 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1459 }
Lena Djokic8098da92017-06-28 12:07:50 +02001460 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001461 } else if (source.IsSIMDStackSlot()) {
1462 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001463 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001464 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001465 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1466 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001468 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1469 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1470 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001471 } else if (destination.IsSIMDStackSlot()) {
1472 if (source.IsFpuRegister()) {
1473 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1474 } else {
1475 DCHECK(source.IsSIMDStackSlot());
1476 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1477 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1478 }
Lena Djokic8098da92017-06-28 12:07:50 +02001479 } else if (destination.IsDoubleStackSlot()) {
1480 int32_t dst_offset = destination.GetStackIndex();
1481 if (source.IsRegisterPair()) {
1482 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1483 } else if (source.IsFpuRegister()) {
1484 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1485 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001486 DCHECK(source.IsDoubleStackSlot())
1487 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001488 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1489 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1490 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1491 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1492 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001493 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001494 DCHECK(destination.IsStackSlot()) << destination;
1495 int32_t dst_offset = destination.GetStackIndex();
1496 if (source.IsRegister()) {
1497 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1498 } else if (source.IsFpuRegister()) {
1499 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1500 } else {
1501 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1502 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1503 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1504 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001505 }
1506 }
1507}
1508
1509void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1510 if (c->IsIntConstant() || c->IsNullConstant()) {
1511 // Move 32 bit constant.
1512 int32_t value = GetInt32ValueOf(c);
1513 if (destination.IsRegister()) {
1514 Register dst = destination.AsRegister<Register>();
1515 __ LoadConst32(dst, value);
1516 } else {
1517 DCHECK(destination.IsStackSlot())
1518 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001519 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520 }
1521 } else if (c->IsLongConstant()) {
1522 // Move 64 bit constant.
1523 int64_t value = GetInt64ValueOf(c);
1524 if (destination.IsRegisterPair()) {
1525 Register r_h = destination.AsRegisterPairHigh<Register>();
1526 Register r_l = destination.AsRegisterPairLow<Register>();
1527 __ LoadConst64(r_h, r_l, value);
1528 } else {
1529 DCHECK(destination.IsDoubleStackSlot())
1530 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001531 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001532 }
1533 } else if (c->IsFloatConstant()) {
1534 // Move 32 bit float constant.
1535 int32_t value = GetInt32ValueOf(c);
1536 if (destination.IsFpuRegister()) {
1537 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1538 } else {
1539 DCHECK(destination.IsStackSlot())
1540 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001541 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001542 }
1543 } else {
1544 // Move 64 bit double constant.
1545 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1546 int64_t value = GetInt64ValueOf(c);
1547 if (destination.IsFpuRegister()) {
1548 FRegister fd = destination.AsFpuRegister<FRegister>();
1549 __ LoadDConst64(fd, value, TMP);
1550 } else {
1551 DCHECK(destination.IsDoubleStackSlot())
1552 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001553 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001554 }
1555 }
1556}
1557
1558void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1559 DCHECK(destination.IsRegister());
1560 Register dst = destination.AsRegister<Register>();
1561 __ LoadConst32(dst, value);
1562}
1563
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001564void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1565 if (location.IsRegister()) {
1566 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001567 } else if (location.IsRegisterPair()) {
1568 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1569 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570 } else {
1571 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1572 }
1573}
1574
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001575template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001576inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1577 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001578 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001579 for (const PcRelativePatchInfo& info : infos) {
1580 const DexFile& dex_file = info.target_dex_file;
1581 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001582 DCHECK(info.label.IsBound());
1583 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001584 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1585 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001586 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1587 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1588 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001589 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001590 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001591 }
1592}
1593
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001594void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001595 DCHECK(linker_patches->empty());
1596 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001597 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001598 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001599 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001600 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001601 pc_relative_string_patches_.size() +
1602 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001603 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001604 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001605 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1606 pc_relative_method_patches_, linker_patches);
1607 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1608 pc_relative_type_patches_, linker_patches);
1609 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1610 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001611 } else {
1612 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001613 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1614 pc_relative_type_patches_, linker_patches);
1615 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1616 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001617 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001618 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1619 method_bss_entry_patches_, linker_patches);
1620 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1621 type_bss_entry_patches_, linker_patches);
1622 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1623 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001624 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001625}
1626
Vladimir Marko65979462017-05-19 17:25:12 +01001627CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001628 MethodReference target_method,
1629 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001630 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001631 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001632 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001633 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001634}
1635
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001636CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001637 MethodReference target_method,
1638 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001639 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001640 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001641 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001642 &method_bss_entry_patches_);
1643}
1644
Alexey Frunze06a46c42016-07-19 15:00:40 -07001645CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001646 const DexFile& dex_file,
1647 dex::TypeIndex type_index,
1648 const PcRelativePatchInfo* info_high) {
1649 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001650}
1651
Vladimir Marko1998cd02017-01-13 13:02:58 +00001652CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001653 const DexFile& dex_file,
1654 dex::TypeIndex type_index,
1655 const PcRelativePatchInfo* info_high) {
1656 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001657}
1658
Vladimir Marko65979462017-05-19 17:25:12 +01001659CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001660 const DexFile& dex_file,
1661 dex::StringIndex string_index,
1662 const PcRelativePatchInfo* info_high) {
1663 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001664}
1665
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001666CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1667 const DexFile& dex_file,
1668 dex::StringIndex string_index,
1669 const PcRelativePatchInfo* info_high) {
1670 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1671}
1672
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001673CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001674 const DexFile& dex_file,
1675 uint32_t offset_or_index,
1676 const PcRelativePatchInfo* info_high,
1677 ArenaDeque<PcRelativePatchInfo>* patches) {
1678 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001679 return &patches->back();
1680}
1681
Alexey Frunze06a46c42016-07-19 15:00:40 -07001682Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1683 return map->GetOrCreate(
1684 value,
1685 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1686}
1687
Alexey Frunze06a46c42016-07-19 15:00:40 -07001688Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001689 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001690}
1691
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001692void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001693 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001694 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001695 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001696 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001697 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001698 if (GetInstructionSetFeatures().IsR6()) {
1699 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001700 __ Bind(&info_high->label);
1701 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001702 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001703 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001704 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001705 } else {
1706 // If base is ZERO, emit NAL to obtain the actual base.
1707 if (base == ZERO) {
1708 // Generate a dummy PC-relative call to obtain PC.
1709 __ Nal();
1710 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001711 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001712 __ Lui(out, /* placeholder */ 0x1234);
1713 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1714 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1715 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001716 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001717 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001718 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001719 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720 __ Addu(out, out, (base == ZERO) ? RA : base);
1721 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001722 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001723 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001724}
1725
Alexey Frunze627c1a02017-01-30 19:28:14 -08001726CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1727 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001728 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001729 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001730 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1731 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001732 return &jit_string_patches_.back();
1733}
1734
1735CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1736 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001737 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001738 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001739 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1740 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001741 return &jit_class_patches_.back();
1742}
1743
1744void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1745 const uint8_t* roots_data,
1746 const CodeGeneratorMIPS::JitPatchInfo& info,
1747 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001748 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1749 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001750 uintptr_t address =
1751 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1752 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1753 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001754 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1755 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1756 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1757 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001758 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001759 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1760 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001761 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001762 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001763 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1764 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001765 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001766 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1767 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001768}
1769
1770void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1771 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001772 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1773 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001774 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001775 }
1776 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001777 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1778 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001779 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001780 }
1781}
1782
Goran Jakovljevice114da22016-12-26 14:21:43 +01001783void CodeGeneratorMIPS::MarkGCCard(Register object,
1784 Register value,
1785 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001786 MipsLabel done;
1787 Register card = AT;
1788 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001789 if (value_can_be_null) {
1790 __ Beqz(value, &done);
1791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001792 __ LoadFromOffset(kLoadWord,
1793 card,
1794 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001795 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001796 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1797 __ Addu(temp, card, temp);
1798 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001799 if (value_can_be_null) {
1800 __ Bind(&done);
1801 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001802}
1803
David Brazdil58282f42016-01-14 12:45:10 +00001804void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1806 blocked_core_registers_[ZERO] = true;
1807 blocked_core_registers_[K0] = true;
1808 blocked_core_registers_[K1] = true;
1809 blocked_core_registers_[GP] = true;
1810 blocked_core_registers_[SP] = true;
1811 blocked_core_registers_[RA] = true;
1812
1813 // AT and TMP(T8) are used as temporary/scratch registers
1814 // (similar to how AT is used by MIPS assemblers).
1815 blocked_core_registers_[AT] = true;
1816 blocked_core_registers_[TMP] = true;
1817 blocked_fpu_registers_[FTMP] = true;
1818
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001819 if (GetInstructionSetFeatures().HasMsa()) {
1820 // To be used just for MSA instructions.
1821 blocked_fpu_registers_[FTMP2] = true;
1822 }
1823
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001824 // Reserve suspend and thread registers.
1825 blocked_core_registers_[S0] = true;
1826 blocked_core_registers_[TR] = true;
1827
1828 // Reserve T9 for function calls
1829 blocked_core_registers_[T9] = true;
1830
1831 // Reserve odd-numbered FPU registers.
1832 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1833 blocked_fpu_registers_[i] = true;
1834 }
1835
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001836 if (GetGraph()->IsDebuggable()) {
1837 // Stubs do not save callee-save floating point registers. If the graph
1838 // is debuggable, we need to deal with these registers differently. For
1839 // now, just block them.
1840 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1841 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1842 }
1843 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844}
1845
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001846size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1847 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1848 return kMipsWordSize;
1849}
1850
1851size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1852 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1853 return kMipsWordSize;
1854}
1855
1856size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001857 if (GetGraph()->HasSIMD()) {
1858 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1859 } else {
1860 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1861 }
1862 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863}
1864
1865size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001866 if (GetGraph()->HasSIMD()) {
1867 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1868 } else {
1869 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1870 }
1871 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872}
1873
1874void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001875 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876}
1877
1878void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001879 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880}
1881
Serban Constantinescufca16662016-07-14 09:21:59 +01001882constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1883
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1885 HInstruction* instruction,
1886 uint32_t dex_pc,
1887 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001888 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001889 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1890 IsDirectEntrypoint(entrypoint));
1891 if (EntrypointRequiresStackMap(entrypoint)) {
1892 RecordPcInfo(instruction, dex_pc, slow_path);
1893 }
1894}
1895
1896void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1897 HInstruction* instruction,
1898 SlowPathCode* slow_path,
1899 bool direct) {
1900 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1901 GenerateInvokeRuntime(entry_point_offset, direct);
1902}
1903
1904void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001905 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001906 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001907 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001908 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 // Reserve argument space on stack (for $a0-$a3) for
1910 // entrypoints that directly reference native implementations.
1911 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001912 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001913 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001914 } else {
1915 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001917 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918}
1919
1920void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1921 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001922 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1923 const size_t status_byte_offset =
1924 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1925 constexpr uint32_t shifted_initialized_value =
1926 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1927
1928 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1929 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001930 __ Bltu(TMP, AT, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001931 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1932 __ Sync(0);
1933 __ Bind(slow_path->GetExitLabel());
1934}
1935
1936void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1937 __ Sync(0); // Only stype 0 is supported.
1938}
1939
1940void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1941 HBasicBlock* successor) {
1942 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001943 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1944
1945 if (slow_path == nullptr) {
1946 slow_path =
1947 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1948 instruction->SetSlowPath(slow_path);
1949 codegen_->AddSlowPath(slow_path);
1950 if (successor != nullptr) {
1951 DCHECK(successor->IsLoopHeader());
1952 }
1953 } else {
1954 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1955 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956
1957 __ LoadFromOffset(kLoadUnsignedHalfword,
1958 TMP,
1959 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001960 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 if (successor == nullptr) {
1962 __ Bnez(TMP, slow_path->GetEntryLabel());
1963 __ Bind(slow_path->GetReturnLabel());
1964 } else {
1965 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1966 __ B(slow_path->GetEntryLabel());
1967 // slow_path will return to GetLabelOf(successor).
1968 }
1969}
1970
1971InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1972 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001973 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001974 assembler_(codegen->GetAssembler()),
1975 codegen_(codegen) {}
1976
1977void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1978 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001979 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001981 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001982 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 locations->SetInAt(0, Location::RequiresRegister());
1985 HInstruction* right = instruction->InputAt(1);
1986 bool can_use_imm = false;
1987 if (right->IsConstant()) {
1988 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1989 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1990 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001991 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001992 DCHECK(instruction->IsSub() || instruction->IsAdd());
1993 if (instruction->IsSub()) {
1994 imm = -imm;
1995 }
1996 if (isR6) {
1997 bool single_use = right->GetUses().HasExactlyOneElement();
1998 int16_t imm_high = High16Bits(imm);
1999 int16_t imm_low = Low16Bits(imm);
2000 if (imm_low < 0) {
2001 imm_high += 1;
2002 }
2003 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2004 } else {
2005 can_use_imm = IsInt<16>(imm);
2006 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002007 }
2008 }
2009 if (can_use_imm)
2010 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2011 else
2012 locations->SetInAt(1, Location::RequiresRegister());
2013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2014 break;
2015 }
2016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002017 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002019 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 break;
2022 }
2023
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 case DataType::Type::kFloat32:
2025 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 DCHECK(instruction->IsAdd() || instruction->IsSub());
2027 locations->SetInAt(0, Location::RequiresFpuRegister());
2028 locations->SetInAt(1, Location::RequiresFpuRegister());
2029 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2030 break;
2031
2032 default:
2033 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2034 }
2035}
2036
2037void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002038 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002039 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002040 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041
2042 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 Register dst = locations->Out().AsRegister<Register>();
2045 Register lhs = locations->InAt(0).AsRegister<Register>();
2046 Location rhs_location = locations->InAt(1);
2047
2048 Register rhs_reg = ZERO;
2049 int32_t rhs_imm = 0;
2050 bool use_imm = rhs_location.IsConstant();
2051 if (use_imm) {
2052 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2053 } else {
2054 rhs_reg = rhs_location.AsRegister<Register>();
2055 }
2056
2057 if (instruction->IsAnd()) {
2058 if (use_imm)
2059 __ Andi(dst, lhs, rhs_imm);
2060 else
2061 __ And(dst, lhs, rhs_reg);
2062 } else if (instruction->IsOr()) {
2063 if (use_imm)
2064 __ Ori(dst, lhs, rhs_imm);
2065 else
2066 __ Or(dst, lhs, rhs_reg);
2067 } else if (instruction->IsXor()) {
2068 if (use_imm)
2069 __ Xori(dst, lhs, rhs_imm);
2070 else
2071 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002072 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002073 DCHECK(instruction->IsAdd() || instruction->IsSub());
2074 if (use_imm) {
2075 if (instruction->IsSub()) {
2076 rhs_imm = -rhs_imm;
2077 }
2078 if (IsInt<16>(rhs_imm)) {
2079 __ Addiu(dst, lhs, rhs_imm);
2080 } else {
2081 DCHECK(isR6);
2082 int16_t rhs_imm_high = High16Bits(rhs_imm);
2083 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2084 if (rhs_imm_low < 0) {
2085 rhs_imm_high += 1;
2086 }
2087 __ Aui(dst, lhs, rhs_imm_high);
2088 if (rhs_imm_low != 0) {
2089 __ Addiu(dst, dst, rhs_imm_low);
2090 }
2091 }
2092 } else if (instruction->IsAdd()) {
2093 __ Addu(dst, lhs, rhs_reg);
2094 } else {
2095 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002096 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002097 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002098 }
2099 break;
2100 }
2101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2104 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2105 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2106 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002107 Location rhs_location = locations->InAt(1);
2108 bool use_imm = rhs_location.IsConstant();
2109 if (!use_imm) {
2110 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2111 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2112 if (instruction->IsAnd()) {
2113 __ And(dst_low, lhs_low, rhs_low);
2114 __ And(dst_high, lhs_high, rhs_high);
2115 } else if (instruction->IsOr()) {
2116 __ Or(dst_low, lhs_low, rhs_low);
2117 __ Or(dst_high, lhs_high, rhs_high);
2118 } else if (instruction->IsXor()) {
2119 __ Xor(dst_low, lhs_low, rhs_low);
2120 __ Xor(dst_high, lhs_high, rhs_high);
2121 } else if (instruction->IsAdd()) {
2122 if (lhs_low == rhs_low) {
2123 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2124 __ Slt(TMP, lhs_low, ZERO);
2125 __ Addu(dst_low, lhs_low, rhs_low);
2126 } else {
2127 __ Addu(dst_low, lhs_low, rhs_low);
2128 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2129 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2130 }
2131 __ Addu(dst_high, lhs_high, rhs_high);
2132 __ Addu(dst_high, dst_high, TMP);
2133 } else {
2134 DCHECK(instruction->IsSub());
2135 __ Sltu(TMP, lhs_low, rhs_low);
2136 __ Subu(dst_low, lhs_low, rhs_low);
2137 __ Subu(dst_high, lhs_high, rhs_high);
2138 __ Subu(dst_high, dst_high, TMP);
2139 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002140 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002141 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2142 if (instruction->IsOr()) {
2143 uint32_t low = Low32Bits(value);
2144 uint32_t high = High32Bits(value);
2145 if (IsUint<16>(low)) {
2146 if (dst_low != lhs_low || low != 0) {
2147 __ Ori(dst_low, lhs_low, low);
2148 }
2149 } else {
2150 __ LoadConst32(TMP, low);
2151 __ Or(dst_low, lhs_low, TMP);
2152 }
2153 if (IsUint<16>(high)) {
2154 if (dst_high != lhs_high || high != 0) {
2155 __ Ori(dst_high, lhs_high, high);
2156 }
2157 } else {
2158 if (high != low) {
2159 __ LoadConst32(TMP, high);
2160 }
2161 __ Or(dst_high, lhs_high, TMP);
2162 }
2163 } else if (instruction->IsXor()) {
2164 uint32_t low = Low32Bits(value);
2165 uint32_t high = High32Bits(value);
2166 if (IsUint<16>(low)) {
2167 if (dst_low != lhs_low || low != 0) {
2168 __ Xori(dst_low, lhs_low, low);
2169 }
2170 } else {
2171 __ LoadConst32(TMP, low);
2172 __ Xor(dst_low, lhs_low, TMP);
2173 }
2174 if (IsUint<16>(high)) {
2175 if (dst_high != lhs_high || high != 0) {
2176 __ Xori(dst_high, lhs_high, high);
2177 }
2178 } else {
2179 if (high != low) {
2180 __ LoadConst32(TMP, high);
2181 }
2182 __ Xor(dst_high, lhs_high, TMP);
2183 }
2184 } else if (instruction->IsAnd()) {
2185 uint32_t low = Low32Bits(value);
2186 uint32_t high = High32Bits(value);
2187 if (IsUint<16>(low)) {
2188 __ Andi(dst_low, lhs_low, low);
2189 } else if (low != 0xFFFFFFFF) {
2190 __ LoadConst32(TMP, low);
2191 __ And(dst_low, lhs_low, TMP);
2192 } else if (dst_low != lhs_low) {
2193 __ Move(dst_low, lhs_low);
2194 }
2195 if (IsUint<16>(high)) {
2196 __ Andi(dst_high, lhs_high, high);
2197 } else if (high != 0xFFFFFFFF) {
2198 if (high != low) {
2199 __ LoadConst32(TMP, high);
2200 }
2201 __ And(dst_high, lhs_high, TMP);
2202 } else if (dst_high != lhs_high) {
2203 __ Move(dst_high, lhs_high);
2204 }
2205 } else {
2206 if (instruction->IsSub()) {
2207 value = -value;
2208 } else {
2209 DCHECK(instruction->IsAdd());
2210 }
2211 int32_t low = Low32Bits(value);
2212 int32_t high = High32Bits(value);
2213 if (IsInt<16>(low)) {
2214 if (dst_low != lhs_low || low != 0) {
2215 __ Addiu(dst_low, lhs_low, low);
2216 }
2217 if (low != 0) {
2218 __ Sltiu(AT, dst_low, low);
2219 }
2220 } else {
2221 __ LoadConst32(TMP, low);
2222 __ Addu(dst_low, lhs_low, TMP);
2223 __ Sltu(AT, dst_low, TMP);
2224 }
2225 if (IsInt<16>(high)) {
2226 if (dst_high != lhs_high || high != 0) {
2227 __ Addiu(dst_high, lhs_high, high);
2228 }
2229 } else {
2230 if (high != low) {
2231 __ LoadConst32(TMP, high);
2232 }
2233 __ Addu(dst_high, lhs_high, TMP);
2234 }
2235 if (low != 0) {
2236 __ Addu(dst_high, dst_high, AT);
2237 }
2238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239 }
2240 break;
2241 }
2242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 case DataType::Type::kFloat32:
2244 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002245 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2246 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2247 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2248 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 __ AddS(dst, lhs, rhs);
2251 } else {
2252 __ AddD(dst, lhs, rhs);
2253 }
2254 } else {
2255 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002257 __ SubS(dst, lhs, rhs);
2258 } else {
2259 __ SubD(dst, lhs, rhs);
2260 }
2261 }
2262 break;
2263 }
2264
2265 default:
2266 LOG(FATAL) << "Unexpected binary operation type " << type;
2267 }
2268}
2269
2270void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002271 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272
Vladimir Markoca6fff82017-10-03 14:49:14 +01002273 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002277 locations->SetInAt(0, Location::RequiresRegister());
2278 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2280 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 locations->SetInAt(0, Location::RequiresRegister());
2283 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2284 locations->SetOut(Location::RequiresRegister());
2285 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002286 default:
2287 LOG(FATAL) << "Unexpected shift type " << type;
2288 }
2289}
2290
2291static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2292
2293void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002294 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297
2298 Location rhs_location = locations->InAt(1);
2299 bool use_imm = rhs_location.IsConstant();
2300 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2301 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002302 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002304 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002305 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2306 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002307
2308 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310 Register dst = locations->Out().AsRegister<Register>();
2311 Register lhs = locations->InAt(0).AsRegister<Register>();
2312 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002313 if (shift_value == 0) {
2314 if (dst != lhs) {
2315 __ Move(dst, lhs);
2316 }
2317 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002318 __ Sll(dst, lhs, shift_value);
2319 } else if (instr->IsShr()) {
2320 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002321 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002322 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002323 } else {
2324 if (has_ins_rotr) {
2325 __ Rotr(dst, lhs, shift_value);
2326 } else {
2327 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2328 __ Srl(dst, lhs, shift_value);
2329 __ Or(dst, dst, TMP);
2330 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002331 }
2332 } else {
2333 if (instr->IsShl()) {
2334 __ Sllv(dst, lhs, rhs_reg);
2335 } else if (instr->IsShr()) {
2336 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002338 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 } else {
2340 if (has_ins_rotr) {
2341 __ Rotrv(dst, lhs, rhs_reg);
2342 } else {
2343 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002344 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2345 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2346 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2347 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2348 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002349 __ Sllv(TMP, lhs, TMP);
2350 __ Srlv(dst, lhs, rhs_reg);
2351 __ Or(dst, dst, TMP);
2352 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 }
2354 }
2355 break;
2356 }
2357
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2360 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2361 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2362 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2363 if (use_imm) {
2364 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002365 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002366 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002367 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002368 if (instr->IsShl()) {
2369 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2370 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2371 __ Sll(dst_low, lhs_low, shift_value);
2372 } else if (instr->IsShr()) {
2373 __ Srl(dst_low, lhs_low, shift_value);
2374 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2375 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002376 } else if (instr->IsUShr()) {
2377 __ Srl(dst_low, lhs_low, shift_value);
2378 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2379 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002380 } else {
2381 __ Srl(dst_low, lhs_low, shift_value);
2382 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2383 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002384 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002385 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002387 if (instr->IsShl()) {
2388 __ Sll(dst_low, lhs_low, shift_value);
2389 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2390 __ Sll(dst_high, lhs_high, shift_value);
2391 __ Or(dst_high, dst_high, TMP);
2392 } else if (instr->IsShr()) {
2393 __ Sra(dst_high, lhs_high, shift_value);
2394 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2395 __ Srl(dst_low, lhs_low, shift_value);
2396 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002397 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002398 __ Srl(dst_high, lhs_high, shift_value);
2399 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2400 __ Srl(dst_low, lhs_low, shift_value);
2401 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002402 } else {
2403 __ Srl(TMP, lhs_low, shift_value);
2404 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2405 __ Or(dst_low, dst_low, TMP);
2406 __ Srl(TMP, lhs_high, shift_value);
2407 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2408 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002409 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002410 }
2411 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002412 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002414 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002415 __ Move(dst_low, ZERO);
2416 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002417 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002419 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002420 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002422 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002423 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002424 // 64-bit rotation by 32 is just a swap.
2425 __ Move(dst_low, lhs_high);
2426 __ Move(dst_high, lhs_low);
2427 } else {
2428 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 __ Srl(dst_low, lhs_high, shift_value_high);
2430 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2431 __ Srl(dst_high, lhs_low, shift_value_high);
2432 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002433 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002434 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2435 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002437 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2438 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 __ Or(dst_high, dst_high, TMP);
2440 }
2441 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 }
2443 }
2444 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002445 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002446 MipsLabel done;
2447 if (instr->IsShl()) {
2448 __ Sllv(dst_low, lhs_low, rhs_reg);
2449 __ Nor(AT, ZERO, rhs_reg);
2450 __ Srl(TMP, lhs_low, 1);
2451 __ Srlv(TMP, TMP, AT);
2452 __ Sllv(dst_high, lhs_high, rhs_reg);
2453 __ Or(dst_high, dst_high, TMP);
2454 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002455 if (isR6) {
2456 __ Beqzc(TMP, &done, /* is_bare */ true);
2457 __ Move(dst_high, dst_low);
2458 __ Move(dst_low, ZERO);
2459 } else {
2460 __ Movn(dst_high, dst_low, TMP);
2461 __ Movn(dst_low, ZERO, TMP);
2462 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002463 } else if (instr->IsShr()) {
2464 __ Srav(dst_high, lhs_high, rhs_reg);
2465 __ Nor(AT, ZERO, rhs_reg);
2466 __ Sll(TMP, lhs_high, 1);
2467 __ Sllv(TMP, TMP, AT);
2468 __ Srlv(dst_low, lhs_low, rhs_reg);
2469 __ Or(dst_low, dst_low, TMP);
2470 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002471 if (isR6) {
2472 __ Beqzc(TMP, &done, /* is_bare */ true);
2473 __ Move(dst_low, dst_high);
2474 __ Sra(dst_high, dst_high, 31);
2475 } else {
2476 __ Sra(AT, dst_high, 31);
2477 __ Movn(dst_low, dst_high, TMP);
2478 __ Movn(dst_high, AT, TMP);
2479 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002480 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002481 __ Srlv(dst_high, lhs_high, rhs_reg);
2482 __ Nor(AT, ZERO, rhs_reg);
2483 __ Sll(TMP, lhs_high, 1);
2484 __ Sllv(TMP, TMP, AT);
2485 __ Srlv(dst_low, lhs_low, rhs_reg);
2486 __ Or(dst_low, dst_low, TMP);
2487 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002488 if (isR6) {
2489 __ Beqzc(TMP, &done, /* is_bare */ true);
2490 __ Move(dst_low, dst_high);
2491 __ Move(dst_high, ZERO);
2492 } else {
2493 __ Movn(dst_low, dst_high, TMP);
2494 __ Movn(dst_high, ZERO, TMP);
2495 }
2496 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002497 __ Nor(AT, ZERO, rhs_reg);
2498 __ Srlv(TMP, lhs_low, rhs_reg);
2499 __ Sll(dst_low, lhs_high, 1);
2500 __ Sllv(dst_low, dst_low, AT);
2501 __ Or(dst_low, dst_low, TMP);
2502 __ Srlv(TMP, lhs_high, rhs_reg);
2503 __ Sll(dst_high, lhs_low, 1);
2504 __ Sllv(dst_high, dst_high, AT);
2505 __ Or(dst_high, dst_high, TMP);
2506 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002507 if (isR6) {
2508 __ Beqzc(TMP, &done, /* is_bare */ true);
2509 __ Move(TMP, dst_high);
2510 __ Move(dst_high, dst_low);
2511 __ Move(dst_low, TMP);
2512 } else {
2513 __ Movn(AT, dst_high, TMP);
2514 __ Movn(dst_high, dst_low, TMP);
2515 __ Movn(dst_low, AT, TMP);
2516 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002517 }
2518 __ Bind(&done);
2519 }
2520 break;
2521 }
2522
2523 default:
2524 LOG(FATAL) << "Unexpected shift operation type " << type;
2525 }
2526}
2527
2528void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2529 HandleBinaryOp(instruction);
2530}
2531
2532void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2533 HandleBinaryOp(instruction);
2534}
2535
2536void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2537 HandleBinaryOp(instruction);
2538}
2539
2540void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2541 HandleBinaryOp(instruction);
2542}
2543
2544void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002545 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002546 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002548 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002549 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2550 object_array_get_with_read_barrier
2551 ? LocationSummary::kCallOnSlowPath
2552 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002553 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2554 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2555 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 locations->SetInAt(0, Location::RequiresRegister());
2557 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002558 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2560 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002561 // The output overlaps in the case of an object array get with
2562 // read barriers enabled: we do not want the move to overwrite the
2563 // array's location, as we need it to emit the read barrier.
2564 locations->SetOut(Location::RequiresRegister(),
2565 object_array_get_with_read_barrier
2566 ? Location::kOutputOverlap
2567 : Location::kNoOutputOverlap);
2568 }
2569 // We need a temporary register for the read barrier marking slow
2570 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2571 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002572 bool temp_needed = instruction->GetIndex()->IsConstant()
2573 ? !kBakerReadBarrierThunksEnableForFields
2574 : !kBakerReadBarrierThunksEnableForArrays;
2575 if (temp_needed) {
2576 locations->AddTemp(Location::RequiresRegister());
2577 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002578 }
2579}
2580
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002581static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2582 auto null_checker = [codegen, instruction]() {
2583 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002584 };
2585 return null_checker;
2586}
2587
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002588void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2589 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002590 Location obj_loc = locations->InAt(0);
2591 Register obj = obj_loc.AsRegister<Register>();
2592 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002593 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002594 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002595 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002598 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2599 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002601 case DataType::Type::kBool:
2602 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002603 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 if (index.IsConstant()) {
2605 size_t offset =
2606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002607 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002608 } else {
2609 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002610 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 }
2612 break;
2613 }
2614
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002616 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 if (index.IsConstant()) {
2618 size_t offset =
2619 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002620 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002621 } else {
2622 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002623 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 }
2625 break;
2626 }
2627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002629 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002630 if (maybe_compressed_char_at) {
2631 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2632 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2633 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2634 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2635 "Expecting 0=compressed, 1=uncompressed");
2636 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002637 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002638 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2639 if (maybe_compressed_char_at) {
2640 MipsLabel uncompressed_load, done;
2641 __ Bnez(TMP, &uncompressed_load);
2642 __ LoadFromOffset(kLoadUnsignedByte,
2643 out,
2644 obj,
2645 data_offset + (const_index << TIMES_1));
2646 __ B(&done);
2647 __ Bind(&uncompressed_load);
2648 __ LoadFromOffset(kLoadUnsignedHalfword,
2649 out,
2650 obj,
2651 data_offset + (const_index << TIMES_2));
2652 __ Bind(&done);
2653 } else {
2654 __ LoadFromOffset(kLoadUnsignedHalfword,
2655 out,
2656 obj,
2657 data_offset + (const_index << TIMES_2),
2658 null_checker);
2659 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002660 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002661 Register index_reg = index.AsRegister<Register>();
2662 if (maybe_compressed_char_at) {
2663 MipsLabel uncompressed_load, done;
2664 __ Bnez(TMP, &uncompressed_load);
2665 __ Addu(TMP, obj, index_reg);
2666 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2667 __ B(&done);
2668 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002669 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002670 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2671 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002672 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2673 __ Addu(TMP, index_reg, obj);
2674 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002675 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002676 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002677 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679 }
2680 break;
2681 }
2682
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002683 case DataType::Type::kInt16: {
2684 Register out = out_loc.AsRegister<Register>();
2685 if (index.IsConstant()) {
2686 size_t offset =
2687 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2688 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002689 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2690 __ Addu(TMP, index.AsRegister<Register>(), obj);
2691 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002692 } else {
2693 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2694 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2695 }
2696 break;
2697 }
2698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002700 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002701 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002702 if (index.IsConstant()) {
2703 size_t offset =
2704 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002705 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002706 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2707 __ Addu(TMP, index.AsRegister<Register>(), obj);
2708 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002710 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002711 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002712 }
2713 break;
2714 }
2715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002717 static_assert(
2718 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2719 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2720 // /* HeapReference<Object> */ out =
2721 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2722 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002723 bool temp_needed = index.IsConstant()
2724 ? !kBakerReadBarrierThunksEnableForFields
2725 : !kBakerReadBarrierThunksEnableForArrays;
2726 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002727 // Note that a potential implicit null check is handled in this
2728 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002729 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2730 if (index.IsConstant()) {
2731 // Array load with a constant index can be treated as a field load.
2732 size_t offset =
2733 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2734 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2735 out_loc,
2736 obj,
2737 offset,
2738 temp,
2739 /* needs_null_check */ false);
2740 } else {
2741 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2742 out_loc,
2743 obj,
2744 data_offset,
2745 index,
2746 temp,
2747 /* needs_null_check */ false);
2748 }
Alexey Frunze15958152017-02-09 19:08:30 -08002749 } else {
2750 Register out = out_loc.AsRegister<Register>();
2751 if (index.IsConstant()) {
2752 size_t offset =
2753 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2754 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2755 // If read barriers are enabled, emit read barriers other than
2756 // Baker's using a slow path (and also unpoison the loaded
2757 // reference, if heap poisoning is enabled).
2758 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2759 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002760 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002761 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2762 // If read barriers are enabled, emit read barriers other than
2763 // Baker's using a slow path (and also unpoison the loaded
2764 // reference, if heap poisoning is enabled).
2765 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2766 out_loc,
2767 out_loc,
2768 obj_loc,
2769 data_offset,
2770 index);
2771 }
2772 }
2773 break;
2774 }
2775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002777 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002778 if (index.IsConstant()) {
2779 size_t offset =
2780 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002781 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002782 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2783 __ Addu(TMP, index.AsRegister<Register>(), obj);
2784 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002785 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002786 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002787 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 }
2789 break;
2790 }
2791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002793 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002794 if (index.IsConstant()) {
2795 size_t offset =
2796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002797 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002798 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2799 __ Addu(TMP, index.AsRegister<Register>(), obj);
2800 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002801 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002802 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002803 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002804 }
2805 break;
2806 }
2807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002809 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810 if (index.IsConstant()) {
2811 size_t offset =
2812 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002813 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002814 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2815 __ Addu(TMP, index.AsRegister<Register>(), obj);
2816 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002818 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002819 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 }
2821 break;
2822 }
2823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002825 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2826 UNREACHABLE();
2827 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828}
2829
2830void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002831 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002832 locations->SetInAt(0, Location::RequiresRegister());
2833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2834}
2835
2836void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2837 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002838 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839 Register obj = locations->InAt(0).AsRegister<Register>();
2840 Register out = locations->Out().AsRegister<Register>();
2841 __ LoadFromOffset(kLoadWord, out, obj, offset);
2842 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002843 // Mask out compression flag from String's array length.
2844 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2845 __ Srl(out, out, 1u);
2846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002847}
2848
Alexey Frunzef58b2482016-09-02 22:14:06 -07002849Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2850 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2851 ? Location::ConstantLocation(instruction->AsConstant())
2852 : Location::RequiresRegister();
2853}
2854
2855Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2856 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2857 // We can store a non-zero float or double constant without first loading it into the FPU,
2858 // but we should only prefer this if the constant has a single use.
2859 if (instruction->IsConstant() &&
2860 (instruction->AsConstant()->IsZeroBitPattern() ||
2861 instruction->GetUses().HasExactlyOneElement())) {
2862 return Location::ConstantLocation(instruction->AsConstant());
2863 // Otherwise fall through and require an FPU register for the constant.
2864 }
2865 return Location::RequiresFpuRegister();
2866}
2867
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002870
2871 bool needs_write_barrier =
2872 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2873 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2874
Vladimir Markoca6fff82017-10-03 14:49:14 +01002875 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002876 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002877 may_need_runtime_call_for_type_check ?
2878 LocationSummary::kCallOnSlowPath :
2879 LocationSummary::kNoCall);
2880
2881 locations->SetInAt(0, Location::RequiresRegister());
2882 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002884 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002885 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002886 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2887 }
2888 if (needs_write_barrier) {
2889 // Temporary register for the write barrier.
2890 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 }
2892}
2893
2894void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2895 LocationSummary* locations = instruction->GetLocations();
2896 Register obj = locations->InAt(0).AsRegister<Register>();
2897 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002898 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002900 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002901 bool needs_write_barrier =
2902 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002903 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002904 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905
2906 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002908 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002909 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002910 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002911 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002912 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002914 __ Addu(base_reg, obj, index.AsRegister<Register>());
2915 }
2916 if (value_location.IsConstant()) {
2917 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2918 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2919 } else {
2920 Register value = value_location.AsRegister<Register>();
2921 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002922 }
2923 break;
2924 }
2925
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002926 case DataType::Type::kUint16:
2927 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002930 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002931 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2932 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002934 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002935 }
2936 if (value_location.IsConstant()) {
2937 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2938 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2939 } else {
2940 Register value = value_location.AsRegister<Register>();
2941 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002942 }
2943 break;
2944 }
2945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2948 if (index.IsConstant()) {
2949 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002950 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2951 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002952 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002953 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002954 }
2955 if (value_location.IsConstant()) {
2956 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2957 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2958 } else {
2959 Register value = value_location.AsRegister<Register>();
2960 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2961 }
2962 break;
2963 }
2964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002966 if (value_location.IsConstant()) {
2967 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002969 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002970 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002971 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002972 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973 }
Alexey Frunze15958152017-02-09 19:08:30 -08002974 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2975 DCHECK_EQ(value, 0);
2976 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2977 DCHECK(!needs_write_barrier);
2978 DCHECK(!may_need_runtime_call_for_type_check);
2979 break;
2980 }
2981
2982 DCHECK(needs_write_barrier);
2983 Register value = value_location.AsRegister<Register>();
2984 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2985 Register temp2 = TMP; // Doesn't need to survive slow path.
2986 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2987 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2988 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2989 MipsLabel done;
2990 SlowPathCodeMIPS* slow_path = nullptr;
2991
2992 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002993 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002994 codegen_->AddSlowPath(slow_path);
2995 if (instruction->GetValueCanBeNull()) {
2996 MipsLabel non_zero;
2997 __ Bnez(value, &non_zero);
2998 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2999 if (index.IsConstant()) {
3000 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003001 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3002 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003003 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003004 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003005 }
Alexey Frunze15958152017-02-09 19:08:30 -08003006 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3007 __ B(&done);
3008 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003009 }
Alexey Frunze15958152017-02-09 19:08:30 -08003010
3011 // Note that when read barriers are enabled, the type checks
3012 // are performed without read barriers. This is fine, even in
3013 // the case where a class object is in the from-space after
3014 // the flip, as a comparison involving such a type would not
3015 // produce a false positive; it may of course produce a false
3016 // negative, in which case we would take the ArraySet slow
3017 // path.
3018
3019 // /* HeapReference<Class> */ temp1 = obj->klass_
3020 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3021 __ MaybeUnpoisonHeapReference(temp1);
3022
3023 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3024 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3025 // /* HeapReference<Class> */ temp2 = value->klass_
3026 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3027 // If heap poisoning is enabled, no need to unpoison `temp1`
3028 // nor `temp2`, as we are comparing two poisoned references.
3029
3030 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3031 MipsLabel do_put;
3032 __ Beq(temp1, temp2, &do_put);
3033 // If heap poisoning is enabled, the `temp1` reference has
3034 // not been unpoisoned yet; unpoison it now.
3035 __ MaybeUnpoisonHeapReference(temp1);
3036
3037 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3038 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3039 // If heap poisoning is enabled, no need to unpoison
3040 // `temp1`, as we are comparing against null below.
3041 __ Bnez(temp1, slow_path->GetEntryLabel());
3042 __ Bind(&do_put);
3043 } else {
3044 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3045 }
3046 }
3047
3048 Register source = value;
3049 if (kPoisonHeapReferences) {
3050 // Note that in the case where `value` is a null reference,
3051 // we do not enter this block, as a null reference does not
3052 // need poisoning.
3053 __ Move(temp1, value);
3054 __ PoisonHeapReference(temp1);
3055 source = temp1;
3056 }
3057
3058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3059 if (index.IsConstant()) {
3060 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003061 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003062 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003063 }
3064 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3065
3066 if (!may_need_runtime_call_for_type_check) {
3067 codegen_->MaybeRecordImplicitNullCheck(instruction);
3068 }
3069
3070 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3071
3072 if (done.IsLinked()) {
3073 __ Bind(&done);
3074 }
3075
3076 if (slow_path != nullptr) {
3077 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078 }
3079 break;
3080 }
3081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003082 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003085 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003086 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3087 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003088 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003089 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003090 }
3091 if (value_location.IsConstant()) {
3092 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3093 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3094 } else {
3095 Register value = value_location.AsRegisterPairLow<Register>();
3096 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 }
3098 break;
3099 }
3100
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003103 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003104 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003105 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3106 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003107 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003108 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003109 }
3110 if (value_location.IsConstant()) {
3111 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3112 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3113 } else {
3114 FRegister value = value_location.AsFpuRegister<FRegister>();
3115 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003116 }
3117 break;
3118 }
3119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003122 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003123 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003124 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3125 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003126 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003127 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003128 }
3129 if (value_location.IsConstant()) {
3130 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3131 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3132 } else {
3133 FRegister value = value_location.AsFpuRegister<FRegister>();
3134 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135 }
3136 break;
3137 }
3138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3141 UNREACHABLE();
3142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003143}
3144
Lena Djokica2901602017-09-21 13:50:52 +02003145void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3146 HIntermediateArrayAddressIndex* instruction) {
3147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003148 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003149
3150 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3151
3152 locations->SetInAt(0, Location::RequiresRegister());
3153 locations->SetInAt(1, Location::ConstantLocation(shift));
3154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3155}
3156
3157void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3158 HIntermediateArrayAddressIndex* instruction) {
3159 LocationSummary* locations = instruction->GetLocations();
3160 Register index_reg = locations->InAt(0).AsRegister<Register>();
3161 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3162 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3163}
3164
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003165void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003166 RegisterSet caller_saves = RegisterSet::Empty();
3167 InvokeRuntimeCallingConvention calling_convention;
3168 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3169 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3170 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003171
3172 HInstruction* index = instruction->InputAt(0);
3173 HInstruction* length = instruction->InputAt(1);
3174
3175 bool const_index = false;
3176 bool const_length = false;
3177
3178 if (index->IsConstant()) {
3179 if (length->IsConstant()) {
3180 const_index = true;
3181 const_length = true;
3182 } else {
3183 int32_t index_value = index->AsIntConstant()->GetValue();
3184 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3185 const_index = true;
3186 }
3187 }
3188 } else if (length->IsConstant()) {
3189 int32_t length_value = length->AsIntConstant()->GetValue();
3190 if (IsUint<15>(length_value)) {
3191 const_length = true;
3192 }
3193 }
3194
3195 locations->SetInAt(0, const_index
3196 ? Location::ConstantLocation(index->AsConstant())
3197 : Location::RequiresRegister());
3198 locations->SetInAt(1, const_length
3199 ? Location::ConstantLocation(length->AsConstant())
3200 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003201}
3202
3203void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3204 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003205 Location index_loc = locations->InAt(0);
3206 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003207
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003208 if (length_loc.IsConstant()) {
3209 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3210 if (index_loc.IsConstant()) {
3211 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3212 if (index < 0 || index >= length) {
3213 BoundsCheckSlowPathMIPS* slow_path =
3214 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3215 codegen_->AddSlowPath(slow_path);
3216 __ B(slow_path->GetEntryLabel());
3217 } else {
3218 // Nothing to be done.
3219 }
3220 return;
3221 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003222
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003223 BoundsCheckSlowPathMIPS* slow_path =
3224 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3225 codegen_->AddSlowPath(slow_path);
3226 Register index = index_loc.AsRegister<Register>();
3227 if (length == 0) {
3228 __ B(slow_path->GetEntryLabel());
3229 } else if (length == 1) {
3230 __ Bnez(index, slow_path->GetEntryLabel());
3231 } else {
3232 DCHECK(IsUint<15>(length)) << length;
3233 __ Sltiu(TMP, index, length);
3234 __ Beqz(TMP, slow_path->GetEntryLabel());
3235 }
3236 } else {
3237 Register length = length_loc.AsRegister<Register>();
3238 BoundsCheckSlowPathMIPS* slow_path =
3239 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3240 codegen_->AddSlowPath(slow_path);
3241 if (index_loc.IsConstant()) {
3242 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3243 if (index < 0) {
3244 __ B(slow_path->GetEntryLabel());
3245 } else if (index == 0) {
3246 __ Blez(length, slow_path->GetEntryLabel());
3247 } else {
3248 DCHECK(IsInt<16>(index + 1)) << index;
3249 __ Sltiu(TMP, length, index + 1);
3250 __ Bnez(TMP, slow_path->GetEntryLabel());
3251 }
3252 } else {
3253 Register index = index_loc.AsRegister<Register>();
3254 __ Bgeu(index, length, slow_path->GetEntryLabel());
3255 }
3256 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003257}
3258
Alexey Frunze15958152017-02-09 19:08:30 -08003259// Temp is used for read barrier.
3260static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3261 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003262 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003263 (kUseBakerReadBarrier ||
3264 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3265 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3266 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3267 return 1;
3268 }
3269 return 0;
3270}
3271
3272// Extra temp is used for read barrier.
3273static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3274 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3275}
3276
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003277void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003278 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3279 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3280
3281 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3282 switch (type_check_kind) {
3283 case TypeCheckKind::kExactCheck:
3284 case TypeCheckKind::kAbstractClassCheck:
3285 case TypeCheckKind::kClassHierarchyCheck:
3286 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003287 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003288 ? LocationSummary::kCallOnSlowPath
3289 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3290 break;
3291 case TypeCheckKind::kArrayCheck:
3292 case TypeCheckKind::kUnresolvedCheck:
3293 case TypeCheckKind::kInterfaceCheck:
3294 call_kind = LocationSummary::kCallOnSlowPath;
3295 break;
3296 }
3297
Vladimir Markoca6fff82017-10-03 14:49:14 +01003298 LocationSummary* locations =
3299 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003301 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003302 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003303}
3304
3305void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003306 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003307 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003308 Location obj_loc = locations->InAt(0);
3309 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003310 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003311 Location temp_loc = locations->GetTemp(0);
3312 Register temp = temp_loc.AsRegister<Register>();
3313 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3314 DCHECK_LE(num_temps, 2u);
3315 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003316 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3317 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3318 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3319 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3320 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3321 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3322 const uint32_t object_array_data_offset =
3323 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3324 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003325
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003326 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3327 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3328 // read barriers is done for performance and code size reasons.
3329 bool is_type_check_slow_path_fatal = false;
3330 if (!kEmitCompilerReadBarrier) {
3331 is_type_check_slow_path_fatal =
3332 (type_check_kind == TypeCheckKind::kExactCheck ||
3333 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3334 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3335 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3336 !instruction->CanThrowIntoCatchBlock();
3337 }
3338 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003339 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3340 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003341 codegen_->AddSlowPath(slow_path);
3342
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003343 // Avoid this check if we know `obj` is not null.
3344 if (instruction->MustDoNullCheck()) {
3345 __ Beqz(obj, &done);
3346 }
3347
3348 switch (type_check_kind) {
3349 case TypeCheckKind::kExactCheck:
3350 case TypeCheckKind::kArrayCheck: {
3351 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003352 GenerateReferenceLoadTwoRegisters(instruction,
3353 temp_loc,
3354 obj_loc,
3355 class_offset,
3356 maybe_temp2_loc,
3357 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003358 // Jump to slow path for throwing the exception or doing a
3359 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003360 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003361 break;
3362 }
3363
3364 case TypeCheckKind::kAbstractClassCheck: {
3365 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003366 GenerateReferenceLoadTwoRegisters(instruction,
3367 temp_loc,
3368 obj_loc,
3369 class_offset,
3370 maybe_temp2_loc,
3371 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003372 // If the class is abstract, we eagerly fetch the super class of the
3373 // object to avoid doing a comparison we know will fail.
3374 MipsLabel loop;
3375 __ Bind(&loop);
3376 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003377 GenerateReferenceLoadOneRegister(instruction,
3378 temp_loc,
3379 super_offset,
3380 maybe_temp2_loc,
3381 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003382 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3383 // exception.
3384 __ Beqz(temp, slow_path->GetEntryLabel());
3385 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003386 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003387 break;
3388 }
3389
3390 case TypeCheckKind::kClassHierarchyCheck: {
3391 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003392 GenerateReferenceLoadTwoRegisters(instruction,
3393 temp_loc,
3394 obj_loc,
3395 class_offset,
3396 maybe_temp2_loc,
3397 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003398 // Walk over the class hierarchy to find a match.
3399 MipsLabel loop;
3400 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003401 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003402 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003403 GenerateReferenceLoadOneRegister(instruction,
3404 temp_loc,
3405 super_offset,
3406 maybe_temp2_loc,
3407 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003408 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3409 // exception. Otherwise, jump to the beginning of the loop.
3410 __ Bnez(temp, &loop);
3411 __ B(slow_path->GetEntryLabel());
3412 break;
3413 }
3414
3415 case TypeCheckKind::kArrayObjectCheck: {
3416 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003417 GenerateReferenceLoadTwoRegisters(instruction,
3418 temp_loc,
3419 obj_loc,
3420 class_offset,
3421 maybe_temp2_loc,
3422 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003423 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003424 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003425 // Otherwise, we need to check that the object's class is a non-primitive array.
3426 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003427 GenerateReferenceLoadOneRegister(instruction,
3428 temp_loc,
3429 component_offset,
3430 maybe_temp2_loc,
3431 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003432 // If the component type is null, jump to the slow path to throw the exception.
3433 __ Beqz(temp, slow_path->GetEntryLabel());
3434 // Otherwise, the object is indeed an array, further check that this component
3435 // type is not a primitive type.
3436 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3437 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3438 __ Bnez(temp, slow_path->GetEntryLabel());
3439 break;
3440 }
3441
3442 case TypeCheckKind::kUnresolvedCheck:
3443 // We always go into the type check slow path for the unresolved check case.
3444 // We cannot directly call the CheckCast runtime entry point
3445 // without resorting to a type checking slow path here (i.e. by
3446 // calling InvokeRuntime directly), as it would require to
3447 // assign fixed registers for the inputs of this HInstanceOf
3448 // instruction (following the runtime calling convention), which
3449 // might be cluttered by the potential first read barrier
3450 // emission at the beginning of this method.
3451 __ B(slow_path->GetEntryLabel());
3452 break;
3453
3454 case TypeCheckKind::kInterfaceCheck: {
3455 // Avoid read barriers to improve performance of the fast path. We can not get false
3456 // positives by doing this.
3457 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003458 GenerateReferenceLoadTwoRegisters(instruction,
3459 temp_loc,
3460 obj_loc,
3461 class_offset,
3462 maybe_temp2_loc,
3463 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003464 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003465 GenerateReferenceLoadTwoRegisters(instruction,
3466 temp_loc,
3467 temp_loc,
3468 iftable_offset,
3469 maybe_temp2_loc,
3470 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003471 // Iftable is never null.
3472 __ Lw(TMP, temp, array_length_offset);
3473 // Loop through the iftable and check if any class matches.
3474 MipsLabel loop;
3475 __ Bind(&loop);
3476 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3477 __ Beqz(TMP, slow_path->GetEntryLabel());
3478 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3479 __ MaybeUnpoisonHeapReference(AT);
3480 // Go to next interface.
3481 __ Addiu(TMP, TMP, -2);
3482 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003483 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003484 break;
3485 }
3486 }
3487
3488 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003489 __ Bind(slow_path->GetExitLabel());
3490}
3491
3492void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3493 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003494 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003495 locations->SetInAt(0, Location::RequiresRegister());
3496 if (check->HasUses()) {
3497 locations->SetOut(Location::SameAsFirstInput());
3498 }
3499}
3500
3501void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3502 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003503 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003504 check->GetLoadClass(),
3505 check,
3506 check->GetDexPc(),
3507 true);
3508 codegen_->AddSlowPath(slow_path);
3509 GenerateClassInitializationCheck(slow_path,
3510 check->GetLocations()->InAt(0).AsRegister<Register>());
3511}
3512
3513void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003515
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003517 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003518
3519 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003520 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003521 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003522 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003524 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003526 locations->SetInAt(0, Location::RequiresRegister());
3527 locations->SetInAt(1, Location::RequiresRegister());
3528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3529 break;
3530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003531 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003532 locations->SetInAt(0, Location::RequiresRegister());
3533 locations->SetInAt(1, Location::RequiresRegister());
3534 // Output overlaps because it is written before doing the low comparison.
3535 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3536 break;
3537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003538 case DataType::Type::kFloat32:
3539 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003540 locations->SetInAt(0, Location::RequiresFpuRegister());
3541 locations->SetInAt(1, Location::RequiresFpuRegister());
3542 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003543 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003544
3545 default:
3546 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3547 }
3548}
3549
3550void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3551 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003552 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003553 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003554 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003555
3556 // 0 if: left == right
3557 // 1 if: left > right
3558 // -1 if: left < right
3559 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003560 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003561 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003563 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003564 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003565 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003566 Register lhs = locations->InAt(0).AsRegister<Register>();
3567 Register rhs = locations->InAt(1).AsRegister<Register>();
3568 __ Slt(TMP, lhs, rhs);
3569 __ Slt(res, rhs, lhs);
3570 __ Subu(res, res, TMP);
3571 break;
3572 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003573 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003574 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003575 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3576 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3577 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3578 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3579 // TODO: more efficient (direct) comparison with a constant.
3580 __ Slt(TMP, lhs_high, rhs_high);
3581 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3582 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3583 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3584 __ Sltu(TMP, lhs_low, rhs_low);
3585 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3586 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3587 __ Bind(&done);
3588 break;
3589 }
3590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003591 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003592 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003593 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3594 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3595 MipsLabel done;
3596 if (isR6) {
3597 __ CmpEqS(FTMP, lhs, rhs);
3598 __ LoadConst32(res, 0);
3599 __ Bc1nez(FTMP, &done);
3600 if (gt_bias) {
3601 __ CmpLtS(FTMP, lhs, rhs);
3602 __ LoadConst32(res, -1);
3603 __ Bc1nez(FTMP, &done);
3604 __ LoadConst32(res, 1);
3605 } else {
3606 __ CmpLtS(FTMP, rhs, lhs);
3607 __ LoadConst32(res, 1);
3608 __ Bc1nez(FTMP, &done);
3609 __ LoadConst32(res, -1);
3610 }
3611 } else {
3612 if (gt_bias) {
3613 __ ColtS(0, lhs, rhs);
3614 __ LoadConst32(res, -1);
3615 __ Bc1t(0, &done);
3616 __ CeqS(0, lhs, rhs);
3617 __ LoadConst32(res, 1);
3618 __ Movt(res, ZERO, 0);
3619 } else {
3620 __ ColtS(0, rhs, lhs);
3621 __ LoadConst32(res, 1);
3622 __ Bc1t(0, &done);
3623 __ CeqS(0, lhs, rhs);
3624 __ LoadConst32(res, -1);
3625 __ Movt(res, ZERO, 0);
3626 }
3627 }
3628 __ Bind(&done);
3629 break;
3630 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003632 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003633 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3634 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3635 MipsLabel done;
3636 if (isR6) {
3637 __ CmpEqD(FTMP, lhs, rhs);
3638 __ LoadConst32(res, 0);
3639 __ Bc1nez(FTMP, &done);
3640 if (gt_bias) {
3641 __ CmpLtD(FTMP, lhs, rhs);
3642 __ LoadConst32(res, -1);
3643 __ Bc1nez(FTMP, &done);
3644 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003645 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003646 __ CmpLtD(FTMP, rhs, lhs);
3647 __ LoadConst32(res, 1);
3648 __ Bc1nez(FTMP, &done);
3649 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650 }
3651 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003652 if (gt_bias) {
3653 __ ColtD(0, lhs, rhs);
3654 __ LoadConst32(res, -1);
3655 __ Bc1t(0, &done);
3656 __ CeqD(0, lhs, rhs);
3657 __ LoadConst32(res, 1);
3658 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003659 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003660 __ ColtD(0, rhs, lhs);
3661 __ LoadConst32(res, 1);
3662 __ Bc1t(0, &done);
3663 __ CeqD(0, lhs, rhs);
3664 __ LoadConst32(res, -1);
3665 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 }
3667 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003668 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003669 break;
3670 }
3671
3672 default:
3673 LOG(FATAL) << "Unimplemented compare type " << in_type;
3674 }
3675}
3676
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003677void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003678 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003679 switch (instruction->InputAt(0)->GetType()) {
3680 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003681 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003682 locations->SetInAt(0, Location::RequiresRegister());
3683 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3684 break;
3685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kFloat32:
3687 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003688 locations->SetInAt(0, Location::RequiresFpuRegister());
3689 locations->SetInAt(1, Location::RequiresFpuRegister());
3690 break;
3691 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003692 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3694 }
3695}
3696
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003697void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003698 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003699 return;
3700 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003702 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003703 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003705 switch (type) {
3706 default:
3707 // Integer case.
3708 GenerateIntCompare(instruction->GetCondition(), locations);
3709 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003712 GenerateLongCompare(instruction->GetCondition(), locations);
3713 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003715 case DataType::Type::kFloat32:
3716 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003717 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3718 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003719 }
3720}
3721
Alexey Frunze7e99e052015-11-24 19:28:01 -08003722void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3723 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003724 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003725
3726 LocationSummary* locations = instruction->GetLocations();
3727 Location second = locations->InAt(1);
3728 DCHECK(second.IsConstant());
3729
3730 Register out = locations->Out().AsRegister<Register>();
3731 Register dividend = locations->InAt(0).AsRegister<Register>();
3732 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3733 DCHECK(imm == 1 || imm == -1);
3734
3735 if (instruction->IsRem()) {
3736 __ Move(out, ZERO);
3737 } else {
3738 if (imm == -1) {
3739 __ Subu(out, ZERO, dividend);
3740 } else if (out != dividend) {
3741 __ Move(out, dividend);
3742 }
3743 }
3744}
3745
3746void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3747 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003749
3750 LocationSummary* locations = instruction->GetLocations();
3751 Location second = locations->InAt(1);
3752 DCHECK(second.IsConstant());
3753
3754 Register out = locations->Out().AsRegister<Register>();
3755 Register dividend = locations->InAt(0).AsRegister<Register>();
3756 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003757 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003758 int ctz_imm = CTZ(abs_imm);
3759
3760 if (instruction->IsDiv()) {
3761 if (ctz_imm == 1) {
3762 // Fast path for division by +/-2, which is very common.
3763 __ Srl(TMP, dividend, 31);
3764 } else {
3765 __ Sra(TMP, dividend, 31);
3766 __ Srl(TMP, TMP, 32 - ctz_imm);
3767 }
3768 __ Addu(out, dividend, TMP);
3769 __ Sra(out, out, ctz_imm);
3770 if (imm < 0) {
3771 __ Subu(out, ZERO, out);
3772 }
3773 } else {
3774 if (ctz_imm == 1) {
3775 // Fast path for modulo +/-2, which is very common.
3776 __ Sra(TMP, dividend, 31);
3777 __ Subu(out, dividend, TMP);
3778 __ Andi(out, out, 1);
3779 __ Addu(out, out, TMP);
3780 } else {
3781 __ Sra(TMP, dividend, 31);
3782 __ Srl(TMP, TMP, 32 - ctz_imm);
3783 __ Addu(out, dividend, TMP);
3784 if (IsUint<16>(abs_imm - 1)) {
3785 __ Andi(out, out, abs_imm - 1);
3786 } else {
Lena Djokica556e6b2017-12-13 12:09:42 +01003787 if (codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2()) {
3788 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3789 } else {
3790 __ Sll(out, out, 32 - ctz_imm);
3791 __ Srl(out, out, 32 - ctz_imm);
3792 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003793 }
3794 __ Subu(out, out, TMP);
3795 }
3796 }
3797}
3798
3799void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3800 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003802
3803 LocationSummary* locations = instruction->GetLocations();
3804 Location second = locations->InAt(1);
3805 DCHECK(second.IsConstant());
3806
3807 Register out = locations->Out().AsRegister<Register>();
3808 Register dividend = locations->InAt(0).AsRegister<Register>();
3809 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3810
3811 int64_t magic;
3812 int shift;
3813 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3814
3815 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3816
3817 __ LoadConst32(TMP, magic);
3818 if (isR6) {
3819 __ MuhR6(TMP, dividend, TMP);
3820 } else {
3821 __ MultR2(dividend, TMP);
3822 __ Mfhi(TMP);
3823 }
3824 if (imm > 0 && magic < 0) {
3825 __ Addu(TMP, TMP, dividend);
3826 } else if (imm < 0 && magic > 0) {
3827 __ Subu(TMP, TMP, dividend);
3828 }
3829
3830 if (shift != 0) {
3831 __ Sra(TMP, TMP, shift);
3832 }
3833
3834 if (instruction->IsDiv()) {
3835 __ Sra(out, TMP, 31);
3836 __ Subu(out, TMP, out);
3837 } else {
3838 __ Sra(AT, TMP, 31);
3839 __ Subu(AT, TMP, AT);
3840 __ LoadConst32(TMP, imm);
3841 if (isR6) {
3842 __ MulR6(TMP, AT, TMP);
3843 } else {
3844 __ MulR2(TMP, AT, TMP);
3845 }
3846 __ Subu(out, dividend, TMP);
3847 }
3848}
3849
3850void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3851 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003853
3854 LocationSummary* locations = instruction->GetLocations();
3855 Register out = locations->Out().AsRegister<Register>();
3856 Location second = locations->InAt(1);
3857
3858 if (second.IsConstant()) {
3859 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3860 if (imm == 0) {
3861 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3862 } else if (imm == 1 || imm == -1) {
3863 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003864 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003865 DivRemByPowerOfTwo(instruction);
3866 } else {
3867 DCHECK(imm <= -2 || imm >= 2);
3868 GenerateDivRemWithAnyConstant(instruction);
3869 }
3870 } else {
3871 Register dividend = locations->InAt(0).AsRegister<Register>();
3872 Register divisor = second.AsRegister<Register>();
3873 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3874 if (instruction->IsDiv()) {
3875 if (isR6) {
3876 __ DivR6(out, dividend, divisor);
3877 } else {
3878 __ DivR2(out, dividend, divisor);
3879 }
3880 } else {
3881 if (isR6) {
3882 __ ModR6(out, dividend, divisor);
3883 } else {
3884 __ ModR2(out, dividend, divisor);
3885 }
3886 }
3887 }
3888}
3889
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003890void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 DataType::Type type = div->GetResultType();
3892 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003893 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003894 : LocationSummary::kNoCall;
3895
Vladimir Markoca6fff82017-10-03 14:49:14 +01003896 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003897
3898 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003900 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003901 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3903 break;
3904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003905 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003906 InvokeRuntimeCallingConvention calling_convention;
3907 locations->SetInAt(0, Location::RegisterPairLocation(
3908 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3909 locations->SetInAt(1, Location::RegisterPairLocation(
3910 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3911 locations->SetOut(calling_convention.GetReturnLocation(type));
3912 break;
3913 }
3914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003915 case DataType::Type::kFloat32:
3916 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003917 locations->SetInAt(0, Location::RequiresFpuRegister());
3918 locations->SetInAt(1, Location::RequiresFpuRegister());
3919 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3920 break;
3921
3922 default:
3923 LOG(FATAL) << "Unexpected div type " << type;
3924 }
3925}
3926
3927void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003929 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003930
3931 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003932 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003933 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003935 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003936 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003937 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3938 break;
3939 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003940 case DataType::Type::kFloat32:
3941 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003942 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3943 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3944 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946 __ DivS(dst, lhs, rhs);
3947 } else {
3948 __ DivD(dst, lhs, rhs);
3949 }
3950 break;
3951 }
3952 default:
3953 LOG(FATAL) << "Unexpected div type " << type;
3954 }
3955}
3956
3957void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003958 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003959 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003960}
3961
3962void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003963 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003964 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003965 codegen_->AddSlowPath(slow_path);
3966 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003967 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003968
3969 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003971 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003972 case DataType::Type::kInt8:
3973 case DataType::Type::kUint16:
3974 case DataType::Type::kInt16:
3975 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003976 if (value.IsConstant()) {
3977 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3978 __ B(slow_path->GetEntryLabel());
3979 } else {
3980 // A division by a non-null constant is valid. We don't need to perform
3981 // any check, so simply fall through.
3982 }
3983 } else {
3984 DCHECK(value.IsRegister()) << value;
3985 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3986 }
3987 break;
3988 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003989 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003990 if (value.IsConstant()) {
3991 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3992 __ B(slow_path->GetEntryLabel());
3993 } else {
3994 // A division by a non-null constant is valid. We don't need to perform
3995 // any check, so simply fall through.
3996 }
3997 } else {
3998 DCHECK(value.IsRegisterPair()) << value;
3999 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4000 __ Beqz(TMP, slow_path->GetEntryLabel());
4001 }
4002 break;
4003 }
4004 default:
4005 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4006 }
4007}
4008
4009void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4010 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004011 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004012 locations->SetOut(Location::ConstantLocation(constant));
4013}
4014
4015void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4016 // Will be generated at use site.
4017}
4018
4019void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4020 exit->SetLocations(nullptr);
4021}
4022
4023void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4024}
4025
4026void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4027 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004028 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004029 locations->SetOut(Location::ConstantLocation(constant));
4030}
4031
4032void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4033 // Will be generated at use site.
4034}
4035
4036void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4037 got->SetLocations(nullptr);
4038}
4039
4040void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004041 if (successor->IsExitBlock()) {
4042 DCHECK(got->GetPrevious()->AlwaysThrows());
4043 return; // no code needed
4044 }
4045
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004046 HBasicBlock* block = got->GetBlock();
4047 HInstruction* previous = got->GetPrevious();
4048 HLoopInformation* info = block->GetLoopInformation();
4049
4050 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004051 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4052 return;
4053 }
4054 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4055 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4056 }
4057 if (!codegen_->GoesToNextBlock(block, successor)) {
4058 __ B(codegen_->GetLabelOf(successor));
4059 }
4060}
4061
4062void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4063 HandleGoto(got, got->GetSuccessor());
4064}
4065
4066void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4067 try_boundary->SetLocations(nullptr);
4068}
4069
4070void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4071 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4072 if (!successor->IsExitBlock()) {
4073 HandleGoto(try_boundary, successor);
4074 }
4075}
4076
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004077void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4078 LocationSummary* locations) {
4079 Register dst = locations->Out().AsRegister<Register>();
4080 Register lhs = locations->InAt(0).AsRegister<Register>();
4081 Location rhs_location = locations->InAt(1);
4082 Register rhs_reg = ZERO;
4083 int64_t rhs_imm = 0;
4084 bool use_imm = rhs_location.IsConstant();
4085 if (use_imm) {
4086 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4087 } else {
4088 rhs_reg = rhs_location.AsRegister<Register>();
4089 }
4090
4091 switch (cond) {
4092 case kCondEQ:
4093 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004094 if (use_imm && IsInt<16>(-rhs_imm)) {
4095 if (rhs_imm == 0) {
4096 if (cond == kCondEQ) {
4097 __ Sltiu(dst, lhs, 1);
4098 } else {
4099 __ Sltu(dst, ZERO, lhs);
4100 }
4101 } else {
4102 __ Addiu(dst, lhs, -rhs_imm);
4103 if (cond == kCondEQ) {
4104 __ Sltiu(dst, dst, 1);
4105 } else {
4106 __ Sltu(dst, ZERO, dst);
4107 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004108 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004109 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004110 if (use_imm && IsUint<16>(rhs_imm)) {
4111 __ Xori(dst, lhs, rhs_imm);
4112 } else {
4113 if (use_imm) {
4114 rhs_reg = TMP;
4115 __ LoadConst32(rhs_reg, rhs_imm);
4116 }
4117 __ Xor(dst, lhs, rhs_reg);
4118 }
4119 if (cond == kCondEQ) {
4120 __ Sltiu(dst, dst, 1);
4121 } else {
4122 __ Sltu(dst, ZERO, dst);
4123 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004124 }
4125 break;
4126
4127 case kCondLT:
4128 case kCondGE:
4129 if (use_imm && IsInt<16>(rhs_imm)) {
4130 __ Slti(dst, lhs, rhs_imm);
4131 } else {
4132 if (use_imm) {
4133 rhs_reg = TMP;
4134 __ LoadConst32(rhs_reg, rhs_imm);
4135 }
4136 __ Slt(dst, lhs, rhs_reg);
4137 }
4138 if (cond == kCondGE) {
4139 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4140 // only the slt instruction but no sge.
4141 __ Xori(dst, dst, 1);
4142 }
4143 break;
4144
4145 case kCondLE:
4146 case kCondGT:
4147 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4148 // Simulate lhs <= rhs via lhs < rhs + 1.
4149 __ Slti(dst, lhs, rhs_imm + 1);
4150 if (cond == kCondGT) {
4151 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4152 // only the slti instruction but no sgti.
4153 __ Xori(dst, dst, 1);
4154 }
4155 } else {
4156 if (use_imm) {
4157 rhs_reg = TMP;
4158 __ LoadConst32(rhs_reg, rhs_imm);
4159 }
4160 __ Slt(dst, rhs_reg, lhs);
4161 if (cond == kCondLE) {
4162 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4163 // only the slt instruction but no sle.
4164 __ Xori(dst, dst, 1);
4165 }
4166 }
4167 break;
4168
4169 case kCondB:
4170 case kCondAE:
4171 if (use_imm && IsInt<16>(rhs_imm)) {
4172 // Sltiu sign-extends its 16-bit immediate operand before
4173 // the comparison and thus lets us compare directly with
4174 // unsigned values in the ranges [0, 0x7fff] and
4175 // [0xffff8000, 0xffffffff].
4176 __ Sltiu(dst, lhs, rhs_imm);
4177 } else {
4178 if (use_imm) {
4179 rhs_reg = TMP;
4180 __ LoadConst32(rhs_reg, rhs_imm);
4181 }
4182 __ Sltu(dst, lhs, rhs_reg);
4183 }
4184 if (cond == kCondAE) {
4185 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4186 // only the sltu instruction but no sgeu.
4187 __ Xori(dst, dst, 1);
4188 }
4189 break;
4190
4191 case kCondBE:
4192 case kCondA:
4193 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4194 // Simulate lhs <= rhs via lhs < rhs + 1.
4195 // Note that this only works if rhs + 1 does not overflow
4196 // to 0, hence the check above.
4197 // Sltiu sign-extends its 16-bit immediate operand before
4198 // the comparison and thus lets us compare directly with
4199 // unsigned values in the ranges [0, 0x7fff] and
4200 // [0xffff8000, 0xffffffff].
4201 __ Sltiu(dst, lhs, rhs_imm + 1);
4202 if (cond == kCondA) {
4203 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4204 // only the sltiu instruction but no sgtiu.
4205 __ Xori(dst, dst, 1);
4206 }
4207 } else {
4208 if (use_imm) {
4209 rhs_reg = TMP;
4210 __ LoadConst32(rhs_reg, rhs_imm);
4211 }
4212 __ Sltu(dst, rhs_reg, lhs);
4213 if (cond == kCondBE) {
4214 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4215 // only the sltu instruction but no sleu.
4216 __ Xori(dst, dst, 1);
4217 }
4218 }
4219 break;
4220 }
4221}
4222
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004223bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4224 LocationSummary* input_locations,
4225 Register dst) {
4226 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4227 Location rhs_location = input_locations->InAt(1);
4228 Register rhs_reg = ZERO;
4229 int64_t rhs_imm = 0;
4230 bool use_imm = rhs_location.IsConstant();
4231 if (use_imm) {
4232 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4233 } else {
4234 rhs_reg = rhs_location.AsRegister<Register>();
4235 }
4236
4237 switch (cond) {
4238 case kCondEQ:
4239 case kCondNE:
4240 if (use_imm && IsInt<16>(-rhs_imm)) {
4241 __ Addiu(dst, lhs, -rhs_imm);
4242 } else if (use_imm && IsUint<16>(rhs_imm)) {
4243 __ Xori(dst, lhs, rhs_imm);
4244 } else {
4245 if (use_imm) {
4246 rhs_reg = TMP;
4247 __ LoadConst32(rhs_reg, rhs_imm);
4248 }
4249 __ Xor(dst, lhs, rhs_reg);
4250 }
4251 return (cond == kCondEQ);
4252
4253 case kCondLT:
4254 case kCondGE:
4255 if (use_imm && IsInt<16>(rhs_imm)) {
4256 __ Slti(dst, lhs, rhs_imm);
4257 } else {
4258 if (use_imm) {
4259 rhs_reg = TMP;
4260 __ LoadConst32(rhs_reg, rhs_imm);
4261 }
4262 __ Slt(dst, lhs, rhs_reg);
4263 }
4264 return (cond == kCondGE);
4265
4266 case kCondLE:
4267 case kCondGT:
4268 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4269 // Simulate lhs <= rhs via lhs < rhs + 1.
4270 __ Slti(dst, lhs, rhs_imm + 1);
4271 return (cond == kCondGT);
4272 } else {
4273 if (use_imm) {
4274 rhs_reg = TMP;
4275 __ LoadConst32(rhs_reg, rhs_imm);
4276 }
4277 __ Slt(dst, rhs_reg, lhs);
4278 return (cond == kCondLE);
4279 }
4280
4281 case kCondB:
4282 case kCondAE:
4283 if (use_imm && IsInt<16>(rhs_imm)) {
4284 // Sltiu sign-extends its 16-bit immediate operand before
4285 // the comparison and thus lets us compare directly with
4286 // unsigned values in the ranges [0, 0x7fff] and
4287 // [0xffff8000, 0xffffffff].
4288 __ Sltiu(dst, lhs, rhs_imm);
4289 } else {
4290 if (use_imm) {
4291 rhs_reg = TMP;
4292 __ LoadConst32(rhs_reg, rhs_imm);
4293 }
4294 __ Sltu(dst, lhs, rhs_reg);
4295 }
4296 return (cond == kCondAE);
4297
4298 case kCondBE:
4299 case kCondA:
4300 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4301 // Simulate lhs <= rhs via lhs < rhs + 1.
4302 // Note that this only works if rhs + 1 does not overflow
4303 // to 0, hence the check above.
4304 // Sltiu sign-extends its 16-bit immediate operand before
4305 // the comparison and thus lets us compare directly with
4306 // unsigned values in the ranges [0, 0x7fff] and
4307 // [0xffff8000, 0xffffffff].
4308 __ Sltiu(dst, lhs, rhs_imm + 1);
4309 return (cond == kCondA);
4310 } else {
4311 if (use_imm) {
4312 rhs_reg = TMP;
4313 __ LoadConst32(rhs_reg, rhs_imm);
4314 }
4315 __ Sltu(dst, rhs_reg, lhs);
4316 return (cond == kCondBE);
4317 }
4318 }
4319}
4320
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004321void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4322 LocationSummary* locations,
4323 MipsLabel* label) {
4324 Register lhs = locations->InAt(0).AsRegister<Register>();
4325 Location rhs_location = locations->InAt(1);
4326 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004327 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004328 bool use_imm = rhs_location.IsConstant();
4329 if (use_imm) {
4330 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4331 } else {
4332 rhs_reg = rhs_location.AsRegister<Register>();
4333 }
4334
4335 if (use_imm && rhs_imm == 0) {
4336 switch (cond) {
4337 case kCondEQ:
4338 case kCondBE: // <= 0 if zero
4339 __ Beqz(lhs, label);
4340 break;
4341 case kCondNE:
4342 case kCondA: // > 0 if non-zero
4343 __ Bnez(lhs, label);
4344 break;
4345 case kCondLT:
4346 __ Bltz(lhs, label);
4347 break;
4348 case kCondGE:
4349 __ Bgez(lhs, label);
4350 break;
4351 case kCondLE:
4352 __ Blez(lhs, label);
4353 break;
4354 case kCondGT:
4355 __ Bgtz(lhs, label);
4356 break;
4357 case kCondB: // always false
4358 break;
4359 case kCondAE: // always true
4360 __ B(label);
4361 break;
4362 }
4363 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004364 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4365 if (isR6 || !use_imm) {
4366 if (use_imm) {
4367 rhs_reg = TMP;
4368 __ LoadConst32(rhs_reg, rhs_imm);
4369 }
4370 switch (cond) {
4371 case kCondEQ:
4372 __ Beq(lhs, rhs_reg, label);
4373 break;
4374 case kCondNE:
4375 __ Bne(lhs, rhs_reg, label);
4376 break;
4377 case kCondLT:
4378 __ Blt(lhs, rhs_reg, label);
4379 break;
4380 case kCondGE:
4381 __ Bge(lhs, rhs_reg, label);
4382 break;
4383 case kCondLE:
4384 __ Bge(rhs_reg, lhs, label);
4385 break;
4386 case kCondGT:
4387 __ Blt(rhs_reg, lhs, label);
4388 break;
4389 case kCondB:
4390 __ Bltu(lhs, rhs_reg, label);
4391 break;
4392 case kCondAE:
4393 __ Bgeu(lhs, rhs_reg, label);
4394 break;
4395 case kCondBE:
4396 __ Bgeu(rhs_reg, lhs, label);
4397 break;
4398 case kCondA:
4399 __ Bltu(rhs_reg, lhs, label);
4400 break;
4401 }
4402 } else {
4403 // Special cases for more efficient comparison with constants on R2.
4404 switch (cond) {
4405 case kCondEQ:
4406 __ LoadConst32(TMP, rhs_imm);
4407 __ Beq(lhs, TMP, label);
4408 break;
4409 case kCondNE:
4410 __ LoadConst32(TMP, rhs_imm);
4411 __ Bne(lhs, TMP, label);
4412 break;
4413 case kCondLT:
4414 if (IsInt<16>(rhs_imm)) {
4415 __ Slti(TMP, lhs, rhs_imm);
4416 __ Bnez(TMP, label);
4417 } else {
4418 __ LoadConst32(TMP, rhs_imm);
4419 __ Blt(lhs, TMP, label);
4420 }
4421 break;
4422 case kCondGE:
4423 if (IsInt<16>(rhs_imm)) {
4424 __ Slti(TMP, lhs, rhs_imm);
4425 __ Beqz(TMP, label);
4426 } else {
4427 __ LoadConst32(TMP, rhs_imm);
4428 __ Bge(lhs, TMP, label);
4429 }
4430 break;
4431 case kCondLE:
4432 if (IsInt<16>(rhs_imm + 1)) {
4433 // Simulate lhs <= rhs via lhs < rhs + 1.
4434 __ Slti(TMP, lhs, rhs_imm + 1);
4435 __ Bnez(TMP, label);
4436 } else {
4437 __ LoadConst32(TMP, rhs_imm);
4438 __ Bge(TMP, lhs, label);
4439 }
4440 break;
4441 case kCondGT:
4442 if (IsInt<16>(rhs_imm + 1)) {
4443 // Simulate lhs > rhs via !(lhs < rhs + 1).
4444 __ Slti(TMP, lhs, rhs_imm + 1);
4445 __ Beqz(TMP, label);
4446 } else {
4447 __ LoadConst32(TMP, rhs_imm);
4448 __ Blt(TMP, lhs, label);
4449 }
4450 break;
4451 case kCondB:
4452 if (IsInt<16>(rhs_imm)) {
4453 __ Sltiu(TMP, lhs, rhs_imm);
4454 __ Bnez(TMP, label);
4455 } else {
4456 __ LoadConst32(TMP, rhs_imm);
4457 __ Bltu(lhs, TMP, label);
4458 }
4459 break;
4460 case kCondAE:
4461 if (IsInt<16>(rhs_imm)) {
4462 __ Sltiu(TMP, lhs, rhs_imm);
4463 __ Beqz(TMP, label);
4464 } else {
4465 __ LoadConst32(TMP, rhs_imm);
4466 __ Bgeu(lhs, TMP, label);
4467 }
4468 break;
4469 case kCondBE:
4470 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4471 // Simulate lhs <= rhs via lhs < rhs + 1.
4472 // Note that this only works if rhs + 1 does not overflow
4473 // to 0, hence the check above.
4474 __ Sltiu(TMP, lhs, rhs_imm + 1);
4475 __ Bnez(TMP, label);
4476 } else {
4477 __ LoadConst32(TMP, rhs_imm);
4478 __ Bgeu(TMP, lhs, label);
4479 }
4480 break;
4481 case kCondA:
4482 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4483 // Simulate lhs > rhs via !(lhs < rhs + 1).
4484 // Note that this only works if rhs + 1 does not overflow
4485 // to 0, hence the check above.
4486 __ Sltiu(TMP, lhs, rhs_imm + 1);
4487 __ Beqz(TMP, label);
4488 } else {
4489 __ LoadConst32(TMP, rhs_imm);
4490 __ Bltu(TMP, lhs, label);
4491 }
4492 break;
4493 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004494 }
4495 }
4496}
4497
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004498void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4499 LocationSummary* locations) {
4500 Register dst = locations->Out().AsRegister<Register>();
4501 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4502 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4503 Location rhs_location = locations->InAt(1);
4504 Register rhs_high = ZERO;
4505 Register rhs_low = ZERO;
4506 int64_t imm = 0;
4507 uint32_t imm_high = 0;
4508 uint32_t imm_low = 0;
4509 bool use_imm = rhs_location.IsConstant();
4510 if (use_imm) {
4511 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4512 imm_high = High32Bits(imm);
4513 imm_low = Low32Bits(imm);
4514 } else {
4515 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4516 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4517 }
4518 if (use_imm && imm == 0) {
4519 switch (cond) {
4520 case kCondEQ:
4521 case kCondBE: // <= 0 if zero
4522 __ Or(dst, lhs_high, lhs_low);
4523 __ Sltiu(dst, dst, 1);
4524 break;
4525 case kCondNE:
4526 case kCondA: // > 0 if non-zero
4527 __ Or(dst, lhs_high, lhs_low);
4528 __ Sltu(dst, ZERO, dst);
4529 break;
4530 case kCondLT:
4531 __ Slt(dst, lhs_high, ZERO);
4532 break;
4533 case kCondGE:
4534 __ Slt(dst, lhs_high, ZERO);
4535 __ Xori(dst, dst, 1);
4536 break;
4537 case kCondLE:
4538 __ Or(TMP, lhs_high, lhs_low);
4539 __ Sra(AT, lhs_high, 31);
4540 __ Sltu(dst, AT, TMP);
4541 __ Xori(dst, dst, 1);
4542 break;
4543 case kCondGT:
4544 __ Or(TMP, lhs_high, lhs_low);
4545 __ Sra(AT, lhs_high, 31);
4546 __ Sltu(dst, AT, TMP);
4547 break;
4548 case kCondB: // always false
4549 __ Andi(dst, dst, 0);
4550 break;
4551 case kCondAE: // always true
4552 __ Ori(dst, ZERO, 1);
4553 break;
4554 }
4555 } else if (use_imm) {
4556 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4557 switch (cond) {
4558 case kCondEQ:
4559 __ LoadConst32(TMP, imm_high);
4560 __ Xor(TMP, TMP, lhs_high);
4561 __ LoadConst32(AT, imm_low);
4562 __ Xor(AT, AT, lhs_low);
4563 __ Or(dst, TMP, AT);
4564 __ Sltiu(dst, dst, 1);
4565 break;
4566 case kCondNE:
4567 __ LoadConst32(TMP, imm_high);
4568 __ Xor(TMP, TMP, lhs_high);
4569 __ LoadConst32(AT, imm_low);
4570 __ Xor(AT, AT, lhs_low);
4571 __ Or(dst, TMP, AT);
4572 __ Sltu(dst, ZERO, dst);
4573 break;
4574 case kCondLT:
4575 case kCondGE:
4576 if (dst == lhs_low) {
4577 __ LoadConst32(TMP, imm_low);
4578 __ Sltu(dst, lhs_low, TMP);
4579 }
4580 __ LoadConst32(TMP, imm_high);
4581 __ Slt(AT, lhs_high, TMP);
4582 __ Slt(TMP, TMP, lhs_high);
4583 if (dst != lhs_low) {
4584 __ LoadConst32(dst, imm_low);
4585 __ Sltu(dst, lhs_low, dst);
4586 }
4587 __ Slt(dst, TMP, dst);
4588 __ Or(dst, dst, AT);
4589 if (cond == kCondGE) {
4590 __ Xori(dst, dst, 1);
4591 }
4592 break;
4593 case kCondGT:
4594 case kCondLE:
4595 if (dst == lhs_low) {
4596 __ LoadConst32(TMP, imm_low);
4597 __ Sltu(dst, TMP, lhs_low);
4598 }
4599 __ LoadConst32(TMP, imm_high);
4600 __ Slt(AT, TMP, lhs_high);
4601 __ Slt(TMP, lhs_high, TMP);
4602 if (dst != lhs_low) {
4603 __ LoadConst32(dst, imm_low);
4604 __ Sltu(dst, dst, lhs_low);
4605 }
4606 __ Slt(dst, TMP, dst);
4607 __ Or(dst, dst, AT);
4608 if (cond == kCondLE) {
4609 __ Xori(dst, dst, 1);
4610 }
4611 break;
4612 case kCondB:
4613 case kCondAE:
4614 if (dst == lhs_low) {
4615 __ LoadConst32(TMP, imm_low);
4616 __ Sltu(dst, lhs_low, TMP);
4617 }
4618 __ LoadConst32(TMP, imm_high);
4619 __ Sltu(AT, lhs_high, TMP);
4620 __ Sltu(TMP, TMP, lhs_high);
4621 if (dst != lhs_low) {
4622 __ LoadConst32(dst, imm_low);
4623 __ Sltu(dst, lhs_low, dst);
4624 }
4625 __ Slt(dst, TMP, dst);
4626 __ Or(dst, dst, AT);
4627 if (cond == kCondAE) {
4628 __ Xori(dst, dst, 1);
4629 }
4630 break;
4631 case kCondA:
4632 case kCondBE:
4633 if (dst == lhs_low) {
4634 __ LoadConst32(TMP, imm_low);
4635 __ Sltu(dst, TMP, lhs_low);
4636 }
4637 __ LoadConst32(TMP, imm_high);
4638 __ Sltu(AT, TMP, lhs_high);
4639 __ Sltu(TMP, lhs_high, TMP);
4640 if (dst != lhs_low) {
4641 __ LoadConst32(dst, imm_low);
4642 __ Sltu(dst, dst, lhs_low);
4643 }
4644 __ Slt(dst, TMP, dst);
4645 __ Or(dst, dst, AT);
4646 if (cond == kCondBE) {
4647 __ Xori(dst, dst, 1);
4648 }
4649 break;
4650 }
4651 } else {
4652 switch (cond) {
4653 case kCondEQ:
4654 __ Xor(TMP, lhs_high, rhs_high);
4655 __ Xor(AT, lhs_low, rhs_low);
4656 __ Or(dst, TMP, AT);
4657 __ Sltiu(dst, dst, 1);
4658 break;
4659 case kCondNE:
4660 __ Xor(TMP, lhs_high, rhs_high);
4661 __ Xor(AT, lhs_low, rhs_low);
4662 __ Or(dst, TMP, AT);
4663 __ Sltu(dst, ZERO, dst);
4664 break;
4665 case kCondLT:
4666 case kCondGE:
4667 __ Slt(TMP, rhs_high, lhs_high);
4668 __ Sltu(AT, lhs_low, rhs_low);
4669 __ Slt(TMP, TMP, AT);
4670 __ Slt(AT, lhs_high, rhs_high);
4671 __ Or(dst, AT, TMP);
4672 if (cond == kCondGE) {
4673 __ Xori(dst, dst, 1);
4674 }
4675 break;
4676 case kCondGT:
4677 case kCondLE:
4678 __ Slt(TMP, lhs_high, rhs_high);
4679 __ Sltu(AT, rhs_low, lhs_low);
4680 __ Slt(TMP, TMP, AT);
4681 __ Slt(AT, rhs_high, lhs_high);
4682 __ Or(dst, AT, TMP);
4683 if (cond == kCondLE) {
4684 __ Xori(dst, dst, 1);
4685 }
4686 break;
4687 case kCondB:
4688 case kCondAE:
4689 __ Sltu(TMP, rhs_high, lhs_high);
4690 __ Sltu(AT, lhs_low, rhs_low);
4691 __ Slt(TMP, TMP, AT);
4692 __ Sltu(AT, lhs_high, rhs_high);
4693 __ Or(dst, AT, TMP);
4694 if (cond == kCondAE) {
4695 __ Xori(dst, dst, 1);
4696 }
4697 break;
4698 case kCondA:
4699 case kCondBE:
4700 __ Sltu(TMP, lhs_high, rhs_high);
4701 __ Sltu(AT, rhs_low, lhs_low);
4702 __ Slt(TMP, TMP, AT);
4703 __ Sltu(AT, rhs_high, lhs_high);
4704 __ Or(dst, AT, TMP);
4705 if (cond == kCondBE) {
4706 __ Xori(dst, dst, 1);
4707 }
4708 break;
4709 }
4710 }
4711}
4712
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004713void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4714 LocationSummary* locations,
4715 MipsLabel* label) {
4716 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4717 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4718 Location rhs_location = locations->InAt(1);
4719 Register rhs_high = ZERO;
4720 Register rhs_low = ZERO;
4721 int64_t imm = 0;
4722 uint32_t imm_high = 0;
4723 uint32_t imm_low = 0;
4724 bool use_imm = rhs_location.IsConstant();
4725 if (use_imm) {
4726 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4727 imm_high = High32Bits(imm);
4728 imm_low = Low32Bits(imm);
4729 } else {
4730 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4731 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4732 }
4733
4734 if (use_imm && imm == 0) {
4735 switch (cond) {
4736 case kCondEQ:
4737 case kCondBE: // <= 0 if zero
4738 __ Or(TMP, lhs_high, lhs_low);
4739 __ Beqz(TMP, label);
4740 break;
4741 case kCondNE:
4742 case kCondA: // > 0 if non-zero
4743 __ Or(TMP, lhs_high, lhs_low);
4744 __ Bnez(TMP, label);
4745 break;
4746 case kCondLT:
4747 __ Bltz(lhs_high, label);
4748 break;
4749 case kCondGE:
4750 __ Bgez(lhs_high, label);
4751 break;
4752 case kCondLE:
4753 __ Or(TMP, lhs_high, lhs_low);
4754 __ Sra(AT, lhs_high, 31);
4755 __ Bgeu(AT, TMP, label);
4756 break;
4757 case kCondGT:
4758 __ Or(TMP, lhs_high, lhs_low);
4759 __ Sra(AT, lhs_high, 31);
4760 __ Bltu(AT, TMP, label);
4761 break;
4762 case kCondB: // always false
4763 break;
4764 case kCondAE: // always true
4765 __ B(label);
4766 break;
4767 }
4768 } else if (use_imm) {
4769 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4770 switch (cond) {
4771 case kCondEQ:
4772 __ LoadConst32(TMP, imm_high);
4773 __ Xor(TMP, TMP, lhs_high);
4774 __ LoadConst32(AT, imm_low);
4775 __ Xor(AT, AT, lhs_low);
4776 __ Or(TMP, TMP, AT);
4777 __ Beqz(TMP, label);
4778 break;
4779 case kCondNE:
4780 __ LoadConst32(TMP, imm_high);
4781 __ Xor(TMP, TMP, lhs_high);
4782 __ LoadConst32(AT, imm_low);
4783 __ Xor(AT, AT, lhs_low);
4784 __ Or(TMP, TMP, AT);
4785 __ Bnez(TMP, label);
4786 break;
4787 case kCondLT:
4788 __ LoadConst32(TMP, imm_high);
4789 __ Blt(lhs_high, TMP, label);
4790 __ Slt(TMP, TMP, lhs_high);
4791 __ LoadConst32(AT, imm_low);
4792 __ Sltu(AT, lhs_low, AT);
4793 __ Blt(TMP, AT, label);
4794 break;
4795 case kCondGE:
4796 __ LoadConst32(TMP, imm_high);
4797 __ Blt(TMP, lhs_high, label);
4798 __ Slt(TMP, lhs_high, TMP);
4799 __ LoadConst32(AT, imm_low);
4800 __ Sltu(AT, lhs_low, AT);
4801 __ Or(TMP, TMP, AT);
4802 __ Beqz(TMP, label);
4803 break;
4804 case kCondLE:
4805 __ LoadConst32(TMP, imm_high);
4806 __ Blt(lhs_high, TMP, label);
4807 __ Slt(TMP, TMP, lhs_high);
4808 __ LoadConst32(AT, imm_low);
4809 __ Sltu(AT, AT, lhs_low);
4810 __ Or(TMP, TMP, AT);
4811 __ Beqz(TMP, label);
4812 break;
4813 case kCondGT:
4814 __ LoadConst32(TMP, imm_high);
4815 __ Blt(TMP, lhs_high, label);
4816 __ Slt(TMP, lhs_high, TMP);
4817 __ LoadConst32(AT, imm_low);
4818 __ Sltu(AT, AT, lhs_low);
4819 __ Blt(TMP, AT, label);
4820 break;
4821 case kCondB:
4822 __ LoadConst32(TMP, imm_high);
4823 __ Bltu(lhs_high, TMP, label);
4824 __ Sltu(TMP, TMP, lhs_high);
4825 __ LoadConst32(AT, imm_low);
4826 __ Sltu(AT, lhs_low, AT);
4827 __ Blt(TMP, AT, label);
4828 break;
4829 case kCondAE:
4830 __ LoadConst32(TMP, imm_high);
4831 __ Bltu(TMP, lhs_high, label);
4832 __ Sltu(TMP, lhs_high, TMP);
4833 __ LoadConst32(AT, imm_low);
4834 __ Sltu(AT, lhs_low, AT);
4835 __ Or(TMP, TMP, AT);
4836 __ Beqz(TMP, label);
4837 break;
4838 case kCondBE:
4839 __ LoadConst32(TMP, imm_high);
4840 __ Bltu(lhs_high, TMP, label);
4841 __ Sltu(TMP, TMP, lhs_high);
4842 __ LoadConst32(AT, imm_low);
4843 __ Sltu(AT, AT, lhs_low);
4844 __ Or(TMP, TMP, AT);
4845 __ Beqz(TMP, label);
4846 break;
4847 case kCondA:
4848 __ LoadConst32(TMP, imm_high);
4849 __ Bltu(TMP, lhs_high, label);
4850 __ Sltu(TMP, lhs_high, TMP);
4851 __ LoadConst32(AT, imm_low);
4852 __ Sltu(AT, AT, lhs_low);
4853 __ Blt(TMP, AT, label);
4854 break;
4855 }
4856 } else {
4857 switch (cond) {
4858 case kCondEQ:
4859 __ Xor(TMP, lhs_high, rhs_high);
4860 __ Xor(AT, lhs_low, rhs_low);
4861 __ Or(TMP, TMP, AT);
4862 __ Beqz(TMP, label);
4863 break;
4864 case kCondNE:
4865 __ Xor(TMP, lhs_high, rhs_high);
4866 __ Xor(AT, lhs_low, rhs_low);
4867 __ Or(TMP, TMP, AT);
4868 __ Bnez(TMP, label);
4869 break;
4870 case kCondLT:
4871 __ Blt(lhs_high, rhs_high, label);
4872 __ Slt(TMP, rhs_high, lhs_high);
4873 __ Sltu(AT, lhs_low, rhs_low);
4874 __ Blt(TMP, AT, label);
4875 break;
4876 case kCondGE:
4877 __ Blt(rhs_high, lhs_high, label);
4878 __ Slt(TMP, lhs_high, rhs_high);
4879 __ Sltu(AT, lhs_low, rhs_low);
4880 __ Or(TMP, TMP, AT);
4881 __ Beqz(TMP, label);
4882 break;
4883 case kCondLE:
4884 __ Blt(lhs_high, rhs_high, label);
4885 __ Slt(TMP, rhs_high, lhs_high);
4886 __ Sltu(AT, rhs_low, lhs_low);
4887 __ Or(TMP, TMP, AT);
4888 __ Beqz(TMP, label);
4889 break;
4890 case kCondGT:
4891 __ Blt(rhs_high, lhs_high, label);
4892 __ Slt(TMP, lhs_high, rhs_high);
4893 __ Sltu(AT, rhs_low, lhs_low);
4894 __ Blt(TMP, AT, label);
4895 break;
4896 case kCondB:
4897 __ Bltu(lhs_high, rhs_high, label);
4898 __ Sltu(TMP, rhs_high, lhs_high);
4899 __ Sltu(AT, lhs_low, rhs_low);
4900 __ Blt(TMP, AT, label);
4901 break;
4902 case kCondAE:
4903 __ Bltu(rhs_high, lhs_high, label);
4904 __ Sltu(TMP, lhs_high, rhs_high);
4905 __ Sltu(AT, lhs_low, rhs_low);
4906 __ Or(TMP, TMP, AT);
4907 __ Beqz(TMP, label);
4908 break;
4909 case kCondBE:
4910 __ Bltu(lhs_high, rhs_high, label);
4911 __ Sltu(TMP, rhs_high, lhs_high);
4912 __ Sltu(AT, rhs_low, lhs_low);
4913 __ Or(TMP, TMP, AT);
4914 __ Beqz(TMP, label);
4915 break;
4916 case kCondA:
4917 __ Bltu(rhs_high, lhs_high, label);
4918 __ Sltu(TMP, lhs_high, rhs_high);
4919 __ Sltu(AT, rhs_low, lhs_low);
4920 __ Blt(TMP, AT, label);
4921 break;
4922 }
4923 }
4924}
4925
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004926void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4927 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004928 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004929 LocationSummary* locations) {
4930 Register dst = locations->Out().AsRegister<Register>();
4931 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4932 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4933 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004934 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004935 if (isR6) {
4936 switch (cond) {
4937 case kCondEQ:
4938 __ CmpEqS(FTMP, lhs, rhs);
4939 __ Mfc1(dst, FTMP);
4940 __ Andi(dst, dst, 1);
4941 break;
4942 case kCondNE:
4943 __ CmpEqS(FTMP, lhs, rhs);
4944 __ Mfc1(dst, FTMP);
4945 __ Addiu(dst, dst, 1);
4946 break;
4947 case kCondLT:
4948 if (gt_bias) {
4949 __ CmpLtS(FTMP, lhs, rhs);
4950 } else {
4951 __ CmpUltS(FTMP, lhs, rhs);
4952 }
4953 __ Mfc1(dst, FTMP);
4954 __ Andi(dst, dst, 1);
4955 break;
4956 case kCondLE:
4957 if (gt_bias) {
4958 __ CmpLeS(FTMP, lhs, rhs);
4959 } else {
4960 __ CmpUleS(FTMP, lhs, rhs);
4961 }
4962 __ Mfc1(dst, FTMP);
4963 __ Andi(dst, dst, 1);
4964 break;
4965 case kCondGT:
4966 if (gt_bias) {
4967 __ CmpUltS(FTMP, rhs, lhs);
4968 } else {
4969 __ CmpLtS(FTMP, rhs, lhs);
4970 }
4971 __ Mfc1(dst, FTMP);
4972 __ Andi(dst, dst, 1);
4973 break;
4974 case kCondGE:
4975 if (gt_bias) {
4976 __ CmpUleS(FTMP, rhs, lhs);
4977 } else {
4978 __ CmpLeS(FTMP, rhs, lhs);
4979 }
4980 __ Mfc1(dst, FTMP);
4981 __ Andi(dst, dst, 1);
4982 break;
4983 default:
4984 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4985 UNREACHABLE();
4986 }
4987 } else {
4988 switch (cond) {
4989 case kCondEQ:
4990 __ CeqS(0, lhs, rhs);
4991 __ LoadConst32(dst, 1);
4992 __ Movf(dst, ZERO, 0);
4993 break;
4994 case kCondNE:
4995 __ CeqS(0, lhs, rhs);
4996 __ LoadConst32(dst, 1);
4997 __ Movt(dst, ZERO, 0);
4998 break;
4999 case kCondLT:
5000 if (gt_bias) {
5001 __ ColtS(0, lhs, rhs);
5002 } else {
5003 __ CultS(0, lhs, rhs);
5004 }
5005 __ LoadConst32(dst, 1);
5006 __ Movf(dst, ZERO, 0);
5007 break;
5008 case kCondLE:
5009 if (gt_bias) {
5010 __ ColeS(0, lhs, rhs);
5011 } else {
5012 __ CuleS(0, lhs, rhs);
5013 }
5014 __ LoadConst32(dst, 1);
5015 __ Movf(dst, ZERO, 0);
5016 break;
5017 case kCondGT:
5018 if (gt_bias) {
5019 __ CultS(0, rhs, lhs);
5020 } else {
5021 __ ColtS(0, rhs, lhs);
5022 }
5023 __ LoadConst32(dst, 1);
5024 __ Movf(dst, ZERO, 0);
5025 break;
5026 case kCondGE:
5027 if (gt_bias) {
5028 __ CuleS(0, rhs, lhs);
5029 } else {
5030 __ ColeS(0, rhs, lhs);
5031 }
5032 __ LoadConst32(dst, 1);
5033 __ Movf(dst, ZERO, 0);
5034 break;
5035 default:
5036 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5037 UNREACHABLE();
5038 }
5039 }
5040 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005041 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005042 if (isR6) {
5043 switch (cond) {
5044 case kCondEQ:
5045 __ CmpEqD(FTMP, lhs, rhs);
5046 __ Mfc1(dst, FTMP);
5047 __ Andi(dst, dst, 1);
5048 break;
5049 case kCondNE:
5050 __ CmpEqD(FTMP, lhs, rhs);
5051 __ Mfc1(dst, FTMP);
5052 __ Addiu(dst, dst, 1);
5053 break;
5054 case kCondLT:
5055 if (gt_bias) {
5056 __ CmpLtD(FTMP, lhs, rhs);
5057 } else {
5058 __ CmpUltD(FTMP, lhs, rhs);
5059 }
5060 __ Mfc1(dst, FTMP);
5061 __ Andi(dst, dst, 1);
5062 break;
5063 case kCondLE:
5064 if (gt_bias) {
5065 __ CmpLeD(FTMP, lhs, rhs);
5066 } else {
5067 __ CmpUleD(FTMP, lhs, rhs);
5068 }
5069 __ Mfc1(dst, FTMP);
5070 __ Andi(dst, dst, 1);
5071 break;
5072 case kCondGT:
5073 if (gt_bias) {
5074 __ CmpUltD(FTMP, rhs, lhs);
5075 } else {
5076 __ CmpLtD(FTMP, rhs, lhs);
5077 }
5078 __ Mfc1(dst, FTMP);
5079 __ Andi(dst, dst, 1);
5080 break;
5081 case kCondGE:
5082 if (gt_bias) {
5083 __ CmpUleD(FTMP, rhs, lhs);
5084 } else {
5085 __ CmpLeD(FTMP, rhs, lhs);
5086 }
5087 __ Mfc1(dst, FTMP);
5088 __ Andi(dst, dst, 1);
5089 break;
5090 default:
5091 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5092 UNREACHABLE();
5093 }
5094 } else {
5095 switch (cond) {
5096 case kCondEQ:
5097 __ CeqD(0, lhs, rhs);
5098 __ LoadConst32(dst, 1);
5099 __ Movf(dst, ZERO, 0);
5100 break;
5101 case kCondNE:
5102 __ CeqD(0, lhs, rhs);
5103 __ LoadConst32(dst, 1);
5104 __ Movt(dst, ZERO, 0);
5105 break;
5106 case kCondLT:
5107 if (gt_bias) {
5108 __ ColtD(0, lhs, rhs);
5109 } else {
5110 __ CultD(0, lhs, rhs);
5111 }
5112 __ LoadConst32(dst, 1);
5113 __ Movf(dst, ZERO, 0);
5114 break;
5115 case kCondLE:
5116 if (gt_bias) {
5117 __ ColeD(0, lhs, rhs);
5118 } else {
5119 __ CuleD(0, lhs, rhs);
5120 }
5121 __ LoadConst32(dst, 1);
5122 __ Movf(dst, ZERO, 0);
5123 break;
5124 case kCondGT:
5125 if (gt_bias) {
5126 __ CultD(0, rhs, lhs);
5127 } else {
5128 __ ColtD(0, rhs, lhs);
5129 }
5130 __ LoadConst32(dst, 1);
5131 __ Movf(dst, ZERO, 0);
5132 break;
5133 case kCondGE:
5134 if (gt_bias) {
5135 __ CuleD(0, rhs, lhs);
5136 } else {
5137 __ ColeD(0, rhs, lhs);
5138 }
5139 __ LoadConst32(dst, 1);
5140 __ Movf(dst, ZERO, 0);
5141 break;
5142 default:
5143 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5144 UNREACHABLE();
5145 }
5146 }
5147 }
5148}
5149
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005150bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5151 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005152 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005153 LocationSummary* input_locations,
5154 int cc) {
5155 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5156 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5157 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005158 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005159 switch (cond) {
5160 case kCondEQ:
5161 __ CeqS(cc, lhs, rhs);
5162 return false;
5163 case kCondNE:
5164 __ CeqS(cc, lhs, rhs);
5165 return true;
5166 case kCondLT:
5167 if (gt_bias) {
5168 __ ColtS(cc, lhs, rhs);
5169 } else {
5170 __ CultS(cc, lhs, rhs);
5171 }
5172 return false;
5173 case kCondLE:
5174 if (gt_bias) {
5175 __ ColeS(cc, lhs, rhs);
5176 } else {
5177 __ CuleS(cc, lhs, rhs);
5178 }
5179 return false;
5180 case kCondGT:
5181 if (gt_bias) {
5182 __ CultS(cc, rhs, lhs);
5183 } else {
5184 __ ColtS(cc, rhs, lhs);
5185 }
5186 return false;
5187 case kCondGE:
5188 if (gt_bias) {
5189 __ CuleS(cc, rhs, lhs);
5190 } else {
5191 __ ColeS(cc, rhs, lhs);
5192 }
5193 return false;
5194 default:
5195 LOG(FATAL) << "Unexpected non-floating-point condition";
5196 UNREACHABLE();
5197 }
5198 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005200 switch (cond) {
5201 case kCondEQ:
5202 __ CeqD(cc, lhs, rhs);
5203 return false;
5204 case kCondNE:
5205 __ CeqD(cc, lhs, rhs);
5206 return true;
5207 case kCondLT:
5208 if (gt_bias) {
5209 __ ColtD(cc, lhs, rhs);
5210 } else {
5211 __ CultD(cc, lhs, rhs);
5212 }
5213 return false;
5214 case kCondLE:
5215 if (gt_bias) {
5216 __ ColeD(cc, lhs, rhs);
5217 } else {
5218 __ CuleD(cc, lhs, rhs);
5219 }
5220 return false;
5221 case kCondGT:
5222 if (gt_bias) {
5223 __ CultD(cc, rhs, lhs);
5224 } else {
5225 __ ColtD(cc, rhs, lhs);
5226 }
5227 return false;
5228 case kCondGE:
5229 if (gt_bias) {
5230 __ CuleD(cc, rhs, lhs);
5231 } else {
5232 __ ColeD(cc, rhs, lhs);
5233 }
5234 return false;
5235 default:
5236 LOG(FATAL) << "Unexpected non-floating-point condition";
5237 UNREACHABLE();
5238 }
5239 }
5240}
5241
5242bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5243 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005245 LocationSummary* input_locations,
5246 FRegister dst) {
5247 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5248 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5249 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005250 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005251 switch (cond) {
5252 case kCondEQ:
5253 __ CmpEqS(dst, lhs, rhs);
5254 return false;
5255 case kCondNE:
5256 __ CmpEqS(dst, lhs, rhs);
5257 return true;
5258 case kCondLT:
5259 if (gt_bias) {
5260 __ CmpLtS(dst, lhs, rhs);
5261 } else {
5262 __ CmpUltS(dst, lhs, rhs);
5263 }
5264 return false;
5265 case kCondLE:
5266 if (gt_bias) {
5267 __ CmpLeS(dst, lhs, rhs);
5268 } else {
5269 __ CmpUleS(dst, lhs, rhs);
5270 }
5271 return false;
5272 case kCondGT:
5273 if (gt_bias) {
5274 __ CmpUltS(dst, rhs, lhs);
5275 } else {
5276 __ CmpLtS(dst, rhs, lhs);
5277 }
5278 return false;
5279 case kCondGE:
5280 if (gt_bias) {
5281 __ CmpUleS(dst, rhs, lhs);
5282 } else {
5283 __ CmpLeS(dst, rhs, lhs);
5284 }
5285 return false;
5286 default:
5287 LOG(FATAL) << "Unexpected non-floating-point condition";
5288 UNREACHABLE();
5289 }
5290 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005292 switch (cond) {
5293 case kCondEQ:
5294 __ CmpEqD(dst, lhs, rhs);
5295 return false;
5296 case kCondNE:
5297 __ CmpEqD(dst, lhs, rhs);
5298 return true;
5299 case kCondLT:
5300 if (gt_bias) {
5301 __ CmpLtD(dst, lhs, rhs);
5302 } else {
5303 __ CmpUltD(dst, lhs, rhs);
5304 }
5305 return false;
5306 case kCondLE:
5307 if (gt_bias) {
5308 __ CmpLeD(dst, lhs, rhs);
5309 } else {
5310 __ CmpUleD(dst, lhs, rhs);
5311 }
5312 return false;
5313 case kCondGT:
5314 if (gt_bias) {
5315 __ CmpUltD(dst, rhs, lhs);
5316 } else {
5317 __ CmpLtD(dst, rhs, lhs);
5318 }
5319 return false;
5320 case kCondGE:
5321 if (gt_bias) {
5322 __ CmpUleD(dst, rhs, lhs);
5323 } else {
5324 __ CmpLeD(dst, rhs, lhs);
5325 }
5326 return false;
5327 default:
5328 LOG(FATAL) << "Unexpected non-floating-point condition";
5329 UNREACHABLE();
5330 }
5331 }
5332}
5333
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005334void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5335 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005336 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005337 LocationSummary* locations,
5338 MipsLabel* label) {
5339 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5340 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5341 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005342 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005343 if (isR6) {
5344 switch (cond) {
5345 case kCondEQ:
5346 __ CmpEqS(FTMP, lhs, rhs);
5347 __ Bc1nez(FTMP, label);
5348 break;
5349 case kCondNE:
5350 __ CmpEqS(FTMP, lhs, rhs);
5351 __ Bc1eqz(FTMP, label);
5352 break;
5353 case kCondLT:
5354 if (gt_bias) {
5355 __ CmpLtS(FTMP, lhs, rhs);
5356 } else {
5357 __ CmpUltS(FTMP, lhs, rhs);
5358 }
5359 __ Bc1nez(FTMP, label);
5360 break;
5361 case kCondLE:
5362 if (gt_bias) {
5363 __ CmpLeS(FTMP, lhs, rhs);
5364 } else {
5365 __ CmpUleS(FTMP, lhs, rhs);
5366 }
5367 __ Bc1nez(FTMP, label);
5368 break;
5369 case kCondGT:
5370 if (gt_bias) {
5371 __ CmpUltS(FTMP, rhs, lhs);
5372 } else {
5373 __ CmpLtS(FTMP, rhs, lhs);
5374 }
5375 __ Bc1nez(FTMP, label);
5376 break;
5377 case kCondGE:
5378 if (gt_bias) {
5379 __ CmpUleS(FTMP, rhs, lhs);
5380 } else {
5381 __ CmpLeS(FTMP, rhs, lhs);
5382 }
5383 __ Bc1nez(FTMP, label);
5384 break;
5385 default:
5386 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005387 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005388 }
5389 } else {
5390 switch (cond) {
5391 case kCondEQ:
5392 __ CeqS(0, lhs, rhs);
5393 __ Bc1t(0, label);
5394 break;
5395 case kCondNE:
5396 __ CeqS(0, lhs, rhs);
5397 __ Bc1f(0, label);
5398 break;
5399 case kCondLT:
5400 if (gt_bias) {
5401 __ ColtS(0, lhs, rhs);
5402 } else {
5403 __ CultS(0, lhs, rhs);
5404 }
5405 __ Bc1t(0, label);
5406 break;
5407 case kCondLE:
5408 if (gt_bias) {
5409 __ ColeS(0, lhs, rhs);
5410 } else {
5411 __ CuleS(0, lhs, rhs);
5412 }
5413 __ Bc1t(0, label);
5414 break;
5415 case kCondGT:
5416 if (gt_bias) {
5417 __ CultS(0, rhs, lhs);
5418 } else {
5419 __ ColtS(0, rhs, lhs);
5420 }
5421 __ Bc1t(0, label);
5422 break;
5423 case kCondGE:
5424 if (gt_bias) {
5425 __ CuleS(0, rhs, lhs);
5426 } else {
5427 __ ColeS(0, rhs, lhs);
5428 }
5429 __ Bc1t(0, label);
5430 break;
5431 default:
5432 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005433 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005434 }
5435 }
5436 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005438 if (isR6) {
5439 switch (cond) {
5440 case kCondEQ:
5441 __ CmpEqD(FTMP, lhs, rhs);
5442 __ Bc1nez(FTMP, label);
5443 break;
5444 case kCondNE:
5445 __ CmpEqD(FTMP, lhs, rhs);
5446 __ Bc1eqz(FTMP, label);
5447 break;
5448 case kCondLT:
5449 if (gt_bias) {
5450 __ CmpLtD(FTMP, lhs, rhs);
5451 } else {
5452 __ CmpUltD(FTMP, lhs, rhs);
5453 }
5454 __ Bc1nez(FTMP, label);
5455 break;
5456 case kCondLE:
5457 if (gt_bias) {
5458 __ CmpLeD(FTMP, lhs, rhs);
5459 } else {
5460 __ CmpUleD(FTMP, lhs, rhs);
5461 }
5462 __ Bc1nez(FTMP, label);
5463 break;
5464 case kCondGT:
5465 if (gt_bias) {
5466 __ CmpUltD(FTMP, rhs, lhs);
5467 } else {
5468 __ CmpLtD(FTMP, rhs, lhs);
5469 }
5470 __ Bc1nez(FTMP, label);
5471 break;
5472 case kCondGE:
5473 if (gt_bias) {
5474 __ CmpUleD(FTMP, rhs, lhs);
5475 } else {
5476 __ CmpLeD(FTMP, rhs, lhs);
5477 }
5478 __ Bc1nez(FTMP, label);
5479 break;
5480 default:
5481 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005482 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005483 }
5484 } else {
5485 switch (cond) {
5486 case kCondEQ:
5487 __ CeqD(0, lhs, rhs);
5488 __ Bc1t(0, label);
5489 break;
5490 case kCondNE:
5491 __ CeqD(0, lhs, rhs);
5492 __ Bc1f(0, label);
5493 break;
5494 case kCondLT:
5495 if (gt_bias) {
5496 __ ColtD(0, lhs, rhs);
5497 } else {
5498 __ CultD(0, lhs, rhs);
5499 }
5500 __ Bc1t(0, label);
5501 break;
5502 case kCondLE:
5503 if (gt_bias) {
5504 __ ColeD(0, lhs, rhs);
5505 } else {
5506 __ CuleD(0, lhs, rhs);
5507 }
5508 __ Bc1t(0, label);
5509 break;
5510 case kCondGT:
5511 if (gt_bias) {
5512 __ CultD(0, rhs, lhs);
5513 } else {
5514 __ ColtD(0, rhs, lhs);
5515 }
5516 __ Bc1t(0, label);
5517 break;
5518 case kCondGE:
5519 if (gt_bias) {
5520 __ CuleD(0, rhs, lhs);
5521 } else {
5522 __ ColeD(0, rhs, lhs);
5523 }
5524 __ Bc1t(0, label);
5525 break;
5526 default:
5527 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005528 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005529 }
5530 }
5531 }
5532}
5533
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005534void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005535 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005537 MipsLabel* false_target) {
5538 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005539
David Brazdil0debae72015-11-12 18:37:00 +00005540 if (true_target == nullptr && false_target == nullptr) {
5541 // Nothing to do. The code always falls through.
5542 return;
5543 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005544 // Constant condition, statically compared against "true" (integer value 1).
5545 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005546 if (true_target != nullptr) {
5547 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005548 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005549 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005550 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005551 if (false_target != nullptr) {
5552 __ B(false_target);
5553 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005554 }
David Brazdil0debae72015-11-12 18:37:00 +00005555 return;
5556 }
5557
5558 // The following code generates these patterns:
5559 // (1) true_target == nullptr && false_target != nullptr
5560 // - opposite condition true => branch to false_target
5561 // (2) true_target != nullptr && false_target == nullptr
5562 // - condition true => branch to true_target
5563 // (3) true_target != nullptr && false_target != nullptr
5564 // - condition true => branch to true_target
5565 // - branch to false_target
5566 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005567 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005568 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005569 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005570 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005571 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5572 } else {
5573 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5574 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005575 } else {
5576 // The condition instruction has not been materialized, use its inputs as
5577 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005578 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005580 LocationSummary* locations = cond->GetLocations();
5581 IfCondition if_cond = condition->GetCondition();
5582 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005583
David Brazdil0debae72015-11-12 18:37:00 +00005584 if (true_target == nullptr) {
5585 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005586 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005587 }
5588
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005589 switch (type) {
5590 default:
5591 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5592 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005593 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005594 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5595 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 case DataType::Type::kFloat32:
5597 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005598 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5599 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005600 }
5601 }
David Brazdil0debae72015-11-12 18:37:00 +00005602
5603 // If neither branch falls through (case 3), the conditional branch to `true_target`
5604 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5605 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005606 __ B(false_target);
5607 }
5608}
5609
5610void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005611 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005612 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005613 locations->SetInAt(0, Location::RequiresRegister());
5614 }
5615}
5616
5617void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005618 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5619 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5620 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5621 nullptr : codegen_->GetLabelOf(true_successor);
5622 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5623 nullptr : codegen_->GetLabelOf(false_successor);
5624 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005625}
5626
5627void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005628 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005629 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005630 InvokeRuntimeCallingConvention calling_convention;
5631 RegisterSet caller_saves = RegisterSet::Empty();
5632 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5633 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005634 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005635 locations->SetInAt(0, Location::RequiresRegister());
5636 }
5637}
5638
5639void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005640 SlowPathCodeMIPS* slow_path =
5641 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005642 GenerateTestAndBranch(deoptimize,
5643 /* condition_input_index */ 0,
5644 slow_path->GetEntryLabel(),
5645 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005646}
5647
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005648// This function returns true if a conditional move can be generated for HSelect.
5649// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5650// branches and regular moves.
5651//
5652// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5653//
5654// While determining feasibility of a conditional move and setting inputs/outputs
5655// are two distinct tasks, this function does both because they share quite a bit
5656// of common logic.
5657static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5658 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5659 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5660 HCondition* condition = cond->AsCondition();
5661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 DataType::Type cond_type =
5663 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5664 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005665
5666 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5667 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5668 bool is_true_value_zero_constant =
5669 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5670 bool is_false_value_zero_constant =
5671 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5672
5673 bool can_move_conditionally = false;
5674 bool use_const_for_false_in = false;
5675 bool use_const_for_true_in = false;
5676
5677 if (!cond->IsConstant()) {
5678 switch (cond_type) {
5679 default:
5680 switch (dst_type) {
5681 default:
5682 // Moving int on int condition.
5683 if (is_r6) {
5684 if (is_true_value_zero_constant) {
5685 // seleqz out_reg, false_reg, cond_reg
5686 can_move_conditionally = true;
5687 use_const_for_true_in = true;
5688 } else if (is_false_value_zero_constant) {
5689 // selnez out_reg, true_reg, cond_reg
5690 can_move_conditionally = true;
5691 use_const_for_false_in = true;
5692 } else if (materialized) {
5693 // Not materializing unmaterialized int conditions
5694 // to keep the instruction count low.
5695 // selnez AT, true_reg, cond_reg
5696 // seleqz TMP, false_reg, cond_reg
5697 // or out_reg, AT, TMP
5698 can_move_conditionally = true;
5699 }
5700 } else {
5701 // movn out_reg, true_reg/ZERO, cond_reg
5702 can_move_conditionally = true;
5703 use_const_for_true_in = is_true_value_zero_constant;
5704 }
5705 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005707 // Moving long on int condition.
5708 if (is_r6) {
5709 if (is_true_value_zero_constant) {
5710 // seleqz out_reg_lo, false_reg_lo, cond_reg
5711 // seleqz out_reg_hi, false_reg_hi, cond_reg
5712 can_move_conditionally = true;
5713 use_const_for_true_in = true;
5714 } else if (is_false_value_zero_constant) {
5715 // selnez out_reg_lo, true_reg_lo, cond_reg
5716 // selnez out_reg_hi, true_reg_hi, cond_reg
5717 can_move_conditionally = true;
5718 use_const_for_false_in = true;
5719 }
5720 // Other long conditional moves would generate 6+ instructions,
5721 // which is too many.
5722 } else {
5723 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5724 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5725 can_move_conditionally = true;
5726 use_const_for_true_in = is_true_value_zero_constant;
5727 }
5728 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 case DataType::Type::kFloat32:
5730 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005731 // Moving float/double on int condition.
5732 if (is_r6) {
5733 if (materialized) {
5734 // Not materializing unmaterialized int conditions
5735 // to keep the instruction count low.
5736 can_move_conditionally = true;
5737 if (is_true_value_zero_constant) {
5738 // sltu TMP, ZERO, cond_reg
5739 // mtc1 TMP, temp_cond_reg
5740 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5741 use_const_for_true_in = true;
5742 } else if (is_false_value_zero_constant) {
5743 // sltu TMP, ZERO, cond_reg
5744 // mtc1 TMP, temp_cond_reg
5745 // selnez.fmt out_reg, true_reg, temp_cond_reg
5746 use_const_for_false_in = true;
5747 } else {
5748 // sltu TMP, ZERO, cond_reg
5749 // mtc1 TMP, temp_cond_reg
5750 // sel.fmt temp_cond_reg, false_reg, true_reg
5751 // mov.fmt out_reg, temp_cond_reg
5752 }
5753 }
5754 } else {
5755 // movn.fmt out_reg, true_reg, cond_reg
5756 can_move_conditionally = true;
5757 }
5758 break;
5759 }
5760 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005761 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005762 // We don't materialize long comparison now
5763 // and use conditional branches instead.
5764 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005765 case DataType::Type::kFloat32:
5766 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005767 switch (dst_type) {
5768 default:
5769 // Moving int on float/double condition.
5770 if (is_r6) {
5771 if (is_true_value_zero_constant) {
5772 // mfc1 TMP, temp_cond_reg
5773 // seleqz out_reg, false_reg, TMP
5774 can_move_conditionally = true;
5775 use_const_for_true_in = true;
5776 } else if (is_false_value_zero_constant) {
5777 // mfc1 TMP, temp_cond_reg
5778 // selnez out_reg, true_reg, TMP
5779 can_move_conditionally = true;
5780 use_const_for_false_in = true;
5781 } else {
5782 // mfc1 TMP, temp_cond_reg
5783 // selnez AT, true_reg, TMP
5784 // seleqz TMP, false_reg, TMP
5785 // or out_reg, AT, TMP
5786 can_move_conditionally = true;
5787 }
5788 } else {
5789 // movt out_reg, true_reg/ZERO, cc
5790 can_move_conditionally = true;
5791 use_const_for_true_in = is_true_value_zero_constant;
5792 }
5793 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005794 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005795 // Moving long on float/double condition.
5796 if (is_r6) {
5797 if (is_true_value_zero_constant) {
5798 // mfc1 TMP, temp_cond_reg
5799 // seleqz out_reg_lo, false_reg_lo, TMP
5800 // seleqz out_reg_hi, false_reg_hi, TMP
5801 can_move_conditionally = true;
5802 use_const_for_true_in = true;
5803 } else if (is_false_value_zero_constant) {
5804 // mfc1 TMP, temp_cond_reg
5805 // selnez out_reg_lo, true_reg_lo, TMP
5806 // selnez out_reg_hi, true_reg_hi, TMP
5807 can_move_conditionally = true;
5808 use_const_for_false_in = true;
5809 }
5810 // Other long conditional moves would generate 6+ instructions,
5811 // which is too many.
5812 } else {
5813 // movt out_reg_lo, true_reg_lo/ZERO, cc
5814 // movt out_reg_hi, true_reg_hi/ZERO, cc
5815 can_move_conditionally = true;
5816 use_const_for_true_in = is_true_value_zero_constant;
5817 }
5818 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 case DataType::Type::kFloat32:
5820 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005821 // Moving float/double on float/double condition.
5822 if (is_r6) {
5823 can_move_conditionally = true;
5824 if (is_true_value_zero_constant) {
5825 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5826 use_const_for_true_in = true;
5827 } else if (is_false_value_zero_constant) {
5828 // selnez.fmt out_reg, true_reg, temp_cond_reg
5829 use_const_for_false_in = true;
5830 } else {
5831 // sel.fmt temp_cond_reg, false_reg, true_reg
5832 // mov.fmt out_reg, temp_cond_reg
5833 }
5834 } else {
5835 // movt.fmt out_reg, true_reg, cc
5836 can_move_conditionally = true;
5837 }
5838 break;
5839 }
5840 break;
5841 }
5842 }
5843
5844 if (can_move_conditionally) {
5845 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5846 } else {
5847 DCHECK(!use_const_for_false_in);
5848 DCHECK(!use_const_for_true_in);
5849 }
5850
5851 if (locations_to_set != nullptr) {
5852 if (use_const_for_false_in) {
5853 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5854 } else {
5855 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005856 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005857 ? Location::RequiresFpuRegister()
5858 : Location::RequiresRegister());
5859 }
5860 if (use_const_for_true_in) {
5861 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5862 } else {
5863 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005865 ? Location::RequiresFpuRegister()
5866 : Location::RequiresRegister());
5867 }
5868 if (materialized) {
5869 locations_to_set->SetInAt(2, Location::RequiresRegister());
5870 }
5871 // On R6 we don't require the output to be the same as the
5872 // first input for conditional moves unlike on R2.
5873 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5874 if (is_out_same_as_first_in) {
5875 locations_to_set->SetOut(Location::SameAsFirstInput());
5876 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005878 ? Location::RequiresFpuRegister()
5879 : Location::RequiresRegister());
5880 }
5881 }
5882
5883 return can_move_conditionally;
5884}
5885
5886void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5887 LocationSummary* locations = select->GetLocations();
5888 Location dst = locations->Out();
5889 Location src = locations->InAt(1);
5890 Register src_reg = ZERO;
5891 Register src_reg_high = ZERO;
5892 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5893 Register cond_reg = TMP;
5894 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005896 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005898
5899 if (IsBooleanValueOrMaterializedCondition(cond)) {
5900 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5901 } else {
5902 HCondition* condition = cond->AsCondition();
5903 LocationSummary* cond_locations = cond->GetLocations();
5904 IfCondition if_cond = condition->GetCondition();
5905 cond_type = condition->InputAt(0)->GetType();
5906 switch (cond_type) {
5907 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005909 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5910 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 case DataType::Type::kFloat32:
5912 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005913 cond_inverted = MaterializeFpCompareR2(if_cond,
5914 condition->IsGtBias(),
5915 cond_type,
5916 cond_locations,
5917 cond_cc);
5918 break;
5919 }
5920 }
5921
5922 DCHECK(dst.Equals(locations->InAt(0)));
5923 if (src.IsRegister()) {
5924 src_reg = src.AsRegister<Register>();
5925 } else if (src.IsRegisterPair()) {
5926 src_reg = src.AsRegisterPairLow<Register>();
5927 src_reg_high = src.AsRegisterPairHigh<Register>();
5928 } else if (src.IsConstant()) {
5929 DCHECK(src.GetConstant()->IsZeroBitPattern());
5930 }
5931
5932 switch (cond_type) {
5933 default:
5934 switch (dst_type) {
5935 default:
5936 if (cond_inverted) {
5937 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5938 } else {
5939 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5940 }
5941 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005942 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005943 if (cond_inverted) {
5944 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5945 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5946 } else {
5947 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5948 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5949 }
5950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005952 if (cond_inverted) {
5953 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5954 } else {
5955 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5956 }
5957 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005958 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005959 if (cond_inverted) {
5960 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5961 } else {
5962 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5963 }
5964 break;
5965 }
5966 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005968 LOG(FATAL) << "Unreachable";
5969 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 case DataType::Type::kFloat32:
5971 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005972 switch (dst_type) {
5973 default:
5974 if (cond_inverted) {
5975 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5976 } else {
5977 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5978 }
5979 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005980 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005981 if (cond_inverted) {
5982 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5983 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5984 } else {
5985 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5986 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5987 }
5988 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005989 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005990 if (cond_inverted) {
5991 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5992 } else {
5993 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5994 }
5995 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005996 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005997 if (cond_inverted) {
5998 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5999 } else {
6000 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6001 }
6002 break;
6003 }
6004 break;
6005 }
6006}
6007
6008void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6009 LocationSummary* locations = select->GetLocations();
6010 Location dst = locations->Out();
6011 Location false_src = locations->InAt(0);
6012 Location true_src = locations->InAt(1);
6013 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6014 Register cond_reg = TMP;
6015 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006016 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006017 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006019
6020 if (IsBooleanValueOrMaterializedCondition(cond)) {
6021 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6022 } else {
6023 HCondition* condition = cond->AsCondition();
6024 LocationSummary* cond_locations = cond->GetLocations();
6025 IfCondition if_cond = condition->GetCondition();
6026 cond_type = condition->InputAt(0)->GetType();
6027 switch (cond_type) {
6028 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006030 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6031 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006032 case DataType::Type::kFloat32:
6033 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006034 cond_inverted = MaterializeFpCompareR6(if_cond,
6035 condition->IsGtBias(),
6036 cond_type,
6037 cond_locations,
6038 fcond_reg);
6039 break;
6040 }
6041 }
6042
6043 if (true_src.IsConstant()) {
6044 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6045 }
6046 if (false_src.IsConstant()) {
6047 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6048 }
6049
6050 switch (dst_type) {
6051 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006052 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006053 __ Mfc1(cond_reg, fcond_reg);
6054 }
6055 if (true_src.IsConstant()) {
6056 if (cond_inverted) {
6057 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6058 } else {
6059 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6060 }
6061 } else if (false_src.IsConstant()) {
6062 if (cond_inverted) {
6063 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6064 } else {
6065 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6066 }
6067 } else {
6068 DCHECK_NE(cond_reg, AT);
6069 if (cond_inverted) {
6070 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6071 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6072 } else {
6073 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6074 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6075 }
6076 __ Or(dst.AsRegister<Register>(), AT, TMP);
6077 }
6078 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006079 case DataType::Type::kInt64: {
6080 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006081 __ Mfc1(cond_reg, fcond_reg);
6082 }
6083 Register dst_lo = dst.AsRegisterPairLow<Register>();
6084 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6085 if (true_src.IsConstant()) {
6086 Register src_lo = false_src.AsRegisterPairLow<Register>();
6087 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6088 if (cond_inverted) {
6089 __ Selnez(dst_lo, src_lo, cond_reg);
6090 __ Selnez(dst_hi, src_hi, cond_reg);
6091 } else {
6092 __ Seleqz(dst_lo, src_lo, cond_reg);
6093 __ Seleqz(dst_hi, src_hi, cond_reg);
6094 }
6095 } else {
6096 DCHECK(false_src.IsConstant());
6097 Register src_lo = true_src.AsRegisterPairLow<Register>();
6098 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6099 if (cond_inverted) {
6100 __ Seleqz(dst_lo, src_lo, cond_reg);
6101 __ Seleqz(dst_hi, src_hi, cond_reg);
6102 } else {
6103 __ Selnez(dst_lo, src_lo, cond_reg);
6104 __ Selnez(dst_hi, src_hi, cond_reg);
6105 }
6106 }
6107 break;
6108 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006109 case DataType::Type::kFloat32: {
6110 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006111 // sel*.fmt tests bit 0 of the condition register, account for that.
6112 __ Sltu(TMP, ZERO, cond_reg);
6113 __ Mtc1(TMP, fcond_reg);
6114 }
6115 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6116 if (true_src.IsConstant()) {
6117 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6118 if (cond_inverted) {
6119 __ SelnezS(dst_reg, src_reg, fcond_reg);
6120 } else {
6121 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6122 }
6123 } else if (false_src.IsConstant()) {
6124 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6125 if (cond_inverted) {
6126 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6127 } else {
6128 __ SelnezS(dst_reg, src_reg, fcond_reg);
6129 }
6130 } else {
6131 if (cond_inverted) {
6132 __ SelS(fcond_reg,
6133 true_src.AsFpuRegister<FRegister>(),
6134 false_src.AsFpuRegister<FRegister>());
6135 } else {
6136 __ SelS(fcond_reg,
6137 false_src.AsFpuRegister<FRegister>(),
6138 true_src.AsFpuRegister<FRegister>());
6139 }
6140 __ MovS(dst_reg, fcond_reg);
6141 }
6142 break;
6143 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 case DataType::Type::kFloat64: {
6145 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006146 // sel*.fmt tests bit 0 of the condition register, account for that.
6147 __ Sltu(TMP, ZERO, cond_reg);
6148 __ Mtc1(TMP, fcond_reg);
6149 }
6150 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6151 if (true_src.IsConstant()) {
6152 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6153 if (cond_inverted) {
6154 __ SelnezD(dst_reg, src_reg, fcond_reg);
6155 } else {
6156 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6157 }
6158 } else if (false_src.IsConstant()) {
6159 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6160 if (cond_inverted) {
6161 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6162 } else {
6163 __ SelnezD(dst_reg, src_reg, fcond_reg);
6164 }
6165 } else {
6166 if (cond_inverted) {
6167 __ SelD(fcond_reg,
6168 true_src.AsFpuRegister<FRegister>(),
6169 false_src.AsFpuRegister<FRegister>());
6170 } else {
6171 __ SelD(fcond_reg,
6172 false_src.AsFpuRegister<FRegister>(),
6173 true_src.AsFpuRegister<FRegister>());
6174 }
6175 __ MovD(dst_reg, fcond_reg);
6176 }
6177 break;
6178 }
6179 }
6180}
6181
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006182void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006183 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006184 LocationSummary(flag, LocationSummary::kNoCall);
6185 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006186}
6187
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006188void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6189 __ LoadFromOffset(kLoadWord,
6190 flag->GetLocations()->Out().AsRegister<Register>(),
6191 SP,
6192 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006193}
6194
David Brazdil74eb1b22015-12-14 11:44:01 +00006195void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006196 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006197 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006198}
6199
6200void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006201 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6202 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6203 if (is_r6) {
6204 GenConditionalMoveR6(select);
6205 } else {
6206 GenConditionalMoveR2(select);
6207 }
6208 } else {
6209 LocationSummary* locations = select->GetLocations();
6210 MipsLabel false_target;
6211 GenerateTestAndBranch(select,
6212 /* condition_input_index */ 2,
6213 /* true_target */ nullptr,
6214 &false_target);
6215 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6216 __ Bind(&false_target);
6217 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006218}
6219
David Srbecky0cf44932015-12-09 14:09:59 +00006220void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006221 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006222}
6223
David Srbeckyd28f4a02016-03-14 17:14:24 +00006224void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6225 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006226}
6227
6228void CodeGeneratorMIPS::GenerateNop() {
6229 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006230}
6231
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006232void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 DataType::Type field_type = field_info.GetFieldType();
6234 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006236 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006238 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006239 instruction,
6240 generate_volatile
6241 ? LocationSummary::kCallOnMainOnly
6242 : (object_field_get_with_read_barrier
6243 ? LocationSummary::kCallOnSlowPath
6244 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006245
Alexey Frunzec61c0762017-04-10 13:54:23 -07006246 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6247 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6248 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 locations->SetInAt(0, Location::RequiresRegister());
6250 if (generate_volatile) {
6251 InvokeRuntimeCallingConvention calling_convention;
6252 // need A0 to hold base + offset
6253 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006254 if (field_type == DataType::Type::kInt64) {
6255 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006256 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006257 // Use Location::Any() to prevent situations when running out of available fp registers.
6258 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006259 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006261 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6262 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6263 }
6264 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266 locations->SetOut(Location::RequiresFpuRegister());
6267 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006268 // The output overlaps in the case of an object field get with
6269 // read barriers enabled: we do not want the move to overwrite the
6270 // object's location, as we need it to emit the read barrier.
6271 locations->SetOut(Location::RequiresRegister(),
6272 object_field_get_with_read_barrier
6273 ? Location::kOutputOverlap
6274 : Location::kNoOutputOverlap);
6275 }
6276 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6277 // We need a temporary register for the read barrier marking slow
6278 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006279 if (!kBakerReadBarrierThunksEnableForFields) {
6280 locations->AddTemp(Location::RequiresRegister());
6281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282 }
6283 }
6284}
6285
6286void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6287 const FieldInfo& field_info,
6288 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006289 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6290 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006291 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006292 Location obj_loc = locations->InAt(0);
6293 Register obj = obj_loc.AsRegister<Register>();
6294 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 LoadOperandType load_type = kLoadUnsignedByte;
6296 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006297 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006298 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006299
6300 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006302 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303 load_type = kLoadUnsignedByte;
6304 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006305 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006306 load_type = kLoadSignedByte;
6307 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006308 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006309 load_type = kLoadUnsignedHalfword;
6310 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006311 case DataType::Type::kInt16:
6312 load_type = kLoadSignedHalfword;
6313 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 case DataType::Type::kInt32:
6315 case DataType::Type::kFloat32:
6316 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006317 load_type = kLoadWord;
6318 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006319 case DataType::Type::kInt64:
6320 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006321 load_type = kLoadDoubleword;
6322 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006323 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006324 LOG(FATAL) << "Unreachable type " << type;
6325 UNREACHABLE();
6326 }
6327
6328 if (is_volatile && load_type == kLoadDoubleword) {
6329 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006330 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006331 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006332 __ LoadFromOffset(kLoadWord,
6333 ZERO,
6334 locations->GetTemp(0).AsRegister<Register>(),
6335 0,
6336 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006337 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006338 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006340 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006341 if (dst_loc.IsFpuRegister()) {
6342 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006343 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006344 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006345 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006346 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006347 __ StoreToOffset(kStoreWord,
6348 locations->GetTemp(1).AsRegister<Register>(),
6349 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006350 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006351 __ StoreToOffset(kStoreWord,
6352 locations->GetTemp(2).AsRegister<Register>(),
6353 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006354 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006356 }
6357 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006358 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006359 // /* HeapReference<Object> */ dst = *(obj + offset)
6360 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006361 Location temp_loc =
6362 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006363 // Note that a potential implicit null check is handled in this
6364 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6365 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6366 dst_loc,
6367 obj,
6368 offset,
6369 temp_loc,
6370 /* needs_null_check */ true);
6371 if (is_volatile) {
6372 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6373 }
6374 } else {
6375 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6376 if (is_volatile) {
6377 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6378 }
6379 // If read barriers are enabled, emit read barriers other than
6380 // Baker's using a slow path (and also unpoison the loaded
6381 // reference, if heap poisoning is enabled).
6382 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6383 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006384 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006386 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006387 DCHECK(dst_loc.IsRegisterPair());
6388 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006389 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006390 DCHECK(dst_loc.IsRegister());
6391 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006393 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006395 DCHECK(dst_loc.IsFpuRegister());
6396 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006398 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006399 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006400 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006401 }
6402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 }
6404
Alexey Frunze15958152017-02-09 19:08:30 -08006405 // Memory barriers, in the case of references, are handled in the
6406 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006407 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006408 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6409 }
6410}
6411
6412void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 DataType::Type field_type = field_info.GetFieldType();
6414 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006415 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006416 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006417 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006418
6419 locations->SetInAt(0, Location::RequiresRegister());
6420 if (generate_volatile) {
6421 InvokeRuntimeCallingConvention calling_convention;
6422 // need A0 to hold base + offset
6423 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006424 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425 locations->SetInAt(1, Location::RegisterPairLocation(
6426 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6427 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006428 // Use Location::Any() to prevent situations when running out of available fp registers.
6429 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006430 // Pass FP parameters in core registers.
6431 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6432 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6433 }
6434 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006435 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006436 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006438 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006439 }
6440 }
6441}
6442
6443void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6444 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006445 uint32_t dex_pc,
6446 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006447 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006448 LocationSummary* locations = instruction->GetLocations();
6449 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006450 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006451 StoreOperandType store_type = kStoreByte;
6452 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006453 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006454 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006455 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006456
6457 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006458 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006459 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006460 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006461 store_type = kStoreByte;
6462 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006463 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006464 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006465 store_type = kStoreHalfword;
6466 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 case DataType::Type::kInt32:
6468 case DataType::Type::kFloat32:
6469 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006470 store_type = kStoreWord;
6471 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006472 case DataType::Type::kInt64:
6473 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006474 store_type = kStoreDoubleword;
6475 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006476 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006477 LOG(FATAL) << "Unreachable type " << type;
6478 UNREACHABLE();
6479 }
6480
6481 if (is_volatile) {
6482 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6483 }
6484
6485 if (is_volatile && store_type == kStoreDoubleword) {
6486 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006487 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006488 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006489 __ LoadFromOffset(kLoadWord,
6490 ZERO,
6491 locations->GetTemp(0).AsRegister<Register>(),
6492 0,
6493 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006494 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006495 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006496 if (value_location.IsFpuRegister()) {
6497 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6498 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006499 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006500 value_location.AsFpuRegister<FRegister>());
6501 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006502 __ LoadFromOffset(kLoadWord,
6503 locations->GetTemp(1).AsRegister<Register>(),
6504 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006505 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006506 __ LoadFromOffset(kLoadWord,
6507 locations->GetTemp(2).AsRegister<Register>(),
6508 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006509 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006510 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006511 DCHECK(value_location.IsConstant());
6512 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6513 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006514 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6515 locations->GetTemp(1).AsRegister<Register>(),
6516 value);
6517 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006518 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006519 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6521 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006522 if (value_location.IsConstant()) {
6523 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6524 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006525 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006526 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006527 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006528 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006529 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006530 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006531 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006532 if (kPoisonHeapReferences && needs_write_barrier) {
6533 // Note that in the case where `value` is a null reference,
6534 // we do not enter this block, as a null reference does not
6535 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006536 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006537 __ PoisonHeapReference(TMP, src);
6538 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6539 } else {
6540 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6541 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006542 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006543 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006544 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006545 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006547 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006548 }
6549 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006550 }
6551
Alexey Frunzec061de12017-02-14 13:27:23 -08006552 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006553 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006554 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006555 }
6556
6557 if (is_volatile) {
6558 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6559 }
6560}
6561
6562void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6563 HandleFieldGet(instruction, instruction->GetFieldInfo());
6564}
6565
6566void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6567 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6568}
6569
6570void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6571 HandleFieldSet(instruction, instruction->GetFieldInfo());
6572}
6573
6574void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006575 HandleFieldSet(instruction,
6576 instruction->GetFieldInfo(),
6577 instruction->GetDexPc(),
6578 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006579}
6580
Alexey Frunze15958152017-02-09 19:08:30 -08006581void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6582 HInstruction* instruction,
6583 Location out,
6584 uint32_t offset,
6585 Location maybe_temp,
6586 ReadBarrierOption read_barrier_option) {
6587 Register out_reg = out.AsRegister<Register>();
6588 if (read_barrier_option == kWithReadBarrier) {
6589 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006590 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6591 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6592 }
Alexey Frunze15958152017-02-09 19:08:30 -08006593 if (kUseBakerReadBarrier) {
6594 // Load with fast path based Baker's read barrier.
6595 // /* HeapReference<Object> */ out = *(out + offset)
6596 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6597 out,
6598 out_reg,
6599 offset,
6600 maybe_temp,
6601 /* needs_null_check */ false);
6602 } else {
6603 // Load with slow path based read barrier.
6604 // Save the value of `out` into `maybe_temp` before overwriting it
6605 // in the following move operation, as we will need it for the
6606 // read barrier below.
6607 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6608 // /* HeapReference<Object> */ out = *(out + offset)
6609 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6610 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6611 }
6612 } else {
6613 // Plain load with no read barrier.
6614 // /* HeapReference<Object> */ out = *(out + offset)
6615 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6616 __ MaybeUnpoisonHeapReference(out_reg);
6617 }
6618}
6619
6620void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6621 HInstruction* instruction,
6622 Location out,
6623 Location obj,
6624 uint32_t offset,
6625 Location maybe_temp,
6626 ReadBarrierOption read_barrier_option) {
6627 Register out_reg = out.AsRegister<Register>();
6628 Register obj_reg = obj.AsRegister<Register>();
6629 if (read_barrier_option == kWithReadBarrier) {
6630 CHECK(kEmitCompilerReadBarrier);
6631 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006632 if (!kBakerReadBarrierThunksEnableForFields) {
6633 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6634 }
Alexey Frunze15958152017-02-09 19:08:30 -08006635 // Load with fast path based Baker's read barrier.
6636 // /* HeapReference<Object> */ out = *(obj + offset)
6637 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6638 out,
6639 obj_reg,
6640 offset,
6641 maybe_temp,
6642 /* needs_null_check */ false);
6643 } else {
6644 // Load with slow path based read barrier.
6645 // /* HeapReference<Object> */ out = *(obj + offset)
6646 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6647 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6648 }
6649 } else {
6650 // Plain load with no read barrier.
6651 // /* HeapReference<Object> */ out = *(obj + offset)
6652 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6653 __ MaybeUnpoisonHeapReference(out_reg);
6654 }
6655}
6656
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006657static inline int GetBakerMarkThunkNumber(Register reg) {
6658 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6659 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6660 return reg - V0;
6661 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6662 return 14 + (reg - S2);
6663 } else if (reg == FP) { // One more.
6664 return 20;
6665 }
6666 LOG(FATAL) << "Unexpected register " << reg;
6667 UNREACHABLE();
6668}
6669
6670static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6671 int num = GetBakerMarkThunkNumber(reg) +
6672 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6673 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6674}
6675
6676static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6677 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6678 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6679}
6680
Alexey Frunze15958152017-02-09 19:08:30 -08006681void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6682 Location root,
6683 Register obj,
6684 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006685 ReadBarrierOption read_barrier_option,
6686 MipsLabel* label_low) {
6687 bool reordering;
6688 if (label_low != nullptr) {
6689 DCHECK_EQ(offset, 0x5678u);
6690 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006691 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006692 if (read_barrier_option == kWithReadBarrier) {
6693 DCHECK(kEmitCompilerReadBarrier);
6694 if (kUseBakerReadBarrier) {
6695 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6696 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006697 if (kBakerReadBarrierThunksEnableForGcRoots) {
6698 // Note that we do not actually check the value of `GetIsGcMarking()`
6699 // to decide whether to mark the loaded GC root or not. Instead, we
6700 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6701 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6702 // vice versa.
6703 //
6704 // We use thunks for the slow path. That thunk checks the reference
6705 // and jumps to the entrypoint if needed.
6706 //
6707 // temp = Thread::Current()->pReadBarrierMarkReg00
6708 // // AKA &art_quick_read_barrier_mark_introspection.
6709 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6710 // if (temp != nullptr) {
6711 // temp = &gc_root_thunk<root_reg>
6712 // root = temp(root)
6713 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006714
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006715 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6716 const int32_t entry_point_offset =
6717 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6718 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6719 int16_t offset_low = Low16Bits(offset);
6720 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6721 // extension in lw.
6722 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6723 Register base = short_offset ? obj : TMP;
6724 // Loading the entrypoint does not require a load acquire since it is only changed when
6725 // threads are suspended or running a checkpoint.
6726 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6727 reordering = __ SetReorder(false);
6728 if (!short_offset) {
6729 DCHECK(!label_low);
6730 __ AddUpper(base, obj, offset_high);
6731 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006732 MipsLabel skip_call;
6733 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006734 if (label_low != nullptr) {
6735 DCHECK(short_offset);
6736 __ Bind(label_low);
6737 }
6738 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6739 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6740 // in delay slot.
6741 if (isR6) {
6742 __ Jialc(T9, thunk_disp);
6743 } else {
6744 __ Addiu(T9, T9, thunk_disp);
6745 __ Jalr(T9);
6746 __ Nop();
6747 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006748 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006749 __ SetReorder(reordering);
6750 } else {
6751 // Note that we do not actually check the value of `GetIsGcMarking()`
6752 // to decide whether to mark the loaded GC root or not. Instead, we
6753 // load into `temp` (T9) the read barrier mark entry point corresponding
6754 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6755 // is false, and vice versa.
6756 //
6757 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6758 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6759 // if (temp != null) {
6760 // root = temp(root)
6761 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006762
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006763 if (label_low != nullptr) {
6764 reordering = __ SetReorder(false);
6765 __ Bind(label_low);
6766 }
6767 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6768 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6769 if (label_low != nullptr) {
6770 __ SetReorder(reordering);
6771 }
6772 static_assert(
6773 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6774 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6775 "have different sizes.");
6776 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6777 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6778 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006779
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006780 // Slow path marking the GC root `root`.
6781 Location temp = Location::RegisterLocation(T9);
6782 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006783 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006784 instruction,
6785 root,
6786 /*entrypoint*/ temp);
6787 codegen_->AddSlowPath(slow_path);
6788
6789 const int32_t entry_point_offset =
6790 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6791 // Loading the entrypoint does not require a load acquire since it is only changed when
6792 // threads are suspended or running a checkpoint.
6793 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6794 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6795 __ Bind(slow_path->GetExitLabel());
6796 }
Alexey Frunze15958152017-02-09 19:08:30 -08006797 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006798 if (label_low != nullptr) {
6799 reordering = __ SetReorder(false);
6800 __ Bind(label_low);
6801 }
Alexey Frunze15958152017-02-09 19:08:30 -08006802 // GC root loaded through a slow path for read barriers other
6803 // than Baker's.
6804 // /* GcRoot<mirror::Object>* */ root = obj + offset
6805 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006806 if (label_low != nullptr) {
6807 __ SetReorder(reordering);
6808 }
Alexey Frunze15958152017-02-09 19:08:30 -08006809 // /* mirror::Object* */ root = root->Read()
6810 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6811 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006812 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006813 if (label_low != nullptr) {
6814 reordering = __ SetReorder(false);
6815 __ Bind(label_low);
6816 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006817 // Plain GC root load with no read barrier.
6818 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6819 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6820 // Note that GC roots are not affected by heap poisoning, thus we
6821 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006822 if (label_low != nullptr) {
6823 __ SetReorder(reordering);
6824 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006825 }
6826}
6827
Alexey Frunze15958152017-02-09 19:08:30 -08006828void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6829 Location ref,
6830 Register obj,
6831 uint32_t offset,
6832 Location temp,
6833 bool needs_null_check) {
6834 DCHECK(kEmitCompilerReadBarrier);
6835 DCHECK(kUseBakerReadBarrier);
6836
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006837 if (kBakerReadBarrierThunksEnableForFields) {
6838 // Note that we do not actually check the value of `GetIsGcMarking()`
6839 // to decide whether to mark the loaded reference or not. Instead, we
6840 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6841 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6842 // vice versa.
6843 //
6844 // We use thunks for the slow path. That thunk checks the reference
6845 // and jumps to the entrypoint if needed. If the holder is not gray,
6846 // it issues a load-load memory barrier and returns to the original
6847 // reference load.
6848 //
6849 // temp = Thread::Current()->pReadBarrierMarkReg00
6850 // // AKA &art_quick_read_barrier_mark_introspection.
6851 // if (temp != nullptr) {
6852 // temp = &field_array_thunk<holder_reg>
6853 // temp()
6854 // }
6855 // not_gray_return_address:
6856 // // If the offset is too large to fit into the lw instruction, we
6857 // // use an adjusted base register (TMP) here. This register
6858 // // receives bits 16 ... 31 of the offset before the thunk invocation
6859 // // and the thunk benefits from it.
6860 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6861 // gray_return_address:
6862
6863 DCHECK(temp.IsInvalid());
6864 bool isR6 = GetInstructionSetFeatures().IsR6();
6865 int16_t offset_low = Low16Bits(offset);
6866 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6867 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6868 bool reordering = __ SetReorder(false);
6869 const int32_t entry_point_offset =
6870 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6871 // There may have or may have not been a null check if the field offset is smaller than
6872 // the page size.
6873 // There must've been a null check in case it's actually a load from an array.
6874 // We will, however, perform an explicit null check in the thunk as it's easier to
6875 // do it than not.
6876 if (instruction->IsArrayGet()) {
6877 DCHECK(!needs_null_check);
6878 }
6879 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6880 // Loading the entrypoint does not require a load acquire since it is only changed when
6881 // threads are suspended or running a checkpoint.
6882 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6883 Register ref_reg = ref.AsRegister<Register>();
6884 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006885 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006886 if (short_offset) {
6887 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006888 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006889 __ Nop(); // In forbidden slot.
6890 __ Jialc(T9, thunk_disp);
6891 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006892 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006893 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6894 __ Jalr(T9);
6895 __ Nop(); // In delay slot.
6896 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006897 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006898 } else {
6899 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006900 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006901 __ Aui(base, obj, offset_high); // In delay slot.
6902 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006903 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904 } else {
6905 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006906 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006907 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6908 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006909 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006910 __ Addu(base, base, obj); // In delay slot.
6911 }
6912 }
6913 // /* HeapReference<Object> */ ref = *(obj + offset)
6914 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6915 if (needs_null_check) {
6916 MaybeRecordImplicitNullCheck(instruction);
6917 }
6918 __ MaybeUnpoisonHeapReference(ref_reg);
6919 __ SetReorder(reordering);
6920 return;
6921 }
6922
Alexey Frunze15958152017-02-09 19:08:30 -08006923 // /* HeapReference<Object> */ ref = *(obj + offset)
6924 Location no_index = Location::NoLocation();
6925 ScaleFactor no_scale_factor = TIMES_1;
6926 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6927 ref,
6928 obj,
6929 offset,
6930 no_index,
6931 no_scale_factor,
6932 temp,
6933 needs_null_check);
6934}
6935
6936void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6937 Location ref,
6938 Register obj,
6939 uint32_t data_offset,
6940 Location index,
6941 Location temp,
6942 bool needs_null_check) {
6943 DCHECK(kEmitCompilerReadBarrier);
6944 DCHECK(kUseBakerReadBarrier);
6945
6946 static_assert(
6947 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6948 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006949 ScaleFactor scale_factor = TIMES_4;
6950
6951 if (kBakerReadBarrierThunksEnableForArrays) {
6952 // Note that we do not actually check the value of `GetIsGcMarking()`
6953 // to decide whether to mark the loaded reference or not. Instead, we
6954 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6955 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6956 // vice versa.
6957 //
6958 // We use thunks for the slow path. That thunk checks the reference
6959 // and jumps to the entrypoint if needed. If the holder is not gray,
6960 // it issues a load-load memory barrier and returns to the original
6961 // reference load.
6962 //
6963 // temp = Thread::Current()->pReadBarrierMarkReg00
6964 // // AKA &art_quick_read_barrier_mark_introspection.
6965 // if (temp != nullptr) {
6966 // temp = &field_array_thunk<holder_reg>
6967 // temp()
6968 // }
6969 // not_gray_return_address:
6970 // // The element address is pre-calculated in the TMP register before the
6971 // // thunk invocation and the thunk benefits from it.
6972 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6973 // gray_return_address:
6974
6975 DCHECK(temp.IsInvalid());
6976 DCHECK(index.IsValid());
6977 bool reordering = __ SetReorder(false);
6978 const int32_t entry_point_offset =
6979 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6980 // We will not do the explicit null check in the thunk as some form of a null check
6981 // must've been done earlier.
6982 DCHECK(!needs_null_check);
6983 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6984 // Loading the entrypoint does not require a load acquire since it is only changed when
6985 // threads are suspended or running a checkpoint.
6986 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6987 Register ref_reg = ref.AsRegister<Register>();
6988 Register index_reg = index.IsRegisterPair()
6989 ? index.AsRegisterPairLow<Register>()
6990 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006991 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006992 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006993 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006994 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6995 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006996 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006997 } else {
6998 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006999 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007000 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7001 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007002 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007003 __ Addu(TMP, TMP, obj); // In delay slot.
7004 }
7005 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7006 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7007 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7008 __ MaybeUnpoisonHeapReference(ref_reg);
7009 __ SetReorder(reordering);
7010 return;
7011 }
7012
Alexey Frunze15958152017-02-09 19:08:30 -08007013 // /* HeapReference<Object> */ ref =
7014 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007015 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7016 ref,
7017 obj,
7018 data_offset,
7019 index,
7020 scale_factor,
7021 temp,
7022 needs_null_check);
7023}
7024
7025void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7026 Location ref,
7027 Register obj,
7028 uint32_t offset,
7029 Location index,
7030 ScaleFactor scale_factor,
7031 Location temp,
7032 bool needs_null_check,
7033 bool always_update_field) {
7034 DCHECK(kEmitCompilerReadBarrier);
7035 DCHECK(kUseBakerReadBarrier);
7036
7037 // In slow path based read barriers, the read barrier call is
7038 // inserted after the original load. However, in fast path based
7039 // Baker's read barriers, we need to perform the load of
7040 // mirror::Object::monitor_ *before* the original reference load.
7041 // This load-load ordering is required by the read barrier.
7042 // The fast path/slow path (for Baker's algorithm) should look like:
7043 //
7044 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7045 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7046 // HeapReference<Object> ref = *src; // Original reference load.
7047 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7048 // if (is_gray) {
7049 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7050 // }
7051 //
7052 // Note: the original implementation in ReadBarrier::Barrier is
7053 // slightly more complex as it performs additional checks that we do
7054 // not do here for performance reasons.
7055
7056 Register ref_reg = ref.AsRegister<Register>();
7057 Register temp_reg = temp.AsRegister<Register>();
7058 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7059
7060 // /* int32_t */ monitor = obj->monitor_
7061 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7062 if (needs_null_check) {
7063 MaybeRecordImplicitNullCheck(instruction);
7064 }
7065 // /* LockWord */ lock_word = LockWord(monitor)
7066 static_assert(sizeof(LockWord) == sizeof(int32_t),
7067 "art::LockWord and int32_t have different sizes.");
7068
7069 __ Sync(0); // Barrier to prevent load-load reordering.
7070
7071 // The actual reference load.
7072 if (index.IsValid()) {
7073 // Load types involving an "index": ArrayGet,
7074 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7075 // intrinsics.
7076 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7077 if (index.IsConstant()) {
7078 size_t computed_offset =
7079 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7080 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7081 } else {
7082 // Handle the special case of the
7083 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7084 // intrinsics, which use a register pair as index ("long
7085 // offset"), of which only the low part contains data.
7086 Register index_reg = index.IsRegisterPair()
7087 ? index.AsRegisterPairLow<Register>()
7088 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007089 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007090 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7091 }
7092 } else {
7093 // /* HeapReference<Object> */ ref = *(obj + offset)
7094 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7095 }
7096
7097 // Object* ref = ref_addr->AsMirrorPtr()
7098 __ MaybeUnpoisonHeapReference(ref_reg);
7099
7100 // Slow path marking the object `ref` when it is gray.
7101 SlowPathCodeMIPS* slow_path;
7102 if (always_update_field) {
7103 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7104 // of the form `obj + field_offset`, where `obj` is a register and
7105 // `field_offset` is a register pair (of which only the lower half
7106 // is used). Thus `offset` and `scale_factor` above are expected
7107 // to be null in this code path.
7108 DCHECK_EQ(offset, 0u);
7109 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007110 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007111 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7112 ref,
7113 obj,
7114 /* field_offset */ index,
7115 temp_reg);
7116 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007117 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007118 }
7119 AddSlowPath(slow_path);
7120
7121 // if (rb_state == ReadBarrier::GrayState())
7122 // ref = ReadBarrier::Mark(ref);
7123 // Given the numeric representation, it's enough to check the low bit of the
7124 // rb_state. We do that by shifting the bit into the sign bit (31) and
7125 // performing a branch on less than zero.
7126 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7127 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7128 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7129 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7130 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7131 __ Bind(slow_path->GetExitLabel());
7132}
7133
7134void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7135 Location out,
7136 Location ref,
7137 Location obj,
7138 uint32_t offset,
7139 Location index) {
7140 DCHECK(kEmitCompilerReadBarrier);
7141
7142 // Insert a slow path based read barrier *after* the reference load.
7143 //
7144 // If heap poisoning is enabled, the unpoisoning of the loaded
7145 // reference will be carried out by the runtime within the slow
7146 // path.
7147 //
7148 // Note that `ref` currently does not get unpoisoned (when heap
7149 // poisoning is enabled), which is alright as the `ref` argument is
7150 // not used by the artReadBarrierSlow entry point.
7151 //
7152 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007153 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007154 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7155 AddSlowPath(slow_path);
7156
7157 __ B(slow_path->GetEntryLabel());
7158 __ Bind(slow_path->GetExitLabel());
7159}
7160
7161void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7162 Location out,
7163 Location ref,
7164 Location obj,
7165 uint32_t offset,
7166 Location index) {
7167 if (kEmitCompilerReadBarrier) {
7168 // Baker's read barriers shall be handled by the fast path
7169 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7170 DCHECK(!kUseBakerReadBarrier);
7171 // If heap poisoning is enabled, unpoisoning will be taken care of
7172 // by the runtime within the slow path.
7173 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7174 } else if (kPoisonHeapReferences) {
7175 __ UnpoisonHeapReference(out.AsRegister<Register>());
7176 }
7177}
7178
7179void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7180 Location out,
7181 Location root) {
7182 DCHECK(kEmitCompilerReadBarrier);
7183
7184 // Insert a slow path based read barrier *after* the GC root load.
7185 //
7186 // Note that GC roots are not affected by heap poisoning, so we do
7187 // not need to do anything special for this here.
7188 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007189 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007190 AddSlowPath(slow_path);
7191
7192 __ B(slow_path->GetEntryLabel());
7193 __ Bind(slow_path->GetExitLabel());
7194}
7195
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007196void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007197 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7198 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007199 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007200 switch (type_check_kind) {
7201 case TypeCheckKind::kExactCheck:
7202 case TypeCheckKind::kAbstractClassCheck:
7203 case TypeCheckKind::kClassHierarchyCheck:
7204 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007205 call_kind =
7206 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007207 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007208 break;
7209 case TypeCheckKind::kArrayCheck:
7210 case TypeCheckKind::kUnresolvedCheck:
7211 case TypeCheckKind::kInterfaceCheck:
7212 call_kind = LocationSummary::kCallOnSlowPath;
7213 break;
7214 }
7215
Vladimir Markoca6fff82017-10-03 14:49:14 +01007216 LocationSummary* locations =
7217 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007218 if (baker_read_barrier_slow_path) {
7219 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7220 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007221 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007222 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007223 // The output does overlap inputs.
7224 // Note that TypeCheckSlowPathMIPS uses this register too.
7225 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007226 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007227}
7228
7229void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007230 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007231 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007232 Location obj_loc = locations->InAt(0);
7233 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007234 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007235 Location out_loc = locations->Out();
7236 Register out = out_loc.AsRegister<Register>();
7237 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7238 DCHECK_LE(num_temps, 1u);
7239 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007240 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7241 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7242 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7243 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007244 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007245 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007246
7247 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007248 // Avoid this check if we know `obj` is not null.
7249 if (instruction->MustDoNullCheck()) {
7250 __ Move(out, ZERO);
7251 __ Beqz(obj, &done);
7252 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007253
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007254 switch (type_check_kind) {
7255 case TypeCheckKind::kExactCheck: {
7256 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007257 GenerateReferenceLoadTwoRegisters(instruction,
7258 out_loc,
7259 obj_loc,
7260 class_offset,
7261 maybe_temp_loc,
7262 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007263 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007264 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007265 __ Sltiu(out, out, 1);
7266 break;
7267 }
7268
7269 case TypeCheckKind::kAbstractClassCheck: {
7270 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007271 GenerateReferenceLoadTwoRegisters(instruction,
7272 out_loc,
7273 obj_loc,
7274 class_offset,
7275 maybe_temp_loc,
7276 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007277 // If the class is abstract, we eagerly fetch the super class of the
7278 // object to avoid doing a comparison we know will fail.
7279 MipsLabel loop;
7280 __ Bind(&loop);
7281 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007282 GenerateReferenceLoadOneRegister(instruction,
7283 out_loc,
7284 super_offset,
7285 maybe_temp_loc,
7286 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007287 // If `out` is null, we use it for the result, and jump to `done`.
7288 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007289 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007290 __ LoadConst32(out, 1);
7291 break;
7292 }
7293
7294 case TypeCheckKind::kClassHierarchyCheck: {
7295 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007296 GenerateReferenceLoadTwoRegisters(instruction,
7297 out_loc,
7298 obj_loc,
7299 class_offset,
7300 maybe_temp_loc,
7301 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007302 // Walk over the class hierarchy to find a match.
7303 MipsLabel loop, success;
7304 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007305 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007306 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007307 GenerateReferenceLoadOneRegister(instruction,
7308 out_loc,
7309 super_offset,
7310 maybe_temp_loc,
7311 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007312 __ Bnez(out, &loop);
7313 // If `out` is null, we use it for the result, and jump to `done`.
7314 __ B(&done);
7315 __ Bind(&success);
7316 __ LoadConst32(out, 1);
7317 break;
7318 }
7319
7320 case TypeCheckKind::kArrayObjectCheck: {
7321 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007322 GenerateReferenceLoadTwoRegisters(instruction,
7323 out_loc,
7324 obj_loc,
7325 class_offset,
7326 maybe_temp_loc,
7327 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007328 // Do an exact check.
7329 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007330 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007331 // Otherwise, we need to check that the object's class is a non-primitive array.
7332 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007333 GenerateReferenceLoadOneRegister(instruction,
7334 out_loc,
7335 component_offset,
7336 maybe_temp_loc,
7337 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007338 // If `out` is null, we use it for the result, and jump to `done`.
7339 __ Beqz(out, &done);
7340 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7341 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7342 __ Sltiu(out, out, 1);
7343 __ B(&done);
7344 __ Bind(&success);
7345 __ LoadConst32(out, 1);
7346 break;
7347 }
7348
7349 case TypeCheckKind::kArrayCheck: {
7350 // No read barrier since the slow path will retry upon failure.
7351 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007352 GenerateReferenceLoadTwoRegisters(instruction,
7353 out_loc,
7354 obj_loc,
7355 class_offset,
7356 maybe_temp_loc,
7357 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007358 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007359 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7360 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007361 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007362 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007363 __ LoadConst32(out, 1);
7364 break;
7365 }
7366
7367 case TypeCheckKind::kUnresolvedCheck:
7368 case TypeCheckKind::kInterfaceCheck: {
7369 // Note that we indeed only call on slow path, but we always go
7370 // into the slow path for the unresolved and interface check
7371 // cases.
7372 //
7373 // We cannot directly call the InstanceofNonTrivial runtime
7374 // entry point without resorting to a type checking slow path
7375 // here (i.e. by calling InvokeRuntime directly), as it would
7376 // require to assign fixed registers for the inputs of this
7377 // HInstanceOf instruction (following the runtime calling
7378 // convention), which might be cluttered by the potential first
7379 // read barrier emission at the beginning of this method.
7380 //
7381 // TODO: Introduce a new runtime entry point taking the object
7382 // to test (instead of its class) as argument, and let it deal
7383 // with the read barrier issues. This will let us refactor this
7384 // case of the `switch` code as it was previously (with a direct
7385 // call to the runtime not using a type checking slow path).
7386 // This should also be beneficial for the other cases above.
7387 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007388 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7389 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007390 codegen_->AddSlowPath(slow_path);
7391 __ B(slow_path->GetEntryLabel());
7392 break;
7393 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007394 }
7395
7396 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007397
7398 if (slow_path != nullptr) {
7399 __ Bind(slow_path->GetExitLabel());
7400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007401}
7402
7403void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007404 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007405 locations->SetOut(Location::ConstantLocation(constant));
7406}
7407
7408void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7409 // Will be generated at use site.
7410}
7411
7412void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007413 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007414 locations->SetOut(Location::ConstantLocation(constant));
7415}
7416
7417void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7418 // Will be generated at use site.
7419}
7420
7421void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7422 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7423 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7424}
7425
7426void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7427 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007428 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007429 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007430 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007431}
7432
7433void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7434 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7435 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007436 Location receiver = invoke->GetLocations()->InAt(0);
7437 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007438 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007439
7440 // Set the hidden argument.
7441 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7442 invoke->GetDexMethodIndex());
7443
7444 // temp = object->GetClass();
7445 if (receiver.IsStackSlot()) {
7446 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7447 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7448 } else {
7449 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7450 }
7451 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007452 // Instead of simply (possibly) unpoisoning `temp` here, we should
7453 // emit a read barrier for the previous class reference load.
7454 // However this is not required in practice, as this is an
7455 // intermediate/temporary reference and because the current
7456 // concurrent copying collector keeps the from-space memory
7457 // intact/accessible until the end of the marking phase (the
7458 // concurrent copying collector may not in the future).
7459 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007460 __ LoadFromOffset(kLoadWord, temp, temp,
7461 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7462 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007463 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007464 // temp = temp->GetImtEntryAt(method_offset);
7465 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7466 // T9 = temp->GetEntryPoint();
7467 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7468 // T9();
7469 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007470 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007471 DCHECK(!codegen_->IsLeafMethod());
7472 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7473}
7474
7475void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007476 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7477 if (intrinsic.TryDispatch(invoke)) {
7478 return;
7479 }
7480
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007481 HandleInvoke(invoke);
7482}
7483
7484void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007485 // Explicit clinit checks triggered by static invokes must have been pruned by
7486 // art::PrepareForRegisterAllocation.
7487 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007488
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007489 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007490 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7491 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007492
Chris Larsen701566a2015-10-27 15:29:13 -07007493 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7494 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007495 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7496 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7497 }
Chris Larsen701566a2015-10-27 15:29:13 -07007498 return;
7499 }
7500
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007501 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007502
7503 // Add the extra input register if either the dex cache array base register
7504 // or the PC-relative base register for accessing literals is needed.
7505 if (has_extra_input) {
7506 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7507 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007508}
7509
Orion Hodsonac141392017-01-13 11:53:47 +00007510void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7511 HandleInvoke(invoke);
7512}
7513
7514void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7515 codegen_->GenerateInvokePolymorphicCall(invoke);
7516}
7517
Chris Larsen701566a2015-10-27 15:29:13 -07007518static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007519 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007520 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7521 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007522 return true;
7523 }
7524 return false;
7525}
7526
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007527HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007528 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007529 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007530 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007531 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007532 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007533 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007534 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007535 case HLoadString::LoadKind::kJitTableAddress:
7536 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007537 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007538 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007539 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007540 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007541 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007542 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007543}
7544
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007545HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7546 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007547 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007548 case HLoadClass::LoadKind::kInvalid:
7549 LOG(FATAL) << "UNREACHABLE";
7550 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007551 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007552 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007553 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007554 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007555 case HLoadClass::LoadKind::kBssEntry:
7556 DCHECK(!Runtime::Current()->UseJitCompilation());
7557 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007558 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007559 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007560 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007561 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007562 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007563 break;
7564 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007565 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007566}
7567
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007568Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7569 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007570 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007571 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007572 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7573 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7574 if (!invoke->GetLocations()->Intrinsified()) {
7575 return location.AsRegister<Register>();
7576 }
7577 // For intrinsics we allow any location, so it may be on the stack.
7578 if (!location.IsRegister()) {
7579 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7580 return temp;
7581 }
7582 // For register locations, check if the register was saved. If so, get it from the stack.
7583 // Note: There is a chance that the register was saved but not overwritten, so we could
7584 // save one load. However, since this is just an intrinsic slow path we prefer this
7585 // simple and more robust approach rather that trying to determine if that's the case.
7586 SlowPathCode* slow_path = GetCurrentSlowPath();
7587 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7588 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7589 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7590 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7591 return temp;
7592 }
7593 return location.AsRegister<Register>();
7594}
7595
Vladimir Markodc151b22015-10-15 18:02:30 +01007596HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7597 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007598 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007599 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007600}
7601
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007602void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7603 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007604 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007605 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007606 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7607 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007608 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007609 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7610 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007611 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7612 : ZERO;
7613
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007614 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007615 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007616 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007617 uint32_t offset =
7618 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619 __ LoadFromOffset(kLoadWord,
7620 temp.AsRegister<Register>(),
7621 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007622 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007623 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007624 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007625 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007626 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007627 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007628 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7629 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007630 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7631 PcRelativePatchInfo* info_low =
7632 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007633 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007634 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7635 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007636 break;
7637 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007638 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7639 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7640 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007641 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007642 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007643 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007644 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7645 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007646 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007647 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7648 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007649 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007650 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007651 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7652 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7653 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007654 }
7655 }
7656
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007657 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007658 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007659 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007660 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007661 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7662 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007663 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007664 T9,
7665 callee_method.AsRegister<Register>(),
7666 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007667 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668 // T9()
7669 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007670 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007671 break;
7672 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007673 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7674
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007675 DCHECK(!IsLeafMethod());
7676}
7677
7678void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007679 // Explicit clinit checks triggered by static invokes must have been pruned by
7680 // art::PrepareForRegisterAllocation.
7681 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007682
7683 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7684 return;
7685 }
7686
7687 LocationSummary* locations = invoke->GetLocations();
7688 codegen_->GenerateStaticOrDirectCall(invoke,
7689 locations->HasTemps()
7690 ? locations->GetTemp(0)
7691 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007692}
7693
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007694void CodeGeneratorMIPS::GenerateVirtualCall(
7695 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007696 // Use the calling convention instead of the location of the receiver, as
7697 // intrinsics may have put the receiver in a different register. In the intrinsics
7698 // slow path, the arguments have been moved to the right place, so here we are
7699 // guaranteed that the receiver is the first register of the calling convention.
7700 InvokeDexCallingConvention calling_convention;
7701 Register receiver = calling_convention.GetRegisterAt(0);
7702
Chris Larsen3acee732015-11-18 13:31:08 -08007703 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007704 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7705 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7706 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007707 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007708
7709 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007710 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007711 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007712 // Instead of simply (possibly) unpoisoning `temp` here, we should
7713 // emit a read barrier for the previous class reference load.
7714 // However this is not required in practice, as this is an
7715 // intermediate/temporary reference and because the current
7716 // concurrent copying collector keeps the from-space memory
7717 // intact/accessible until the end of the marking phase (the
7718 // concurrent copying collector may not in the future).
7719 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007720 // temp = temp->GetMethodAt(method_offset);
7721 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7722 // T9 = temp->GetEntryPoint();
7723 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7724 // T9();
7725 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007726 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007727 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007728}
7729
7730void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7731 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7732 return;
7733 }
7734
7735 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007736 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007737}
7738
7739void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007740 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007741 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007742 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007743 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7744 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007745 return;
7746 }
Vladimir Marko41559982017-01-06 14:04:23 +00007747 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007748 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007749 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007750 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7751 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007752 ? LocationSummary::kCallOnSlowPath
7753 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007754 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007755 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7756 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7757 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007758 switch (load_kind) {
7759 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007761 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007762 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007763 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007764 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007765 break;
7766 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007767 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007768 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7769 codegen_->ClobberRA();
7770 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007771 break;
7772 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007773 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007774 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007775 locations->SetInAt(0, Location::RequiresRegister());
7776 break;
7777 default:
7778 break;
7779 }
7780 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007781 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7782 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7783 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007784 RegisterSet caller_saves = RegisterSet::Empty();
7785 InvokeRuntimeCallingConvention calling_convention;
7786 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7787 locations->SetCustomSlowPathCallerSaves(caller_saves);
7788 } else {
7789 // For non-Baker read barriers we have a temp-clobbering call.
7790 }
7791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007792}
7793
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007794// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7795// move.
7796void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007797 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007798 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007799 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007800 return;
7801 }
Vladimir Marko41559982017-01-06 14:04:23 +00007802 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007803
Vladimir Marko41559982017-01-06 14:04:23 +00007804 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007805 Location out_loc = locations->Out();
7806 Register out = out_loc.AsRegister<Register>();
7807 Register base_or_current_method_reg;
7808 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007809 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007810 switch (load_kind) {
7811 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007812 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007813 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007814 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007815 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007816 base_or_current_method_reg =
7817 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007818 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007819 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007820 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007821 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7822 break;
7823 default:
7824 base_or_current_method_reg = ZERO;
7825 break;
7826 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007827
Alexey Frunze15958152017-02-09 19:08:30 -08007828 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7829 ? kWithoutReadBarrier
7830 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007831 bool generate_null_check = false;
7832 switch (load_kind) {
7833 case HLoadClass::LoadKind::kReferrersClass: {
7834 DCHECK(!cls->CanCallRuntime());
7835 DCHECK(!cls->MustGenerateClinitCheck());
7836 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7837 GenerateGcRootFieldLoad(cls,
7838 out_loc,
7839 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007840 ArtMethod::DeclaringClassOffset().Int32Value(),
7841 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 break;
7843 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007844 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007845 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007846 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007847 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007848 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007849 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7850 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007851 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7852 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007853 base_or_current_method_reg);
7854 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007855 break;
7856 }
7857 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007858 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007859 uint32_t address = dchecked_integral_cast<uint32_t>(
7860 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7861 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007862 if (isR6 || !has_irreducible_loops) {
7863 __ LoadLiteral(out,
7864 base_or_current_method_reg,
7865 codegen_->DeduplicateBootImageAddressLiteral(address));
7866 } else {
7867 __ LoadConst32(out, address);
7868 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007869 break;
7870 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007871 case HLoadClass::LoadKind::kBootImageClassTable: {
7872 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7873 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7874 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7875 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7876 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7877 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7878 out,
7879 base_or_current_method_reg);
7880 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7881 // Extract the reference from the slot data, i.e. clear the hash bits.
7882 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7883 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7884 if (masked_hash != 0) {
7885 __ Addiu(out, out, -masked_hash);
7886 }
7887 break;
7888 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007889 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007890 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7891 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007892 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7893 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007894 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007895 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007896 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007897 GenerateGcRootFieldLoad(cls,
7898 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007899 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007900 /* placeholder */ 0x5678,
7901 read_barrier_option,
7902 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007903 generate_null_check = true;
7904 break;
7905 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007906 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007907 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7908 cls->GetTypeIndex(),
7909 cls->GetClass());
7910 bool reordering = __ SetReorder(false);
7911 __ Bind(&info->high_label);
7912 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007913 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007914 GenerateGcRootFieldLoad(cls,
7915 out_loc,
7916 out,
7917 /* placeholder */ 0x5678,
7918 read_barrier_option,
7919 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007920 break;
7921 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007922 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007923 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007924 LOG(FATAL) << "UNREACHABLE";
7925 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007926 }
7927
7928 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7929 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007930 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007931 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007932 codegen_->AddSlowPath(slow_path);
7933 if (generate_null_check) {
7934 __ Beqz(out, slow_path->GetEntryLabel());
7935 }
7936 if (cls->MustGenerateClinitCheck()) {
7937 GenerateClassInitializationCheck(slow_path, out);
7938 } else {
7939 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007940 }
7941 }
7942}
7943
7944static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007945 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007946}
7947
7948void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7949 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007950 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007951 locations->SetOut(Location::RequiresRegister());
7952}
7953
7954void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7955 Register out = load->GetLocations()->Out().AsRegister<Register>();
7956 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7957}
7958
7959void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007960 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007961}
7962
7963void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7964 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7965}
7966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007967void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007968 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007969 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007970 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007971 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007972 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007973 switch (load_kind) {
7974 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007975 case HLoadString::LoadKind::kBootImageAddress:
7976 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007977 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007978 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007979 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007980 break;
7981 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007982 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007983 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7984 codegen_->ClobberRA();
7985 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007986 break;
7987 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007988 FALLTHROUGH_INTENDED;
7989 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007990 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007991 locations->SetInAt(0, Location::RequiresRegister());
7992 break;
7993 default:
7994 break;
7995 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007996 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007997 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007998 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007999 } else {
8000 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008001 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8002 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8003 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008004 RegisterSet caller_saves = RegisterSet::Empty();
8005 InvokeRuntimeCallingConvention calling_convention;
8006 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8007 locations->SetCustomSlowPathCallerSaves(caller_saves);
8008 } else {
8009 // For non-Baker read barriers we have a temp-clobbering call.
8010 }
8011 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008012 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008013}
8014
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008015// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8016// move.
8017void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008018 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008019 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008020 Location out_loc = locations->Out();
8021 Register out = out_loc.AsRegister<Register>();
8022 Register base_or_current_method_reg;
8023 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008024 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008025 switch (load_kind) {
8026 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008027 case HLoadString::LoadKind::kBootImageAddress:
8028 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008029 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008030 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008031 base_or_current_method_reg =
8032 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008033 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008034 default:
8035 base_or_current_method_reg = ZERO;
8036 break;
8037 }
8038
8039 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008040 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008041 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008042 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008043 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008044 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8045 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008046 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8047 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008048 base_or_current_method_reg);
8049 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008050 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008051 }
8052 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008053 uint32_t address = dchecked_integral_cast<uint32_t>(
8054 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8055 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008056 if (isR6 || !has_irreducible_loops) {
8057 __ LoadLiteral(out,
8058 base_or_current_method_reg,
8059 codegen_->DeduplicateBootImageAddressLiteral(address));
8060 } else {
8061 __ LoadConst32(out, address);
8062 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008063 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008064 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008065 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008066 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008067 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008068 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008069 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8070 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008071 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8072 out,
8073 base_or_current_method_reg);
8074 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8075 return;
8076 }
8077 case HLoadString::LoadKind::kBssEntry: {
8078 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8079 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8080 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8081 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8082 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008083 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008084 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008085 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008086 GenerateGcRootFieldLoad(load,
8087 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008088 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008089 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008090 kCompilerReadBarrierOption,
8091 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008092 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008093 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008094 codegen_->AddSlowPath(slow_path);
8095 __ Beqz(out, slow_path->GetEntryLabel());
8096 __ Bind(slow_path->GetExitLabel());
8097 return;
8098 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008099 case HLoadString::LoadKind::kJitTableAddress: {
8100 CodeGeneratorMIPS::JitPatchInfo* info =
8101 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8102 load->GetStringIndex(),
8103 load->GetString());
8104 bool reordering = __ SetReorder(false);
8105 __ Bind(&info->high_label);
8106 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008107 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008108 GenerateGcRootFieldLoad(load,
8109 out_loc,
8110 out,
8111 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008112 kCompilerReadBarrierOption,
8113 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008114 return;
8115 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008116 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008117 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008118 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008119
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008120 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008121 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008122 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008123 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008124 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008125 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8126 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008127}
8128
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008129void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008130 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008131 locations->SetOut(Location::ConstantLocation(constant));
8132}
8133
8134void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8135 // Will be generated at use site.
8136}
8137
8138void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008139 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8140 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008141 InvokeRuntimeCallingConvention calling_convention;
8142 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8143}
8144
8145void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8146 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008147 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008148 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8149 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008150 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008151 }
8152 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8153}
8154
8155void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008157 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008158 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008159 case DataType::Type::kInt32:
8160 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008161 locations->SetInAt(0, Location::RequiresRegister());
8162 locations->SetInAt(1, Location::RequiresRegister());
8163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8164 break;
8165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008166 case DataType::Type::kFloat32:
8167 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008168 locations->SetInAt(0, Location::RequiresFpuRegister());
8169 locations->SetInAt(1, Location::RequiresFpuRegister());
8170 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8171 break;
8172
8173 default:
8174 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8175 }
8176}
8177
8178void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008179 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008180 LocationSummary* locations = instruction->GetLocations();
8181 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8182
8183 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008184 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008185 Register dst = locations->Out().AsRegister<Register>();
8186 Register lhs = locations->InAt(0).AsRegister<Register>();
8187 Register rhs = locations->InAt(1).AsRegister<Register>();
8188
8189 if (isR6) {
8190 __ MulR6(dst, lhs, rhs);
8191 } else {
8192 __ MulR2(dst, lhs, rhs);
8193 }
8194 break;
8195 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008196 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008197 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8198 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8199 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8200 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8201 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8202 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8203
8204 // Extra checks to protect caused by the existance of A1_A2.
8205 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8206 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8207 DCHECK_NE(dst_high, lhs_low);
8208 DCHECK_NE(dst_high, rhs_low);
8209
8210 // A_B * C_D
8211 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8212 // dst_lo: [ low(B*D) ]
8213 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8214
8215 if (isR6) {
8216 __ MulR6(TMP, lhs_high, rhs_low);
8217 __ MulR6(dst_high, lhs_low, rhs_high);
8218 __ Addu(dst_high, dst_high, TMP);
8219 __ MuhuR6(TMP, lhs_low, rhs_low);
8220 __ Addu(dst_high, dst_high, TMP);
8221 __ MulR6(dst_low, lhs_low, rhs_low);
8222 } else {
8223 __ MulR2(TMP, lhs_high, rhs_low);
8224 __ MulR2(dst_high, lhs_low, rhs_high);
8225 __ Addu(dst_high, dst_high, TMP);
8226 __ MultuR2(lhs_low, rhs_low);
8227 __ Mfhi(TMP);
8228 __ Addu(dst_high, dst_high, TMP);
8229 __ Mflo(dst_low);
8230 }
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 lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008238 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008239 __ MulS(dst, lhs, rhs);
8240 } else {
8241 __ MulD(dst, lhs, rhs);
8242 }
8243 break;
8244 }
8245 default:
8246 LOG(FATAL) << "Unexpected mul type " << type;
8247 }
8248}
8249
8250void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008252 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008253 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008254 case DataType::Type::kInt32:
8255 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008256 locations->SetInAt(0, Location::RequiresRegister());
8257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8258 break;
8259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008260 case DataType::Type::kFloat32:
8261 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008262 locations->SetInAt(0, Location::RequiresFpuRegister());
8263 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8264 break;
8265
8266 default:
8267 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8268 }
8269}
8270
8271void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008272 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008273 LocationSummary* locations = instruction->GetLocations();
8274
8275 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008276 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008277 Register dst = locations->Out().AsRegister<Register>();
8278 Register src = locations->InAt(0).AsRegister<Register>();
8279 __ Subu(dst, ZERO, src);
8280 break;
8281 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008282 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8284 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8285 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8286 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8287 __ Subu(dst_low, ZERO, src_low);
8288 __ Sltu(TMP, ZERO, dst_low);
8289 __ Subu(dst_high, ZERO, src_high);
8290 __ Subu(dst_high, dst_high, TMP);
8291 break;
8292 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008293 case DataType::Type::kFloat32:
8294 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008295 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8296 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008297 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298 __ NegS(dst, src);
8299 } else {
8300 __ NegD(dst, src);
8301 }
8302 break;
8303 }
8304 default:
8305 LOG(FATAL) << "Unexpected neg type " << type;
8306 }
8307}
8308
8309void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008310 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8311 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008312 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008313 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008314 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8315 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008316}
8317
8318void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008319 // Note: if heap poisoning is enabled, the entry point takes care
8320 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008321 QuickEntrypointEnum entrypoint =
8322 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8323 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008324 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008325 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008326}
8327
8328void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008329 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8330 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008332 if (instruction->IsStringAlloc()) {
8333 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8334 } else {
8335 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008336 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008337 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008338}
8339
8340void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008341 // Note: if heap poisoning is enabled, the entry point takes care
8342 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008343 if (instruction->IsStringAlloc()) {
8344 // String is allocated through StringFactory. Call NewEmptyString entry point.
8345 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008346 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008347 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8348 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8349 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008350 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008351 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8352 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008353 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008354 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008356}
8357
8358void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008359 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008360 locations->SetInAt(0, Location::RequiresRegister());
8361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8362}
8363
8364void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008365 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 LocationSummary* locations = instruction->GetLocations();
8367
8368 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008369 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008370 Register dst = locations->Out().AsRegister<Register>();
8371 Register src = locations->InAt(0).AsRegister<Register>();
8372 __ Nor(dst, src, ZERO);
8373 break;
8374 }
8375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008376 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8378 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8379 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8380 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8381 __ Nor(dst_high, src_high, ZERO);
8382 __ Nor(dst_low, src_low, ZERO);
8383 break;
8384 }
8385
8386 default:
8387 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8388 }
8389}
8390
8391void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008392 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008393 locations->SetInAt(0, Location::RequiresRegister());
8394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8395}
8396
8397void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8398 LocationSummary* locations = instruction->GetLocations();
8399 __ Xori(locations->Out().AsRegister<Register>(),
8400 locations->InAt(0).AsRegister<Register>(),
8401 1);
8402}
8403
8404void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008405 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8406 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008407}
8408
Calin Juravle2ae48182016-03-16 14:05:09 +00008409void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8410 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008411 return;
8412 }
8413 Location obj = instruction->GetLocations()->InAt(0);
8414
8415 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008416 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008417}
8418
Calin Juravle2ae48182016-03-16 14:05:09 +00008419void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008420 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008421 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008422
8423 Location obj = instruction->GetLocations()->InAt(0);
8424
8425 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8426}
8427
8428void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008429 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008430}
8431
8432void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8433 HandleBinaryOp(instruction);
8434}
8435
8436void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8437 HandleBinaryOp(instruction);
8438}
8439
8440void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8441 LOG(FATAL) << "Unreachable";
8442}
8443
8444void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008445 if (instruction->GetNext()->IsSuspendCheck() &&
8446 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8447 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8448 // The back edge will generate the suspend check.
8449 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8450 }
8451
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008452 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8453}
8454
8455void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008456 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008457 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8458 if (location.IsStackSlot()) {
8459 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8460 } else if (location.IsDoubleStackSlot()) {
8461 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8462 }
8463 locations->SetOut(location);
8464}
8465
8466void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8467 ATTRIBUTE_UNUSED) {
8468 // Nothing to do, the parameter is already at its location.
8469}
8470
8471void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8472 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008473 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8475}
8476
8477void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8478 ATTRIBUTE_UNUSED) {
8479 // Nothing to do, the method is already at its location.
8480}
8481
8482void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008483 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008484 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485 locations->SetInAt(i, Location::Any());
8486 }
8487 locations->SetOut(Location::Any());
8488}
8489
8490void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8491 LOG(FATAL) << "Unreachable";
8492}
8493
8494void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008495 DataType::Type type = rem->GetResultType();
8496 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8497 ? LocationSummary::kNoCall
8498 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008499 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008500
8501 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008502 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008503 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008504 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8506 break;
8507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008508 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008509 InvokeRuntimeCallingConvention calling_convention;
8510 locations->SetInAt(0, Location::RegisterPairLocation(
8511 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8512 locations->SetInAt(1, Location::RegisterPairLocation(
8513 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8514 locations->SetOut(calling_convention.GetReturnLocation(type));
8515 break;
8516 }
8517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008518 case DataType::Type::kFloat32:
8519 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520 InvokeRuntimeCallingConvention calling_convention;
8521 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8522 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8523 locations->SetOut(calling_convention.GetReturnLocation(type));
8524 break;
8525 }
8526
8527 default:
8528 LOG(FATAL) << "Unexpected rem type " << type;
8529 }
8530}
8531
8532void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008533 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008534
8535 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008536 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008537 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008539 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008540 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008541 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8542 break;
8543 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008544 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008545 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008546 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008547 break;
8548 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008549 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008550 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008551 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008552 break;
8553 }
8554 default:
8555 LOG(FATAL) << "Unexpected rem type " << type;
8556 }
8557}
8558
Igor Murashkind01745e2017-04-05 16:40:31 -07008559void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8560 constructor_fence->SetLocations(nullptr);
8561}
8562
8563void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8564 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8565 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8566}
8567
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8569 memory_barrier->SetLocations(nullptr);
8570}
8571
8572void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8573 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8574}
8575
8576void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008577 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008578 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008579 locations->SetInAt(0, MipsReturnLocation(return_type));
8580}
8581
8582void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8583 codegen_->GenerateFrameExit();
8584}
8585
8586void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8587 ret->SetLocations(nullptr);
8588}
8589
8590void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8591 codegen_->GenerateFrameExit();
8592}
8593
Alexey Frunze92d90602015-12-18 18:16:36 -08008594void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8595 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008596}
8597
Alexey Frunze92d90602015-12-18 18:16:36 -08008598void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8599 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008600}
8601
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008602void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8603 HandleShift(shl);
8604}
8605
8606void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8607 HandleShift(shl);
8608}
8609
8610void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8611 HandleShift(shr);
8612}
8613
8614void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8615 HandleShift(shr);
8616}
8617
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008618void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8619 HandleBinaryOp(instruction);
8620}
8621
8622void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8623 HandleBinaryOp(instruction);
8624}
8625
8626void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8627 HandleFieldGet(instruction, instruction->GetFieldInfo());
8628}
8629
8630void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8631 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8632}
8633
8634void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8635 HandleFieldSet(instruction, instruction->GetFieldInfo());
8636}
8637
8638void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008639 HandleFieldSet(instruction,
8640 instruction->GetFieldInfo(),
8641 instruction->GetDexPc(),
8642 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008643}
8644
8645void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8646 HUnresolvedInstanceFieldGet* instruction) {
8647 FieldAccessCallingConventionMIPS calling_convention;
8648 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8649 instruction->GetFieldType(),
8650 calling_convention);
8651}
8652
8653void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8654 HUnresolvedInstanceFieldGet* instruction) {
8655 FieldAccessCallingConventionMIPS calling_convention;
8656 codegen_->GenerateUnresolvedFieldAccess(instruction,
8657 instruction->GetFieldType(),
8658 instruction->GetFieldIndex(),
8659 instruction->GetDexPc(),
8660 calling_convention);
8661}
8662
8663void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8664 HUnresolvedInstanceFieldSet* instruction) {
8665 FieldAccessCallingConventionMIPS calling_convention;
8666 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8667 instruction->GetFieldType(),
8668 calling_convention);
8669}
8670
8671void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8672 HUnresolvedInstanceFieldSet* instruction) {
8673 FieldAccessCallingConventionMIPS calling_convention;
8674 codegen_->GenerateUnresolvedFieldAccess(instruction,
8675 instruction->GetFieldType(),
8676 instruction->GetFieldIndex(),
8677 instruction->GetDexPc(),
8678 calling_convention);
8679}
8680
8681void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8682 HUnresolvedStaticFieldGet* instruction) {
8683 FieldAccessCallingConventionMIPS calling_convention;
8684 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8685 instruction->GetFieldType(),
8686 calling_convention);
8687}
8688
8689void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8690 HUnresolvedStaticFieldGet* instruction) {
8691 FieldAccessCallingConventionMIPS calling_convention;
8692 codegen_->GenerateUnresolvedFieldAccess(instruction,
8693 instruction->GetFieldType(),
8694 instruction->GetFieldIndex(),
8695 instruction->GetDexPc(),
8696 calling_convention);
8697}
8698
8699void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8700 HUnresolvedStaticFieldSet* instruction) {
8701 FieldAccessCallingConventionMIPS calling_convention;
8702 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8703 instruction->GetFieldType(),
8704 calling_convention);
8705}
8706
8707void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8708 HUnresolvedStaticFieldSet* instruction) {
8709 FieldAccessCallingConventionMIPS calling_convention;
8710 codegen_->GenerateUnresolvedFieldAccess(instruction,
8711 instruction->GetFieldType(),
8712 instruction->GetFieldIndex(),
8713 instruction->GetDexPc(),
8714 calling_convention);
8715}
8716
8717void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008718 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8719 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008720 // In suspend check slow path, usually there are no caller-save registers at all.
8721 // If SIMD instructions are present, however, we force spilling all live SIMD
8722 // registers in full width (since the runtime only saves/restores lower part).
8723 locations->SetCustomSlowPathCallerSaves(
8724 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725}
8726
8727void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8728 HBasicBlock* block = instruction->GetBlock();
8729 if (block->GetLoopInformation() != nullptr) {
8730 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8731 // The back edge will generate the suspend check.
8732 return;
8733 }
8734 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8735 // The goto will generate the suspend check.
8736 return;
8737 }
8738 GenerateSuspendCheck(instruction, nullptr);
8739}
8740
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008741void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008742 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8743 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 InvokeRuntimeCallingConvention calling_convention;
8745 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8746}
8747
8748void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008749 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8751}
8752
8753void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008754 DataType::Type input_type = conversion->GetInputType();
8755 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008756 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8757 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008758 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008760 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8761 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008762 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8763 }
8764
8765 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008766 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008767 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8768 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008769 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770 }
8771
Vladimir Markoca6fff82017-10-03 14:49:14 +01008772 LocationSummary* locations =
8773 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008774
8775 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008776 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008777 locations->SetInAt(0, Location::RequiresFpuRegister());
8778 } else {
8779 locations->SetInAt(0, Location::RequiresRegister());
8780 }
8781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008782 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008783 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8784 } else {
8785 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8786 }
8787 } else {
8788 InvokeRuntimeCallingConvention calling_convention;
8789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008790 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008791 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8792 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008793 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 locations->SetInAt(0, Location::RegisterPairLocation(
8795 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8796 }
8797
8798 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8799 }
8800}
8801
8802void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8803 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008804 DataType::Type result_type = conversion->GetResultType();
8805 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008807 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008808
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008809 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8810 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008812 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008813 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8814 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8815 Register src = locations->InAt(0).AsRegister<Register>();
8816
Alexey Frunzea871ef12016-06-27 15:20:11 -07008817 if (dst_low != src) {
8818 __ Move(dst_low, src);
8819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008820 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008821 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008822 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008823 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008824 ? locations->InAt(0).AsRegisterPairLow<Register>()
8825 : locations->InAt(0).AsRegister<Register>();
8826
8827 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008828 case DataType::Type::kUint8:
8829 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008830 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008831 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008832 if (has_sign_extension) {
8833 __ Seb(dst, src);
8834 } else {
8835 __ Sll(dst, src, 24);
8836 __ Sra(dst, dst, 24);
8837 }
8838 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008839 case DataType::Type::kUint16:
8840 __ Andi(dst, src, 0xFFFF);
8841 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008843 if (has_sign_extension) {
8844 __ Seh(dst, src);
8845 } else {
8846 __ Sll(dst, src, 16);
8847 __ Sra(dst, dst, 16);
8848 }
8849 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008850 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008851 if (dst != src) {
8852 __ Move(dst, src);
8853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008854 break;
8855
8856 default:
8857 LOG(FATAL) << "Unexpected type conversion from " << input_type
8858 << " to " << result_type;
8859 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008860 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8861 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008862 if (isR6) {
8863 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8864 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8865 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8866 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8867 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8868 __ Mtc1(src_low, FTMP);
8869 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008870 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008871 __ Cvtsl(dst, FTMP);
8872 } else {
8873 __ Cvtdl(dst, FTMP);
8874 }
8875 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008876 QuickEntrypointEnum entrypoint =
8877 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008878 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008879 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008880 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8881 } else {
8882 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8883 }
8884 }
8885 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008886 Register src = locations->InAt(0).AsRegister<Register>();
8887 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8888 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008889 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008890 __ Cvtsw(dst, FTMP);
8891 } else {
8892 __ Cvtdw(dst, FTMP);
8893 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008894 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008895 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8896 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008897
8898 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8899 // value of the output type if the input is outside of the range after the truncation or
8900 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8901 // results. This matches the desired float/double-to-int/long conversion exactly.
8902 //
8903 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8904 // value when the input is either a NaN or is outside of the range of the output type
8905 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8906 // the same result.
8907 //
8908 // The code takes care of the different behaviors by first comparing the input to the
8909 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8910 // If the input is greater than or equal to the minimum, it procedes to the truncate
8911 // instruction, which will handle such an input the same way irrespective of NAN2008.
8912 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8913 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008914 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008915 if (isR6) {
8916 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8917 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8918 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8919 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8920 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008922 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008923 __ TruncLS(FTMP, src);
8924 } else {
8925 __ TruncLD(FTMP, src);
8926 }
8927 __ Mfc1(dst_low, FTMP);
8928 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008929 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008930 QuickEntrypointEnum entrypoint =
8931 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008932 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008933 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008934 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8935 } else {
8936 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8937 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008938 }
8939 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008940 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8941 Register dst = locations->Out().AsRegister<Register>();
8942 MipsLabel truncate;
8943 MipsLabel done;
8944
Lena Djokicf4e23a82017-05-09 15:43:45 +02008945 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008946 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008947 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8948 __ LoadConst32(TMP, min_val);
8949 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008950 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008951 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8952 __ LoadConst32(TMP, High32Bits(min_val));
8953 __ Mtc1(ZERO, FTMP);
8954 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008955 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008957 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008958 __ ColeS(0, FTMP, src);
8959 } else {
8960 __ ColeD(0, FTMP, src);
8961 }
8962 __ Bc1t(0, &truncate);
8963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008964 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008965 __ CeqS(0, src, src);
8966 } else {
8967 __ CeqD(0, src, src);
8968 }
8969 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8970 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008971
8972 __ B(&done);
8973
8974 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008975 }
8976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008977 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008978 __ TruncWS(FTMP, src);
8979 } else {
8980 __ TruncWD(FTMP, src);
8981 }
8982 __ Mfc1(dst, FTMP);
8983
Lena Djokicf4e23a82017-05-09 15:43:45 +02008984 if (!isR6) {
8985 __ Bind(&done);
8986 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008987 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008988 } else if (DataType::IsFloatingPointType(result_type) &&
8989 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008990 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8991 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008992 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008993 __ Cvtsd(dst, src);
8994 } else {
8995 __ Cvtds(dst, src);
8996 }
8997 } else {
8998 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8999 << " to " << result_type;
9000 }
9001}
9002
9003void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9004 HandleShift(ushr);
9005}
9006
9007void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9008 HandleShift(ushr);
9009}
9010
9011void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9012 HandleBinaryOp(instruction);
9013}
9014
9015void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9016 HandleBinaryOp(instruction);
9017}
9018
9019void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9020 // Nothing to do, this should be removed during prepare for register allocator.
9021 LOG(FATAL) << "Unreachable";
9022}
9023
9024void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9025 // Nothing to do, this should be removed during prepare for register allocator.
9026 LOG(FATAL) << "Unreachable";
9027}
9028
9029void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009030 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009031}
9032
9033void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009034 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009035}
9036
9037void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009038 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009039}
9040
9041void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009042 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009043}
9044
9045void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009046 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009047}
9048
9049void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009050 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009051}
9052
9053void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009054 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009055}
9056
9057void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009058 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009059}
9060
9061void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009062 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009063}
9064
9065void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009066 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009067}
9068
9069void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009070 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009071}
9072
9073void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009074 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009075}
9076
9077void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009078 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009079}
9080
9081void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009082 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009083}
9084
9085void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009086 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009087}
9088
9089void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009090 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009091}
9092
9093void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009094 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009095}
9096
9097void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009098 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009099}
9100
9101void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009102 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009103}
9104
9105void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009106 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009107}
9108
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009109void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9110 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009111 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009112 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009113 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9114 uint32_t num_entries = switch_instr->GetNumEntries();
9115 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9116 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9117 // instruction to simulate PC-relative addressing when accessing the jump table.
9118 // NAL clobbers RA. Make sure RA is preserved.
9119 codegen_->ClobberRA();
9120 }
9121 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009122}
9123
Alexey Frunze96b66822016-09-10 02:32:44 -07009124void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9125 int32_t lower_bound,
9126 uint32_t num_entries,
9127 HBasicBlock* switch_block,
9128 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009129 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009130 Register temp_reg = TMP;
9131 __ Addiu32(temp_reg, value_reg, -lower_bound);
9132 // Jump to default if index is negative
9133 // Note: We don't check the case that index is positive while value < lower_bound, because in
9134 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9135 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9136
Alexey Frunze96b66822016-09-10 02:32:44 -07009137 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009138 // Jump to successors[0] if value == lower_bound.
9139 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9140 int32_t last_index = 0;
9141 for (; num_entries - last_index > 2; last_index += 2) {
9142 __ Addiu(temp_reg, temp_reg, -2);
9143 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9144 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9145 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9146 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9147 }
9148 if (num_entries - last_index == 2) {
9149 // The last missing case_value.
9150 __ Addiu(temp_reg, temp_reg, -1);
9151 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009152 }
9153
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009154 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009155 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009156 __ B(codegen_->GetLabelOf(default_block));
9157 }
9158}
9159
Alexey Frunze96b66822016-09-10 02:32:44 -07009160void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9161 Register constant_area,
9162 int32_t lower_bound,
9163 uint32_t num_entries,
9164 HBasicBlock* switch_block,
9165 HBasicBlock* default_block) {
9166 // Create a jump table.
9167 std::vector<MipsLabel*> labels(num_entries);
9168 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9169 for (uint32_t i = 0; i < num_entries; i++) {
9170 labels[i] = codegen_->GetLabelOf(successors[i]);
9171 }
9172 JumpTable* table = __ CreateJumpTable(std::move(labels));
9173
9174 // Is the value in range?
9175 __ Addiu32(TMP, value_reg, -lower_bound);
9176 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9177 __ Sltiu(AT, TMP, num_entries);
9178 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9179 } else {
9180 __ LoadConst32(AT, num_entries);
9181 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9182 }
9183
9184 // We are in the range of the table.
9185 // Load the target address from the jump table, indexing by the value.
9186 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009187 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009188 __ Lw(TMP, TMP, 0);
9189 // Compute the absolute target address by adding the table start address
9190 // (the table contains offsets to targets relative to its start).
9191 __ Addu(TMP, TMP, AT);
9192 // And jump.
9193 __ Jr(TMP);
9194 __ NopIfNoReordering();
9195}
9196
9197void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9198 int32_t lower_bound = switch_instr->GetStartValue();
9199 uint32_t num_entries = switch_instr->GetNumEntries();
9200 LocationSummary* locations = switch_instr->GetLocations();
9201 Register value_reg = locations->InAt(0).AsRegister<Register>();
9202 HBasicBlock* switch_block = switch_instr->GetBlock();
9203 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9204
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009205 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009206 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009207 //
9208 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9209 // to access the jump table and it is implemented by changing HPackedSwitch to
9210 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9211 // VisitMipsPackedSwitch()).
9212 //
9213 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9214 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9215 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009216 GenTableBasedPackedSwitch(value_reg,
9217 ZERO,
9218 lower_bound,
9219 num_entries,
9220 switch_block,
9221 default_block);
9222 } else {
9223 GenPackedSwitchWithCompares(value_reg,
9224 lower_bound,
9225 num_entries,
9226 switch_block,
9227 default_block);
9228 }
9229}
9230
9231void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009233 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009234 locations->SetInAt(0, Location::RequiresRegister());
9235 // Constant area pointer (HMipsComputeBaseMethodAddress).
9236 locations->SetInAt(1, Location::RequiresRegister());
9237}
9238
9239void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9240 int32_t lower_bound = switch_instr->GetStartValue();
9241 uint32_t num_entries = switch_instr->GetNumEntries();
9242 LocationSummary* locations = switch_instr->GetLocations();
9243 Register value_reg = locations->InAt(0).AsRegister<Register>();
9244 Register constant_area = locations->InAt(1).AsRegister<Register>();
9245 HBasicBlock* switch_block = switch_instr->GetBlock();
9246 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9247
9248 // This is an R2-only path. HPackedSwitch has been changed to
9249 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9250 // required to address the jump table relative to PC.
9251 GenTableBasedPackedSwitch(value_reg,
9252 constant_area,
9253 lower_bound,
9254 num_entries,
9255 switch_block,
9256 default_block);
9257}
9258
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009259void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9260 HMipsComputeBaseMethodAddress* insn) {
9261 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009262 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009263 locations->SetOut(Location::RequiresRegister());
9264}
9265
9266void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9267 HMipsComputeBaseMethodAddress* insn) {
9268 LocationSummary* locations = insn->GetLocations();
9269 Register reg = locations->Out().AsRegister<Register>();
9270
9271 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9272
9273 // Generate a dummy PC-relative call to obtain PC.
9274 __ Nal();
9275 // Grab the return address off RA.
9276 __ Move(reg, RA);
9277
9278 // Remember this offset (the obtained PC value) for later use with constant area.
9279 __ BindPcRelBaseLabel();
9280}
9281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009282void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9283 // The trampoline uses the same calling convention as dex calling conventions,
9284 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9285 // the method_idx.
9286 HandleInvoke(invoke);
9287}
9288
9289void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9290 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9291}
9292
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009293void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9294 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009295 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009296 locations->SetInAt(0, Location::RequiresRegister());
9297 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009298}
9299
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009300void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9301 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009302 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009303 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009304 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009305 __ LoadFromOffset(kLoadWord,
9306 locations->Out().AsRegister<Register>(),
9307 locations->InAt(0).AsRegister<Register>(),
9308 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009309 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009310 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009311 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009312 __ LoadFromOffset(kLoadWord,
9313 locations->Out().AsRegister<Register>(),
9314 locations->InAt(0).AsRegister<Register>(),
9315 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009316 __ LoadFromOffset(kLoadWord,
9317 locations->Out().AsRegister<Register>(),
9318 locations->Out().AsRegister<Register>(),
9319 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009320 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009321}
9322
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009323void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9324 ATTRIBUTE_UNUSED) {
9325 LOG(FATAL) << "Unreachable";
9326}
9327
9328void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9329 ATTRIBUTE_UNUSED) {
9330 LOG(FATAL) << "Unreachable";
9331}
9332
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009333#undef __
9334#undef QUICK_ENTRY_POINT
9335
9336} // namespace mips
9337} // namespace art