blob: ebe252a9c8e192051311147c878e2fc308501e76 [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
Vladimir Marko33bff252017-11-01 14:35:42 +00001279 bool do_overflow_check =
1280 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281
1282 if (do_overflow_check) {
1283 __ LoadFromOffset(kLoadWord,
1284 ZERO,
1285 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001286 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001287 RecordPcInfo(nullptr, 0);
1288 }
1289
1290 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001291 CHECK_EQ(fpu_spill_mask_, 0u);
1292 CHECK_EQ(core_spill_mask_, 1u << RA);
1293 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001294 return;
1295 }
1296
1297 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001298 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1299 LOG(FATAL) << "Stack frame larger than "
1300 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001301 }
1302
1303 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304
Alexey Frunze73296a72016-06-03 22:51:46 -07001305 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001306 __ IncreaseFrameSize(ofs);
1307
Alexey Frunze73296a72016-06-03 22:51:46 -07001308 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1309 Register reg = static_cast<Register>(MostSignificantBit(mask));
1310 mask ^= 1u << reg;
1311 ofs -= kMipsWordSize;
1312 // The ZERO register is only included for alignment.
1313 if (reg != ZERO) {
1314 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 __ cfi().RelOffset(DWARFReg(reg), ofs);
1316 }
1317 }
1318
Alexey Frunze73296a72016-06-03 22:51:46 -07001319 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1320 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1321 mask ^= 1u << reg;
1322 ofs -= kMipsDoublewordSize;
1323 __ StoreDToOffset(reg, SP, ofs);
1324 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 }
1326
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001327 // Save the current method if we need it. Note that we do not
1328 // do this in HCurrentMethod, as the instruction might have been removed
1329 // in the SSA graph.
1330 if (RequiresCurrentMethod()) {
1331 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1332 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001333
1334 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1335 // Initialize should deoptimize flag to 0.
1336 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001338}
1339
1340void CodeGeneratorMIPS::GenerateFrameExit() {
1341 __ cfi().RememberState();
1342
1343 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001344 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345
Alexey Frunze73296a72016-06-03 22:51:46 -07001346 // For better instruction scheduling restore RA before other registers.
1347 uint32_t ofs = GetFrameSize();
1348 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1349 Register reg = static_cast<Register>(MostSignificantBit(mask));
1350 mask ^= 1u << reg;
1351 ofs -= kMipsWordSize;
1352 // The ZERO register is only included for alignment.
1353 if (reg != ZERO) {
1354 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355 __ cfi().Restore(DWARFReg(reg));
1356 }
1357 }
1358
Alexey Frunze73296a72016-06-03 22:51:46 -07001359 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1360 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1361 mask ^= 1u << reg;
1362 ofs -= kMipsDoublewordSize;
1363 __ LoadDFromOffset(reg, SP, ofs);
1364 // TODO: __ cfi().Restore(DWARFReg(reg));
1365 }
1366
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001367 size_t frame_size = GetFrameSize();
1368 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1369 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1370 bool reordering = __ SetReorder(false);
1371 if (exchange) {
1372 __ Jr(RA);
1373 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1374 } else {
1375 __ DecreaseFrameSize(frame_size);
1376 __ Jr(RA);
1377 __ Nop(); // In delay slot.
1378 }
1379 __ SetReorder(reordering);
1380 } else {
1381 __ Jr(RA);
1382 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001383 }
1384
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001385 __ cfi().RestoreState();
1386 __ cfi().DefCFAOffset(GetFrameSize());
1387}
1388
1389void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1390 __ Bind(GetLabelOf(block));
1391}
1392
Lena Djokicca8c2952017-05-29 11:31:46 +02001393VectorRegister VectorRegisterFrom(Location location) {
1394 DCHECK(location.IsFpuRegister());
1395 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1396}
1397
Lena Djokic8098da92017-06-28 12:07:50 +02001398void CodeGeneratorMIPS::MoveLocation(Location destination,
1399 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001400 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001401 if (source.Equals(destination)) {
1402 return;
1403 }
1404
Lena Djokic8098da92017-06-28 12:07:50 +02001405 if (source.IsConstant()) {
1406 MoveConstant(destination, source.GetConstant());
1407 } else {
1408 if (destination.IsRegister()) {
1409 if (source.IsRegister()) {
1410 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1411 } else if (source.IsFpuRegister()) {
1412 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1413 } else {
1414 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001416 }
1417 } else if (destination.IsRegisterPair()) {
1418 if (source.IsRegisterPair()) {
1419 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1420 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 Register dst_high = destination.AsRegisterPairHigh<Register>();
1423 Register dst_low = destination.AsRegisterPairLow<Register>();
1424 FRegister src = source.AsFpuRegister<FRegister>();
1425 __ Mfc1(dst_low, src);
1426 __ MoveFromFpuHigh(dst_high, src);
1427 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001428 DCHECK(source.IsDoubleStackSlot())
1429 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001430 int32_t off = source.GetStackIndex();
1431 Register r = destination.AsRegisterPairLow<Register>();
1432 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1433 }
1434 } else if (destination.IsFpuRegister()) {
1435 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001436 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001437 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1438 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001439 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001440 FRegister dst = destination.AsFpuRegister<FRegister>();
1441 Register src_high = source.AsRegisterPairHigh<Register>();
1442 Register src_low = source.AsRegisterPairLow<Register>();
1443 __ Mtc1(src_low, dst);
1444 __ MoveToFpuHigh(src_high, dst);
1445 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001446 if (GetGraph()->HasSIMD()) {
1447 __ MoveV(VectorRegisterFrom(destination),
1448 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001449 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001451 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1452 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001453 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001454 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1455 }
Lena Djokic8098da92017-06-28 12:07:50 +02001456 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001457 } else if (source.IsSIMDStackSlot()) {
1458 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001461 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001464 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1465 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (destination.IsSIMDStackSlot()) {
1468 if (source.IsFpuRegister()) {
1469 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1470 } else {
1471 DCHECK(source.IsSIMDStackSlot());
1472 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1473 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1474 }
Lena Djokic8098da92017-06-28 12:07:50 +02001475 } else if (destination.IsDoubleStackSlot()) {
1476 int32_t dst_offset = destination.GetStackIndex();
1477 if (source.IsRegisterPair()) {
1478 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1479 } else if (source.IsFpuRegister()) {
1480 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1481 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001482 DCHECK(source.IsDoubleStackSlot())
1483 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001484 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1485 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1486 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1487 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001489 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001490 DCHECK(destination.IsStackSlot()) << destination;
1491 int32_t dst_offset = destination.GetStackIndex();
1492 if (source.IsRegister()) {
1493 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1494 } else if (source.IsFpuRegister()) {
1495 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1496 } else {
1497 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1498 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1499 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1500 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001501 }
1502 }
1503}
1504
1505void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1506 if (c->IsIntConstant() || c->IsNullConstant()) {
1507 // Move 32 bit constant.
1508 int32_t value = GetInt32ValueOf(c);
1509 if (destination.IsRegister()) {
1510 Register dst = destination.AsRegister<Register>();
1511 __ LoadConst32(dst, value);
1512 } else {
1513 DCHECK(destination.IsStackSlot())
1514 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001515 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001516 }
1517 } else if (c->IsLongConstant()) {
1518 // Move 64 bit constant.
1519 int64_t value = GetInt64ValueOf(c);
1520 if (destination.IsRegisterPair()) {
1521 Register r_h = destination.AsRegisterPairHigh<Register>();
1522 Register r_l = destination.AsRegisterPairLow<Register>();
1523 __ LoadConst64(r_h, r_l, value);
1524 } else {
1525 DCHECK(destination.IsDoubleStackSlot())
1526 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001527 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001528 }
1529 } else if (c->IsFloatConstant()) {
1530 // Move 32 bit float constant.
1531 int32_t value = GetInt32ValueOf(c);
1532 if (destination.IsFpuRegister()) {
1533 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1534 } else {
1535 DCHECK(destination.IsStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else {
1540 // Move 64 bit double constant.
1541 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1542 int64_t value = GetInt64ValueOf(c);
1543 if (destination.IsFpuRegister()) {
1544 FRegister fd = destination.AsFpuRegister<FRegister>();
1545 __ LoadDConst64(fd, value, TMP);
1546 } else {
1547 DCHECK(destination.IsDoubleStackSlot())
1548 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001549 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001550 }
1551 }
1552}
1553
1554void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1555 DCHECK(destination.IsRegister());
1556 Register dst = destination.AsRegister<Register>();
1557 __ LoadConst32(dst, value);
1558}
1559
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1561 if (location.IsRegister()) {
1562 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001563 } else if (location.IsRegisterPair()) {
1564 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1565 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001566 } else {
1567 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1568 }
1569}
1570
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001571template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001572inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1573 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001574 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001575 for (const PcRelativePatchInfo& info : infos) {
1576 const DexFile& dex_file = info.target_dex_file;
1577 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001578 DCHECK(info.label.IsBound());
1579 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001580 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1581 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001582 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1583 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1584 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001586 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 }
1588}
1589
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001590void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001591 DCHECK(linker_patches->empty());
1592 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001593 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001594 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001595 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001596 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001597 pc_relative_string_patches_.size() +
1598 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001599 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001600 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001601 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1602 pc_relative_method_patches_, linker_patches);
1603 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1604 pc_relative_type_patches_, linker_patches);
1605 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1606 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001607 } else {
1608 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001609 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1610 pc_relative_type_patches_, linker_patches);
1611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1612 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001613 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001614 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1615 method_bss_entry_patches_, linker_patches);
1616 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1617 type_bss_entry_patches_, linker_patches);
1618 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1619 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001620 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001621}
1622
Vladimir Marko65979462017-05-19 17:25:12 +01001623CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001624 MethodReference target_method,
1625 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001626 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001627 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001628 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001629 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001630}
1631
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001632CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001633 MethodReference target_method,
1634 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001635 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001636 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001637 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001638 &method_bss_entry_patches_);
1639}
1640
Alexey Frunze06a46c42016-07-19 15:00:40 -07001641CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001642 const DexFile& dex_file,
1643 dex::TypeIndex type_index,
1644 const PcRelativePatchInfo* info_high) {
1645 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001646}
1647
Vladimir Marko1998cd02017-01-13 13:02:58 +00001648CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001649 const DexFile& dex_file,
1650 dex::TypeIndex type_index,
1651 const PcRelativePatchInfo* info_high) {
1652 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001653}
1654
Vladimir Marko65979462017-05-19 17:25:12 +01001655CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001656 const DexFile& dex_file,
1657 dex::StringIndex string_index,
1658 const PcRelativePatchInfo* info_high) {
1659 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001660}
1661
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001662CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1663 const DexFile& dex_file,
1664 dex::StringIndex string_index,
1665 const PcRelativePatchInfo* info_high) {
1666 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1667}
1668
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001669CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001670 const DexFile& dex_file,
1671 uint32_t offset_or_index,
1672 const PcRelativePatchInfo* info_high,
1673 ArenaDeque<PcRelativePatchInfo>* patches) {
1674 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001675 return &patches->back();
1676}
1677
Alexey Frunze06a46c42016-07-19 15:00:40 -07001678Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1679 return map->GetOrCreate(
1680 value,
1681 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1682}
1683
Alexey Frunze06a46c42016-07-19 15:00:40 -07001684Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001685 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001686}
1687
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001689 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001690 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001691 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001692 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001693 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001694 if (GetInstructionSetFeatures().IsR6()) {
1695 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001696 __ Bind(&info_high->label);
1697 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001698 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001699 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001700 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001701 } else {
1702 // If base is ZERO, emit NAL to obtain the actual base.
1703 if (base == ZERO) {
1704 // Generate a dummy PC-relative call to obtain PC.
1705 __ Nal();
1706 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001707 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001708 __ Lui(out, /* placeholder */ 0x1234);
1709 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1710 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1711 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001713 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001714 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001715 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001716 __ Addu(out, out, (base == ZERO) ? RA : base);
1717 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001718 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001719 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720}
1721
Alexey Frunze627c1a02017-01-30 19:28:14 -08001722CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1723 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001724 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001725 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001726 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1727 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001728 return &jit_string_patches_.back();
1729}
1730
1731CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1732 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001733 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001734 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001735 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1736 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001737 return &jit_class_patches_.back();
1738}
1739
1740void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1741 const uint8_t* roots_data,
1742 const CodeGeneratorMIPS::JitPatchInfo& info,
1743 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001744 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1745 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001746 uintptr_t address =
1747 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1748 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1749 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001750 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1751 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1752 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1753 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001754 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001755 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1756 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001757 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001759 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1760 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001761 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001762 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1763 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001764}
1765
1766void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1767 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001768 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1769 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001770 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001771 }
1772 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001773 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1774 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001775 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001776 }
1777}
1778
Goran Jakovljevice114da22016-12-26 14:21:43 +01001779void CodeGeneratorMIPS::MarkGCCard(Register object,
1780 Register value,
1781 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001782 MipsLabel done;
1783 Register card = AT;
1784 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001785 if (value_can_be_null) {
1786 __ Beqz(value, &done);
1787 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001788 __ LoadFromOffset(kLoadWord,
1789 card,
1790 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001791 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001792 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1793 __ Addu(temp, card, temp);
1794 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001795 if (value_can_be_null) {
1796 __ Bind(&done);
1797 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001798}
1799
David Brazdil58282f42016-01-14 12:45:10 +00001800void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001801 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1802 blocked_core_registers_[ZERO] = true;
1803 blocked_core_registers_[K0] = true;
1804 blocked_core_registers_[K1] = true;
1805 blocked_core_registers_[GP] = true;
1806 blocked_core_registers_[SP] = true;
1807 blocked_core_registers_[RA] = true;
1808
1809 // AT and TMP(T8) are used as temporary/scratch registers
1810 // (similar to how AT is used by MIPS assemblers).
1811 blocked_core_registers_[AT] = true;
1812 blocked_core_registers_[TMP] = true;
1813 blocked_fpu_registers_[FTMP] = true;
1814
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001815 if (GetInstructionSetFeatures().HasMsa()) {
1816 // To be used just for MSA instructions.
1817 blocked_fpu_registers_[FTMP2] = true;
1818 }
1819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001820 // Reserve suspend and thread registers.
1821 blocked_core_registers_[S0] = true;
1822 blocked_core_registers_[TR] = true;
1823
1824 // Reserve T9 for function calls
1825 blocked_core_registers_[T9] = true;
1826
1827 // Reserve odd-numbered FPU registers.
1828 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1829 blocked_fpu_registers_[i] = true;
1830 }
1831
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001832 if (GetGraph()->IsDebuggable()) {
1833 // Stubs do not save callee-save floating point registers. If the graph
1834 // is debuggable, we need to deal with these registers differently. For
1835 // now, just block them.
1836 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1837 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1838 }
1839 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840}
1841
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001842size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1843 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1844 return kMipsWordSize;
1845}
1846
1847size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1848 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1849 return kMipsWordSize;
1850}
1851
1852size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001853 if (GetGraph()->HasSIMD()) {
1854 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1855 } else {
1856 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1857 }
1858 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001859}
1860
1861size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001862 if (GetGraph()->HasSIMD()) {
1863 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1864 } else {
1865 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1866 }
1867 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868}
1869
1870void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001871 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872}
1873
1874void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001875 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876}
1877
Serban Constantinescufca16662016-07-14 09:21:59 +01001878constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1879
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1881 HInstruction* instruction,
1882 uint32_t dex_pc,
1883 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001884 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001885 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1886 IsDirectEntrypoint(entrypoint));
1887 if (EntrypointRequiresStackMap(entrypoint)) {
1888 RecordPcInfo(instruction, dex_pc, slow_path);
1889 }
1890}
1891
1892void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1893 HInstruction* instruction,
1894 SlowPathCode* slow_path,
1895 bool direct) {
1896 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1897 GenerateInvokeRuntime(entry_point_offset, direct);
1898}
1899
1900void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001901 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001902 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001903 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001904 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 // Reserve argument space on stack (for $a0-$a3) for
1906 // entrypoints that directly reference native implementations.
1907 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001908 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001910 } else {
1911 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001913 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914}
1915
1916void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1917 Register class_reg) {
Vladimir Marko2c64a832018-01-04 11:31:56 +00001918 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1919 __ LoadConst32(AT, enum_cast<>(ClassStatus::kInitialized));
1920 __ Bltu(TMP, AT, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1922 __ Sync(0);
1923 __ Bind(slow_path->GetExitLabel());
1924}
1925
1926void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1927 __ Sync(0); // Only stype 0 is supported.
1928}
1929
1930void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1931 HBasicBlock* successor) {
1932 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001933 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1934
1935 if (slow_path == nullptr) {
1936 slow_path =
1937 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1938 instruction->SetSlowPath(slow_path);
1939 codegen_->AddSlowPath(slow_path);
1940 if (successor != nullptr) {
1941 DCHECK(successor->IsLoopHeader());
1942 }
1943 } else {
1944 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1945 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946
1947 __ LoadFromOffset(kLoadUnsignedHalfword,
1948 TMP,
1949 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001950 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 if (successor == nullptr) {
1952 __ Bnez(TMP, slow_path->GetEntryLabel());
1953 __ Bind(slow_path->GetReturnLabel());
1954 } else {
1955 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1956 __ B(slow_path->GetEntryLabel());
1957 // slow_path will return to GetLabelOf(successor).
1958 }
1959}
1960
1961InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1962 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001963 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001964 assembler_(codegen->GetAssembler()),
1965 codegen_(codegen) {}
1966
1967void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1968 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001969 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001970 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001971 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001972 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001974 locations->SetInAt(0, Location::RequiresRegister());
1975 HInstruction* right = instruction->InputAt(1);
1976 bool can_use_imm = false;
1977 if (right->IsConstant()) {
1978 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1979 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1980 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001981 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001982 DCHECK(instruction->IsSub() || instruction->IsAdd());
1983 if (instruction->IsSub()) {
1984 imm = -imm;
1985 }
1986 if (isR6) {
1987 bool single_use = right->GetUses().HasExactlyOneElement();
1988 int16_t imm_high = High16Bits(imm);
1989 int16_t imm_low = Low16Bits(imm);
1990 if (imm_low < 0) {
1991 imm_high += 1;
1992 }
1993 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
1994 } else {
1995 can_use_imm = IsInt<16>(imm);
1996 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001997 }
1998 }
1999 if (can_use_imm)
2000 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2001 else
2002 locations->SetInAt(1, Location::RequiresRegister());
2003 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2004 break;
2005 }
2006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 break;
2012 }
2013
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 case DataType::Type::kFloat32:
2015 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016 DCHECK(instruction->IsAdd() || instruction->IsSub());
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
2018 locations->SetInAt(1, Location::RequiresFpuRegister());
2019 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2020 break;
2021
2022 default:
2023 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2024 }
2025}
2026
2027void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002030 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031
2032 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034 Register dst = locations->Out().AsRegister<Register>();
2035 Register lhs = locations->InAt(0).AsRegister<Register>();
2036 Location rhs_location = locations->InAt(1);
2037
2038 Register rhs_reg = ZERO;
2039 int32_t rhs_imm = 0;
2040 bool use_imm = rhs_location.IsConstant();
2041 if (use_imm) {
2042 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2043 } else {
2044 rhs_reg = rhs_location.AsRegister<Register>();
2045 }
2046
2047 if (instruction->IsAnd()) {
2048 if (use_imm)
2049 __ Andi(dst, lhs, rhs_imm);
2050 else
2051 __ And(dst, lhs, rhs_reg);
2052 } else if (instruction->IsOr()) {
2053 if (use_imm)
2054 __ Ori(dst, lhs, rhs_imm);
2055 else
2056 __ Or(dst, lhs, rhs_reg);
2057 } else if (instruction->IsXor()) {
2058 if (use_imm)
2059 __ Xori(dst, lhs, rhs_imm);
2060 else
2061 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002062 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002063 DCHECK(instruction->IsAdd() || instruction->IsSub());
2064 if (use_imm) {
2065 if (instruction->IsSub()) {
2066 rhs_imm = -rhs_imm;
2067 }
2068 if (IsInt<16>(rhs_imm)) {
2069 __ Addiu(dst, lhs, rhs_imm);
2070 } else {
2071 DCHECK(isR6);
2072 int16_t rhs_imm_high = High16Bits(rhs_imm);
2073 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2074 if (rhs_imm_low < 0) {
2075 rhs_imm_high += 1;
2076 }
2077 __ Aui(dst, lhs, rhs_imm_high);
2078 if (rhs_imm_low != 0) {
2079 __ Addiu(dst, dst, rhs_imm_low);
2080 }
2081 }
2082 } else if (instruction->IsAdd()) {
2083 __ Addu(dst, lhs, rhs_reg);
2084 } else {
2085 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002087 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002088 }
2089 break;
2090 }
2091
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002092 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2094 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2095 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2096 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002097 Location rhs_location = locations->InAt(1);
2098 bool use_imm = rhs_location.IsConstant();
2099 if (!use_imm) {
2100 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2101 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2102 if (instruction->IsAnd()) {
2103 __ And(dst_low, lhs_low, rhs_low);
2104 __ And(dst_high, lhs_high, rhs_high);
2105 } else if (instruction->IsOr()) {
2106 __ Or(dst_low, lhs_low, rhs_low);
2107 __ Or(dst_high, lhs_high, rhs_high);
2108 } else if (instruction->IsXor()) {
2109 __ Xor(dst_low, lhs_low, rhs_low);
2110 __ Xor(dst_high, lhs_high, rhs_high);
2111 } else if (instruction->IsAdd()) {
2112 if (lhs_low == rhs_low) {
2113 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2114 __ Slt(TMP, lhs_low, ZERO);
2115 __ Addu(dst_low, lhs_low, rhs_low);
2116 } else {
2117 __ Addu(dst_low, lhs_low, rhs_low);
2118 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2119 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2120 }
2121 __ Addu(dst_high, lhs_high, rhs_high);
2122 __ Addu(dst_high, dst_high, TMP);
2123 } else {
2124 DCHECK(instruction->IsSub());
2125 __ Sltu(TMP, lhs_low, rhs_low);
2126 __ Subu(dst_low, lhs_low, rhs_low);
2127 __ Subu(dst_high, lhs_high, rhs_high);
2128 __ Subu(dst_high, dst_high, TMP);
2129 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002131 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2132 if (instruction->IsOr()) {
2133 uint32_t low = Low32Bits(value);
2134 uint32_t high = High32Bits(value);
2135 if (IsUint<16>(low)) {
2136 if (dst_low != lhs_low || low != 0) {
2137 __ Ori(dst_low, lhs_low, low);
2138 }
2139 } else {
2140 __ LoadConst32(TMP, low);
2141 __ Or(dst_low, lhs_low, TMP);
2142 }
2143 if (IsUint<16>(high)) {
2144 if (dst_high != lhs_high || high != 0) {
2145 __ Ori(dst_high, lhs_high, high);
2146 }
2147 } else {
2148 if (high != low) {
2149 __ LoadConst32(TMP, high);
2150 }
2151 __ Or(dst_high, lhs_high, TMP);
2152 }
2153 } else if (instruction->IsXor()) {
2154 uint32_t low = Low32Bits(value);
2155 uint32_t high = High32Bits(value);
2156 if (IsUint<16>(low)) {
2157 if (dst_low != lhs_low || low != 0) {
2158 __ Xori(dst_low, lhs_low, low);
2159 }
2160 } else {
2161 __ LoadConst32(TMP, low);
2162 __ Xor(dst_low, lhs_low, TMP);
2163 }
2164 if (IsUint<16>(high)) {
2165 if (dst_high != lhs_high || high != 0) {
2166 __ Xori(dst_high, lhs_high, high);
2167 }
2168 } else {
2169 if (high != low) {
2170 __ LoadConst32(TMP, high);
2171 }
2172 __ Xor(dst_high, lhs_high, TMP);
2173 }
2174 } else if (instruction->IsAnd()) {
2175 uint32_t low = Low32Bits(value);
2176 uint32_t high = High32Bits(value);
2177 if (IsUint<16>(low)) {
2178 __ Andi(dst_low, lhs_low, low);
2179 } else if (low != 0xFFFFFFFF) {
2180 __ LoadConst32(TMP, low);
2181 __ And(dst_low, lhs_low, TMP);
2182 } else if (dst_low != lhs_low) {
2183 __ Move(dst_low, lhs_low);
2184 }
2185 if (IsUint<16>(high)) {
2186 __ Andi(dst_high, lhs_high, high);
2187 } else if (high != 0xFFFFFFFF) {
2188 if (high != low) {
2189 __ LoadConst32(TMP, high);
2190 }
2191 __ And(dst_high, lhs_high, TMP);
2192 } else if (dst_high != lhs_high) {
2193 __ Move(dst_high, lhs_high);
2194 }
2195 } else {
2196 if (instruction->IsSub()) {
2197 value = -value;
2198 } else {
2199 DCHECK(instruction->IsAdd());
2200 }
2201 int32_t low = Low32Bits(value);
2202 int32_t high = High32Bits(value);
2203 if (IsInt<16>(low)) {
2204 if (dst_low != lhs_low || low != 0) {
2205 __ Addiu(dst_low, lhs_low, low);
2206 }
2207 if (low != 0) {
2208 __ Sltiu(AT, dst_low, low);
2209 }
2210 } else {
2211 __ LoadConst32(TMP, low);
2212 __ Addu(dst_low, lhs_low, TMP);
2213 __ Sltu(AT, dst_low, TMP);
2214 }
2215 if (IsInt<16>(high)) {
2216 if (dst_high != lhs_high || high != 0) {
2217 __ Addiu(dst_high, lhs_high, high);
2218 }
2219 } else {
2220 if (high != low) {
2221 __ LoadConst32(TMP, high);
2222 }
2223 __ Addu(dst_high, lhs_high, TMP);
2224 }
2225 if (low != 0) {
2226 __ Addu(dst_high, dst_high, AT);
2227 }
2228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002229 }
2230 break;
2231 }
2232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kFloat32:
2234 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002235 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2236 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2238 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240 __ AddS(dst, lhs, rhs);
2241 } else {
2242 __ AddD(dst, lhs, rhs);
2243 }
2244 } else {
2245 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 __ SubS(dst, lhs, rhs);
2248 } else {
2249 __ SubD(dst, lhs, rhs);
2250 }
2251 }
2252 break;
2253 }
2254
2255 default:
2256 LOG(FATAL) << "Unexpected binary operation type " << type;
2257 }
2258}
2259
2260void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002261 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262
Vladimir Markoca6fff82017-10-03 14:49:14 +01002263 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002265 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002267 locations->SetInAt(0, Location::RequiresRegister());
2268 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2270 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 locations->SetInAt(0, Location::RequiresRegister());
2273 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2274 locations->SetOut(Location::RequiresRegister());
2275 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 default:
2277 LOG(FATAL) << "Unexpected shift type " << type;
2278 }
2279}
2280
2281static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2282
2283void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002284 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287
2288 Location rhs_location = locations->InAt(1);
2289 bool use_imm = rhs_location.IsConstant();
2290 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2291 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002292 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002294 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002295 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2296 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297
2298 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002300 Register dst = locations->Out().AsRegister<Register>();
2301 Register lhs = locations->InAt(0).AsRegister<Register>();
2302 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002303 if (shift_value == 0) {
2304 if (dst != lhs) {
2305 __ Move(dst, lhs);
2306 }
2307 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002308 __ Sll(dst, lhs, shift_value);
2309 } else if (instr->IsShr()) {
2310 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002313 } else {
2314 if (has_ins_rotr) {
2315 __ Rotr(dst, lhs, shift_value);
2316 } else {
2317 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2318 __ Srl(dst, lhs, shift_value);
2319 __ Or(dst, dst, TMP);
2320 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 }
2322 } else {
2323 if (instr->IsShl()) {
2324 __ Sllv(dst, lhs, rhs_reg);
2325 } else if (instr->IsShr()) {
2326 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002327 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002328 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002329 } else {
2330 if (has_ins_rotr) {
2331 __ Rotrv(dst, lhs, rhs_reg);
2332 } else {
2333 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002334 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2335 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2336 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2337 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2338 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 __ Sllv(TMP, lhs, TMP);
2340 __ Srlv(dst, lhs, rhs_reg);
2341 __ Or(dst, dst, TMP);
2342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002343 }
2344 }
2345 break;
2346 }
2347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002349 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2350 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2351 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2352 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2353 if (use_imm) {
2354 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002355 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002357 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002358 if (instr->IsShl()) {
2359 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2360 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2361 __ Sll(dst_low, lhs_low, shift_value);
2362 } else if (instr->IsShr()) {
2363 __ Srl(dst_low, lhs_low, shift_value);
2364 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2365 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002366 } else if (instr->IsUShr()) {
2367 __ Srl(dst_low, lhs_low, shift_value);
2368 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2369 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002370 } else {
2371 __ Srl(dst_low, lhs_low, shift_value);
2372 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2373 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002375 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002377 if (instr->IsShl()) {
2378 __ Sll(dst_low, lhs_low, shift_value);
2379 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2380 __ Sll(dst_high, lhs_high, shift_value);
2381 __ Or(dst_high, dst_high, TMP);
2382 } else if (instr->IsShr()) {
2383 __ Sra(dst_high, lhs_high, shift_value);
2384 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2385 __ Srl(dst_low, lhs_low, shift_value);
2386 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002387 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002388 __ Srl(dst_high, lhs_high, shift_value);
2389 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2390 __ Srl(dst_low, lhs_low, shift_value);
2391 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002392 } else {
2393 __ Srl(TMP, lhs_low, shift_value);
2394 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2395 __ Or(dst_low, dst_low, TMP);
2396 __ Srl(TMP, lhs_high, shift_value);
2397 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2398 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002399 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002400 }
2401 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002402 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002404 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 __ Move(dst_low, ZERO);
2406 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002407 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002409 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002410 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002412 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002413 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002414 // 64-bit rotation by 32 is just a swap.
2415 __ Move(dst_low, lhs_high);
2416 __ Move(dst_high, lhs_low);
2417 } else {
2418 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002419 __ Srl(dst_low, lhs_high, shift_value_high);
2420 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2421 __ Srl(dst_high, lhs_low, shift_value_high);
2422 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002423 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002424 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2425 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002426 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002427 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2428 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 __ Or(dst_high, dst_high, TMP);
2430 }
2431 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 }
2433 }
2434 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002435 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436 MipsLabel done;
2437 if (instr->IsShl()) {
2438 __ Sllv(dst_low, lhs_low, rhs_reg);
2439 __ Nor(AT, ZERO, rhs_reg);
2440 __ Srl(TMP, lhs_low, 1);
2441 __ Srlv(TMP, TMP, AT);
2442 __ Sllv(dst_high, lhs_high, rhs_reg);
2443 __ Or(dst_high, dst_high, TMP);
2444 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002445 if (isR6) {
2446 __ Beqzc(TMP, &done, /* is_bare */ true);
2447 __ Move(dst_high, dst_low);
2448 __ Move(dst_low, ZERO);
2449 } else {
2450 __ Movn(dst_high, dst_low, TMP);
2451 __ Movn(dst_low, ZERO, TMP);
2452 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002453 } else if (instr->IsShr()) {
2454 __ Srav(dst_high, lhs_high, rhs_reg);
2455 __ Nor(AT, ZERO, rhs_reg);
2456 __ Sll(TMP, lhs_high, 1);
2457 __ Sllv(TMP, TMP, AT);
2458 __ Srlv(dst_low, lhs_low, rhs_reg);
2459 __ Or(dst_low, dst_low, TMP);
2460 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002461 if (isR6) {
2462 __ Beqzc(TMP, &done, /* is_bare */ true);
2463 __ Move(dst_low, dst_high);
2464 __ Sra(dst_high, dst_high, 31);
2465 } else {
2466 __ Sra(AT, dst_high, 31);
2467 __ Movn(dst_low, dst_high, TMP);
2468 __ Movn(dst_high, AT, TMP);
2469 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002470 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002471 __ Srlv(dst_high, lhs_high, rhs_reg);
2472 __ Nor(AT, ZERO, rhs_reg);
2473 __ Sll(TMP, lhs_high, 1);
2474 __ Sllv(TMP, TMP, AT);
2475 __ Srlv(dst_low, lhs_low, rhs_reg);
2476 __ Or(dst_low, dst_low, TMP);
2477 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002478 if (isR6) {
2479 __ Beqzc(TMP, &done, /* is_bare */ true);
2480 __ Move(dst_low, dst_high);
2481 __ Move(dst_high, ZERO);
2482 } else {
2483 __ Movn(dst_low, dst_high, TMP);
2484 __ Movn(dst_high, ZERO, TMP);
2485 }
2486 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 __ Nor(AT, ZERO, rhs_reg);
2488 __ Srlv(TMP, lhs_low, rhs_reg);
2489 __ Sll(dst_low, lhs_high, 1);
2490 __ Sllv(dst_low, dst_low, AT);
2491 __ Or(dst_low, dst_low, TMP);
2492 __ Srlv(TMP, lhs_high, rhs_reg);
2493 __ Sll(dst_high, lhs_low, 1);
2494 __ Sllv(dst_high, dst_high, AT);
2495 __ Or(dst_high, dst_high, TMP);
2496 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002497 if (isR6) {
2498 __ Beqzc(TMP, &done, /* is_bare */ true);
2499 __ Move(TMP, dst_high);
2500 __ Move(dst_high, dst_low);
2501 __ Move(dst_low, TMP);
2502 } else {
2503 __ Movn(AT, dst_high, TMP);
2504 __ Movn(dst_high, dst_low, TMP);
2505 __ Movn(dst_low, AT, TMP);
2506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 }
2508 __ Bind(&done);
2509 }
2510 break;
2511 }
2512
2513 default:
2514 LOG(FATAL) << "Unexpected shift operation type " << type;
2515 }
2516}
2517
2518void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2519 HandleBinaryOp(instruction);
2520}
2521
2522void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2523 HandleBinaryOp(instruction);
2524}
2525
2526void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2527 HandleBinaryOp(instruction);
2528}
2529
2530void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2531 HandleBinaryOp(instruction);
2532}
2533
2534void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002536 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002539 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2540 object_array_get_with_read_barrier
2541 ? LocationSummary::kCallOnSlowPath
2542 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002543 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2544 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002546 locations->SetInAt(0, Location::RequiresRegister());
2547 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2550 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002551 // The output overlaps in the case of an object array get with
2552 // read barriers enabled: we do not want the move to overwrite the
2553 // array's location, as we need it to emit the read barrier.
2554 locations->SetOut(Location::RequiresRegister(),
2555 object_array_get_with_read_barrier
2556 ? Location::kOutputOverlap
2557 : Location::kNoOutputOverlap);
2558 }
2559 // We need a temporary register for the read barrier marking slow
2560 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2561 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002562 bool temp_needed = instruction->GetIndex()->IsConstant()
2563 ? !kBakerReadBarrierThunksEnableForFields
2564 : !kBakerReadBarrierThunksEnableForArrays;
2565 if (temp_needed) {
2566 locations->AddTemp(Location::RequiresRegister());
2567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002568 }
2569}
2570
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002571static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2572 auto null_checker = [codegen, instruction]() {
2573 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002574 };
2575 return null_checker;
2576}
2577
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002578void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2579 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002580 Location obj_loc = locations->InAt(0);
2581 Register obj = obj_loc.AsRegister<Register>();
2582 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002583 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002584 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002585 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002588 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2589 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002590 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002591 case DataType::Type::kBool:
2592 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002593 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002594 if (index.IsConstant()) {
2595 size_t offset =
2596 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002597 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002598 } else {
2599 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002600 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002601 }
2602 break;
2603 }
2604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002606 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 if (index.IsConstant()) {
2608 size_t offset =
2609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002610 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 } else {
2612 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002613 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002614 }
2615 break;
2616 }
2617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002619 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002620 if (maybe_compressed_char_at) {
2621 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2622 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2623 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2624 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2625 "Expecting 0=compressed, 1=uncompressed");
2626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002628 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2629 if (maybe_compressed_char_at) {
2630 MipsLabel uncompressed_load, done;
2631 __ Bnez(TMP, &uncompressed_load);
2632 __ LoadFromOffset(kLoadUnsignedByte,
2633 out,
2634 obj,
2635 data_offset + (const_index << TIMES_1));
2636 __ B(&done);
2637 __ Bind(&uncompressed_load);
2638 __ LoadFromOffset(kLoadUnsignedHalfword,
2639 out,
2640 obj,
2641 data_offset + (const_index << TIMES_2));
2642 __ Bind(&done);
2643 } else {
2644 __ LoadFromOffset(kLoadUnsignedHalfword,
2645 out,
2646 obj,
2647 data_offset + (const_index << TIMES_2),
2648 null_checker);
2649 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002650 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002651 Register index_reg = index.AsRegister<Register>();
2652 if (maybe_compressed_char_at) {
2653 MipsLabel uncompressed_load, done;
2654 __ Bnez(TMP, &uncompressed_load);
2655 __ Addu(TMP, obj, index_reg);
2656 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2657 __ B(&done);
2658 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002659 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002660 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2661 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002662 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2663 __ Addu(TMP, index_reg, obj);
2664 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002665 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002666 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002667 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 }
2670 break;
2671 }
2672
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002673 case DataType::Type::kInt16: {
2674 Register out = out_loc.AsRegister<Register>();
2675 if (index.IsConstant()) {
2676 size_t offset =
2677 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2678 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002679 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2680 __ Addu(TMP, index.AsRegister<Register>(), obj);
2681 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002682 } else {
2683 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2684 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2685 }
2686 break;
2687 }
2688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002691 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 if (index.IsConstant()) {
2693 size_t offset =
2694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002695 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002696 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2697 __ Addu(TMP, index.AsRegister<Register>(), obj);
2698 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002700 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002701 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002702 }
2703 break;
2704 }
2705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002707 static_assert(
2708 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2709 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2710 // /* HeapReference<Object> */ out =
2711 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2712 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002713 bool temp_needed = index.IsConstant()
2714 ? !kBakerReadBarrierThunksEnableForFields
2715 : !kBakerReadBarrierThunksEnableForArrays;
2716 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002717 // Note that a potential implicit null check is handled in this
2718 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002719 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2720 if (index.IsConstant()) {
2721 // Array load with a constant index can be treated as a field load.
2722 size_t offset =
2723 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2724 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2725 out_loc,
2726 obj,
2727 offset,
2728 temp,
2729 /* needs_null_check */ false);
2730 } else {
2731 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2732 out_loc,
2733 obj,
2734 data_offset,
2735 index,
2736 temp,
2737 /* needs_null_check */ false);
2738 }
Alexey Frunze15958152017-02-09 19:08:30 -08002739 } else {
2740 Register out = out_loc.AsRegister<Register>();
2741 if (index.IsConstant()) {
2742 size_t offset =
2743 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2744 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2745 // If read barriers are enabled, emit read barriers other than
2746 // Baker's using a slow path (and also unpoison the loaded
2747 // reference, if heap poisoning is enabled).
2748 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2749 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002750 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002751 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2752 // If read barriers are enabled, emit read barriers other than
2753 // Baker's using a slow path (and also unpoison the loaded
2754 // reference, if heap poisoning is enabled).
2755 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2756 out_loc,
2757 out_loc,
2758 obj_loc,
2759 data_offset,
2760 index);
2761 }
2762 }
2763 break;
2764 }
2765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002767 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002768 if (index.IsConstant()) {
2769 size_t offset =
2770 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002771 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002772 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2773 __ Addu(TMP, index.AsRegister<Register>(), obj);
2774 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002775 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002776 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002777 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002778 }
2779 break;
2780 }
2781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002783 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002784 if (index.IsConstant()) {
2785 size_t offset =
2786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002787 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002788 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2789 __ Addu(TMP, index.AsRegister<Register>(), obj);
2790 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002792 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002793 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002794 }
2795 break;
2796 }
2797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002799 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 if (index.IsConstant()) {
2801 size_t offset =
2802 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002803 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002804 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2805 __ Addu(TMP, index.AsRegister<Register>(), obj);
2806 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002807 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002808 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002809 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810 }
2811 break;
2812 }
2813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2816 UNREACHABLE();
2817 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002818}
2819
2820void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002821 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 locations->SetInAt(0, Location::RequiresRegister());
2823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2824}
2825
2826void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2827 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002828 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002829 Register obj = locations->InAt(0).AsRegister<Register>();
2830 Register out = locations->Out().AsRegister<Register>();
2831 __ LoadFromOffset(kLoadWord, out, obj, offset);
2832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002833 // Mask out compression flag from String's array length.
2834 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2835 __ Srl(out, out, 1u);
2836 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837}
2838
Alexey Frunzef58b2482016-09-02 22:14:06 -07002839Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2840 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2841 ? Location::ConstantLocation(instruction->AsConstant())
2842 : Location::RequiresRegister();
2843}
2844
2845Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2846 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2847 // We can store a non-zero float or double constant without first loading it into the FPU,
2848 // but we should only prefer this if the constant has a single use.
2849 if (instruction->IsConstant() &&
2850 (instruction->AsConstant()->IsZeroBitPattern() ||
2851 instruction->GetUses().HasExactlyOneElement())) {
2852 return Location::ConstantLocation(instruction->AsConstant());
2853 // Otherwise fall through and require an FPU register for the constant.
2854 }
2855 return Location::RequiresFpuRegister();
2856}
2857
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002858void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002859 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002860
2861 bool needs_write_barrier =
2862 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2863 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2864
Vladimir Markoca6fff82017-10-03 14:49:14 +01002865 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002866 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002867 may_need_runtime_call_for_type_check ?
2868 LocationSummary::kCallOnSlowPath :
2869 LocationSummary::kNoCall);
2870
2871 locations->SetInAt(0, Location::RequiresRegister());
2872 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002874 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002875 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002876 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2877 }
2878 if (needs_write_barrier) {
2879 // Temporary register for the write barrier.
2880 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 }
2882}
2883
2884void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2885 LocationSummary* locations = instruction->GetLocations();
2886 Register obj = locations->InAt(0).AsRegister<Register>();
2887 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002888 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002889 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002890 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 bool needs_write_barrier =
2892 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002893 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002894 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002895
2896 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002898 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002901 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002902 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002903 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002904 __ Addu(base_reg, obj, index.AsRegister<Register>());
2905 }
2906 if (value_location.IsConstant()) {
2907 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2908 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2909 } else {
2910 Register value = value_location.AsRegister<Register>();
2911 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002912 }
2913 break;
2914 }
2915
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002916 case DataType::Type::kUint16:
2917 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002919 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002920 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002921 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2922 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002924 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002925 }
2926 if (value_location.IsConstant()) {
2927 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2928 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2929 } else {
2930 Register value = value_location.AsRegister<Register>();
2931 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002932 }
2933 break;
2934 }
2935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2938 if (index.IsConstant()) {
2939 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002940 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2941 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002942 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002943 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002944 }
2945 if (value_location.IsConstant()) {
2946 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2947 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2948 } else {
2949 Register value = value_location.AsRegister<Register>();
2950 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2951 }
2952 break;
2953 }
2954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002956 if (value_location.IsConstant()) {
2957 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002959 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002960 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002961 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002962 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 }
Alexey Frunze15958152017-02-09 19:08:30 -08002964 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2965 DCHECK_EQ(value, 0);
2966 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2967 DCHECK(!needs_write_barrier);
2968 DCHECK(!may_need_runtime_call_for_type_check);
2969 break;
2970 }
2971
2972 DCHECK(needs_write_barrier);
2973 Register value = value_location.AsRegister<Register>();
2974 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2975 Register temp2 = TMP; // Doesn't need to survive slow path.
2976 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2977 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2978 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2979 MipsLabel done;
2980 SlowPathCodeMIPS* slow_path = nullptr;
2981
2982 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002983 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002984 codegen_->AddSlowPath(slow_path);
2985 if (instruction->GetValueCanBeNull()) {
2986 MipsLabel non_zero;
2987 __ Bnez(value, &non_zero);
2988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2989 if (index.IsConstant()) {
2990 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002991 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2992 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002993 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002994 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002995 }
Alexey Frunze15958152017-02-09 19:08:30 -08002996 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2997 __ B(&done);
2998 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999 }
Alexey Frunze15958152017-02-09 19:08:30 -08003000
3001 // Note that when read barriers are enabled, the type checks
3002 // are performed without read barriers. This is fine, even in
3003 // the case where a class object is in the from-space after
3004 // the flip, as a comparison involving such a type would not
3005 // produce a false positive; it may of course produce a false
3006 // negative, in which case we would take the ArraySet slow
3007 // path.
3008
3009 // /* HeapReference<Class> */ temp1 = obj->klass_
3010 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3011 __ MaybeUnpoisonHeapReference(temp1);
3012
3013 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3014 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3015 // /* HeapReference<Class> */ temp2 = value->klass_
3016 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3017 // If heap poisoning is enabled, no need to unpoison `temp1`
3018 // nor `temp2`, as we are comparing two poisoned references.
3019
3020 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3021 MipsLabel do_put;
3022 __ Beq(temp1, temp2, &do_put);
3023 // If heap poisoning is enabled, the `temp1` reference has
3024 // not been unpoisoned yet; unpoison it now.
3025 __ MaybeUnpoisonHeapReference(temp1);
3026
3027 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3028 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3029 // If heap poisoning is enabled, no need to unpoison
3030 // `temp1`, as we are comparing against null below.
3031 __ Bnez(temp1, slow_path->GetEntryLabel());
3032 __ Bind(&do_put);
3033 } else {
3034 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3035 }
3036 }
3037
3038 Register source = value;
3039 if (kPoisonHeapReferences) {
3040 // Note that in the case where `value` is a null reference,
3041 // we do not enter this block, as a null reference does not
3042 // need poisoning.
3043 __ Move(temp1, value);
3044 __ PoisonHeapReference(temp1);
3045 source = temp1;
3046 }
3047
3048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3049 if (index.IsConstant()) {
3050 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003051 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003052 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003053 }
3054 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3055
3056 if (!may_need_runtime_call_for_type_check) {
3057 codegen_->MaybeRecordImplicitNullCheck(instruction);
3058 }
3059
3060 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3061
3062 if (done.IsLinked()) {
3063 __ Bind(&done);
3064 }
3065
3066 if (slow_path != nullptr) {
3067 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068 }
3069 break;
3070 }
3071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003074 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003075 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003076 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3077 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003079 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003080 }
3081 if (value_location.IsConstant()) {
3082 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3083 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3084 } else {
3085 Register value = value_location.AsRegisterPairLow<Register>();
3086 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003087 }
3088 break;
3089 }
3090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003092 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003094 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003095 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3096 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003098 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003099 }
3100 if (value_location.IsConstant()) {
3101 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3102 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3103 } else {
3104 FRegister value = value_location.AsFpuRegister<FRegister>();
3105 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003106 }
3107 break;
3108 }
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003114 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3115 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003116 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003117 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003118 }
3119 if (value_location.IsConstant()) {
3120 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3121 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3122 } else {
3123 FRegister value = value_location.AsFpuRegister<FRegister>();
3124 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003125 }
3126 break;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003130 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3131 UNREACHABLE();
3132 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003133}
3134
Lena Djokica2901602017-09-21 13:50:52 +02003135void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3136 HIntermediateArrayAddressIndex* instruction) {
3137 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003138 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003139
3140 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3141
3142 locations->SetInAt(0, Location::RequiresRegister());
3143 locations->SetInAt(1, Location::ConstantLocation(shift));
3144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3145}
3146
3147void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3148 HIntermediateArrayAddressIndex* instruction) {
3149 LocationSummary* locations = instruction->GetLocations();
3150 Register index_reg = locations->InAt(0).AsRegister<Register>();
3151 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3152 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3153}
3154
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003155void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003156 RegisterSet caller_saves = RegisterSet::Empty();
3157 InvokeRuntimeCallingConvention calling_convention;
3158 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3159 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3160 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003161
3162 HInstruction* index = instruction->InputAt(0);
3163 HInstruction* length = instruction->InputAt(1);
3164
3165 bool const_index = false;
3166 bool const_length = false;
3167
3168 if (index->IsConstant()) {
3169 if (length->IsConstant()) {
3170 const_index = true;
3171 const_length = true;
3172 } else {
3173 int32_t index_value = index->AsIntConstant()->GetValue();
3174 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3175 const_index = true;
3176 }
3177 }
3178 } else if (length->IsConstant()) {
3179 int32_t length_value = length->AsIntConstant()->GetValue();
3180 if (IsUint<15>(length_value)) {
3181 const_length = true;
3182 }
3183 }
3184
3185 locations->SetInAt(0, const_index
3186 ? Location::ConstantLocation(index->AsConstant())
3187 : Location::RequiresRegister());
3188 locations->SetInAt(1, const_length
3189 ? Location::ConstantLocation(length->AsConstant())
3190 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003191}
3192
3193void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3194 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003195 Location index_loc = locations->InAt(0);
3196 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003197
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003198 if (length_loc.IsConstant()) {
3199 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3200 if (index_loc.IsConstant()) {
3201 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3202 if (index < 0 || index >= length) {
3203 BoundsCheckSlowPathMIPS* slow_path =
3204 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3205 codegen_->AddSlowPath(slow_path);
3206 __ B(slow_path->GetEntryLabel());
3207 } else {
3208 // Nothing to be done.
3209 }
3210 return;
3211 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003212
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003213 BoundsCheckSlowPathMIPS* slow_path =
3214 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3215 codegen_->AddSlowPath(slow_path);
3216 Register index = index_loc.AsRegister<Register>();
3217 if (length == 0) {
3218 __ B(slow_path->GetEntryLabel());
3219 } else if (length == 1) {
3220 __ Bnez(index, slow_path->GetEntryLabel());
3221 } else {
3222 DCHECK(IsUint<15>(length)) << length;
3223 __ Sltiu(TMP, index, length);
3224 __ Beqz(TMP, slow_path->GetEntryLabel());
3225 }
3226 } else {
3227 Register length = length_loc.AsRegister<Register>();
3228 BoundsCheckSlowPathMIPS* slow_path =
3229 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3230 codegen_->AddSlowPath(slow_path);
3231 if (index_loc.IsConstant()) {
3232 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3233 if (index < 0) {
3234 __ B(slow_path->GetEntryLabel());
3235 } else if (index == 0) {
3236 __ Blez(length, slow_path->GetEntryLabel());
3237 } else {
3238 DCHECK(IsInt<16>(index + 1)) << index;
3239 __ Sltiu(TMP, length, index + 1);
3240 __ Bnez(TMP, slow_path->GetEntryLabel());
3241 }
3242 } else {
3243 Register index = index_loc.AsRegister<Register>();
3244 __ Bgeu(index, length, slow_path->GetEntryLabel());
3245 }
3246 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247}
3248
Alexey Frunze15958152017-02-09 19:08:30 -08003249// Temp is used for read barrier.
3250static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3251 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003252 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003253 (kUseBakerReadBarrier ||
3254 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3255 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3256 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3257 return 1;
3258 }
3259 return 0;
3260}
3261
3262// Extra temp is used for read barrier.
3263static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3264 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3265}
3266
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003267void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003268 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3269 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3270
3271 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3272 switch (type_check_kind) {
3273 case TypeCheckKind::kExactCheck:
3274 case TypeCheckKind::kAbstractClassCheck:
3275 case TypeCheckKind::kClassHierarchyCheck:
3276 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003277 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003278 ? LocationSummary::kCallOnSlowPath
3279 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3280 break;
3281 case TypeCheckKind::kArrayCheck:
3282 case TypeCheckKind::kUnresolvedCheck:
3283 case TypeCheckKind::kInterfaceCheck:
3284 call_kind = LocationSummary::kCallOnSlowPath;
3285 break;
3286 }
3287
Vladimir Markoca6fff82017-10-03 14:49:14 +01003288 LocationSummary* locations =
3289 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003290 locations->SetInAt(0, Location::RequiresRegister());
3291 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003292 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003293}
3294
3295void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003296 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003297 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003298 Location obj_loc = locations->InAt(0);
3299 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003301 Location temp_loc = locations->GetTemp(0);
3302 Register temp = temp_loc.AsRegister<Register>();
3303 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3304 DCHECK_LE(num_temps, 2u);
3305 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003306 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3307 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3308 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3309 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3310 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3311 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3312 const uint32_t object_array_data_offset =
3313 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3314 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003315
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003316 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3317 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3318 // read barriers is done for performance and code size reasons.
3319 bool is_type_check_slow_path_fatal = false;
3320 if (!kEmitCompilerReadBarrier) {
3321 is_type_check_slow_path_fatal =
3322 (type_check_kind == TypeCheckKind::kExactCheck ||
3323 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3324 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3325 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3326 !instruction->CanThrowIntoCatchBlock();
3327 }
3328 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003329 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3330 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003331 codegen_->AddSlowPath(slow_path);
3332
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003333 // Avoid this check if we know `obj` is not null.
3334 if (instruction->MustDoNullCheck()) {
3335 __ Beqz(obj, &done);
3336 }
3337
3338 switch (type_check_kind) {
3339 case TypeCheckKind::kExactCheck:
3340 case TypeCheckKind::kArrayCheck: {
3341 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003342 GenerateReferenceLoadTwoRegisters(instruction,
3343 temp_loc,
3344 obj_loc,
3345 class_offset,
3346 maybe_temp2_loc,
3347 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003348 // Jump to slow path for throwing the exception or doing a
3349 // more involved array check.
3350 __ Bne(temp, cls, slow_path->GetEntryLabel());
3351 break;
3352 }
3353
3354 case TypeCheckKind::kAbstractClassCheck: {
3355 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003356 GenerateReferenceLoadTwoRegisters(instruction,
3357 temp_loc,
3358 obj_loc,
3359 class_offset,
3360 maybe_temp2_loc,
3361 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003362 // If the class is abstract, we eagerly fetch the super class of the
3363 // object to avoid doing a comparison we know will fail.
3364 MipsLabel loop;
3365 __ Bind(&loop);
3366 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003367 GenerateReferenceLoadOneRegister(instruction,
3368 temp_loc,
3369 super_offset,
3370 maybe_temp2_loc,
3371 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003372 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3373 // exception.
3374 __ Beqz(temp, slow_path->GetEntryLabel());
3375 // Otherwise, compare the classes.
3376 __ Bne(temp, cls, &loop);
3377 break;
3378 }
3379
3380 case TypeCheckKind::kClassHierarchyCheck: {
3381 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003382 GenerateReferenceLoadTwoRegisters(instruction,
3383 temp_loc,
3384 obj_loc,
3385 class_offset,
3386 maybe_temp2_loc,
3387 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003388 // Walk over the class hierarchy to find a match.
3389 MipsLabel loop;
3390 __ Bind(&loop);
3391 __ Beq(temp, cls, &done);
3392 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003393 GenerateReferenceLoadOneRegister(instruction,
3394 temp_loc,
3395 super_offset,
3396 maybe_temp2_loc,
3397 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003398 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3399 // exception. Otherwise, jump to the beginning of the loop.
3400 __ Bnez(temp, &loop);
3401 __ B(slow_path->GetEntryLabel());
3402 break;
3403 }
3404
3405 case TypeCheckKind::kArrayObjectCheck: {
3406 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003407 GenerateReferenceLoadTwoRegisters(instruction,
3408 temp_loc,
3409 obj_loc,
3410 class_offset,
3411 maybe_temp2_loc,
3412 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003413 // Do an exact check.
3414 __ Beq(temp, cls, &done);
3415 // Otherwise, we need to check that the object's class is a non-primitive array.
3416 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003417 GenerateReferenceLoadOneRegister(instruction,
3418 temp_loc,
3419 component_offset,
3420 maybe_temp2_loc,
3421 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003422 // If the component type is null, jump to the slow path to throw the exception.
3423 __ Beqz(temp, slow_path->GetEntryLabel());
3424 // Otherwise, the object is indeed an array, further check that this component
3425 // type is not a primitive type.
3426 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3427 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3428 __ Bnez(temp, slow_path->GetEntryLabel());
3429 break;
3430 }
3431
3432 case TypeCheckKind::kUnresolvedCheck:
3433 // We always go into the type check slow path for the unresolved check case.
3434 // We cannot directly call the CheckCast runtime entry point
3435 // without resorting to a type checking slow path here (i.e. by
3436 // calling InvokeRuntime directly), as it would require to
3437 // assign fixed registers for the inputs of this HInstanceOf
3438 // instruction (following the runtime calling convention), which
3439 // might be cluttered by the potential first read barrier
3440 // emission at the beginning of this method.
3441 __ B(slow_path->GetEntryLabel());
3442 break;
3443
3444 case TypeCheckKind::kInterfaceCheck: {
3445 // Avoid read barriers to improve performance of the fast path. We can not get false
3446 // positives by doing this.
3447 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003448 GenerateReferenceLoadTwoRegisters(instruction,
3449 temp_loc,
3450 obj_loc,
3451 class_offset,
3452 maybe_temp2_loc,
3453 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003454 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003455 GenerateReferenceLoadTwoRegisters(instruction,
3456 temp_loc,
3457 temp_loc,
3458 iftable_offset,
3459 maybe_temp2_loc,
3460 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003461 // Iftable is never null.
3462 __ Lw(TMP, temp, array_length_offset);
3463 // Loop through the iftable and check if any class matches.
3464 MipsLabel loop;
3465 __ Bind(&loop);
3466 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3467 __ Beqz(TMP, slow_path->GetEntryLabel());
3468 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3469 __ MaybeUnpoisonHeapReference(AT);
3470 // Go to next interface.
3471 __ Addiu(TMP, TMP, -2);
3472 // Compare the classes and continue the loop if they do not match.
3473 __ Bne(AT, cls, &loop);
3474 break;
3475 }
3476 }
3477
3478 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003479 __ Bind(slow_path->GetExitLabel());
3480}
3481
3482void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3483 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003484 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003485 locations->SetInAt(0, Location::RequiresRegister());
3486 if (check->HasUses()) {
3487 locations->SetOut(Location::SameAsFirstInput());
3488 }
3489}
3490
3491void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3492 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003493 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003494 check->GetLoadClass(),
3495 check,
3496 check->GetDexPc(),
3497 true);
3498 codegen_->AddSlowPath(slow_path);
3499 GenerateClassInitializationCheck(slow_path,
3500 check->GetLocations()->InAt(0).AsRegister<Register>());
3501}
3502
3503void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003505
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003506 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003507 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003508
3509 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003511 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003513 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003514 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003515 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003516 locations->SetInAt(0, Location::RequiresRegister());
3517 locations->SetInAt(1, Location::RequiresRegister());
3518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3519 break;
3520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522 locations->SetInAt(0, Location::RequiresRegister());
3523 locations->SetInAt(1, Location::RequiresRegister());
3524 // Output overlaps because it is written before doing the low comparison.
3525 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3526 break;
3527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kFloat32:
3529 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003530 locations->SetInAt(0, Location::RequiresFpuRegister());
3531 locations->SetInAt(1, Location::RequiresFpuRegister());
3532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003533 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003534
3535 default:
3536 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3537 }
3538}
3539
3540void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3541 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003542 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003544 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003545
3546 // 0 if: left == right
3547 // 1 if: left > right
3548 // -1 if: left < right
3549 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003550 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003551 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003553 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003554 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003555 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003556 Register lhs = locations->InAt(0).AsRegister<Register>();
3557 Register rhs = locations->InAt(1).AsRegister<Register>();
3558 __ Slt(TMP, lhs, rhs);
3559 __ Slt(res, rhs, lhs);
3560 __ Subu(res, res, TMP);
3561 break;
3562 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003563 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003564 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003565 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3566 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3567 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3568 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3569 // TODO: more efficient (direct) comparison with a constant.
3570 __ Slt(TMP, lhs_high, rhs_high);
3571 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3572 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3573 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3574 __ Sltu(TMP, lhs_low, rhs_low);
3575 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3576 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3577 __ Bind(&done);
3578 break;
3579 }
3580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003582 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003583 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3584 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3585 MipsLabel done;
3586 if (isR6) {
3587 __ CmpEqS(FTMP, lhs, rhs);
3588 __ LoadConst32(res, 0);
3589 __ Bc1nez(FTMP, &done);
3590 if (gt_bias) {
3591 __ CmpLtS(FTMP, lhs, rhs);
3592 __ LoadConst32(res, -1);
3593 __ Bc1nez(FTMP, &done);
3594 __ LoadConst32(res, 1);
3595 } else {
3596 __ CmpLtS(FTMP, rhs, lhs);
3597 __ LoadConst32(res, 1);
3598 __ Bc1nez(FTMP, &done);
3599 __ LoadConst32(res, -1);
3600 }
3601 } else {
3602 if (gt_bias) {
3603 __ ColtS(0, lhs, rhs);
3604 __ LoadConst32(res, -1);
3605 __ Bc1t(0, &done);
3606 __ CeqS(0, lhs, rhs);
3607 __ LoadConst32(res, 1);
3608 __ Movt(res, ZERO, 0);
3609 } else {
3610 __ ColtS(0, rhs, lhs);
3611 __ LoadConst32(res, 1);
3612 __ Bc1t(0, &done);
3613 __ CeqS(0, lhs, rhs);
3614 __ LoadConst32(res, -1);
3615 __ Movt(res, ZERO, 0);
3616 }
3617 }
3618 __ Bind(&done);
3619 break;
3620 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003622 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003623 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3624 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3625 MipsLabel done;
3626 if (isR6) {
3627 __ CmpEqD(FTMP, lhs, rhs);
3628 __ LoadConst32(res, 0);
3629 __ Bc1nez(FTMP, &done);
3630 if (gt_bias) {
3631 __ CmpLtD(FTMP, lhs, rhs);
3632 __ LoadConst32(res, -1);
3633 __ Bc1nez(FTMP, &done);
3634 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003635 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003636 __ CmpLtD(FTMP, rhs, lhs);
3637 __ LoadConst32(res, 1);
3638 __ Bc1nez(FTMP, &done);
3639 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003640 }
3641 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003642 if (gt_bias) {
3643 __ ColtD(0, lhs, rhs);
3644 __ LoadConst32(res, -1);
3645 __ Bc1t(0, &done);
3646 __ CeqD(0, lhs, rhs);
3647 __ LoadConst32(res, 1);
3648 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003650 __ ColtD(0, rhs, lhs);
3651 __ LoadConst32(res, 1);
3652 __ Bc1t(0, &done);
3653 __ CeqD(0, lhs, rhs);
3654 __ LoadConst32(res, -1);
3655 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656 }
3657 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003658 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003659 break;
3660 }
3661
3662 default:
3663 LOG(FATAL) << "Unimplemented compare type " << in_type;
3664 }
3665}
3666
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003667void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003668 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003669 switch (instruction->InputAt(0)->GetType()) {
3670 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003672 locations->SetInAt(0, Location::RequiresRegister());
3673 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3674 break;
3675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 case DataType::Type::kFloat32:
3677 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003678 locations->SetInAt(0, Location::RequiresFpuRegister());
3679 locations->SetInAt(1, Location::RequiresFpuRegister());
3680 break;
3681 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003682 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3684 }
3685}
3686
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003687void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003688 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003689 return;
3690 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003694
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003695 switch (type) {
3696 default:
3697 // Integer case.
3698 GenerateIntCompare(instruction->GetCondition(), locations);
3699 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003701 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003702 GenerateLongCompare(instruction->GetCondition(), locations);
3703 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003705 case DataType::Type::kFloat32:
3706 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003707 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3708 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003709 }
3710}
3711
Alexey Frunze7e99e052015-11-24 19:28:01 -08003712void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3713 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003715
3716 LocationSummary* locations = instruction->GetLocations();
3717 Location second = locations->InAt(1);
3718 DCHECK(second.IsConstant());
3719
3720 Register out = locations->Out().AsRegister<Register>();
3721 Register dividend = locations->InAt(0).AsRegister<Register>();
3722 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3723 DCHECK(imm == 1 || imm == -1);
3724
3725 if (instruction->IsRem()) {
3726 __ Move(out, ZERO);
3727 } else {
3728 if (imm == -1) {
3729 __ Subu(out, ZERO, dividend);
3730 } else if (out != dividend) {
3731 __ Move(out, dividend);
3732 }
3733 }
3734}
3735
3736void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3737 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003739
3740 LocationSummary* locations = instruction->GetLocations();
3741 Location second = locations->InAt(1);
3742 DCHECK(second.IsConstant());
3743
3744 Register out = locations->Out().AsRegister<Register>();
3745 Register dividend = locations->InAt(0).AsRegister<Register>();
3746 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003747 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003748 int ctz_imm = CTZ(abs_imm);
3749
3750 if (instruction->IsDiv()) {
3751 if (ctz_imm == 1) {
3752 // Fast path for division by +/-2, which is very common.
3753 __ Srl(TMP, dividend, 31);
3754 } else {
3755 __ Sra(TMP, dividend, 31);
3756 __ Srl(TMP, TMP, 32 - ctz_imm);
3757 }
3758 __ Addu(out, dividend, TMP);
3759 __ Sra(out, out, ctz_imm);
3760 if (imm < 0) {
3761 __ Subu(out, ZERO, out);
3762 }
3763 } else {
3764 if (ctz_imm == 1) {
3765 // Fast path for modulo +/-2, which is very common.
3766 __ Sra(TMP, dividend, 31);
3767 __ Subu(out, dividend, TMP);
3768 __ Andi(out, out, 1);
3769 __ Addu(out, out, TMP);
3770 } else {
3771 __ Sra(TMP, dividend, 31);
3772 __ Srl(TMP, TMP, 32 - ctz_imm);
3773 __ Addu(out, dividend, TMP);
3774 if (IsUint<16>(abs_imm - 1)) {
3775 __ Andi(out, out, abs_imm - 1);
3776 } else {
Lena Djokica556e6b2017-12-13 12:09:42 +01003777 if (codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2()) {
3778 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3779 } else {
3780 __ Sll(out, out, 32 - ctz_imm);
3781 __ Srl(out, out, 32 - ctz_imm);
3782 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003783 }
3784 __ Subu(out, out, TMP);
3785 }
3786 }
3787}
3788
3789void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3790 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003792
3793 LocationSummary* locations = instruction->GetLocations();
3794 Location second = locations->InAt(1);
3795 DCHECK(second.IsConstant());
3796
3797 Register out = locations->Out().AsRegister<Register>();
3798 Register dividend = locations->InAt(0).AsRegister<Register>();
3799 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3800
3801 int64_t magic;
3802 int shift;
3803 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3804
3805 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3806
3807 __ LoadConst32(TMP, magic);
3808 if (isR6) {
3809 __ MuhR6(TMP, dividend, TMP);
3810 } else {
3811 __ MultR2(dividend, TMP);
3812 __ Mfhi(TMP);
3813 }
3814 if (imm > 0 && magic < 0) {
3815 __ Addu(TMP, TMP, dividend);
3816 } else if (imm < 0 && magic > 0) {
3817 __ Subu(TMP, TMP, dividend);
3818 }
3819
3820 if (shift != 0) {
3821 __ Sra(TMP, TMP, shift);
3822 }
3823
3824 if (instruction->IsDiv()) {
3825 __ Sra(out, TMP, 31);
3826 __ Subu(out, TMP, out);
3827 } else {
3828 __ Sra(AT, TMP, 31);
3829 __ Subu(AT, TMP, AT);
3830 __ LoadConst32(TMP, imm);
3831 if (isR6) {
3832 __ MulR6(TMP, AT, TMP);
3833 } else {
3834 __ MulR2(TMP, AT, TMP);
3835 }
3836 __ Subu(out, dividend, TMP);
3837 }
3838}
3839
3840void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3841 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003842 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003843
3844 LocationSummary* locations = instruction->GetLocations();
3845 Register out = locations->Out().AsRegister<Register>();
3846 Location second = locations->InAt(1);
3847
3848 if (second.IsConstant()) {
3849 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3850 if (imm == 0) {
3851 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3852 } else if (imm == 1 || imm == -1) {
3853 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003854 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003855 DivRemByPowerOfTwo(instruction);
3856 } else {
3857 DCHECK(imm <= -2 || imm >= 2);
3858 GenerateDivRemWithAnyConstant(instruction);
3859 }
3860 } else {
3861 Register dividend = locations->InAt(0).AsRegister<Register>();
3862 Register divisor = second.AsRegister<Register>();
3863 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3864 if (instruction->IsDiv()) {
3865 if (isR6) {
3866 __ DivR6(out, dividend, divisor);
3867 } else {
3868 __ DivR2(out, dividend, divisor);
3869 }
3870 } else {
3871 if (isR6) {
3872 __ ModR6(out, dividend, divisor);
3873 } else {
3874 __ ModR2(out, dividend, divisor);
3875 }
3876 }
3877 }
3878}
3879
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003880void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003881 DataType::Type type = div->GetResultType();
3882 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003883 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003884 : LocationSummary::kNoCall;
3885
Vladimir Markoca6fff82017-10-03 14:49:14 +01003886 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003887
3888 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003890 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003891 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3893 break;
3894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896 InvokeRuntimeCallingConvention calling_convention;
3897 locations->SetInAt(0, Location::RegisterPairLocation(
3898 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3899 locations->SetInAt(1, Location::RegisterPairLocation(
3900 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3901 locations->SetOut(calling_convention.GetReturnLocation(type));
3902 break;
3903 }
3904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003905 case DataType::Type::kFloat32:
3906 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 locations->SetInAt(0, Location::RequiresFpuRegister());
3908 locations->SetInAt(1, Location::RequiresFpuRegister());
3909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3910 break;
3911
3912 default:
3913 LOG(FATAL) << "Unexpected div type " << type;
3914 }
3915}
3916
3917void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003918 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003919 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003920
3921 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003923 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003924 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003925 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003926 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003927 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3928 break;
3929 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003930 case DataType::Type::kFloat32:
3931 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003932 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3933 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3934 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003935 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003936 __ DivS(dst, lhs, rhs);
3937 } else {
3938 __ DivD(dst, lhs, rhs);
3939 }
3940 break;
3941 }
3942 default:
3943 LOG(FATAL) << "Unexpected div type " << type;
3944 }
3945}
3946
3947void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003948 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003950}
3951
3952void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003953 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003954 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003955 codegen_->AddSlowPath(slow_path);
3956 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003957 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003958
3959 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003961 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kInt8:
3963 case DataType::Type::kUint16:
3964 case DataType::Type::kInt16:
3965 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003966 if (value.IsConstant()) {
3967 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3968 __ B(slow_path->GetEntryLabel());
3969 } else {
3970 // A division by a non-null constant is valid. We don't need to perform
3971 // any check, so simply fall through.
3972 }
3973 } else {
3974 DCHECK(value.IsRegister()) << value;
3975 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3976 }
3977 break;
3978 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003979 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003980 if (value.IsConstant()) {
3981 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3982 __ B(slow_path->GetEntryLabel());
3983 } else {
3984 // A division by a non-null constant is valid. We don't need to perform
3985 // any check, so simply fall through.
3986 }
3987 } else {
3988 DCHECK(value.IsRegisterPair()) << value;
3989 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3990 __ Beqz(TMP, slow_path->GetEntryLabel());
3991 }
3992 break;
3993 }
3994 default:
3995 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3996 }
3997}
3998
3999void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4000 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004001 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004002 locations->SetOut(Location::ConstantLocation(constant));
4003}
4004
4005void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4006 // Will be generated at use site.
4007}
4008
4009void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4010 exit->SetLocations(nullptr);
4011}
4012
4013void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4014}
4015
4016void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4017 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004018 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004019 locations->SetOut(Location::ConstantLocation(constant));
4020}
4021
4022void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4023 // Will be generated at use site.
4024}
4025
4026void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4027 got->SetLocations(nullptr);
4028}
4029
4030void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
4031 DCHECK(!successor->IsExitBlock());
4032 HBasicBlock* block = got->GetBlock();
4033 HInstruction* previous = got->GetPrevious();
4034 HLoopInformation* info = block->GetLoopInformation();
4035
4036 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004037 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4038 return;
4039 }
4040 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4041 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4042 }
4043 if (!codegen_->GoesToNextBlock(block, successor)) {
4044 __ B(codegen_->GetLabelOf(successor));
4045 }
4046}
4047
4048void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4049 HandleGoto(got, got->GetSuccessor());
4050}
4051
4052void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4053 try_boundary->SetLocations(nullptr);
4054}
4055
4056void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4057 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4058 if (!successor->IsExitBlock()) {
4059 HandleGoto(try_boundary, successor);
4060 }
4061}
4062
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004063void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4064 LocationSummary* locations) {
4065 Register dst = locations->Out().AsRegister<Register>();
4066 Register lhs = locations->InAt(0).AsRegister<Register>();
4067 Location rhs_location = locations->InAt(1);
4068 Register rhs_reg = ZERO;
4069 int64_t rhs_imm = 0;
4070 bool use_imm = rhs_location.IsConstant();
4071 if (use_imm) {
4072 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4073 } else {
4074 rhs_reg = rhs_location.AsRegister<Register>();
4075 }
4076
4077 switch (cond) {
4078 case kCondEQ:
4079 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004080 if (use_imm && IsInt<16>(-rhs_imm)) {
4081 if (rhs_imm == 0) {
4082 if (cond == kCondEQ) {
4083 __ Sltiu(dst, lhs, 1);
4084 } else {
4085 __ Sltu(dst, ZERO, lhs);
4086 }
4087 } else {
4088 __ Addiu(dst, lhs, -rhs_imm);
4089 if (cond == kCondEQ) {
4090 __ Sltiu(dst, dst, 1);
4091 } else {
4092 __ Sltu(dst, ZERO, dst);
4093 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004094 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004095 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004096 if (use_imm && IsUint<16>(rhs_imm)) {
4097 __ Xori(dst, lhs, rhs_imm);
4098 } else {
4099 if (use_imm) {
4100 rhs_reg = TMP;
4101 __ LoadConst32(rhs_reg, rhs_imm);
4102 }
4103 __ Xor(dst, lhs, rhs_reg);
4104 }
4105 if (cond == kCondEQ) {
4106 __ Sltiu(dst, dst, 1);
4107 } else {
4108 __ Sltu(dst, ZERO, dst);
4109 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004110 }
4111 break;
4112
4113 case kCondLT:
4114 case kCondGE:
4115 if (use_imm && IsInt<16>(rhs_imm)) {
4116 __ Slti(dst, lhs, rhs_imm);
4117 } else {
4118 if (use_imm) {
4119 rhs_reg = TMP;
4120 __ LoadConst32(rhs_reg, rhs_imm);
4121 }
4122 __ Slt(dst, lhs, rhs_reg);
4123 }
4124 if (cond == kCondGE) {
4125 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4126 // only the slt instruction but no sge.
4127 __ Xori(dst, dst, 1);
4128 }
4129 break;
4130
4131 case kCondLE:
4132 case kCondGT:
4133 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4134 // Simulate lhs <= rhs via lhs < rhs + 1.
4135 __ Slti(dst, lhs, rhs_imm + 1);
4136 if (cond == kCondGT) {
4137 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4138 // only the slti instruction but no sgti.
4139 __ Xori(dst, dst, 1);
4140 }
4141 } else {
4142 if (use_imm) {
4143 rhs_reg = TMP;
4144 __ LoadConst32(rhs_reg, rhs_imm);
4145 }
4146 __ Slt(dst, rhs_reg, lhs);
4147 if (cond == kCondLE) {
4148 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4149 // only the slt instruction but no sle.
4150 __ Xori(dst, dst, 1);
4151 }
4152 }
4153 break;
4154
4155 case kCondB:
4156 case kCondAE:
4157 if (use_imm && IsInt<16>(rhs_imm)) {
4158 // Sltiu sign-extends its 16-bit immediate operand before
4159 // the comparison and thus lets us compare directly with
4160 // unsigned values in the ranges [0, 0x7fff] and
4161 // [0xffff8000, 0xffffffff].
4162 __ Sltiu(dst, lhs, rhs_imm);
4163 } else {
4164 if (use_imm) {
4165 rhs_reg = TMP;
4166 __ LoadConst32(rhs_reg, rhs_imm);
4167 }
4168 __ Sltu(dst, lhs, rhs_reg);
4169 }
4170 if (cond == kCondAE) {
4171 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4172 // only the sltu instruction but no sgeu.
4173 __ Xori(dst, dst, 1);
4174 }
4175 break;
4176
4177 case kCondBE:
4178 case kCondA:
4179 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4180 // Simulate lhs <= rhs via lhs < rhs + 1.
4181 // Note that this only works if rhs + 1 does not overflow
4182 // to 0, hence the check above.
4183 // Sltiu sign-extends its 16-bit immediate operand before
4184 // the comparison and thus lets us compare directly with
4185 // unsigned values in the ranges [0, 0x7fff] and
4186 // [0xffff8000, 0xffffffff].
4187 __ Sltiu(dst, lhs, rhs_imm + 1);
4188 if (cond == kCondA) {
4189 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4190 // only the sltiu instruction but no sgtiu.
4191 __ Xori(dst, dst, 1);
4192 }
4193 } else {
4194 if (use_imm) {
4195 rhs_reg = TMP;
4196 __ LoadConst32(rhs_reg, rhs_imm);
4197 }
4198 __ Sltu(dst, rhs_reg, lhs);
4199 if (cond == kCondBE) {
4200 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4201 // only the sltu instruction but no sleu.
4202 __ Xori(dst, dst, 1);
4203 }
4204 }
4205 break;
4206 }
4207}
4208
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004209bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4210 LocationSummary* input_locations,
4211 Register dst) {
4212 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4213 Location rhs_location = input_locations->InAt(1);
4214 Register rhs_reg = ZERO;
4215 int64_t rhs_imm = 0;
4216 bool use_imm = rhs_location.IsConstant();
4217 if (use_imm) {
4218 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4219 } else {
4220 rhs_reg = rhs_location.AsRegister<Register>();
4221 }
4222
4223 switch (cond) {
4224 case kCondEQ:
4225 case kCondNE:
4226 if (use_imm && IsInt<16>(-rhs_imm)) {
4227 __ Addiu(dst, lhs, -rhs_imm);
4228 } else if (use_imm && IsUint<16>(rhs_imm)) {
4229 __ Xori(dst, lhs, rhs_imm);
4230 } else {
4231 if (use_imm) {
4232 rhs_reg = TMP;
4233 __ LoadConst32(rhs_reg, rhs_imm);
4234 }
4235 __ Xor(dst, lhs, rhs_reg);
4236 }
4237 return (cond == kCondEQ);
4238
4239 case kCondLT:
4240 case kCondGE:
4241 if (use_imm && IsInt<16>(rhs_imm)) {
4242 __ Slti(dst, lhs, rhs_imm);
4243 } else {
4244 if (use_imm) {
4245 rhs_reg = TMP;
4246 __ LoadConst32(rhs_reg, rhs_imm);
4247 }
4248 __ Slt(dst, lhs, rhs_reg);
4249 }
4250 return (cond == kCondGE);
4251
4252 case kCondLE:
4253 case kCondGT:
4254 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4255 // Simulate lhs <= rhs via lhs < rhs + 1.
4256 __ Slti(dst, lhs, rhs_imm + 1);
4257 return (cond == kCondGT);
4258 } else {
4259 if (use_imm) {
4260 rhs_reg = TMP;
4261 __ LoadConst32(rhs_reg, rhs_imm);
4262 }
4263 __ Slt(dst, rhs_reg, lhs);
4264 return (cond == kCondLE);
4265 }
4266
4267 case kCondB:
4268 case kCondAE:
4269 if (use_imm && IsInt<16>(rhs_imm)) {
4270 // Sltiu sign-extends its 16-bit immediate operand before
4271 // the comparison and thus lets us compare directly with
4272 // unsigned values in the ranges [0, 0x7fff] and
4273 // [0xffff8000, 0xffffffff].
4274 __ Sltiu(dst, lhs, rhs_imm);
4275 } else {
4276 if (use_imm) {
4277 rhs_reg = TMP;
4278 __ LoadConst32(rhs_reg, rhs_imm);
4279 }
4280 __ Sltu(dst, lhs, rhs_reg);
4281 }
4282 return (cond == kCondAE);
4283
4284 case kCondBE:
4285 case kCondA:
4286 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4287 // Simulate lhs <= rhs via lhs < rhs + 1.
4288 // Note that this only works if rhs + 1 does not overflow
4289 // to 0, hence the check above.
4290 // Sltiu sign-extends its 16-bit immediate operand before
4291 // the comparison and thus lets us compare directly with
4292 // unsigned values in the ranges [0, 0x7fff] and
4293 // [0xffff8000, 0xffffffff].
4294 __ Sltiu(dst, lhs, rhs_imm + 1);
4295 return (cond == kCondA);
4296 } else {
4297 if (use_imm) {
4298 rhs_reg = TMP;
4299 __ LoadConst32(rhs_reg, rhs_imm);
4300 }
4301 __ Sltu(dst, rhs_reg, lhs);
4302 return (cond == kCondBE);
4303 }
4304 }
4305}
4306
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004307void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4308 LocationSummary* locations,
4309 MipsLabel* label) {
4310 Register lhs = locations->InAt(0).AsRegister<Register>();
4311 Location rhs_location = locations->InAt(1);
4312 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004313 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004314 bool use_imm = rhs_location.IsConstant();
4315 if (use_imm) {
4316 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4317 } else {
4318 rhs_reg = rhs_location.AsRegister<Register>();
4319 }
4320
4321 if (use_imm && rhs_imm == 0) {
4322 switch (cond) {
4323 case kCondEQ:
4324 case kCondBE: // <= 0 if zero
4325 __ Beqz(lhs, label);
4326 break;
4327 case kCondNE:
4328 case kCondA: // > 0 if non-zero
4329 __ Bnez(lhs, label);
4330 break;
4331 case kCondLT:
4332 __ Bltz(lhs, label);
4333 break;
4334 case kCondGE:
4335 __ Bgez(lhs, label);
4336 break;
4337 case kCondLE:
4338 __ Blez(lhs, label);
4339 break;
4340 case kCondGT:
4341 __ Bgtz(lhs, label);
4342 break;
4343 case kCondB: // always false
4344 break;
4345 case kCondAE: // always true
4346 __ B(label);
4347 break;
4348 }
4349 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004350 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4351 if (isR6 || !use_imm) {
4352 if (use_imm) {
4353 rhs_reg = TMP;
4354 __ LoadConst32(rhs_reg, rhs_imm);
4355 }
4356 switch (cond) {
4357 case kCondEQ:
4358 __ Beq(lhs, rhs_reg, label);
4359 break;
4360 case kCondNE:
4361 __ Bne(lhs, rhs_reg, label);
4362 break;
4363 case kCondLT:
4364 __ Blt(lhs, rhs_reg, label);
4365 break;
4366 case kCondGE:
4367 __ Bge(lhs, rhs_reg, label);
4368 break;
4369 case kCondLE:
4370 __ Bge(rhs_reg, lhs, label);
4371 break;
4372 case kCondGT:
4373 __ Blt(rhs_reg, lhs, label);
4374 break;
4375 case kCondB:
4376 __ Bltu(lhs, rhs_reg, label);
4377 break;
4378 case kCondAE:
4379 __ Bgeu(lhs, rhs_reg, label);
4380 break;
4381 case kCondBE:
4382 __ Bgeu(rhs_reg, lhs, label);
4383 break;
4384 case kCondA:
4385 __ Bltu(rhs_reg, lhs, label);
4386 break;
4387 }
4388 } else {
4389 // Special cases for more efficient comparison with constants on R2.
4390 switch (cond) {
4391 case kCondEQ:
4392 __ LoadConst32(TMP, rhs_imm);
4393 __ Beq(lhs, TMP, label);
4394 break;
4395 case kCondNE:
4396 __ LoadConst32(TMP, rhs_imm);
4397 __ Bne(lhs, TMP, label);
4398 break;
4399 case kCondLT:
4400 if (IsInt<16>(rhs_imm)) {
4401 __ Slti(TMP, lhs, rhs_imm);
4402 __ Bnez(TMP, label);
4403 } else {
4404 __ LoadConst32(TMP, rhs_imm);
4405 __ Blt(lhs, TMP, label);
4406 }
4407 break;
4408 case kCondGE:
4409 if (IsInt<16>(rhs_imm)) {
4410 __ Slti(TMP, lhs, rhs_imm);
4411 __ Beqz(TMP, label);
4412 } else {
4413 __ LoadConst32(TMP, rhs_imm);
4414 __ Bge(lhs, TMP, label);
4415 }
4416 break;
4417 case kCondLE:
4418 if (IsInt<16>(rhs_imm + 1)) {
4419 // Simulate lhs <= rhs via lhs < rhs + 1.
4420 __ Slti(TMP, lhs, rhs_imm + 1);
4421 __ Bnez(TMP, label);
4422 } else {
4423 __ LoadConst32(TMP, rhs_imm);
4424 __ Bge(TMP, lhs, label);
4425 }
4426 break;
4427 case kCondGT:
4428 if (IsInt<16>(rhs_imm + 1)) {
4429 // Simulate lhs > rhs via !(lhs < rhs + 1).
4430 __ Slti(TMP, lhs, rhs_imm + 1);
4431 __ Beqz(TMP, label);
4432 } else {
4433 __ LoadConst32(TMP, rhs_imm);
4434 __ Blt(TMP, lhs, label);
4435 }
4436 break;
4437 case kCondB:
4438 if (IsInt<16>(rhs_imm)) {
4439 __ Sltiu(TMP, lhs, rhs_imm);
4440 __ Bnez(TMP, label);
4441 } else {
4442 __ LoadConst32(TMP, rhs_imm);
4443 __ Bltu(lhs, TMP, label);
4444 }
4445 break;
4446 case kCondAE:
4447 if (IsInt<16>(rhs_imm)) {
4448 __ Sltiu(TMP, lhs, rhs_imm);
4449 __ Beqz(TMP, label);
4450 } else {
4451 __ LoadConst32(TMP, rhs_imm);
4452 __ Bgeu(lhs, TMP, label);
4453 }
4454 break;
4455 case kCondBE:
4456 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4457 // Simulate lhs <= rhs via lhs < rhs + 1.
4458 // Note that this only works if rhs + 1 does not overflow
4459 // to 0, hence the check above.
4460 __ Sltiu(TMP, lhs, rhs_imm + 1);
4461 __ Bnez(TMP, label);
4462 } else {
4463 __ LoadConst32(TMP, rhs_imm);
4464 __ Bgeu(TMP, lhs, label);
4465 }
4466 break;
4467 case kCondA:
4468 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4469 // Simulate lhs > rhs via !(lhs < rhs + 1).
4470 // Note that this only works if rhs + 1 does not overflow
4471 // to 0, hence the check above.
4472 __ Sltiu(TMP, lhs, rhs_imm + 1);
4473 __ Beqz(TMP, label);
4474 } else {
4475 __ LoadConst32(TMP, rhs_imm);
4476 __ Bltu(TMP, lhs, label);
4477 }
4478 break;
4479 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004480 }
4481 }
4482}
4483
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004484void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4485 LocationSummary* locations) {
4486 Register dst = locations->Out().AsRegister<Register>();
4487 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4488 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4489 Location rhs_location = locations->InAt(1);
4490 Register rhs_high = ZERO;
4491 Register rhs_low = ZERO;
4492 int64_t imm = 0;
4493 uint32_t imm_high = 0;
4494 uint32_t imm_low = 0;
4495 bool use_imm = rhs_location.IsConstant();
4496 if (use_imm) {
4497 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4498 imm_high = High32Bits(imm);
4499 imm_low = Low32Bits(imm);
4500 } else {
4501 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4502 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4503 }
4504 if (use_imm && imm == 0) {
4505 switch (cond) {
4506 case kCondEQ:
4507 case kCondBE: // <= 0 if zero
4508 __ Or(dst, lhs_high, lhs_low);
4509 __ Sltiu(dst, dst, 1);
4510 break;
4511 case kCondNE:
4512 case kCondA: // > 0 if non-zero
4513 __ Or(dst, lhs_high, lhs_low);
4514 __ Sltu(dst, ZERO, dst);
4515 break;
4516 case kCondLT:
4517 __ Slt(dst, lhs_high, ZERO);
4518 break;
4519 case kCondGE:
4520 __ Slt(dst, lhs_high, ZERO);
4521 __ Xori(dst, dst, 1);
4522 break;
4523 case kCondLE:
4524 __ Or(TMP, lhs_high, lhs_low);
4525 __ Sra(AT, lhs_high, 31);
4526 __ Sltu(dst, AT, TMP);
4527 __ Xori(dst, dst, 1);
4528 break;
4529 case kCondGT:
4530 __ Or(TMP, lhs_high, lhs_low);
4531 __ Sra(AT, lhs_high, 31);
4532 __ Sltu(dst, AT, TMP);
4533 break;
4534 case kCondB: // always false
4535 __ Andi(dst, dst, 0);
4536 break;
4537 case kCondAE: // always true
4538 __ Ori(dst, ZERO, 1);
4539 break;
4540 }
4541 } else if (use_imm) {
4542 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4543 switch (cond) {
4544 case kCondEQ:
4545 __ LoadConst32(TMP, imm_high);
4546 __ Xor(TMP, TMP, lhs_high);
4547 __ LoadConst32(AT, imm_low);
4548 __ Xor(AT, AT, lhs_low);
4549 __ Or(dst, TMP, AT);
4550 __ Sltiu(dst, dst, 1);
4551 break;
4552 case kCondNE:
4553 __ LoadConst32(TMP, imm_high);
4554 __ Xor(TMP, TMP, lhs_high);
4555 __ LoadConst32(AT, imm_low);
4556 __ Xor(AT, AT, lhs_low);
4557 __ Or(dst, TMP, AT);
4558 __ Sltu(dst, ZERO, dst);
4559 break;
4560 case kCondLT:
4561 case kCondGE:
4562 if (dst == lhs_low) {
4563 __ LoadConst32(TMP, imm_low);
4564 __ Sltu(dst, lhs_low, TMP);
4565 }
4566 __ LoadConst32(TMP, imm_high);
4567 __ Slt(AT, lhs_high, TMP);
4568 __ Slt(TMP, TMP, lhs_high);
4569 if (dst != lhs_low) {
4570 __ LoadConst32(dst, imm_low);
4571 __ Sltu(dst, lhs_low, dst);
4572 }
4573 __ Slt(dst, TMP, dst);
4574 __ Or(dst, dst, AT);
4575 if (cond == kCondGE) {
4576 __ Xori(dst, dst, 1);
4577 }
4578 break;
4579 case kCondGT:
4580 case kCondLE:
4581 if (dst == lhs_low) {
4582 __ LoadConst32(TMP, imm_low);
4583 __ Sltu(dst, TMP, lhs_low);
4584 }
4585 __ LoadConst32(TMP, imm_high);
4586 __ Slt(AT, TMP, lhs_high);
4587 __ Slt(TMP, lhs_high, TMP);
4588 if (dst != lhs_low) {
4589 __ LoadConst32(dst, imm_low);
4590 __ Sltu(dst, dst, lhs_low);
4591 }
4592 __ Slt(dst, TMP, dst);
4593 __ Or(dst, dst, AT);
4594 if (cond == kCondLE) {
4595 __ Xori(dst, dst, 1);
4596 }
4597 break;
4598 case kCondB:
4599 case kCondAE:
4600 if (dst == lhs_low) {
4601 __ LoadConst32(TMP, imm_low);
4602 __ Sltu(dst, lhs_low, TMP);
4603 }
4604 __ LoadConst32(TMP, imm_high);
4605 __ Sltu(AT, lhs_high, TMP);
4606 __ Sltu(TMP, TMP, lhs_high);
4607 if (dst != lhs_low) {
4608 __ LoadConst32(dst, imm_low);
4609 __ Sltu(dst, lhs_low, dst);
4610 }
4611 __ Slt(dst, TMP, dst);
4612 __ Or(dst, dst, AT);
4613 if (cond == kCondAE) {
4614 __ Xori(dst, dst, 1);
4615 }
4616 break;
4617 case kCondA:
4618 case kCondBE:
4619 if (dst == lhs_low) {
4620 __ LoadConst32(TMP, imm_low);
4621 __ Sltu(dst, TMP, lhs_low);
4622 }
4623 __ LoadConst32(TMP, imm_high);
4624 __ Sltu(AT, TMP, lhs_high);
4625 __ Sltu(TMP, lhs_high, TMP);
4626 if (dst != lhs_low) {
4627 __ LoadConst32(dst, imm_low);
4628 __ Sltu(dst, dst, lhs_low);
4629 }
4630 __ Slt(dst, TMP, dst);
4631 __ Or(dst, dst, AT);
4632 if (cond == kCondBE) {
4633 __ Xori(dst, dst, 1);
4634 }
4635 break;
4636 }
4637 } else {
4638 switch (cond) {
4639 case kCondEQ:
4640 __ Xor(TMP, lhs_high, rhs_high);
4641 __ Xor(AT, lhs_low, rhs_low);
4642 __ Or(dst, TMP, AT);
4643 __ Sltiu(dst, dst, 1);
4644 break;
4645 case kCondNE:
4646 __ Xor(TMP, lhs_high, rhs_high);
4647 __ Xor(AT, lhs_low, rhs_low);
4648 __ Or(dst, TMP, AT);
4649 __ Sltu(dst, ZERO, dst);
4650 break;
4651 case kCondLT:
4652 case kCondGE:
4653 __ Slt(TMP, rhs_high, lhs_high);
4654 __ Sltu(AT, lhs_low, rhs_low);
4655 __ Slt(TMP, TMP, AT);
4656 __ Slt(AT, lhs_high, rhs_high);
4657 __ Or(dst, AT, TMP);
4658 if (cond == kCondGE) {
4659 __ Xori(dst, dst, 1);
4660 }
4661 break;
4662 case kCondGT:
4663 case kCondLE:
4664 __ Slt(TMP, lhs_high, rhs_high);
4665 __ Sltu(AT, rhs_low, lhs_low);
4666 __ Slt(TMP, TMP, AT);
4667 __ Slt(AT, rhs_high, lhs_high);
4668 __ Or(dst, AT, TMP);
4669 if (cond == kCondLE) {
4670 __ Xori(dst, dst, 1);
4671 }
4672 break;
4673 case kCondB:
4674 case kCondAE:
4675 __ Sltu(TMP, rhs_high, lhs_high);
4676 __ Sltu(AT, lhs_low, rhs_low);
4677 __ Slt(TMP, TMP, AT);
4678 __ Sltu(AT, lhs_high, rhs_high);
4679 __ Or(dst, AT, TMP);
4680 if (cond == kCondAE) {
4681 __ Xori(dst, dst, 1);
4682 }
4683 break;
4684 case kCondA:
4685 case kCondBE:
4686 __ Sltu(TMP, lhs_high, rhs_high);
4687 __ Sltu(AT, rhs_low, lhs_low);
4688 __ Slt(TMP, TMP, AT);
4689 __ Sltu(AT, rhs_high, lhs_high);
4690 __ Or(dst, AT, TMP);
4691 if (cond == kCondBE) {
4692 __ Xori(dst, dst, 1);
4693 }
4694 break;
4695 }
4696 }
4697}
4698
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004699void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4700 LocationSummary* locations,
4701 MipsLabel* label) {
4702 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4703 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4704 Location rhs_location = locations->InAt(1);
4705 Register rhs_high = ZERO;
4706 Register rhs_low = ZERO;
4707 int64_t imm = 0;
4708 uint32_t imm_high = 0;
4709 uint32_t imm_low = 0;
4710 bool use_imm = rhs_location.IsConstant();
4711 if (use_imm) {
4712 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4713 imm_high = High32Bits(imm);
4714 imm_low = Low32Bits(imm);
4715 } else {
4716 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4717 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4718 }
4719
4720 if (use_imm && imm == 0) {
4721 switch (cond) {
4722 case kCondEQ:
4723 case kCondBE: // <= 0 if zero
4724 __ Or(TMP, lhs_high, lhs_low);
4725 __ Beqz(TMP, label);
4726 break;
4727 case kCondNE:
4728 case kCondA: // > 0 if non-zero
4729 __ Or(TMP, lhs_high, lhs_low);
4730 __ Bnez(TMP, label);
4731 break;
4732 case kCondLT:
4733 __ Bltz(lhs_high, label);
4734 break;
4735 case kCondGE:
4736 __ Bgez(lhs_high, label);
4737 break;
4738 case kCondLE:
4739 __ Or(TMP, lhs_high, lhs_low);
4740 __ Sra(AT, lhs_high, 31);
4741 __ Bgeu(AT, TMP, label);
4742 break;
4743 case kCondGT:
4744 __ Or(TMP, lhs_high, lhs_low);
4745 __ Sra(AT, lhs_high, 31);
4746 __ Bltu(AT, TMP, label);
4747 break;
4748 case kCondB: // always false
4749 break;
4750 case kCondAE: // always true
4751 __ B(label);
4752 break;
4753 }
4754 } else if (use_imm) {
4755 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4756 switch (cond) {
4757 case kCondEQ:
4758 __ LoadConst32(TMP, imm_high);
4759 __ Xor(TMP, TMP, lhs_high);
4760 __ LoadConst32(AT, imm_low);
4761 __ Xor(AT, AT, lhs_low);
4762 __ Or(TMP, TMP, AT);
4763 __ Beqz(TMP, label);
4764 break;
4765 case kCondNE:
4766 __ LoadConst32(TMP, imm_high);
4767 __ Xor(TMP, TMP, lhs_high);
4768 __ LoadConst32(AT, imm_low);
4769 __ Xor(AT, AT, lhs_low);
4770 __ Or(TMP, TMP, AT);
4771 __ Bnez(TMP, label);
4772 break;
4773 case kCondLT:
4774 __ LoadConst32(TMP, imm_high);
4775 __ Blt(lhs_high, TMP, label);
4776 __ Slt(TMP, TMP, lhs_high);
4777 __ LoadConst32(AT, imm_low);
4778 __ Sltu(AT, lhs_low, AT);
4779 __ Blt(TMP, AT, label);
4780 break;
4781 case kCondGE:
4782 __ LoadConst32(TMP, imm_high);
4783 __ Blt(TMP, lhs_high, label);
4784 __ Slt(TMP, lhs_high, TMP);
4785 __ LoadConst32(AT, imm_low);
4786 __ Sltu(AT, lhs_low, AT);
4787 __ Or(TMP, TMP, AT);
4788 __ Beqz(TMP, label);
4789 break;
4790 case kCondLE:
4791 __ LoadConst32(TMP, imm_high);
4792 __ Blt(lhs_high, TMP, label);
4793 __ Slt(TMP, TMP, lhs_high);
4794 __ LoadConst32(AT, imm_low);
4795 __ Sltu(AT, AT, lhs_low);
4796 __ Or(TMP, TMP, AT);
4797 __ Beqz(TMP, label);
4798 break;
4799 case kCondGT:
4800 __ LoadConst32(TMP, imm_high);
4801 __ Blt(TMP, lhs_high, label);
4802 __ Slt(TMP, lhs_high, TMP);
4803 __ LoadConst32(AT, imm_low);
4804 __ Sltu(AT, AT, lhs_low);
4805 __ Blt(TMP, AT, label);
4806 break;
4807 case kCondB:
4808 __ LoadConst32(TMP, imm_high);
4809 __ Bltu(lhs_high, TMP, label);
4810 __ Sltu(TMP, TMP, lhs_high);
4811 __ LoadConst32(AT, imm_low);
4812 __ Sltu(AT, lhs_low, AT);
4813 __ Blt(TMP, AT, label);
4814 break;
4815 case kCondAE:
4816 __ LoadConst32(TMP, imm_high);
4817 __ Bltu(TMP, lhs_high, label);
4818 __ Sltu(TMP, lhs_high, TMP);
4819 __ LoadConst32(AT, imm_low);
4820 __ Sltu(AT, lhs_low, AT);
4821 __ Or(TMP, TMP, AT);
4822 __ Beqz(TMP, label);
4823 break;
4824 case kCondBE:
4825 __ LoadConst32(TMP, imm_high);
4826 __ Bltu(lhs_high, TMP, label);
4827 __ Sltu(TMP, TMP, lhs_high);
4828 __ LoadConst32(AT, imm_low);
4829 __ Sltu(AT, AT, lhs_low);
4830 __ Or(TMP, TMP, AT);
4831 __ Beqz(TMP, label);
4832 break;
4833 case kCondA:
4834 __ LoadConst32(TMP, imm_high);
4835 __ Bltu(TMP, lhs_high, label);
4836 __ Sltu(TMP, lhs_high, TMP);
4837 __ LoadConst32(AT, imm_low);
4838 __ Sltu(AT, AT, lhs_low);
4839 __ Blt(TMP, AT, label);
4840 break;
4841 }
4842 } else {
4843 switch (cond) {
4844 case kCondEQ:
4845 __ Xor(TMP, lhs_high, rhs_high);
4846 __ Xor(AT, lhs_low, rhs_low);
4847 __ Or(TMP, TMP, AT);
4848 __ Beqz(TMP, label);
4849 break;
4850 case kCondNE:
4851 __ Xor(TMP, lhs_high, rhs_high);
4852 __ Xor(AT, lhs_low, rhs_low);
4853 __ Or(TMP, TMP, AT);
4854 __ Bnez(TMP, label);
4855 break;
4856 case kCondLT:
4857 __ Blt(lhs_high, rhs_high, label);
4858 __ Slt(TMP, rhs_high, lhs_high);
4859 __ Sltu(AT, lhs_low, rhs_low);
4860 __ Blt(TMP, AT, label);
4861 break;
4862 case kCondGE:
4863 __ Blt(rhs_high, lhs_high, label);
4864 __ Slt(TMP, lhs_high, rhs_high);
4865 __ Sltu(AT, lhs_low, rhs_low);
4866 __ Or(TMP, TMP, AT);
4867 __ Beqz(TMP, label);
4868 break;
4869 case kCondLE:
4870 __ Blt(lhs_high, rhs_high, label);
4871 __ Slt(TMP, rhs_high, lhs_high);
4872 __ Sltu(AT, rhs_low, lhs_low);
4873 __ Or(TMP, TMP, AT);
4874 __ Beqz(TMP, label);
4875 break;
4876 case kCondGT:
4877 __ Blt(rhs_high, lhs_high, label);
4878 __ Slt(TMP, lhs_high, rhs_high);
4879 __ Sltu(AT, rhs_low, lhs_low);
4880 __ Blt(TMP, AT, label);
4881 break;
4882 case kCondB:
4883 __ Bltu(lhs_high, rhs_high, label);
4884 __ Sltu(TMP, rhs_high, lhs_high);
4885 __ Sltu(AT, lhs_low, rhs_low);
4886 __ Blt(TMP, AT, label);
4887 break;
4888 case kCondAE:
4889 __ Bltu(rhs_high, lhs_high, label);
4890 __ Sltu(TMP, lhs_high, rhs_high);
4891 __ Sltu(AT, lhs_low, rhs_low);
4892 __ Or(TMP, TMP, AT);
4893 __ Beqz(TMP, label);
4894 break;
4895 case kCondBE:
4896 __ Bltu(lhs_high, rhs_high, label);
4897 __ Sltu(TMP, rhs_high, lhs_high);
4898 __ Sltu(AT, rhs_low, lhs_low);
4899 __ Or(TMP, TMP, AT);
4900 __ Beqz(TMP, label);
4901 break;
4902 case kCondA:
4903 __ Bltu(rhs_high, lhs_high, label);
4904 __ Sltu(TMP, lhs_high, rhs_high);
4905 __ Sltu(AT, rhs_low, lhs_low);
4906 __ Blt(TMP, AT, label);
4907 break;
4908 }
4909 }
4910}
4911
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004912void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4913 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004914 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004915 LocationSummary* locations) {
4916 Register dst = locations->Out().AsRegister<Register>();
4917 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4918 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4919 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004920 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004921 if (isR6) {
4922 switch (cond) {
4923 case kCondEQ:
4924 __ CmpEqS(FTMP, lhs, rhs);
4925 __ Mfc1(dst, FTMP);
4926 __ Andi(dst, dst, 1);
4927 break;
4928 case kCondNE:
4929 __ CmpEqS(FTMP, lhs, rhs);
4930 __ Mfc1(dst, FTMP);
4931 __ Addiu(dst, dst, 1);
4932 break;
4933 case kCondLT:
4934 if (gt_bias) {
4935 __ CmpLtS(FTMP, lhs, rhs);
4936 } else {
4937 __ CmpUltS(FTMP, lhs, rhs);
4938 }
4939 __ Mfc1(dst, FTMP);
4940 __ Andi(dst, dst, 1);
4941 break;
4942 case kCondLE:
4943 if (gt_bias) {
4944 __ CmpLeS(FTMP, lhs, rhs);
4945 } else {
4946 __ CmpUleS(FTMP, lhs, rhs);
4947 }
4948 __ Mfc1(dst, FTMP);
4949 __ Andi(dst, dst, 1);
4950 break;
4951 case kCondGT:
4952 if (gt_bias) {
4953 __ CmpUltS(FTMP, rhs, lhs);
4954 } else {
4955 __ CmpLtS(FTMP, rhs, lhs);
4956 }
4957 __ Mfc1(dst, FTMP);
4958 __ Andi(dst, dst, 1);
4959 break;
4960 case kCondGE:
4961 if (gt_bias) {
4962 __ CmpUleS(FTMP, rhs, lhs);
4963 } else {
4964 __ CmpLeS(FTMP, rhs, lhs);
4965 }
4966 __ Mfc1(dst, FTMP);
4967 __ Andi(dst, dst, 1);
4968 break;
4969 default:
4970 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4971 UNREACHABLE();
4972 }
4973 } else {
4974 switch (cond) {
4975 case kCondEQ:
4976 __ CeqS(0, lhs, rhs);
4977 __ LoadConst32(dst, 1);
4978 __ Movf(dst, ZERO, 0);
4979 break;
4980 case kCondNE:
4981 __ CeqS(0, lhs, rhs);
4982 __ LoadConst32(dst, 1);
4983 __ Movt(dst, ZERO, 0);
4984 break;
4985 case kCondLT:
4986 if (gt_bias) {
4987 __ ColtS(0, lhs, rhs);
4988 } else {
4989 __ CultS(0, lhs, rhs);
4990 }
4991 __ LoadConst32(dst, 1);
4992 __ Movf(dst, ZERO, 0);
4993 break;
4994 case kCondLE:
4995 if (gt_bias) {
4996 __ ColeS(0, lhs, rhs);
4997 } else {
4998 __ CuleS(0, lhs, rhs);
4999 }
5000 __ LoadConst32(dst, 1);
5001 __ Movf(dst, ZERO, 0);
5002 break;
5003 case kCondGT:
5004 if (gt_bias) {
5005 __ CultS(0, rhs, lhs);
5006 } else {
5007 __ ColtS(0, rhs, lhs);
5008 }
5009 __ LoadConst32(dst, 1);
5010 __ Movf(dst, ZERO, 0);
5011 break;
5012 case kCondGE:
5013 if (gt_bias) {
5014 __ CuleS(0, rhs, lhs);
5015 } else {
5016 __ ColeS(0, rhs, lhs);
5017 }
5018 __ LoadConst32(dst, 1);
5019 __ Movf(dst, ZERO, 0);
5020 break;
5021 default:
5022 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5023 UNREACHABLE();
5024 }
5025 }
5026 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005027 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005028 if (isR6) {
5029 switch (cond) {
5030 case kCondEQ:
5031 __ CmpEqD(FTMP, lhs, rhs);
5032 __ Mfc1(dst, FTMP);
5033 __ Andi(dst, dst, 1);
5034 break;
5035 case kCondNE:
5036 __ CmpEqD(FTMP, lhs, rhs);
5037 __ Mfc1(dst, FTMP);
5038 __ Addiu(dst, dst, 1);
5039 break;
5040 case kCondLT:
5041 if (gt_bias) {
5042 __ CmpLtD(FTMP, lhs, rhs);
5043 } else {
5044 __ CmpUltD(FTMP, lhs, rhs);
5045 }
5046 __ Mfc1(dst, FTMP);
5047 __ Andi(dst, dst, 1);
5048 break;
5049 case kCondLE:
5050 if (gt_bias) {
5051 __ CmpLeD(FTMP, lhs, rhs);
5052 } else {
5053 __ CmpUleD(FTMP, lhs, rhs);
5054 }
5055 __ Mfc1(dst, FTMP);
5056 __ Andi(dst, dst, 1);
5057 break;
5058 case kCondGT:
5059 if (gt_bias) {
5060 __ CmpUltD(FTMP, rhs, lhs);
5061 } else {
5062 __ CmpLtD(FTMP, rhs, lhs);
5063 }
5064 __ Mfc1(dst, FTMP);
5065 __ Andi(dst, dst, 1);
5066 break;
5067 case kCondGE:
5068 if (gt_bias) {
5069 __ CmpUleD(FTMP, rhs, lhs);
5070 } else {
5071 __ CmpLeD(FTMP, rhs, lhs);
5072 }
5073 __ Mfc1(dst, FTMP);
5074 __ Andi(dst, dst, 1);
5075 break;
5076 default:
5077 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5078 UNREACHABLE();
5079 }
5080 } else {
5081 switch (cond) {
5082 case kCondEQ:
5083 __ CeqD(0, lhs, rhs);
5084 __ LoadConst32(dst, 1);
5085 __ Movf(dst, ZERO, 0);
5086 break;
5087 case kCondNE:
5088 __ CeqD(0, lhs, rhs);
5089 __ LoadConst32(dst, 1);
5090 __ Movt(dst, ZERO, 0);
5091 break;
5092 case kCondLT:
5093 if (gt_bias) {
5094 __ ColtD(0, lhs, rhs);
5095 } else {
5096 __ CultD(0, lhs, rhs);
5097 }
5098 __ LoadConst32(dst, 1);
5099 __ Movf(dst, ZERO, 0);
5100 break;
5101 case kCondLE:
5102 if (gt_bias) {
5103 __ ColeD(0, lhs, rhs);
5104 } else {
5105 __ CuleD(0, lhs, rhs);
5106 }
5107 __ LoadConst32(dst, 1);
5108 __ Movf(dst, ZERO, 0);
5109 break;
5110 case kCondGT:
5111 if (gt_bias) {
5112 __ CultD(0, rhs, lhs);
5113 } else {
5114 __ ColtD(0, rhs, lhs);
5115 }
5116 __ LoadConst32(dst, 1);
5117 __ Movf(dst, ZERO, 0);
5118 break;
5119 case kCondGE:
5120 if (gt_bias) {
5121 __ CuleD(0, rhs, lhs);
5122 } else {
5123 __ ColeD(0, rhs, lhs);
5124 }
5125 __ LoadConst32(dst, 1);
5126 __ Movf(dst, ZERO, 0);
5127 break;
5128 default:
5129 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5130 UNREACHABLE();
5131 }
5132 }
5133 }
5134}
5135
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005136bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5137 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005139 LocationSummary* input_locations,
5140 int cc) {
5141 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5142 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5143 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005144 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005145 switch (cond) {
5146 case kCondEQ:
5147 __ CeqS(cc, lhs, rhs);
5148 return false;
5149 case kCondNE:
5150 __ CeqS(cc, lhs, rhs);
5151 return true;
5152 case kCondLT:
5153 if (gt_bias) {
5154 __ ColtS(cc, lhs, rhs);
5155 } else {
5156 __ CultS(cc, lhs, rhs);
5157 }
5158 return false;
5159 case kCondLE:
5160 if (gt_bias) {
5161 __ ColeS(cc, lhs, rhs);
5162 } else {
5163 __ CuleS(cc, lhs, rhs);
5164 }
5165 return false;
5166 case kCondGT:
5167 if (gt_bias) {
5168 __ CultS(cc, rhs, lhs);
5169 } else {
5170 __ ColtS(cc, rhs, lhs);
5171 }
5172 return false;
5173 case kCondGE:
5174 if (gt_bias) {
5175 __ CuleS(cc, rhs, lhs);
5176 } else {
5177 __ ColeS(cc, rhs, lhs);
5178 }
5179 return false;
5180 default:
5181 LOG(FATAL) << "Unexpected non-floating-point condition";
5182 UNREACHABLE();
5183 }
5184 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005186 switch (cond) {
5187 case kCondEQ:
5188 __ CeqD(cc, lhs, rhs);
5189 return false;
5190 case kCondNE:
5191 __ CeqD(cc, lhs, rhs);
5192 return true;
5193 case kCondLT:
5194 if (gt_bias) {
5195 __ ColtD(cc, lhs, rhs);
5196 } else {
5197 __ CultD(cc, lhs, rhs);
5198 }
5199 return false;
5200 case kCondLE:
5201 if (gt_bias) {
5202 __ ColeD(cc, lhs, rhs);
5203 } else {
5204 __ CuleD(cc, lhs, rhs);
5205 }
5206 return false;
5207 case kCondGT:
5208 if (gt_bias) {
5209 __ CultD(cc, rhs, lhs);
5210 } else {
5211 __ ColtD(cc, rhs, lhs);
5212 }
5213 return false;
5214 case kCondGE:
5215 if (gt_bias) {
5216 __ CuleD(cc, rhs, lhs);
5217 } else {
5218 __ ColeD(cc, rhs, lhs);
5219 }
5220 return false;
5221 default:
5222 LOG(FATAL) << "Unexpected non-floating-point condition";
5223 UNREACHABLE();
5224 }
5225 }
5226}
5227
5228bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5229 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005231 LocationSummary* input_locations,
5232 FRegister dst) {
5233 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5234 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5235 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005237 switch (cond) {
5238 case kCondEQ:
5239 __ CmpEqS(dst, lhs, rhs);
5240 return false;
5241 case kCondNE:
5242 __ CmpEqS(dst, lhs, rhs);
5243 return true;
5244 case kCondLT:
5245 if (gt_bias) {
5246 __ CmpLtS(dst, lhs, rhs);
5247 } else {
5248 __ CmpUltS(dst, lhs, rhs);
5249 }
5250 return false;
5251 case kCondLE:
5252 if (gt_bias) {
5253 __ CmpLeS(dst, lhs, rhs);
5254 } else {
5255 __ CmpUleS(dst, lhs, rhs);
5256 }
5257 return false;
5258 case kCondGT:
5259 if (gt_bias) {
5260 __ CmpUltS(dst, rhs, lhs);
5261 } else {
5262 __ CmpLtS(dst, rhs, lhs);
5263 }
5264 return false;
5265 case kCondGE:
5266 if (gt_bias) {
5267 __ CmpUleS(dst, rhs, lhs);
5268 } else {
5269 __ CmpLeS(dst, rhs, lhs);
5270 }
5271 return false;
5272 default:
5273 LOG(FATAL) << "Unexpected non-floating-point condition";
5274 UNREACHABLE();
5275 }
5276 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005278 switch (cond) {
5279 case kCondEQ:
5280 __ CmpEqD(dst, lhs, rhs);
5281 return false;
5282 case kCondNE:
5283 __ CmpEqD(dst, lhs, rhs);
5284 return true;
5285 case kCondLT:
5286 if (gt_bias) {
5287 __ CmpLtD(dst, lhs, rhs);
5288 } else {
5289 __ CmpUltD(dst, lhs, rhs);
5290 }
5291 return false;
5292 case kCondLE:
5293 if (gt_bias) {
5294 __ CmpLeD(dst, lhs, rhs);
5295 } else {
5296 __ CmpUleD(dst, lhs, rhs);
5297 }
5298 return false;
5299 case kCondGT:
5300 if (gt_bias) {
5301 __ CmpUltD(dst, rhs, lhs);
5302 } else {
5303 __ CmpLtD(dst, rhs, lhs);
5304 }
5305 return false;
5306 case kCondGE:
5307 if (gt_bias) {
5308 __ CmpUleD(dst, rhs, lhs);
5309 } else {
5310 __ CmpLeD(dst, rhs, lhs);
5311 }
5312 return false;
5313 default:
5314 LOG(FATAL) << "Unexpected non-floating-point condition";
5315 UNREACHABLE();
5316 }
5317 }
5318}
5319
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005320void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5321 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005322 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005323 LocationSummary* locations,
5324 MipsLabel* label) {
5325 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5326 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5327 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005328 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005329 if (isR6) {
5330 switch (cond) {
5331 case kCondEQ:
5332 __ CmpEqS(FTMP, lhs, rhs);
5333 __ Bc1nez(FTMP, label);
5334 break;
5335 case kCondNE:
5336 __ CmpEqS(FTMP, lhs, rhs);
5337 __ Bc1eqz(FTMP, label);
5338 break;
5339 case kCondLT:
5340 if (gt_bias) {
5341 __ CmpLtS(FTMP, lhs, rhs);
5342 } else {
5343 __ CmpUltS(FTMP, lhs, rhs);
5344 }
5345 __ Bc1nez(FTMP, label);
5346 break;
5347 case kCondLE:
5348 if (gt_bias) {
5349 __ CmpLeS(FTMP, lhs, rhs);
5350 } else {
5351 __ CmpUleS(FTMP, lhs, rhs);
5352 }
5353 __ Bc1nez(FTMP, label);
5354 break;
5355 case kCondGT:
5356 if (gt_bias) {
5357 __ CmpUltS(FTMP, rhs, lhs);
5358 } else {
5359 __ CmpLtS(FTMP, rhs, lhs);
5360 }
5361 __ Bc1nez(FTMP, label);
5362 break;
5363 case kCondGE:
5364 if (gt_bias) {
5365 __ CmpUleS(FTMP, rhs, lhs);
5366 } else {
5367 __ CmpLeS(FTMP, rhs, lhs);
5368 }
5369 __ Bc1nez(FTMP, label);
5370 break;
5371 default:
5372 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005373 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005374 }
5375 } else {
5376 switch (cond) {
5377 case kCondEQ:
5378 __ CeqS(0, lhs, rhs);
5379 __ Bc1t(0, label);
5380 break;
5381 case kCondNE:
5382 __ CeqS(0, lhs, rhs);
5383 __ Bc1f(0, label);
5384 break;
5385 case kCondLT:
5386 if (gt_bias) {
5387 __ ColtS(0, lhs, rhs);
5388 } else {
5389 __ CultS(0, lhs, rhs);
5390 }
5391 __ Bc1t(0, label);
5392 break;
5393 case kCondLE:
5394 if (gt_bias) {
5395 __ ColeS(0, lhs, rhs);
5396 } else {
5397 __ CuleS(0, lhs, rhs);
5398 }
5399 __ Bc1t(0, label);
5400 break;
5401 case kCondGT:
5402 if (gt_bias) {
5403 __ CultS(0, rhs, lhs);
5404 } else {
5405 __ ColtS(0, rhs, lhs);
5406 }
5407 __ Bc1t(0, label);
5408 break;
5409 case kCondGE:
5410 if (gt_bias) {
5411 __ CuleS(0, rhs, lhs);
5412 } else {
5413 __ ColeS(0, rhs, lhs);
5414 }
5415 __ Bc1t(0, label);
5416 break;
5417 default:
5418 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005419 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005420 }
5421 }
5422 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005424 if (isR6) {
5425 switch (cond) {
5426 case kCondEQ:
5427 __ CmpEqD(FTMP, lhs, rhs);
5428 __ Bc1nez(FTMP, label);
5429 break;
5430 case kCondNE:
5431 __ CmpEqD(FTMP, lhs, rhs);
5432 __ Bc1eqz(FTMP, label);
5433 break;
5434 case kCondLT:
5435 if (gt_bias) {
5436 __ CmpLtD(FTMP, lhs, rhs);
5437 } else {
5438 __ CmpUltD(FTMP, lhs, rhs);
5439 }
5440 __ Bc1nez(FTMP, label);
5441 break;
5442 case kCondLE:
5443 if (gt_bias) {
5444 __ CmpLeD(FTMP, lhs, rhs);
5445 } else {
5446 __ CmpUleD(FTMP, lhs, rhs);
5447 }
5448 __ Bc1nez(FTMP, label);
5449 break;
5450 case kCondGT:
5451 if (gt_bias) {
5452 __ CmpUltD(FTMP, rhs, lhs);
5453 } else {
5454 __ CmpLtD(FTMP, rhs, lhs);
5455 }
5456 __ Bc1nez(FTMP, label);
5457 break;
5458 case kCondGE:
5459 if (gt_bias) {
5460 __ CmpUleD(FTMP, rhs, lhs);
5461 } else {
5462 __ CmpLeD(FTMP, rhs, lhs);
5463 }
5464 __ Bc1nez(FTMP, label);
5465 break;
5466 default:
5467 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005468 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005469 }
5470 } else {
5471 switch (cond) {
5472 case kCondEQ:
5473 __ CeqD(0, lhs, rhs);
5474 __ Bc1t(0, label);
5475 break;
5476 case kCondNE:
5477 __ CeqD(0, lhs, rhs);
5478 __ Bc1f(0, label);
5479 break;
5480 case kCondLT:
5481 if (gt_bias) {
5482 __ ColtD(0, lhs, rhs);
5483 } else {
5484 __ CultD(0, lhs, rhs);
5485 }
5486 __ Bc1t(0, label);
5487 break;
5488 case kCondLE:
5489 if (gt_bias) {
5490 __ ColeD(0, lhs, rhs);
5491 } else {
5492 __ CuleD(0, lhs, rhs);
5493 }
5494 __ Bc1t(0, label);
5495 break;
5496 case kCondGT:
5497 if (gt_bias) {
5498 __ CultD(0, rhs, lhs);
5499 } else {
5500 __ ColtD(0, rhs, lhs);
5501 }
5502 __ Bc1t(0, label);
5503 break;
5504 case kCondGE:
5505 if (gt_bias) {
5506 __ CuleD(0, rhs, lhs);
5507 } else {
5508 __ ColeD(0, rhs, lhs);
5509 }
5510 __ Bc1t(0, label);
5511 break;
5512 default:
5513 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005514 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005515 }
5516 }
5517 }
5518}
5519
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005520void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005521 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005522 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005523 MipsLabel* false_target) {
5524 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005525
David Brazdil0debae72015-11-12 18:37:00 +00005526 if (true_target == nullptr && false_target == nullptr) {
5527 // Nothing to do. The code always falls through.
5528 return;
5529 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005530 // Constant condition, statically compared against "true" (integer value 1).
5531 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005532 if (true_target != nullptr) {
5533 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005534 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005535 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005536 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005537 if (false_target != nullptr) {
5538 __ B(false_target);
5539 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005540 }
David Brazdil0debae72015-11-12 18:37:00 +00005541 return;
5542 }
5543
5544 // The following code generates these patterns:
5545 // (1) true_target == nullptr && false_target != nullptr
5546 // - opposite condition true => branch to false_target
5547 // (2) true_target != nullptr && false_target == nullptr
5548 // - condition true => branch to true_target
5549 // (3) true_target != nullptr && false_target != nullptr
5550 // - condition true => branch to true_target
5551 // - branch to false_target
5552 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005553 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005554 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005555 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005556 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005557 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5558 } else {
5559 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5560 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561 } else {
5562 // The condition instruction has not been materialized, use its inputs as
5563 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005564 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005565 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005566 LocationSummary* locations = cond->GetLocations();
5567 IfCondition if_cond = condition->GetCondition();
5568 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005569
David Brazdil0debae72015-11-12 18:37:00 +00005570 if (true_target == nullptr) {
5571 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005572 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005573 }
5574
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005575 switch (type) {
5576 default:
5577 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5578 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005580 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5581 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 case DataType::Type::kFloat32:
5583 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005584 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5585 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005586 }
5587 }
David Brazdil0debae72015-11-12 18:37:00 +00005588
5589 // If neither branch falls through (case 3), the conditional branch to `true_target`
5590 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5591 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005592 __ B(false_target);
5593 }
5594}
5595
5596void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005597 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005598 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005599 locations->SetInAt(0, Location::RequiresRegister());
5600 }
5601}
5602
5603void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005604 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5605 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5606 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5607 nullptr : codegen_->GetLabelOf(true_successor);
5608 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5609 nullptr : codegen_->GetLabelOf(false_successor);
5610 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005611}
5612
5613void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005614 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005615 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005616 InvokeRuntimeCallingConvention calling_convention;
5617 RegisterSet caller_saves = RegisterSet::Empty();
5618 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5619 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005620 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005621 locations->SetInAt(0, Location::RequiresRegister());
5622 }
5623}
5624
5625void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005626 SlowPathCodeMIPS* slow_path =
5627 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005628 GenerateTestAndBranch(deoptimize,
5629 /* condition_input_index */ 0,
5630 slow_path->GetEntryLabel(),
5631 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005632}
5633
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005634// This function returns true if a conditional move can be generated for HSelect.
5635// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5636// branches and regular moves.
5637//
5638// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5639//
5640// While determining feasibility of a conditional move and setting inputs/outputs
5641// are two distinct tasks, this function does both because they share quite a bit
5642// of common logic.
5643static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5644 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5645 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5646 HCondition* condition = cond->AsCondition();
5647
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005648 DataType::Type cond_type =
5649 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5650 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005651
5652 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5653 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5654 bool is_true_value_zero_constant =
5655 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5656 bool is_false_value_zero_constant =
5657 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5658
5659 bool can_move_conditionally = false;
5660 bool use_const_for_false_in = false;
5661 bool use_const_for_true_in = false;
5662
5663 if (!cond->IsConstant()) {
5664 switch (cond_type) {
5665 default:
5666 switch (dst_type) {
5667 default:
5668 // Moving int on int condition.
5669 if (is_r6) {
5670 if (is_true_value_zero_constant) {
5671 // seleqz out_reg, false_reg, cond_reg
5672 can_move_conditionally = true;
5673 use_const_for_true_in = true;
5674 } else if (is_false_value_zero_constant) {
5675 // selnez out_reg, true_reg, cond_reg
5676 can_move_conditionally = true;
5677 use_const_for_false_in = true;
5678 } else if (materialized) {
5679 // Not materializing unmaterialized int conditions
5680 // to keep the instruction count low.
5681 // selnez AT, true_reg, cond_reg
5682 // seleqz TMP, false_reg, cond_reg
5683 // or out_reg, AT, TMP
5684 can_move_conditionally = true;
5685 }
5686 } else {
5687 // movn out_reg, true_reg/ZERO, cond_reg
5688 can_move_conditionally = true;
5689 use_const_for_true_in = is_true_value_zero_constant;
5690 }
5691 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005692 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005693 // Moving long on int condition.
5694 if (is_r6) {
5695 if (is_true_value_zero_constant) {
5696 // seleqz out_reg_lo, false_reg_lo, cond_reg
5697 // seleqz out_reg_hi, false_reg_hi, cond_reg
5698 can_move_conditionally = true;
5699 use_const_for_true_in = true;
5700 } else if (is_false_value_zero_constant) {
5701 // selnez out_reg_lo, true_reg_lo, cond_reg
5702 // selnez out_reg_hi, true_reg_hi, cond_reg
5703 can_move_conditionally = true;
5704 use_const_for_false_in = true;
5705 }
5706 // Other long conditional moves would generate 6+ instructions,
5707 // which is too many.
5708 } else {
5709 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5710 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5711 can_move_conditionally = true;
5712 use_const_for_true_in = is_true_value_zero_constant;
5713 }
5714 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 case DataType::Type::kFloat32:
5716 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005717 // Moving float/double on int condition.
5718 if (is_r6) {
5719 if (materialized) {
5720 // Not materializing unmaterialized int conditions
5721 // to keep the instruction count low.
5722 can_move_conditionally = true;
5723 if (is_true_value_zero_constant) {
5724 // sltu TMP, ZERO, cond_reg
5725 // mtc1 TMP, temp_cond_reg
5726 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5727 use_const_for_true_in = true;
5728 } else if (is_false_value_zero_constant) {
5729 // sltu TMP, ZERO, cond_reg
5730 // mtc1 TMP, temp_cond_reg
5731 // selnez.fmt out_reg, true_reg, temp_cond_reg
5732 use_const_for_false_in = true;
5733 } else {
5734 // sltu TMP, ZERO, cond_reg
5735 // mtc1 TMP, temp_cond_reg
5736 // sel.fmt temp_cond_reg, false_reg, true_reg
5737 // mov.fmt out_reg, temp_cond_reg
5738 }
5739 }
5740 } else {
5741 // movn.fmt out_reg, true_reg, cond_reg
5742 can_move_conditionally = true;
5743 }
5744 break;
5745 }
5746 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005748 // We don't materialize long comparison now
5749 // and use conditional branches instead.
5750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005751 case DataType::Type::kFloat32:
5752 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005753 switch (dst_type) {
5754 default:
5755 // Moving int on float/double condition.
5756 if (is_r6) {
5757 if (is_true_value_zero_constant) {
5758 // mfc1 TMP, temp_cond_reg
5759 // seleqz out_reg, false_reg, TMP
5760 can_move_conditionally = true;
5761 use_const_for_true_in = true;
5762 } else if (is_false_value_zero_constant) {
5763 // mfc1 TMP, temp_cond_reg
5764 // selnez out_reg, true_reg, TMP
5765 can_move_conditionally = true;
5766 use_const_for_false_in = true;
5767 } else {
5768 // mfc1 TMP, temp_cond_reg
5769 // selnez AT, true_reg, TMP
5770 // seleqz TMP, false_reg, TMP
5771 // or out_reg, AT, TMP
5772 can_move_conditionally = true;
5773 }
5774 } else {
5775 // movt out_reg, true_reg/ZERO, cc
5776 can_move_conditionally = true;
5777 use_const_for_true_in = is_true_value_zero_constant;
5778 }
5779 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005781 // Moving long on float/double condition.
5782 if (is_r6) {
5783 if (is_true_value_zero_constant) {
5784 // mfc1 TMP, temp_cond_reg
5785 // seleqz out_reg_lo, false_reg_lo, TMP
5786 // seleqz out_reg_hi, false_reg_hi, TMP
5787 can_move_conditionally = true;
5788 use_const_for_true_in = true;
5789 } else if (is_false_value_zero_constant) {
5790 // mfc1 TMP, temp_cond_reg
5791 // selnez out_reg_lo, true_reg_lo, TMP
5792 // selnez out_reg_hi, true_reg_hi, TMP
5793 can_move_conditionally = true;
5794 use_const_for_false_in = true;
5795 }
5796 // Other long conditional moves would generate 6+ instructions,
5797 // which is too many.
5798 } else {
5799 // movt out_reg_lo, true_reg_lo/ZERO, cc
5800 // movt out_reg_hi, true_reg_hi/ZERO, cc
5801 can_move_conditionally = true;
5802 use_const_for_true_in = is_true_value_zero_constant;
5803 }
5804 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005805 case DataType::Type::kFloat32:
5806 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005807 // Moving float/double on float/double condition.
5808 if (is_r6) {
5809 can_move_conditionally = true;
5810 if (is_true_value_zero_constant) {
5811 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5812 use_const_for_true_in = true;
5813 } else if (is_false_value_zero_constant) {
5814 // selnez.fmt out_reg, true_reg, temp_cond_reg
5815 use_const_for_false_in = true;
5816 } else {
5817 // sel.fmt temp_cond_reg, false_reg, true_reg
5818 // mov.fmt out_reg, temp_cond_reg
5819 }
5820 } else {
5821 // movt.fmt out_reg, true_reg, cc
5822 can_move_conditionally = true;
5823 }
5824 break;
5825 }
5826 break;
5827 }
5828 }
5829
5830 if (can_move_conditionally) {
5831 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5832 } else {
5833 DCHECK(!use_const_for_false_in);
5834 DCHECK(!use_const_for_true_in);
5835 }
5836
5837 if (locations_to_set != nullptr) {
5838 if (use_const_for_false_in) {
5839 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5840 } else {
5841 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005842 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005843 ? Location::RequiresFpuRegister()
5844 : Location::RequiresRegister());
5845 }
5846 if (use_const_for_true_in) {
5847 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5848 } else {
5849 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005851 ? Location::RequiresFpuRegister()
5852 : Location::RequiresRegister());
5853 }
5854 if (materialized) {
5855 locations_to_set->SetInAt(2, Location::RequiresRegister());
5856 }
5857 // On R6 we don't require the output to be the same as the
5858 // first input for conditional moves unlike on R2.
5859 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5860 if (is_out_same_as_first_in) {
5861 locations_to_set->SetOut(Location::SameAsFirstInput());
5862 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005864 ? Location::RequiresFpuRegister()
5865 : Location::RequiresRegister());
5866 }
5867 }
5868
5869 return can_move_conditionally;
5870}
5871
5872void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5873 LocationSummary* locations = select->GetLocations();
5874 Location dst = locations->Out();
5875 Location src = locations->InAt(1);
5876 Register src_reg = ZERO;
5877 Register src_reg_high = ZERO;
5878 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5879 Register cond_reg = TMP;
5880 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005882 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005884
5885 if (IsBooleanValueOrMaterializedCondition(cond)) {
5886 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5887 } else {
5888 HCondition* condition = cond->AsCondition();
5889 LocationSummary* cond_locations = cond->GetLocations();
5890 IfCondition if_cond = condition->GetCondition();
5891 cond_type = condition->InputAt(0)->GetType();
5892 switch (cond_type) {
5893 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005895 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5896 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 case DataType::Type::kFloat32:
5898 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005899 cond_inverted = MaterializeFpCompareR2(if_cond,
5900 condition->IsGtBias(),
5901 cond_type,
5902 cond_locations,
5903 cond_cc);
5904 break;
5905 }
5906 }
5907
5908 DCHECK(dst.Equals(locations->InAt(0)));
5909 if (src.IsRegister()) {
5910 src_reg = src.AsRegister<Register>();
5911 } else if (src.IsRegisterPair()) {
5912 src_reg = src.AsRegisterPairLow<Register>();
5913 src_reg_high = src.AsRegisterPairHigh<Register>();
5914 } else if (src.IsConstant()) {
5915 DCHECK(src.GetConstant()->IsZeroBitPattern());
5916 }
5917
5918 switch (cond_type) {
5919 default:
5920 switch (dst_type) {
5921 default:
5922 if (cond_inverted) {
5923 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5924 } else {
5925 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5926 }
5927 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005929 if (cond_inverted) {
5930 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5931 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5932 } else {
5933 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5934 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5935 }
5936 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005938 if (cond_inverted) {
5939 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5940 } else {
5941 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5942 }
5943 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005945 if (cond_inverted) {
5946 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5947 } else {
5948 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5949 }
5950 break;
5951 }
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005954 LOG(FATAL) << "Unreachable";
5955 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 case DataType::Type::kFloat32:
5957 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005958 switch (dst_type) {
5959 default:
5960 if (cond_inverted) {
5961 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5962 } else {
5963 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5964 }
5965 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005966 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005967 if (cond_inverted) {
5968 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5969 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5970 } else {
5971 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5972 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5973 }
5974 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005975 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005976 if (cond_inverted) {
5977 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5978 } else {
5979 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5980 }
5981 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005983 if (cond_inverted) {
5984 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5985 } else {
5986 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5987 }
5988 break;
5989 }
5990 break;
5991 }
5992}
5993
5994void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5995 LocationSummary* locations = select->GetLocations();
5996 Location dst = locations->Out();
5997 Location false_src = locations->InAt(0);
5998 Location true_src = locations->InAt(1);
5999 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6000 Register cond_reg = TMP;
6001 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006002 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006003 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006005
6006 if (IsBooleanValueOrMaterializedCondition(cond)) {
6007 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6008 } else {
6009 HCondition* condition = cond->AsCondition();
6010 LocationSummary* cond_locations = cond->GetLocations();
6011 IfCondition if_cond = condition->GetCondition();
6012 cond_type = condition->InputAt(0)->GetType();
6013 switch (cond_type) {
6014 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006015 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006016 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6017 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 case DataType::Type::kFloat32:
6019 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006020 cond_inverted = MaterializeFpCompareR6(if_cond,
6021 condition->IsGtBias(),
6022 cond_type,
6023 cond_locations,
6024 fcond_reg);
6025 break;
6026 }
6027 }
6028
6029 if (true_src.IsConstant()) {
6030 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6031 }
6032 if (false_src.IsConstant()) {
6033 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6034 }
6035
6036 switch (dst_type) {
6037 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006038 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006039 __ Mfc1(cond_reg, fcond_reg);
6040 }
6041 if (true_src.IsConstant()) {
6042 if (cond_inverted) {
6043 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6044 } else {
6045 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6046 }
6047 } else if (false_src.IsConstant()) {
6048 if (cond_inverted) {
6049 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6050 } else {
6051 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6052 }
6053 } else {
6054 DCHECK_NE(cond_reg, AT);
6055 if (cond_inverted) {
6056 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6057 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6058 } else {
6059 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6060 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6061 }
6062 __ Or(dst.AsRegister<Register>(), AT, TMP);
6063 }
6064 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 case DataType::Type::kInt64: {
6066 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006067 __ Mfc1(cond_reg, fcond_reg);
6068 }
6069 Register dst_lo = dst.AsRegisterPairLow<Register>();
6070 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6071 if (true_src.IsConstant()) {
6072 Register src_lo = false_src.AsRegisterPairLow<Register>();
6073 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6074 if (cond_inverted) {
6075 __ Selnez(dst_lo, src_lo, cond_reg);
6076 __ Selnez(dst_hi, src_hi, cond_reg);
6077 } else {
6078 __ Seleqz(dst_lo, src_lo, cond_reg);
6079 __ Seleqz(dst_hi, src_hi, cond_reg);
6080 }
6081 } else {
6082 DCHECK(false_src.IsConstant());
6083 Register src_lo = true_src.AsRegisterPairLow<Register>();
6084 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6085 if (cond_inverted) {
6086 __ Seleqz(dst_lo, src_lo, cond_reg);
6087 __ Seleqz(dst_hi, src_hi, cond_reg);
6088 } else {
6089 __ Selnez(dst_lo, src_lo, cond_reg);
6090 __ Selnez(dst_hi, src_hi, cond_reg);
6091 }
6092 }
6093 break;
6094 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006095 case DataType::Type::kFloat32: {
6096 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006097 // sel*.fmt tests bit 0 of the condition register, account for that.
6098 __ Sltu(TMP, ZERO, cond_reg);
6099 __ Mtc1(TMP, fcond_reg);
6100 }
6101 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6102 if (true_src.IsConstant()) {
6103 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6104 if (cond_inverted) {
6105 __ SelnezS(dst_reg, src_reg, fcond_reg);
6106 } else {
6107 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6108 }
6109 } else if (false_src.IsConstant()) {
6110 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6111 if (cond_inverted) {
6112 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6113 } else {
6114 __ SelnezS(dst_reg, src_reg, fcond_reg);
6115 }
6116 } else {
6117 if (cond_inverted) {
6118 __ SelS(fcond_reg,
6119 true_src.AsFpuRegister<FRegister>(),
6120 false_src.AsFpuRegister<FRegister>());
6121 } else {
6122 __ SelS(fcond_reg,
6123 false_src.AsFpuRegister<FRegister>(),
6124 true_src.AsFpuRegister<FRegister>());
6125 }
6126 __ MovS(dst_reg, fcond_reg);
6127 }
6128 break;
6129 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006130 case DataType::Type::kFloat64: {
6131 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006132 // sel*.fmt tests bit 0 of the condition register, account for that.
6133 __ Sltu(TMP, ZERO, cond_reg);
6134 __ Mtc1(TMP, fcond_reg);
6135 }
6136 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6137 if (true_src.IsConstant()) {
6138 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6139 if (cond_inverted) {
6140 __ SelnezD(dst_reg, src_reg, fcond_reg);
6141 } else {
6142 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6143 }
6144 } else if (false_src.IsConstant()) {
6145 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6146 if (cond_inverted) {
6147 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6148 } else {
6149 __ SelnezD(dst_reg, src_reg, fcond_reg);
6150 }
6151 } else {
6152 if (cond_inverted) {
6153 __ SelD(fcond_reg,
6154 true_src.AsFpuRegister<FRegister>(),
6155 false_src.AsFpuRegister<FRegister>());
6156 } else {
6157 __ SelD(fcond_reg,
6158 false_src.AsFpuRegister<FRegister>(),
6159 true_src.AsFpuRegister<FRegister>());
6160 }
6161 __ MovD(dst_reg, fcond_reg);
6162 }
6163 break;
6164 }
6165 }
6166}
6167
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006168void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006169 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006170 LocationSummary(flag, LocationSummary::kNoCall);
6171 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006172}
6173
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006174void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6175 __ LoadFromOffset(kLoadWord,
6176 flag->GetLocations()->Out().AsRegister<Register>(),
6177 SP,
6178 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006179}
6180
David Brazdil74eb1b22015-12-14 11:44:01 +00006181void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006182 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006183 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006184}
6185
6186void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006187 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6188 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6189 if (is_r6) {
6190 GenConditionalMoveR6(select);
6191 } else {
6192 GenConditionalMoveR2(select);
6193 }
6194 } else {
6195 LocationSummary* locations = select->GetLocations();
6196 MipsLabel false_target;
6197 GenerateTestAndBranch(select,
6198 /* condition_input_index */ 2,
6199 /* true_target */ nullptr,
6200 &false_target);
6201 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6202 __ Bind(&false_target);
6203 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006204}
6205
David Srbecky0cf44932015-12-09 14:09:59 +00006206void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006207 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006208}
6209
David Srbeckyd28f4a02016-03-14 17:14:24 +00006210void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6211 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006212}
6213
6214void CodeGeneratorMIPS::GenerateNop() {
6215 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006216}
6217
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006218void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006219 DataType::Type field_type = field_info.GetFieldType();
6220 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006222 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006224 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006225 instruction,
6226 generate_volatile
6227 ? LocationSummary::kCallOnMainOnly
6228 : (object_field_get_with_read_barrier
6229 ? LocationSummary::kCallOnSlowPath
6230 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006231
Alexey Frunzec61c0762017-04-10 13:54:23 -07006232 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6233 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6234 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 locations->SetInAt(0, Location::RequiresRegister());
6236 if (generate_volatile) {
6237 InvokeRuntimeCallingConvention calling_convention;
6238 // need A0 to hold base + offset
6239 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 if (field_type == DataType::Type::kInt64) {
6241 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006242 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006243 // Use Location::Any() to prevent situations when running out of available fp registers.
6244 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006245 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006247 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6248 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6249 }
6250 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006251 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006252 locations->SetOut(Location::RequiresFpuRegister());
6253 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006254 // The output overlaps in the case of an object field get with
6255 // read barriers enabled: we do not want the move to overwrite the
6256 // object's location, as we need it to emit the read barrier.
6257 locations->SetOut(Location::RequiresRegister(),
6258 object_field_get_with_read_barrier
6259 ? Location::kOutputOverlap
6260 : Location::kNoOutputOverlap);
6261 }
6262 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6263 // We need a temporary register for the read barrier marking slow
6264 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006265 if (!kBakerReadBarrierThunksEnableForFields) {
6266 locations->AddTemp(Location::RequiresRegister());
6267 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006268 }
6269 }
6270}
6271
6272void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6273 const FieldInfo& field_info,
6274 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006275 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6276 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006277 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006278 Location obj_loc = locations->InAt(0);
6279 Register obj = obj_loc.AsRegister<Register>();
6280 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006281 LoadOperandType load_type = kLoadUnsignedByte;
6282 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006283 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006284 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006285
6286 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006287 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006288 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006289 load_type = kLoadUnsignedByte;
6290 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006291 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006292 load_type = kLoadSignedByte;
6293 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 load_type = kLoadUnsignedHalfword;
6296 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006297 case DataType::Type::kInt16:
6298 load_type = kLoadSignedHalfword;
6299 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006300 case DataType::Type::kInt32:
6301 case DataType::Type::kFloat32:
6302 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303 load_type = kLoadWord;
6304 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006305 case DataType::Type::kInt64:
6306 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006307 load_type = kLoadDoubleword;
6308 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006309 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006310 LOG(FATAL) << "Unreachable type " << type;
6311 UNREACHABLE();
6312 }
6313
6314 if (is_volatile && load_type == kLoadDoubleword) {
6315 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006316 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006317 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006318 __ LoadFromOffset(kLoadWord,
6319 ZERO,
6320 locations->GetTemp(0).AsRegister<Register>(),
6321 0,
6322 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006323 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006324 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006325 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006326 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006327 if (dst_loc.IsFpuRegister()) {
6328 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006329 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006330 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006331 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006332 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006333 __ StoreToOffset(kStoreWord,
6334 locations->GetTemp(1).AsRegister<Register>(),
6335 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006336 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006337 __ StoreToOffset(kStoreWord,
6338 locations->GetTemp(2).AsRegister<Register>(),
6339 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006340 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006341 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006342 }
6343 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006344 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006345 // /* HeapReference<Object> */ dst = *(obj + offset)
6346 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006347 Location temp_loc =
6348 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006349 // Note that a potential implicit null check is handled in this
6350 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6351 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6352 dst_loc,
6353 obj,
6354 offset,
6355 temp_loc,
6356 /* needs_null_check */ true);
6357 if (is_volatile) {
6358 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6359 }
6360 } else {
6361 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6362 if (is_volatile) {
6363 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6364 }
6365 // If read barriers are enabled, emit read barriers other than
6366 // Baker's using a slow path (and also unpoison the loaded
6367 // reference, if heap poisoning is enabled).
6368 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6369 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006370 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006371 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006372 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006373 DCHECK(dst_loc.IsRegisterPair());
6374 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006375 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006376 DCHECK(dst_loc.IsRegister());
6377 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006379 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006381 DCHECK(dst_loc.IsFpuRegister());
6382 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006384 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006386 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387 }
6388 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006389 }
6390
Alexey Frunze15958152017-02-09 19:08:30 -08006391 // Memory barriers, in the case of references, are handled in the
6392 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006393 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6395 }
6396}
6397
6398void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 DataType::Type field_type = field_info.GetFieldType();
6400 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006401 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006402 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006403 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006404
6405 locations->SetInAt(0, Location::RequiresRegister());
6406 if (generate_volatile) {
6407 InvokeRuntimeCallingConvention calling_convention;
6408 // need A0 to hold base + offset
6409 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006411 locations->SetInAt(1, Location::RegisterPairLocation(
6412 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6413 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006414 // Use Location::Any() to prevent situations when running out of available fp registers.
6415 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006416 // Pass FP parameters in core registers.
6417 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6418 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6419 }
6420 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006422 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006423 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006424 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425 }
6426 }
6427}
6428
6429void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6430 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006431 uint32_t dex_pc,
6432 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006433 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006434 LocationSummary* locations = instruction->GetLocations();
6435 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006436 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 StoreOperandType store_type = kStoreByte;
6438 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006439 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006440 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006441 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006442
6443 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006444 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006445 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006446 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006447 store_type = kStoreByte;
6448 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006450 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006451 store_type = kStoreHalfword;
6452 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006453 case DataType::Type::kInt32:
6454 case DataType::Type::kFloat32:
6455 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006456 store_type = kStoreWord;
6457 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006458 case DataType::Type::kInt64:
6459 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006460 store_type = kStoreDoubleword;
6461 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006462 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006463 LOG(FATAL) << "Unreachable type " << type;
6464 UNREACHABLE();
6465 }
6466
6467 if (is_volatile) {
6468 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6469 }
6470
6471 if (is_volatile && store_type == kStoreDoubleword) {
6472 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006473 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006474 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006475 __ LoadFromOffset(kLoadWord,
6476 ZERO,
6477 locations->GetTemp(0).AsRegister<Register>(),
6478 0,
6479 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006481 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006482 if (value_location.IsFpuRegister()) {
6483 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6484 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006485 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006486 value_location.AsFpuRegister<FRegister>());
6487 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006488 __ LoadFromOffset(kLoadWord,
6489 locations->GetTemp(1).AsRegister<Register>(),
6490 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006491 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006492 __ LoadFromOffset(kLoadWord,
6493 locations->GetTemp(2).AsRegister<Register>(),
6494 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006495 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006496 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006497 DCHECK(value_location.IsConstant());
6498 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6499 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006500 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6501 locations->GetTemp(1).AsRegister<Register>(),
6502 value);
6503 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006504 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006505 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6507 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006508 if (value_location.IsConstant()) {
6509 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6510 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006511 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006512 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006514 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006515 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006516 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006517 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006518 if (kPoisonHeapReferences && needs_write_barrier) {
6519 // Note that in the case where `value` is a null reference,
6520 // we do not enter this block, as a null reference does not
6521 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006522 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006523 __ PoisonHeapReference(TMP, src);
6524 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6525 } else {
6526 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6527 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006528 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006529 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006530 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006531 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006532 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006533 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534 }
6535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006536 }
6537
Alexey Frunzec061de12017-02-14 13:27:23 -08006538 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006539 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006540 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006541 }
6542
6543 if (is_volatile) {
6544 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6545 }
6546}
6547
6548void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6549 HandleFieldGet(instruction, instruction->GetFieldInfo());
6550}
6551
6552void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6553 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6554}
6555
6556void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6557 HandleFieldSet(instruction, instruction->GetFieldInfo());
6558}
6559
6560void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006561 HandleFieldSet(instruction,
6562 instruction->GetFieldInfo(),
6563 instruction->GetDexPc(),
6564 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006565}
6566
Alexey Frunze15958152017-02-09 19:08:30 -08006567void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6568 HInstruction* instruction,
6569 Location out,
6570 uint32_t offset,
6571 Location maybe_temp,
6572 ReadBarrierOption read_barrier_option) {
6573 Register out_reg = out.AsRegister<Register>();
6574 if (read_barrier_option == kWithReadBarrier) {
6575 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006576 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6577 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6578 }
Alexey Frunze15958152017-02-09 19:08:30 -08006579 if (kUseBakerReadBarrier) {
6580 // Load with fast path based Baker's read barrier.
6581 // /* HeapReference<Object> */ out = *(out + offset)
6582 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6583 out,
6584 out_reg,
6585 offset,
6586 maybe_temp,
6587 /* needs_null_check */ false);
6588 } else {
6589 // Load with slow path based read barrier.
6590 // Save the value of `out` into `maybe_temp` before overwriting it
6591 // in the following move operation, as we will need it for the
6592 // read barrier below.
6593 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6594 // /* HeapReference<Object> */ out = *(out + offset)
6595 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6596 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6597 }
6598 } else {
6599 // Plain load with no read barrier.
6600 // /* HeapReference<Object> */ out = *(out + offset)
6601 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6602 __ MaybeUnpoisonHeapReference(out_reg);
6603 }
6604}
6605
6606void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6607 HInstruction* instruction,
6608 Location out,
6609 Location obj,
6610 uint32_t offset,
6611 Location maybe_temp,
6612 ReadBarrierOption read_barrier_option) {
6613 Register out_reg = out.AsRegister<Register>();
6614 Register obj_reg = obj.AsRegister<Register>();
6615 if (read_barrier_option == kWithReadBarrier) {
6616 CHECK(kEmitCompilerReadBarrier);
6617 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006618 if (!kBakerReadBarrierThunksEnableForFields) {
6619 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6620 }
Alexey Frunze15958152017-02-09 19:08:30 -08006621 // Load with fast path based Baker's read barrier.
6622 // /* HeapReference<Object> */ out = *(obj + offset)
6623 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6624 out,
6625 obj_reg,
6626 offset,
6627 maybe_temp,
6628 /* needs_null_check */ false);
6629 } else {
6630 // Load with slow path based read barrier.
6631 // /* HeapReference<Object> */ out = *(obj + offset)
6632 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6633 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6634 }
6635 } else {
6636 // Plain load with no read barrier.
6637 // /* HeapReference<Object> */ out = *(obj + offset)
6638 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6639 __ MaybeUnpoisonHeapReference(out_reg);
6640 }
6641}
6642
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006643static inline int GetBakerMarkThunkNumber(Register reg) {
6644 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6645 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6646 return reg - V0;
6647 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6648 return 14 + (reg - S2);
6649 } else if (reg == FP) { // One more.
6650 return 20;
6651 }
6652 LOG(FATAL) << "Unexpected register " << reg;
6653 UNREACHABLE();
6654}
6655
6656static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6657 int num = GetBakerMarkThunkNumber(reg) +
6658 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6659 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6660}
6661
6662static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6663 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6664 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6665}
6666
Alexey Frunze15958152017-02-09 19:08:30 -08006667void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6668 Location root,
6669 Register obj,
6670 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006671 ReadBarrierOption read_barrier_option,
6672 MipsLabel* label_low) {
6673 bool reordering;
6674 if (label_low != nullptr) {
6675 DCHECK_EQ(offset, 0x5678u);
6676 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006677 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006678 if (read_barrier_option == kWithReadBarrier) {
6679 DCHECK(kEmitCompilerReadBarrier);
6680 if (kUseBakerReadBarrier) {
6681 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6682 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006683 if (kBakerReadBarrierThunksEnableForGcRoots) {
6684 // Note that we do not actually check the value of `GetIsGcMarking()`
6685 // to decide whether to mark the loaded GC root or not. Instead, we
6686 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6687 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6688 // vice versa.
6689 //
6690 // We use thunks for the slow path. That thunk checks the reference
6691 // and jumps to the entrypoint if needed.
6692 //
6693 // temp = Thread::Current()->pReadBarrierMarkReg00
6694 // // AKA &art_quick_read_barrier_mark_introspection.
6695 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6696 // if (temp != nullptr) {
6697 // temp = &gc_root_thunk<root_reg>
6698 // root = temp(root)
6699 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006700
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006701 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6702 const int32_t entry_point_offset =
6703 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6704 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6705 int16_t offset_low = Low16Bits(offset);
6706 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6707 // extension in lw.
6708 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6709 Register base = short_offset ? obj : TMP;
6710 // Loading the entrypoint does not require a load acquire since it is only changed when
6711 // threads are suspended or running a checkpoint.
6712 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6713 reordering = __ SetReorder(false);
6714 if (!short_offset) {
6715 DCHECK(!label_low);
6716 __ AddUpper(base, obj, offset_high);
6717 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006718 MipsLabel skip_call;
6719 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006720 if (label_low != nullptr) {
6721 DCHECK(short_offset);
6722 __ Bind(label_low);
6723 }
6724 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6725 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6726 // in delay slot.
6727 if (isR6) {
6728 __ Jialc(T9, thunk_disp);
6729 } else {
6730 __ Addiu(T9, T9, thunk_disp);
6731 __ Jalr(T9);
6732 __ Nop();
6733 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006734 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006735 __ SetReorder(reordering);
6736 } else {
6737 // Note that we do not actually check the value of `GetIsGcMarking()`
6738 // to decide whether to mark the loaded GC root or not. Instead, we
6739 // load into `temp` (T9) the read barrier mark entry point corresponding
6740 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6741 // is false, and vice versa.
6742 //
6743 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6744 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6745 // if (temp != null) {
6746 // root = temp(root)
6747 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006748
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006749 if (label_low != nullptr) {
6750 reordering = __ SetReorder(false);
6751 __ Bind(label_low);
6752 }
6753 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6754 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6755 if (label_low != nullptr) {
6756 __ SetReorder(reordering);
6757 }
6758 static_assert(
6759 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6760 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6761 "have different sizes.");
6762 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6763 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6764 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006765
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006766 // Slow path marking the GC root `root`.
6767 Location temp = Location::RegisterLocation(T9);
6768 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006769 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006770 instruction,
6771 root,
6772 /*entrypoint*/ temp);
6773 codegen_->AddSlowPath(slow_path);
6774
6775 const int32_t entry_point_offset =
6776 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6777 // Loading the entrypoint does not require a load acquire since it is only changed when
6778 // threads are suspended or running a checkpoint.
6779 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6780 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6781 __ Bind(slow_path->GetExitLabel());
6782 }
Alexey Frunze15958152017-02-09 19:08:30 -08006783 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006784 if (label_low != nullptr) {
6785 reordering = __ SetReorder(false);
6786 __ Bind(label_low);
6787 }
Alexey Frunze15958152017-02-09 19:08:30 -08006788 // GC root loaded through a slow path for read barriers other
6789 // than Baker's.
6790 // /* GcRoot<mirror::Object>* */ root = obj + offset
6791 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006792 if (label_low != nullptr) {
6793 __ SetReorder(reordering);
6794 }
Alexey Frunze15958152017-02-09 19:08:30 -08006795 // /* mirror::Object* */ root = root->Read()
6796 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6797 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006798 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006799 if (label_low != nullptr) {
6800 reordering = __ SetReorder(false);
6801 __ Bind(label_low);
6802 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006803 // Plain GC root load with no read barrier.
6804 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6805 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6806 // Note that GC roots are not affected by heap poisoning, thus we
6807 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006808 if (label_low != nullptr) {
6809 __ SetReorder(reordering);
6810 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006811 }
6812}
6813
Alexey Frunze15958152017-02-09 19:08:30 -08006814void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6815 Location ref,
6816 Register obj,
6817 uint32_t offset,
6818 Location temp,
6819 bool needs_null_check) {
6820 DCHECK(kEmitCompilerReadBarrier);
6821 DCHECK(kUseBakerReadBarrier);
6822
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006823 if (kBakerReadBarrierThunksEnableForFields) {
6824 // Note that we do not actually check the value of `GetIsGcMarking()`
6825 // to decide whether to mark the loaded reference or not. Instead, we
6826 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6827 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6828 // vice versa.
6829 //
6830 // We use thunks for the slow path. That thunk checks the reference
6831 // and jumps to the entrypoint if needed. If the holder is not gray,
6832 // it issues a load-load memory barrier and returns to the original
6833 // reference load.
6834 //
6835 // temp = Thread::Current()->pReadBarrierMarkReg00
6836 // // AKA &art_quick_read_barrier_mark_introspection.
6837 // if (temp != nullptr) {
6838 // temp = &field_array_thunk<holder_reg>
6839 // temp()
6840 // }
6841 // not_gray_return_address:
6842 // // If the offset is too large to fit into the lw instruction, we
6843 // // use an adjusted base register (TMP) here. This register
6844 // // receives bits 16 ... 31 of the offset before the thunk invocation
6845 // // and the thunk benefits from it.
6846 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6847 // gray_return_address:
6848
6849 DCHECK(temp.IsInvalid());
6850 bool isR6 = GetInstructionSetFeatures().IsR6();
6851 int16_t offset_low = Low16Bits(offset);
6852 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6853 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6854 bool reordering = __ SetReorder(false);
6855 const int32_t entry_point_offset =
6856 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6857 // There may have or may have not been a null check if the field offset is smaller than
6858 // the page size.
6859 // There must've been a null check in case it's actually a load from an array.
6860 // We will, however, perform an explicit null check in the thunk as it's easier to
6861 // do it than not.
6862 if (instruction->IsArrayGet()) {
6863 DCHECK(!needs_null_check);
6864 }
6865 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6866 // Loading the entrypoint does not require a load acquire since it is only changed when
6867 // threads are suspended or running a checkpoint.
6868 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6869 Register ref_reg = ref.AsRegister<Register>();
6870 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006871 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006872 if (short_offset) {
6873 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006874 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006875 __ Nop(); // In forbidden slot.
6876 __ Jialc(T9, thunk_disp);
6877 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006878 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006879 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6880 __ Jalr(T9);
6881 __ Nop(); // In delay slot.
6882 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006883 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006884 } else {
6885 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006886 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006887 __ Aui(base, obj, offset_high); // In delay slot.
6888 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006889 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006890 } else {
6891 __ Lui(base, offset_high);
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);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006895 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006896 __ Addu(base, base, obj); // In delay slot.
6897 }
6898 }
6899 // /* HeapReference<Object> */ ref = *(obj + offset)
6900 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6901 if (needs_null_check) {
6902 MaybeRecordImplicitNullCheck(instruction);
6903 }
6904 __ MaybeUnpoisonHeapReference(ref_reg);
6905 __ SetReorder(reordering);
6906 return;
6907 }
6908
Alexey Frunze15958152017-02-09 19:08:30 -08006909 // /* HeapReference<Object> */ ref = *(obj + offset)
6910 Location no_index = Location::NoLocation();
6911 ScaleFactor no_scale_factor = TIMES_1;
6912 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6913 ref,
6914 obj,
6915 offset,
6916 no_index,
6917 no_scale_factor,
6918 temp,
6919 needs_null_check);
6920}
6921
6922void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6923 Location ref,
6924 Register obj,
6925 uint32_t data_offset,
6926 Location index,
6927 Location temp,
6928 bool needs_null_check) {
6929 DCHECK(kEmitCompilerReadBarrier);
6930 DCHECK(kUseBakerReadBarrier);
6931
6932 static_assert(
6933 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6934 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006935 ScaleFactor scale_factor = TIMES_4;
6936
6937 if (kBakerReadBarrierThunksEnableForArrays) {
6938 // Note that we do not actually check the value of `GetIsGcMarking()`
6939 // to decide whether to mark the loaded reference or not. Instead, we
6940 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6941 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6942 // vice versa.
6943 //
6944 // We use thunks for the slow path. That thunk checks the reference
6945 // and jumps to the entrypoint if needed. If the holder is not gray,
6946 // it issues a load-load memory barrier and returns to the original
6947 // reference load.
6948 //
6949 // temp = Thread::Current()->pReadBarrierMarkReg00
6950 // // AKA &art_quick_read_barrier_mark_introspection.
6951 // if (temp != nullptr) {
6952 // temp = &field_array_thunk<holder_reg>
6953 // temp()
6954 // }
6955 // not_gray_return_address:
6956 // // The element address is pre-calculated in the TMP register before the
6957 // // thunk invocation and the thunk benefits from it.
6958 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6959 // gray_return_address:
6960
6961 DCHECK(temp.IsInvalid());
6962 DCHECK(index.IsValid());
6963 bool reordering = __ SetReorder(false);
6964 const int32_t entry_point_offset =
6965 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6966 // We will not do the explicit null check in the thunk as some form of a null check
6967 // must've been done earlier.
6968 DCHECK(!needs_null_check);
6969 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6970 // Loading the entrypoint does not require a load acquire since it is only changed when
6971 // threads are suspended or running a checkpoint.
6972 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6973 Register ref_reg = ref.AsRegister<Register>();
6974 Register index_reg = index.IsRegisterPair()
6975 ? index.AsRegisterPairLow<Register>()
6976 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006977 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006978 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006979 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006980 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6981 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006982 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006983 } else {
6984 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006985 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006986 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6987 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006988 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006989 __ Addu(TMP, TMP, obj); // In delay slot.
6990 }
6991 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6992 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6993 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6994 __ MaybeUnpoisonHeapReference(ref_reg);
6995 __ SetReorder(reordering);
6996 return;
6997 }
6998
Alexey Frunze15958152017-02-09 19:08:30 -08006999 // /* HeapReference<Object> */ ref =
7000 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007001 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7002 ref,
7003 obj,
7004 data_offset,
7005 index,
7006 scale_factor,
7007 temp,
7008 needs_null_check);
7009}
7010
7011void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7012 Location ref,
7013 Register obj,
7014 uint32_t offset,
7015 Location index,
7016 ScaleFactor scale_factor,
7017 Location temp,
7018 bool needs_null_check,
7019 bool always_update_field) {
7020 DCHECK(kEmitCompilerReadBarrier);
7021 DCHECK(kUseBakerReadBarrier);
7022
7023 // In slow path based read barriers, the read barrier call is
7024 // inserted after the original load. However, in fast path based
7025 // Baker's read barriers, we need to perform the load of
7026 // mirror::Object::monitor_ *before* the original reference load.
7027 // This load-load ordering is required by the read barrier.
7028 // The fast path/slow path (for Baker's algorithm) should look like:
7029 //
7030 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7031 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7032 // HeapReference<Object> ref = *src; // Original reference load.
7033 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7034 // if (is_gray) {
7035 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7036 // }
7037 //
7038 // Note: the original implementation in ReadBarrier::Barrier is
7039 // slightly more complex as it performs additional checks that we do
7040 // not do here for performance reasons.
7041
7042 Register ref_reg = ref.AsRegister<Register>();
7043 Register temp_reg = temp.AsRegister<Register>();
7044 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7045
7046 // /* int32_t */ monitor = obj->monitor_
7047 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7048 if (needs_null_check) {
7049 MaybeRecordImplicitNullCheck(instruction);
7050 }
7051 // /* LockWord */ lock_word = LockWord(monitor)
7052 static_assert(sizeof(LockWord) == sizeof(int32_t),
7053 "art::LockWord and int32_t have different sizes.");
7054
7055 __ Sync(0); // Barrier to prevent load-load reordering.
7056
7057 // The actual reference load.
7058 if (index.IsValid()) {
7059 // Load types involving an "index": ArrayGet,
7060 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7061 // intrinsics.
7062 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7063 if (index.IsConstant()) {
7064 size_t computed_offset =
7065 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7066 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7067 } else {
7068 // Handle the special case of the
7069 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7070 // intrinsics, which use a register pair as index ("long
7071 // offset"), of which only the low part contains data.
7072 Register index_reg = index.IsRegisterPair()
7073 ? index.AsRegisterPairLow<Register>()
7074 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007075 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007076 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7077 }
7078 } else {
7079 // /* HeapReference<Object> */ ref = *(obj + offset)
7080 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7081 }
7082
7083 // Object* ref = ref_addr->AsMirrorPtr()
7084 __ MaybeUnpoisonHeapReference(ref_reg);
7085
7086 // Slow path marking the object `ref` when it is gray.
7087 SlowPathCodeMIPS* slow_path;
7088 if (always_update_field) {
7089 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7090 // of the form `obj + field_offset`, where `obj` is a register and
7091 // `field_offset` is a register pair (of which only the lower half
7092 // is used). Thus `offset` and `scale_factor` above are expected
7093 // to be null in this code path.
7094 DCHECK_EQ(offset, 0u);
7095 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007096 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007097 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7098 ref,
7099 obj,
7100 /* field_offset */ index,
7101 temp_reg);
7102 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007103 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007104 }
7105 AddSlowPath(slow_path);
7106
7107 // if (rb_state == ReadBarrier::GrayState())
7108 // ref = ReadBarrier::Mark(ref);
7109 // Given the numeric representation, it's enough to check the low bit of the
7110 // rb_state. We do that by shifting the bit into the sign bit (31) and
7111 // performing a branch on less than zero.
7112 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7113 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7114 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7115 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7116 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7117 __ Bind(slow_path->GetExitLabel());
7118}
7119
7120void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7121 Location out,
7122 Location ref,
7123 Location obj,
7124 uint32_t offset,
7125 Location index) {
7126 DCHECK(kEmitCompilerReadBarrier);
7127
7128 // Insert a slow path based read barrier *after* the reference load.
7129 //
7130 // If heap poisoning is enabled, the unpoisoning of the loaded
7131 // reference will be carried out by the runtime within the slow
7132 // path.
7133 //
7134 // Note that `ref` currently does not get unpoisoned (when heap
7135 // poisoning is enabled), which is alright as the `ref` argument is
7136 // not used by the artReadBarrierSlow entry point.
7137 //
7138 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007139 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007140 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7141 AddSlowPath(slow_path);
7142
7143 __ B(slow_path->GetEntryLabel());
7144 __ Bind(slow_path->GetExitLabel());
7145}
7146
7147void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7148 Location out,
7149 Location ref,
7150 Location obj,
7151 uint32_t offset,
7152 Location index) {
7153 if (kEmitCompilerReadBarrier) {
7154 // Baker's read barriers shall be handled by the fast path
7155 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7156 DCHECK(!kUseBakerReadBarrier);
7157 // If heap poisoning is enabled, unpoisoning will be taken care of
7158 // by the runtime within the slow path.
7159 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7160 } else if (kPoisonHeapReferences) {
7161 __ UnpoisonHeapReference(out.AsRegister<Register>());
7162 }
7163}
7164
7165void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7166 Location out,
7167 Location root) {
7168 DCHECK(kEmitCompilerReadBarrier);
7169
7170 // Insert a slow path based read barrier *after* the GC root load.
7171 //
7172 // Note that GC roots are not affected by heap poisoning, so we do
7173 // not need to do anything special for this here.
7174 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007175 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007176 AddSlowPath(slow_path);
7177
7178 __ B(slow_path->GetEntryLabel());
7179 __ Bind(slow_path->GetExitLabel());
7180}
7181
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007182void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007183 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7184 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007185 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007186 switch (type_check_kind) {
7187 case TypeCheckKind::kExactCheck:
7188 case TypeCheckKind::kAbstractClassCheck:
7189 case TypeCheckKind::kClassHierarchyCheck:
7190 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007191 call_kind =
7192 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007193 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007194 break;
7195 case TypeCheckKind::kArrayCheck:
7196 case TypeCheckKind::kUnresolvedCheck:
7197 case TypeCheckKind::kInterfaceCheck:
7198 call_kind = LocationSummary::kCallOnSlowPath;
7199 break;
7200 }
7201
Vladimir Markoca6fff82017-10-03 14:49:14 +01007202 LocationSummary* locations =
7203 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007204 if (baker_read_barrier_slow_path) {
7205 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7206 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007207 locations->SetInAt(0, Location::RequiresRegister());
7208 locations->SetInAt(1, Location::RequiresRegister());
7209 // The output does overlap inputs.
7210 // Note that TypeCheckSlowPathMIPS uses this register too.
7211 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007212 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007213}
7214
7215void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007216 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007217 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007218 Location obj_loc = locations->InAt(0);
7219 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007220 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007221 Location out_loc = locations->Out();
7222 Register out = out_loc.AsRegister<Register>();
7223 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7224 DCHECK_LE(num_temps, 1u);
7225 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007226 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7227 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7228 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7229 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007230 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007231 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007232
7233 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007234 // Avoid this check if we know `obj` is not null.
7235 if (instruction->MustDoNullCheck()) {
7236 __ Move(out, ZERO);
7237 __ Beqz(obj, &done);
7238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007239
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007240 switch (type_check_kind) {
7241 case TypeCheckKind::kExactCheck: {
7242 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007243 GenerateReferenceLoadTwoRegisters(instruction,
7244 out_loc,
7245 obj_loc,
7246 class_offset,
7247 maybe_temp_loc,
7248 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007249 // Classes must be equal for the instanceof to succeed.
7250 __ Xor(out, out, cls);
7251 __ Sltiu(out, out, 1);
7252 break;
7253 }
7254
7255 case TypeCheckKind::kAbstractClassCheck: {
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 // If the class is abstract, we eagerly fetch the super class of the
7264 // object to avoid doing a comparison we know will fail.
7265 MipsLabel loop;
7266 __ Bind(&loop);
7267 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007268 GenerateReferenceLoadOneRegister(instruction,
7269 out_loc,
7270 super_offset,
7271 maybe_temp_loc,
7272 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007273 // If `out` is null, we use it for the result, and jump to `done`.
7274 __ Beqz(out, &done);
7275 __ Bne(out, cls, &loop);
7276 __ LoadConst32(out, 1);
7277 break;
7278 }
7279
7280 case TypeCheckKind::kClassHierarchyCheck: {
7281 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007282 GenerateReferenceLoadTwoRegisters(instruction,
7283 out_loc,
7284 obj_loc,
7285 class_offset,
7286 maybe_temp_loc,
7287 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007288 // Walk over the class hierarchy to find a match.
7289 MipsLabel loop, success;
7290 __ Bind(&loop);
7291 __ Beq(out, cls, &success);
7292 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007293 GenerateReferenceLoadOneRegister(instruction,
7294 out_loc,
7295 super_offset,
7296 maybe_temp_loc,
7297 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007298 __ Bnez(out, &loop);
7299 // If `out` is null, we use it for the result, and jump to `done`.
7300 __ B(&done);
7301 __ Bind(&success);
7302 __ LoadConst32(out, 1);
7303 break;
7304 }
7305
7306 case TypeCheckKind::kArrayObjectCheck: {
7307 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007308 GenerateReferenceLoadTwoRegisters(instruction,
7309 out_loc,
7310 obj_loc,
7311 class_offset,
7312 maybe_temp_loc,
7313 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007314 // Do an exact check.
7315 MipsLabel success;
7316 __ Beq(out, cls, &success);
7317 // Otherwise, we need to check that the object's class is a non-primitive array.
7318 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007319 GenerateReferenceLoadOneRegister(instruction,
7320 out_loc,
7321 component_offset,
7322 maybe_temp_loc,
7323 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007324 // If `out` is null, we use it for the result, and jump to `done`.
7325 __ Beqz(out, &done);
7326 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7327 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7328 __ Sltiu(out, out, 1);
7329 __ B(&done);
7330 __ Bind(&success);
7331 __ LoadConst32(out, 1);
7332 break;
7333 }
7334
7335 case TypeCheckKind::kArrayCheck: {
7336 // No read barrier since the slow path will retry upon failure.
7337 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007338 GenerateReferenceLoadTwoRegisters(instruction,
7339 out_loc,
7340 obj_loc,
7341 class_offset,
7342 maybe_temp_loc,
7343 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007344 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007345 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7346 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007347 codegen_->AddSlowPath(slow_path);
7348 __ Bne(out, cls, slow_path->GetEntryLabel());
7349 __ LoadConst32(out, 1);
7350 break;
7351 }
7352
7353 case TypeCheckKind::kUnresolvedCheck:
7354 case TypeCheckKind::kInterfaceCheck: {
7355 // Note that we indeed only call on slow path, but we always go
7356 // into the slow path for the unresolved and interface check
7357 // cases.
7358 //
7359 // We cannot directly call the InstanceofNonTrivial runtime
7360 // entry point without resorting to a type checking slow path
7361 // here (i.e. by calling InvokeRuntime directly), as it would
7362 // require to assign fixed registers for the inputs of this
7363 // HInstanceOf instruction (following the runtime calling
7364 // convention), which might be cluttered by the potential first
7365 // read barrier emission at the beginning of this method.
7366 //
7367 // TODO: Introduce a new runtime entry point taking the object
7368 // to test (instead of its class) as argument, and let it deal
7369 // with the read barrier issues. This will let us refactor this
7370 // case of the `switch` code as it was previously (with a direct
7371 // call to the runtime not using a type checking slow path).
7372 // This should also be beneficial for the other cases above.
7373 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007374 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7375 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007376 codegen_->AddSlowPath(slow_path);
7377 __ B(slow_path->GetEntryLabel());
7378 break;
7379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007380 }
7381
7382 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007383
7384 if (slow_path != nullptr) {
7385 __ Bind(slow_path->GetExitLabel());
7386 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007387}
7388
7389void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007390 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007391 locations->SetOut(Location::ConstantLocation(constant));
7392}
7393
7394void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7395 // Will be generated at use site.
7396}
7397
7398void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007399 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007400 locations->SetOut(Location::ConstantLocation(constant));
7401}
7402
7403void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7404 // Will be generated at use site.
7405}
7406
7407void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7408 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7409 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7410}
7411
7412void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7413 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007414 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007415 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007416 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007417}
7418
7419void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7420 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7421 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007422 Location receiver = invoke->GetLocations()->InAt(0);
7423 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007424 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007425
7426 // Set the hidden argument.
7427 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7428 invoke->GetDexMethodIndex());
7429
7430 // temp = object->GetClass();
7431 if (receiver.IsStackSlot()) {
7432 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7433 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7434 } else {
7435 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7436 }
7437 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007438 // Instead of simply (possibly) unpoisoning `temp` here, we should
7439 // emit a read barrier for the previous class reference load.
7440 // However this is not required in practice, as this is an
7441 // intermediate/temporary reference and because the current
7442 // concurrent copying collector keeps the from-space memory
7443 // intact/accessible until the end of the marking phase (the
7444 // concurrent copying collector may not in the future).
7445 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007446 __ LoadFromOffset(kLoadWord, temp, temp,
7447 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7448 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007449 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007450 // temp = temp->GetImtEntryAt(method_offset);
7451 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7452 // T9 = temp->GetEntryPoint();
7453 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7454 // T9();
7455 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007456 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007457 DCHECK(!codegen_->IsLeafMethod());
7458 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7459}
7460
7461void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007462 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7463 if (intrinsic.TryDispatch(invoke)) {
7464 return;
7465 }
7466
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007467 HandleInvoke(invoke);
7468}
7469
7470void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007471 // Explicit clinit checks triggered by static invokes must have been pruned by
7472 // art::PrepareForRegisterAllocation.
7473 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007474
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007475 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007476 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7477 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007478
Chris Larsen701566a2015-10-27 15:29:13 -07007479 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7480 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007481 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7482 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7483 }
Chris Larsen701566a2015-10-27 15:29:13 -07007484 return;
7485 }
7486
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007487 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007488
7489 // Add the extra input register if either the dex cache array base register
7490 // or the PC-relative base register for accessing literals is needed.
7491 if (has_extra_input) {
7492 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007494}
7495
Orion Hodsonac141392017-01-13 11:53:47 +00007496void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7497 HandleInvoke(invoke);
7498}
7499
7500void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7501 codegen_->GenerateInvokePolymorphicCall(invoke);
7502}
7503
Chris Larsen701566a2015-10-27 15:29:13 -07007504static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007505 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007506 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7507 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007508 return true;
7509 }
7510 return false;
7511}
7512
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007513HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007514 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007515 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007516 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007517 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007518 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007519 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007520 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007521 case HLoadString::LoadKind::kJitTableAddress:
7522 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007523 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007524 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007525 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007526 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007527 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007528 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007529}
7530
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007531HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7532 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007533 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007534 case HLoadClass::LoadKind::kInvalid:
7535 LOG(FATAL) << "UNREACHABLE";
7536 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007537 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007538 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007539 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007540 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007541 case HLoadClass::LoadKind::kBssEntry:
7542 DCHECK(!Runtime::Current()->UseJitCompilation());
7543 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007544 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007545 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007546 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007547 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007548 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007549 break;
7550 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007551 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007552}
7553
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007554Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7555 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007556 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007557 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007558 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7559 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7560 if (!invoke->GetLocations()->Intrinsified()) {
7561 return location.AsRegister<Register>();
7562 }
7563 // For intrinsics we allow any location, so it may be on the stack.
7564 if (!location.IsRegister()) {
7565 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7566 return temp;
7567 }
7568 // For register locations, check if the register was saved. If so, get it from the stack.
7569 // Note: There is a chance that the register was saved but not overwritten, so we could
7570 // save one load. However, since this is just an intrinsic slow path we prefer this
7571 // simple and more robust approach rather that trying to determine if that's the case.
7572 SlowPathCode* slow_path = GetCurrentSlowPath();
7573 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7574 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7575 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7576 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7577 return temp;
7578 }
7579 return location.AsRegister<Register>();
7580}
7581
Vladimir Markodc151b22015-10-15 18:02:30 +01007582HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7583 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007584 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007585 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007586}
7587
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007588void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7589 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007590 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007591 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007592 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7593 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007594 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007595 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7596 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007597 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7598 : ZERO;
7599
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007600 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007601 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007602 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007603 uint32_t offset =
7604 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007605 __ LoadFromOffset(kLoadWord,
7606 temp.AsRegister<Register>(),
7607 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007608 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007610 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007611 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007612 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007613 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007614 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7615 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007616 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7617 PcRelativePatchInfo* info_low =
7618 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007619 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007620 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7621 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007622 break;
7623 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007624 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7625 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7626 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007627 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007628 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007629 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007630 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7631 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007632 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007633 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7634 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007635 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007636 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007637 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7638 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7639 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640 }
7641 }
7642
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007643 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007644 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007645 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007646 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007647 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7648 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007649 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007650 T9,
7651 callee_method.AsRegister<Register>(),
7652 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007653 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007654 // T9()
7655 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007656 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007657 break;
7658 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007659 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7660
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007661 DCHECK(!IsLeafMethod());
7662}
7663
7664void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007665 // Explicit clinit checks triggered by static invokes must have been pruned by
7666 // art::PrepareForRegisterAllocation.
7667 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668
7669 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7670 return;
7671 }
7672
7673 LocationSummary* locations = invoke->GetLocations();
7674 codegen_->GenerateStaticOrDirectCall(invoke,
7675 locations->HasTemps()
7676 ? locations->GetTemp(0)
7677 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007678}
7679
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007680void CodeGeneratorMIPS::GenerateVirtualCall(
7681 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007682 // Use the calling convention instead of the location of the receiver, as
7683 // intrinsics may have put the receiver in a different register. In the intrinsics
7684 // slow path, the arguments have been moved to the right place, so here we are
7685 // guaranteed that the receiver is the first register of the calling convention.
7686 InvokeDexCallingConvention calling_convention;
7687 Register receiver = calling_convention.GetRegisterAt(0);
7688
Chris Larsen3acee732015-11-18 13:31:08 -08007689 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007690 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7691 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7692 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007693 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007694
7695 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007696 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007697 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007698 // Instead of simply (possibly) unpoisoning `temp` here, we should
7699 // emit a read barrier for the previous class reference load.
7700 // However this is not required in practice, as this is an
7701 // intermediate/temporary reference and because the current
7702 // concurrent copying collector keeps the from-space memory
7703 // intact/accessible until the end of the marking phase (the
7704 // concurrent copying collector may not in the future).
7705 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007706 // temp = temp->GetMethodAt(method_offset);
7707 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7708 // T9 = temp->GetEntryPoint();
7709 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7710 // T9();
7711 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007712 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007713 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007714}
7715
7716void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7717 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7718 return;
7719 }
7720
7721 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007722 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007723}
7724
7725void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007726 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007727 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007728 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007729 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7730 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007731 return;
7732 }
Vladimir Marko41559982017-01-06 14:04:23 +00007733 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007734 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007735 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007736 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7737 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007738 ? LocationSummary::kCallOnSlowPath
7739 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007740 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007741 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7742 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7743 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007744 switch (load_kind) {
7745 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007746 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007747 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007748 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007749 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007750 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007751 break;
7752 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007753 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007754 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7755 codegen_->ClobberRA();
7756 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007757 break;
7758 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007759 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007761 locations->SetInAt(0, Location::RequiresRegister());
7762 break;
7763 default:
7764 break;
7765 }
7766 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007767 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7768 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7769 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007770 RegisterSet caller_saves = RegisterSet::Empty();
7771 InvokeRuntimeCallingConvention calling_convention;
7772 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7773 locations->SetCustomSlowPathCallerSaves(caller_saves);
7774 } else {
7775 // For non-Baker read barriers we have a temp-clobbering call.
7776 }
7777 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007778}
7779
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007780// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7781// move.
7782void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007783 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007784 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007785 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007786 return;
7787 }
Vladimir Marko41559982017-01-06 14:04:23 +00007788 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007789
Vladimir Marko41559982017-01-06 14:04:23 +00007790 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007791 Location out_loc = locations->Out();
7792 Register out = out_loc.AsRegister<Register>();
7793 Register base_or_current_method_reg;
7794 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007795 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007796 switch (load_kind) {
7797 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007798 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007799 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007800 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007801 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007802 base_or_current_method_reg =
7803 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007804 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007805 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007806 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007807 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7808 break;
7809 default:
7810 base_or_current_method_reg = ZERO;
7811 break;
7812 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007813
Alexey Frunze15958152017-02-09 19:08:30 -08007814 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7815 ? kWithoutReadBarrier
7816 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007817 bool generate_null_check = false;
7818 switch (load_kind) {
7819 case HLoadClass::LoadKind::kReferrersClass: {
7820 DCHECK(!cls->CanCallRuntime());
7821 DCHECK(!cls->MustGenerateClinitCheck());
7822 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7823 GenerateGcRootFieldLoad(cls,
7824 out_loc,
7825 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007826 ArtMethod::DeclaringClassOffset().Int32Value(),
7827 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007828 break;
7829 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007830 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007831 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007832 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007833 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007834 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007835 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7836 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007837 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7838 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007839 base_or_current_method_reg);
7840 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007841 break;
7842 }
7843 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007844 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007845 uint32_t address = dchecked_integral_cast<uint32_t>(
7846 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7847 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007848 if (isR6 || !has_irreducible_loops) {
7849 __ LoadLiteral(out,
7850 base_or_current_method_reg,
7851 codegen_->DeduplicateBootImageAddressLiteral(address));
7852 } else {
7853 __ LoadConst32(out, address);
7854 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007855 break;
7856 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007857 case HLoadClass::LoadKind::kBootImageClassTable: {
7858 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7859 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7860 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7861 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7862 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7863 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7864 out,
7865 base_or_current_method_reg);
7866 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7867 // Extract the reference from the slot data, i.e. clear the hash bits.
7868 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7869 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7870 if (masked_hash != 0) {
7871 __ Addiu(out, out, -masked_hash);
7872 }
7873 break;
7874 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007875 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007876 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7877 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007878 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7879 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007880 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007881 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007882 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007883 GenerateGcRootFieldLoad(cls,
7884 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007885 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007886 /* placeholder */ 0x5678,
7887 read_barrier_option,
7888 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007889 generate_null_check = true;
7890 break;
7891 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007892 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007893 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7894 cls->GetTypeIndex(),
7895 cls->GetClass());
7896 bool reordering = __ SetReorder(false);
7897 __ Bind(&info->high_label);
7898 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007899 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007900 GenerateGcRootFieldLoad(cls,
7901 out_loc,
7902 out,
7903 /* placeholder */ 0x5678,
7904 read_barrier_option,
7905 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007906 break;
7907 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007908 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007909 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007910 LOG(FATAL) << "UNREACHABLE";
7911 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007912 }
7913
7914 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7915 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007916 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007917 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007918 codegen_->AddSlowPath(slow_path);
7919 if (generate_null_check) {
7920 __ Beqz(out, slow_path->GetEntryLabel());
7921 }
7922 if (cls->MustGenerateClinitCheck()) {
7923 GenerateClassInitializationCheck(slow_path, out);
7924 } else {
7925 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007926 }
7927 }
7928}
7929
7930static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007931 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007932}
7933
7934void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7935 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007936 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007937 locations->SetOut(Location::RequiresRegister());
7938}
7939
7940void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7941 Register out = load->GetLocations()->Out().AsRegister<Register>();
7942 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7943}
7944
7945void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007946 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007947}
7948
7949void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7950 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7951}
7952
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007953void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007954 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007955 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007956 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007957 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007958 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007959 switch (load_kind) {
7960 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007961 case HLoadString::LoadKind::kBootImageAddress:
7962 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007963 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007964 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007965 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007966 break;
7967 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007968 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007969 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7970 codegen_->ClobberRA();
7971 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007972 break;
7973 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007974 FALLTHROUGH_INTENDED;
7975 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007976 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007977 locations->SetInAt(0, Location::RequiresRegister());
7978 break;
7979 default:
7980 break;
7981 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007982 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007983 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007984 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007985 } else {
7986 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007987 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7988 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7989 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007990 RegisterSet caller_saves = RegisterSet::Empty();
7991 InvokeRuntimeCallingConvention calling_convention;
7992 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7993 locations->SetCustomSlowPathCallerSaves(caller_saves);
7994 } else {
7995 // For non-Baker read barriers we have a temp-clobbering call.
7996 }
7997 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007998 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007999}
8000
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008001// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8002// move.
8003void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008004 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008005 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008006 Location out_loc = locations->Out();
8007 Register out = out_loc.AsRegister<Register>();
8008 Register base_or_current_method_reg;
8009 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008010 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008011 switch (load_kind) {
8012 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008013 case HLoadString::LoadKind::kBootImageAddress:
8014 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008015 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008016 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008017 base_or_current_method_reg =
8018 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008019 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008020 default:
8021 base_or_current_method_reg = ZERO;
8022 break;
8023 }
8024
8025 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008026 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008027 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008028 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008029 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008030 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8031 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008032 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8033 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008034 base_or_current_method_reg);
8035 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008036 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008037 }
8038 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008039 uint32_t address = dchecked_integral_cast<uint32_t>(
8040 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8041 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008042 if (isR6 || !has_irreducible_loops) {
8043 __ LoadLiteral(out,
8044 base_or_current_method_reg,
8045 codegen_->DeduplicateBootImageAddressLiteral(address));
8046 } else {
8047 __ LoadConst32(out, address);
8048 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008049 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008050 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008051 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008052 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008053 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008054 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008055 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8056 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008057 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8058 out,
8059 base_or_current_method_reg);
8060 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8061 return;
8062 }
8063 case HLoadString::LoadKind::kBssEntry: {
8064 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8065 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8066 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8067 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8068 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008069 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008070 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008071 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008072 GenerateGcRootFieldLoad(load,
8073 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008074 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008075 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008076 kCompilerReadBarrierOption,
8077 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008078 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008079 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008080 codegen_->AddSlowPath(slow_path);
8081 __ Beqz(out, slow_path->GetEntryLabel());
8082 __ Bind(slow_path->GetExitLabel());
8083 return;
8084 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008085 case HLoadString::LoadKind::kJitTableAddress: {
8086 CodeGeneratorMIPS::JitPatchInfo* info =
8087 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8088 load->GetStringIndex(),
8089 load->GetString());
8090 bool reordering = __ SetReorder(false);
8091 __ Bind(&info->high_label);
8092 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008093 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008094 GenerateGcRootFieldLoad(load,
8095 out_loc,
8096 out,
8097 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008098 kCompilerReadBarrierOption,
8099 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008100 return;
8101 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008102 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008103 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008104 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008105
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008106 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008107 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008108 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008109 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008110 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008111 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8112 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008113}
8114
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008115void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008116 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008117 locations->SetOut(Location::ConstantLocation(constant));
8118}
8119
8120void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8121 // Will be generated at use site.
8122}
8123
8124void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008125 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8126 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008127 InvokeRuntimeCallingConvention calling_convention;
8128 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8129}
8130
8131void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8132 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008133 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008134 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8135 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008136 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008137 }
8138 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8139}
8140
8141void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008143 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008144 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008145 case DataType::Type::kInt32:
8146 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008147 locations->SetInAt(0, Location::RequiresRegister());
8148 locations->SetInAt(1, Location::RequiresRegister());
8149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8150 break;
8151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008152 case DataType::Type::kFloat32:
8153 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008154 locations->SetInAt(0, Location::RequiresFpuRegister());
8155 locations->SetInAt(1, Location::RequiresFpuRegister());
8156 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8157 break;
8158
8159 default:
8160 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8161 }
8162}
8163
8164void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008165 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008166 LocationSummary* locations = instruction->GetLocations();
8167 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8168
8169 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008170 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008171 Register dst = locations->Out().AsRegister<Register>();
8172 Register lhs = locations->InAt(0).AsRegister<Register>();
8173 Register rhs = locations->InAt(1).AsRegister<Register>();
8174
8175 if (isR6) {
8176 __ MulR6(dst, lhs, rhs);
8177 } else {
8178 __ MulR2(dst, lhs, rhs);
8179 }
8180 break;
8181 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008182 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008183 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8184 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8185 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8186 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8187 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8188 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8189
8190 // Extra checks to protect caused by the existance of A1_A2.
8191 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8192 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8193 DCHECK_NE(dst_high, lhs_low);
8194 DCHECK_NE(dst_high, rhs_low);
8195
8196 // A_B * C_D
8197 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8198 // dst_lo: [ low(B*D) ]
8199 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8200
8201 if (isR6) {
8202 __ MulR6(TMP, lhs_high, rhs_low);
8203 __ MulR6(dst_high, lhs_low, rhs_high);
8204 __ Addu(dst_high, dst_high, TMP);
8205 __ MuhuR6(TMP, lhs_low, rhs_low);
8206 __ Addu(dst_high, dst_high, TMP);
8207 __ MulR6(dst_low, lhs_low, rhs_low);
8208 } else {
8209 __ MulR2(TMP, lhs_high, rhs_low);
8210 __ MulR2(dst_high, lhs_low, rhs_high);
8211 __ Addu(dst_high, dst_high, TMP);
8212 __ MultuR2(lhs_low, rhs_low);
8213 __ Mfhi(TMP);
8214 __ Addu(dst_high, dst_high, TMP);
8215 __ Mflo(dst_low);
8216 }
8217 break;
8218 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008219 case DataType::Type::kFloat32:
8220 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008221 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8222 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8223 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008224 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008225 __ MulS(dst, lhs, rhs);
8226 } else {
8227 __ MulD(dst, lhs, rhs);
8228 }
8229 break;
8230 }
8231 default:
8232 LOG(FATAL) << "Unexpected mul type " << type;
8233 }
8234}
8235
8236void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8237 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008238 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008239 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008240 case DataType::Type::kInt32:
8241 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008242 locations->SetInAt(0, Location::RequiresRegister());
8243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8244 break;
8245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008246 case DataType::Type::kFloat32:
8247 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008248 locations->SetInAt(0, Location::RequiresFpuRegister());
8249 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8250 break;
8251
8252 default:
8253 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8254 }
8255}
8256
8257void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008258 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008259 LocationSummary* locations = instruction->GetLocations();
8260
8261 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008262 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008263 Register dst = locations->Out().AsRegister<Register>();
8264 Register src = locations->InAt(0).AsRegister<Register>();
8265 __ Subu(dst, ZERO, src);
8266 break;
8267 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008268 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008269 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8270 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8271 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8272 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8273 __ Subu(dst_low, ZERO, src_low);
8274 __ Sltu(TMP, ZERO, dst_low);
8275 __ Subu(dst_high, ZERO, src_high);
8276 __ Subu(dst_high, dst_high, TMP);
8277 break;
8278 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008279 case DataType::Type::kFloat32:
8280 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008281 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8282 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008283 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008284 __ NegS(dst, src);
8285 } else {
8286 __ NegD(dst, src);
8287 }
8288 break;
8289 }
8290 default:
8291 LOG(FATAL) << "Unexpected neg type " << type;
8292 }
8293}
8294
8295void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008296 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8297 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008299 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008300 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8301 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008302}
8303
8304void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008305 // Note: if heap poisoning is enabled, the entry point takes care
8306 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008307 QuickEntrypointEnum entrypoint =
8308 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8309 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008310 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008311 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008312}
8313
8314void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008315 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8316 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008317 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008318 if (instruction->IsStringAlloc()) {
8319 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8320 } else {
8321 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008322 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008323 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008324}
8325
8326void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008327 // Note: if heap poisoning is enabled, the entry point takes care
8328 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008329 if (instruction->IsStringAlloc()) {
8330 // String is allocated through StringFactory. Call NewEmptyString entry point.
8331 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008332 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008333 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8334 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8335 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008336 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008337 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8338 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008339 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008340 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008341 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342}
8343
8344void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008345 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008346 locations->SetInAt(0, Location::RequiresRegister());
8347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8348}
8349
8350void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008351 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008352 LocationSummary* locations = instruction->GetLocations();
8353
8354 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008355 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008356 Register dst = locations->Out().AsRegister<Register>();
8357 Register src = locations->InAt(0).AsRegister<Register>();
8358 __ Nor(dst, src, ZERO);
8359 break;
8360 }
8361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008362 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008363 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8364 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8365 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8366 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8367 __ Nor(dst_high, src_high, ZERO);
8368 __ Nor(dst_low, src_low, ZERO);
8369 break;
8370 }
8371
8372 default:
8373 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8374 }
8375}
8376
8377void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008378 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008379 locations->SetInAt(0, Location::RequiresRegister());
8380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8381}
8382
8383void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8384 LocationSummary* locations = instruction->GetLocations();
8385 __ Xori(locations->Out().AsRegister<Register>(),
8386 locations->InAt(0).AsRegister<Register>(),
8387 1);
8388}
8389
8390void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008391 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8392 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008393}
8394
Calin Juravle2ae48182016-03-16 14:05:09 +00008395void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8396 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008397 return;
8398 }
8399 Location obj = instruction->GetLocations()->InAt(0);
8400
8401 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008402 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008403}
8404
Calin Juravle2ae48182016-03-16 14:05:09 +00008405void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008406 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008407 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008408
8409 Location obj = instruction->GetLocations()->InAt(0);
8410
8411 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8412}
8413
8414void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008415 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008416}
8417
8418void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8419 HandleBinaryOp(instruction);
8420}
8421
8422void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8423 HandleBinaryOp(instruction);
8424}
8425
8426void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8427 LOG(FATAL) << "Unreachable";
8428}
8429
8430void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008431 if (instruction->GetNext()->IsSuspendCheck() &&
8432 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8433 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8434 // The back edge will generate the suspend check.
8435 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8436 }
8437
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008438 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8439}
8440
8441void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008442 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008443 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8444 if (location.IsStackSlot()) {
8445 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8446 } else if (location.IsDoubleStackSlot()) {
8447 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8448 }
8449 locations->SetOut(location);
8450}
8451
8452void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8453 ATTRIBUTE_UNUSED) {
8454 // Nothing to do, the parameter is already at its location.
8455}
8456
8457void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8458 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008459 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8461}
8462
8463void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8464 ATTRIBUTE_UNUSED) {
8465 // Nothing to do, the method is already at its location.
8466}
8467
8468void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008469 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008470 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008471 locations->SetInAt(i, Location::Any());
8472 }
8473 locations->SetOut(Location::Any());
8474}
8475
8476void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8477 LOG(FATAL) << "Unreachable";
8478}
8479
8480void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008481 DataType::Type type = rem->GetResultType();
8482 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8483 ? LocationSummary::kNoCall
8484 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008485 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008486
8487 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008488 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008489 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008490 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8492 break;
8493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008495 InvokeRuntimeCallingConvention calling_convention;
8496 locations->SetInAt(0, Location::RegisterPairLocation(
8497 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8498 locations->SetInAt(1, Location::RegisterPairLocation(
8499 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8500 locations->SetOut(calling_convention.GetReturnLocation(type));
8501 break;
8502 }
8503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008504 case DataType::Type::kFloat32:
8505 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008506 InvokeRuntimeCallingConvention calling_convention;
8507 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8508 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8509 locations->SetOut(calling_convention.GetReturnLocation(type));
8510 break;
8511 }
8512
8513 default:
8514 LOG(FATAL) << "Unexpected rem type " << type;
8515 }
8516}
8517
8518void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008519 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520
8521 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008522 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008523 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008524 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008525 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008526 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008527 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8528 break;
8529 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008530 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008531 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008532 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008533 break;
8534 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008535 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008536 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008537 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538 break;
8539 }
8540 default:
8541 LOG(FATAL) << "Unexpected rem type " << type;
8542 }
8543}
8544
Igor Murashkind01745e2017-04-05 16:40:31 -07008545void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8546 constructor_fence->SetLocations(nullptr);
8547}
8548
8549void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8550 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8551 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8552}
8553
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008554void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8555 memory_barrier->SetLocations(nullptr);
8556}
8557
8558void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8559 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8560}
8561
8562void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008563 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008564 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008565 locations->SetInAt(0, MipsReturnLocation(return_type));
8566}
8567
8568void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8569 codegen_->GenerateFrameExit();
8570}
8571
8572void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8573 ret->SetLocations(nullptr);
8574}
8575
8576void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8577 codegen_->GenerateFrameExit();
8578}
8579
Alexey Frunze92d90602015-12-18 18:16:36 -08008580void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8581 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008582}
8583
Alexey Frunze92d90602015-12-18 18:16:36 -08008584void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8585 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008586}
8587
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008588void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8589 HandleShift(shl);
8590}
8591
8592void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8593 HandleShift(shl);
8594}
8595
8596void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8597 HandleShift(shr);
8598}
8599
8600void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8601 HandleShift(shr);
8602}
8603
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008604void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8605 HandleBinaryOp(instruction);
8606}
8607
8608void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8609 HandleBinaryOp(instruction);
8610}
8611
8612void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8613 HandleFieldGet(instruction, instruction->GetFieldInfo());
8614}
8615
8616void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8617 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8618}
8619
8620void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8621 HandleFieldSet(instruction, instruction->GetFieldInfo());
8622}
8623
8624void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008625 HandleFieldSet(instruction,
8626 instruction->GetFieldInfo(),
8627 instruction->GetDexPc(),
8628 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629}
8630
8631void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8632 HUnresolvedInstanceFieldGet* instruction) {
8633 FieldAccessCallingConventionMIPS calling_convention;
8634 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8635 instruction->GetFieldType(),
8636 calling_convention);
8637}
8638
8639void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8640 HUnresolvedInstanceFieldGet* instruction) {
8641 FieldAccessCallingConventionMIPS calling_convention;
8642 codegen_->GenerateUnresolvedFieldAccess(instruction,
8643 instruction->GetFieldType(),
8644 instruction->GetFieldIndex(),
8645 instruction->GetDexPc(),
8646 calling_convention);
8647}
8648
8649void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8650 HUnresolvedInstanceFieldSet* instruction) {
8651 FieldAccessCallingConventionMIPS calling_convention;
8652 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8653 instruction->GetFieldType(),
8654 calling_convention);
8655}
8656
8657void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8658 HUnresolvedInstanceFieldSet* instruction) {
8659 FieldAccessCallingConventionMIPS calling_convention;
8660 codegen_->GenerateUnresolvedFieldAccess(instruction,
8661 instruction->GetFieldType(),
8662 instruction->GetFieldIndex(),
8663 instruction->GetDexPc(),
8664 calling_convention);
8665}
8666
8667void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8668 HUnresolvedStaticFieldGet* instruction) {
8669 FieldAccessCallingConventionMIPS calling_convention;
8670 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8671 instruction->GetFieldType(),
8672 calling_convention);
8673}
8674
8675void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8676 HUnresolvedStaticFieldGet* instruction) {
8677 FieldAccessCallingConventionMIPS calling_convention;
8678 codegen_->GenerateUnresolvedFieldAccess(instruction,
8679 instruction->GetFieldType(),
8680 instruction->GetFieldIndex(),
8681 instruction->GetDexPc(),
8682 calling_convention);
8683}
8684
8685void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8686 HUnresolvedStaticFieldSet* instruction) {
8687 FieldAccessCallingConventionMIPS calling_convention;
8688 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8689 instruction->GetFieldType(),
8690 calling_convention);
8691}
8692
8693void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8694 HUnresolvedStaticFieldSet* instruction) {
8695 FieldAccessCallingConventionMIPS calling_convention;
8696 codegen_->GenerateUnresolvedFieldAccess(instruction,
8697 instruction->GetFieldType(),
8698 instruction->GetFieldIndex(),
8699 instruction->GetDexPc(),
8700 calling_convention);
8701}
8702
8703void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008704 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8705 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008706 // In suspend check slow path, usually there are no caller-save registers at all.
8707 // If SIMD instructions are present, however, we force spilling all live SIMD
8708 // registers in full width (since the runtime only saves/restores lower part).
8709 locations->SetCustomSlowPathCallerSaves(
8710 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008711}
8712
8713void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8714 HBasicBlock* block = instruction->GetBlock();
8715 if (block->GetLoopInformation() != nullptr) {
8716 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8717 // The back edge will generate the suspend check.
8718 return;
8719 }
8720 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8721 // The goto will generate the suspend check.
8722 return;
8723 }
8724 GenerateSuspendCheck(instruction, nullptr);
8725}
8726
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008727void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008728 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8729 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008730 InvokeRuntimeCallingConvention calling_convention;
8731 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8732}
8733
8734void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008735 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008736 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8737}
8738
8739void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008740 DataType::Type input_type = conversion->GetInputType();
8741 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008742 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8743 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008744 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008746 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8747 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008748 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8749 }
8750
8751 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008752 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008753 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8754 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008755 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008756 }
8757
Vladimir Markoca6fff82017-10-03 14:49:14 +01008758 LocationSummary* locations =
8759 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008760
8761 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008762 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008763 locations->SetInAt(0, Location::RequiresFpuRegister());
8764 } else {
8765 locations->SetInAt(0, Location::RequiresRegister());
8766 }
8767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008768 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008769 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8770 } else {
8771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8772 }
8773 } else {
8774 InvokeRuntimeCallingConvention calling_convention;
8775
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::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8778 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008779 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008780 locations->SetInAt(0, Location::RegisterPairLocation(
8781 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8782 }
8783
8784 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8785 }
8786}
8787
8788void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8789 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008790 DataType::Type result_type = conversion->GetResultType();
8791 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008792 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008793 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008795 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8796 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008798 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008799 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8800 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8801 Register src = locations->InAt(0).AsRegister<Register>();
8802
Alexey Frunzea871ef12016-06-27 15:20:11 -07008803 if (dst_low != src) {
8804 __ Move(dst_low, src);
8805 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008807 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008808 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008809 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008810 ? locations->InAt(0).AsRegisterPairLow<Register>()
8811 : locations->InAt(0).AsRegister<Register>();
8812
8813 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008814 case DataType::Type::kUint8:
8815 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008816 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008817 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008818 if (has_sign_extension) {
8819 __ Seb(dst, src);
8820 } else {
8821 __ Sll(dst, src, 24);
8822 __ Sra(dst, dst, 24);
8823 }
8824 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008825 case DataType::Type::kUint16:
8826 __ Andi(dst, src, 0xFFFF);
8827 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008828 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008829 if (has_sign_extension) {
8830 __ Seh(dst, src);
8831 } else {
8832 __ Sll(dst, src, 16);
8833 __ Sra(dst, dst, 16);
8834 }
8835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008836 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008837 if (dst != src) {
8838 __ Move(dst, src);
8839 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008840 break;
8841
8842 default:
8843 LOG(FATAL) << "Unexpected type conversion from " << input_type
8844 << " to " << result_type;
8845 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008846 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8847 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008848 if (isR6) {
8849 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8850 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8851 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8852 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8853 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8854 __ Mtc1(src_low, FTMP);
8855 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008856 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008857 __ Cvtsl(dst, FTMP);
8858 } else {
8859 __ Cvtdl(dst, FTMP);
8860 }
8861 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008862 QuickEntrypointEnum entrypoint =
8863 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008864 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008865 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008866 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8867 } else {
8868 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8869 }
8870 }
8871 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008872 Register src = locations->InAt(0).AsRegister<Register>();
8873 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8874 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008875 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008876 __ Cvtsw(dst, FTMP);
8877 } else {
8878 __ Cvtdw(dst, FTMP);
8879 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008880 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008881 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8882 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008883
8884 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8885 // value of the output type if the input is outside of the range after the truncation or
8886 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8887 // results. This matches the desired float/double-to-int/long conversion exactly.
8888 //
8889 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8890 // value when the input is either a NaN or is outside of the range of the output type
8891 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8892 // the same result.
8893 //
8894 // The code takes care of the different behaviors by first comparing the input to the
8895 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8896 // If the input is greater than or equal to the minimum, it procedes to the truncate
8897 // instruction, which will handle such an input the same way irrespective of NAN2008.
8898 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8899 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008900 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008901 if (isR6) {
8902 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8903 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8904 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8905 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8906 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008908 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008909 __ TruncLS(FTMP, src);
8910 } else {
8911 __ TruncLD(FTMP, src);
8912 }
8913 __ Mfc1(dst_low, FTMP);
8914 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008915 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008916 QuickEntrypointEnum entrypoint =
8917 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008918 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008919 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008920 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8921 } else {
8922 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8923 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008924 }
8925 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008926 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8927 Register dst = locations->Out().AsRegister<Register>();
8928 MipsLabel truncate;
8929 MipsLabel done;
8930
Lena Djokicf4e23a82017-05-09 15:43:45 +02008931 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008932 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008933 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8934 __ LoadConst32(TMP, min_val);
8935 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008936 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008937 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8938 __ LoadConst32(TMP, High32Bits(min_val));
8939 __ Mtc1(ZERO, FTMP);
8940 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008941 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008943 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008944 __ ColeS(0, FTMP, src);
8945 } else {
8946 __ ColeD(0, FTMP, src);
8947 }
8948 __ Bc1t(0, &truncate);
8949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008950 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008951 __ CeqS(0, src, src);
8952 } else {
8953 __ CeqD(0, src, src);
8954 }
8955 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8956 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008957
8958 __ B(&done);
8959
8960 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008961 }
8962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008963 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008964 __ TruncWS(FTMP, src);
8965 } else {
8966 __ TruncWD(FTMP, src);
8967 }
8968 __ Mfc1(dst, FTMP);
8969
Lena Djokicf4e23a82017-05-09 15:43:45 +02008970 if (!isR6) {
8971 __ Bind(&done);
8972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008973 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008974 } else if (DataType::IsFloatingPointType(result_type) &&
8975 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008976 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8977 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008978 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008979 __ Cvtsd(dst, src);
8980 } else {
8981 __ Cvtds(dst, src);
8982 }
8983 } else {
8984 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8985 << " to " << result_type;
8986 }
8987}
8988
8989void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8990 HandleShift(ushr);
8991}
8992
8993void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8994 HandleShift(ushr);
8995}
8996
8997void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8998 HandleBinaryOp(instruction);
8999}
9000
9001void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9002 HandleBinaryOp(instruction);
9003}
9004
9005void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9006 // Nothing to do, this should be removed during prepare for register allocator.
9007 LOG(FATAL) << "Unreachable";
9008}
9009
9010void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9011 // Nothing to do, this should be removed during prepare for register allocator.
9012 LOG(FATAL) << "Unreachable";
9013}
9014
9015void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009016 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009017}
9018
9019void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009020 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009021}
9022
9023void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009024 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009025}
9026
9027void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009028 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009029}
9030
9031void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009032 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009033}
9034
9035void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009036 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009037}
9038
9039void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009040 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009041}
9042
9043void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009044 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009045}
9046
9047void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009048 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009049}
9050
9051void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009052 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009053}
9054
9055void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009056 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009057}
9058
9059void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009060 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009061}
9062
9063void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009064 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009065}
9066
9067void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009068 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009069}
9070
9071void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009072 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009073}
9074
9075void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009076 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009077}
9078
9079void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009080 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009081}
9082
9083void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009084 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009085}
9086
9087void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009088 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009089}
9090
9091void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009092 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009093}
9094
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009095void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9096 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009097 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009098 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009099 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9100 uint32_t num_entries = switch_instr->GetNumEntries();
9101 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9102 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9103 // instruction to simulate PC-relative addressing when accessing the jump table.
9104 // NAL clobbers RA. Make sure RA is preserved.
9105 codegen_->ClobberRA();
9106 }
9107 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009108}
9109
Alexey Frunze96b66822016-09-10 02:32:44 -07009110void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9111 int32_t lower_bound,
9112 uint32_t num_entries,
9113 HBasicBlock* switch_block,
9114 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009115 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009116 Register temp_reg = TMP;
9117 __ Addiu32(temp_reg, value_reg, -lower_bound);
9118 // Jump to default if index is negative
9119 // Note: We don't check the case that index is positive while value < lower_bound, because in
9120 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9121 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9122
Alexey Frunze96b66822016-09-10 02:32:44 -07009123 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009124 // Jump to successors[0] if value == lower_bound.
9125 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9126 int32_t last_index = 0;
9127 for (; num_entries - last_index > 2; last_index += 2) {
9128 __ Addiu(temp_reg, temp_reg, -2);
9129 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9130 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9131 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9132 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9133 }
9134 if (num_entries - last_index == 2) {
9135 // The last missing case_value.
9136 __ Addiu(temp_reg, temp_reg, -1);
9137 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009138 }
9139
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009140 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009141 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009142 __ B(codegen_->GetLabelOf(default_block));
9143 }
9144}
9145
Alexey Frunze96b66822016-09-10 02:32:44 -07009146void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9147 Register constant_area,
9148 int32_t lower_bound,
9149 uint32_t num_entries,
9150 HBasicBlock* switch_block,
9151 HBasicBlock* default_block) {
9152 // Create a jump table.
9153 std::vector<MipsLabel*> labels(num_entries);
9154 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9155 for (uint32_t i = 0; i < num_entries; i++) {
9156 labels[i] = codegen_->GetLabelOf(successors[i]);
9157 }
9158 JumpTable* table = __ CreateJumpTable(std::move(labels));
9159
9160 // Is the value in range?
9161 __ Addiu32(TMP, value_reg, -lower_bound);
9162 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9163 __ Sltiu(AT, TMP, num_entries);
9164 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9165 } else {
9166 __ LoadConst32(AT, num_entries);
9167 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9168 }
9169
9170 // We are in the range of the table.
9171 // Load the target address from the jump table, indexing by the value.
9172 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009173 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009174 __ Lw(TMP, TMP, 0);
9175 // Compute the absolute target address by adding the table start address
9176 // (the table contains offsets to targets relative to its start).
9177 __ Addu(TMP, TMP, AT);
9178 // And jump.
9179 __ Jr(TMP);
9180 __ NopIfNoReordering();
9181}
9182
9183void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9184 int32_t lower_bound = switch_instr->GetStartValue();
9185 uint32_t num_entries = switch_instr->GetNumEntries();
9186 LocationSummary* locations = switch_instr->GetLocations();
9187 Register value_reg = locations->InAt(0).AsRegister<Register>();
9188 HBasicBlock* switch_block = switch_instr->GetBlock();
9189 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9190
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009191 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009192 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009193 //
9194 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9195 // to access the jump table and it is implemented by changing HPackedSwitch to
9196 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9197 // VisitMipsPackedSwitch()).
9198 //
9199 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9200 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9201 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009202 GenTableBasedPackedSwitch(value_reg,
9203 ZERO,
9204 lower_bound,
9205 num_entries,
9206 switch_block,
9207 default_block);
9208 } else {
9209 GenPackedSwitchWithCompares(value_reg,
9210 lower_bound,
9211 num_entries,
9212 switch_block,
9213 default_block);
9214 }
9215}
9216
9217void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9218 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009219 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009220 locations->SetInAt(0, Location::RequiresRegister());
9221 // Constant area pointer (HMipsComputeBaseMethodAddress).
9222 locations->SetInAt(1, Location::RequiresRegister());
9223}
9224
9225void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9226 int32_t lower_bound = switch_instr->GetStartValue();
9227 uint32_t num_entries = switch_instr->GetNumEntries();
9228 LocationSummary* locations = switch_instr->GetLocations();
9229 Register value_reg = locations->InAt(0).AsRegister<Register>();
9230 Register constant_area = locations->InAt(1).AsRegister<Register>();
9231 HBasicBlock* switch_block = switch_instr->GetBlock();
9232 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9233
9234 // This is an R2-only path. HPackedSwitch has been changed to
9235 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9236 // required to address the jump table relative to PC.
9237 GenTableBasedPackedSwitch(value_reg,
9238 constant_area,
9239 lower_bound,
9240 num_entries,
9241 switch_block,
9242 default_block);
9243}
9244
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009245void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9246 HMipsComputeBaseMethodAddress* insn) {
9247 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009248 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009249 locations->SetOut(Location::RequiresRegister());
9250}
9251
9252void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9253 HMipsComputeBaseMethodAddress* insn) {
9254 LocationSummary* locations = insn->GetLocations();
9255 Register reg = locations->Out().AsRegister<Register>();
9256
9257 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9258
9259 // Generate a dummy PC-relative call to obtain PC.
9260 __ Nal();
9261 // Grab the return address off RA.
9262 __ Move(reg, RA);
9263
9264 // Remember this offset (the obtained PC value) for later use with constant area.
9265 __ BindPcRelBaseLabel();
9266}
9267
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009268void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9269 // The trampoline uses the same calling convention as dex calling conventions,
9270 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9271 // the method_idx.
9272 HandleInvoke(invoke);
9273}
9274
9275void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9276 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9277}
9278
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009279void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9280 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009281 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009282 locations->SetInAt(0, Location::RequiresRegister());
9283 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009284}
9285
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009286void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9287 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009288 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009289 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009290 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009291 __ LoadFromOffset(kLoadWord,
9292 locations->Out().AsRegister<Register>(),
9293 locations->InAt(0).AsRegister<Register>(),
9294 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009295 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009296 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009297 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009298 __ LoadFromOffset(kLoadWord,
9299 locations->Out().AsRegister<Register>(),
9300 locations->InAt(0).AsRegister<Register>(),
9301 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009302 __ LoadFromOffset(kLoadWord,
9303 locations->Out().AsRegister<Register>(),
9304 locations->Out().AsRegister<Register>(),
9305 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009306 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009307}
9308
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009309void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9310 ATTRIBUTE_UNUSED) {
9311 LOG(FATAL) << "Unreachable";
9312}
9313
9314void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9315 ATTRIBUTE_UNUSED) {
9316 LOG(FATAL) << "Unreachable";
9317}
9318
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009319#undef __
9320#undef QUICK_ENTRY_POINT
9321
9322} // namespace mips
9323} // namespace art