blob: 637698f1f1b2bf05ad074af1e0fdba788dccdfd3 [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 Frunzedfc30af2018-01-24 16:25:10 -0800394 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800395 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 Markodc682aa2018-01-04 18:42:57 +00001918 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1919 const size_t status_byte_offset =
1920 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1921 constexpr uint32_t shifted_initialized_value =
1922 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1923
1924 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1925 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001926 __ Bltu(TMP, AT, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1928 __ Sync(0);
1929 __ Bind(slow_path->GetExitLabel());
1930}
1931
1932void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1933 __ Sync(0); // Only stype 0 is supported.
1934}
1935
1936void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1937 HBasicBlock* successor) {
1938 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001939 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1940
1941 if (slow_path == nullptr) {
1942 slow_path =
1943 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1944 instruction->SetSlowPath(slow_path);
1945 codegen_->AddSlowPath(slow_path);
1946 if (successor != nullptr) {
1947 DCHECK(successor->IsLoopHeader());
1948 }
1949 } else {
1950 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1951 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952
1953 __ LoadFromOffset(kLoadUnsignedHalfword,
1954 TMP,
1955 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001956 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 if (successor == nullptr) {
1958 __ Bnez(TMP, slow_path->GetEntryLabel());
1959 __ Bind(slow_path->GetReturnLabel());
1960 } else {
1961 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1962 __ B(slow_path->GetEntryLabel());
1963 // slow_path will return to GetLabelOf(successor).
1964 }
1965}
1966
1967InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1968 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001969 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001970 assembler_(codegen->GetAssembler()),
1971 codegen_(codegen) {}
1972
1973void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1974 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001975 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001977 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001978 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001979 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 locations->SetInAt(0, Location::RequiresRegister());
1981 HInstruction* right = instruction->InputAt(1);
1982 bool can_use_imm = false;
1983 if (right->IsConstant()) {
1984 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1985 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1986 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001988 DCHECK(instruction->IsSub() || instruction->IsAdd());
1989 if (instruction->IsSub()) {
1990 imm = -imm;
1991 }
1992 if (isR6) {
1993 bool single_use = right->GetUses().HasExactlyOneElement();
1994 int16_t imm_high = High16Bits(imm);
1995 int16_t imm_low = Low16Bits(imm);
1996 if (imm_low < 0) {
1997 imm_high += 1;
1998 }
1999 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2000 } else {
2001 can_use_imm = IsInt<16>(imm);
2002 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003 }
2004 }
2005 if (can_use_imm)
2006 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2007 else
2008 locations->SetInAt(1, Location::RequiresRegister());
2009 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2010 break;
2011 }
2012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002015 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 break;
2018 }
2019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 case DataType::Type::kFloat32:
2021 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 DCHECK(instruction->IsAdd() || instruction->IsSub());
2023 locations->SetInAt(0, Location::RequiresFpuRegister());
2024 locations->SetInAt(1, Location::RequiresFpuRegister());
2025 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2026 break;
2027
2028 default:
2029 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2030 }
2031}
2032
2033void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002036 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002037
2038 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 Register dst = locations->Out().AsRegister<Register>();
2041 Register lhs = locations->InAt(0).AsRegister<Register>();
2042 Location rhs_location = locations->InAt(1);
2043
2044 Register rhs_reg = ZERO;
2045 int32_t rhs_imm = 0;
2046 bool use_imm = rhs_location.IsConstant();
2047 if (use_imm) {
2048 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2049 } else {
2050 rhs_reg = rhs_location.AsRegister<Register>();
2051 }
2052
2053 if (instruction->IsAnd()) {
2054 if (use_imm)
2055 __ Andi(dst, lhs, rhs_imm);
2056 else
2057 __ And(dst, lhs, rhs_reg);
2058 } else if (instruction->IsOr()) {
2059 if (use_imm)
2060 __ Ori(dst, lhs, rhs_imm);
2061 else
2062 __ Or(dst, lhs, rhs_reg);
2063 } else if (instruction->IsXor()) {
2064 if (use_imm)
2065 __ Xori(dst, lhs, rhs_imm);
2066 else
2067 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002069 DCHECK(instruction->IsAdd() || instruction->IsSub());
2070 if (use_imm) {
2071 if (instruction->IsSub()) {
2072 rhs_imm = -rhs_imm;
2073 }
2074 if (IsInt<16>(rhs_imm)) {
2075 __ Addiu(dst, lhs, rhs_imm);
2076 } else {
2077 DCHECK(isR6);
2078 int16_t rhs_imm_high = High16Bits(rhs_imm);
2079 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2080 if (rhs_imm_low < 0) {
2081 rhs_imm_high += 1;
2082 }
2083 __ Aui(dst, lhs, rhs_imm_high);
2084 if (rhs_imm_low != 0) {
2085 __ Addiu(dst, dst, rhs_imm_low);
2086 }
2087 }
2088 } else if (instruction->IsAdd()) {
2089 __ Addu(dst, lhs, rhs_reg);
2090 } else {
2091 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002093 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002094 }
2095 break;
2096 }
2097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2100 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2101 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2102 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002103 Location rhs_location = locations->InAt(1);
2104 bool use_imm = rhs_location.IsConstant();
2105 if (!use_imm) {
2106 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2107 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2108 if (instruction->IsAnd()) {
2109 __ And(dst_low, lhs_low, rhs_low);
2110 __ And(dst_high, lhs_high, rhs_high);
2111 } else if (instruction->IsOr()) {
2112 __ Or(dst_low, lhs_low, rhs_low);
2113 __ Or(dst_high, lhs_high, rhs_high);
2114 } else if (instruction->IsXor()) {
2115 __ Xor(dst_low, lhs_low, rhs_low);
2116 __ Xor(dst_high, lhs_high, rhs_high);
2117 } else if (instruction->IsAdd()) {
2118 if (lhs_low == rhs_low) {
2119 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2120 __ Slt(TMP, lhs_low, ZERO);
2121 __ Addu(dst_low, lhs_low, rhs_low);
2122 } else {
2123 __ Addu(dst_low, lhs_low, rhs_low);
2124 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2125 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2126 }
2127 __ Addu(dst_high, lhs_high, rhs_high);
2128 __ Addu(dst_high, dst_high, TMP);
2129 } else {
2130 DCHECK(instruction->IsSub());
2131 __ Sltu(TMP, lhs_low, rhs_low);
2132 __ Subu(dst_low, lhs_low, rhs_low);
2133 __ Subu(dst_high, lhs_high, rhs_high);
2134 __ Subu(dst_high, dst_high, TMP);
2135 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002137 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2138 if (instruction->IsOr()) {
2139 uint32_t low = Low32Bits(value);
2140 uint32_t high = High32Bits(value);
2141 if (IsUint<16>(low)) {
2142 if (dst_low != lhs_low || low != 0) {
2143 __ Ori(dst_low, lhs_low, low);
2144 }
2145 } else {
2146 __ LoadConst32(TMP, low);
2147 __ Or(dst_low, lhs_low, TMP);
2148 }
2149 if (IsUint<16>(high)) {
2150 if (dst_high != lhs_high || high != 0) {
2151 __ Ori(dst_high, lhs_high, high);
2152 }
2153 } else {
2154 if (high != low) {
2155 __ LoadConst32(TMP, high);
2156 }
2157 __ Or(dst_high, lhs_high, TMP);
2158 }
2159 } else if (instruction->IsXor()) {
2160 uint32_t low = Low32Bits(value);
2161 uint32_t high = High32Bits(value);
2162 if (IsUint<16>(low)) {
2163 if (dst_low != lhs_low || low != 0) {
2164 __ Xori(dst_low, lhs_low, low);
2165 }
2166 } else {
2167 __ LoadConst32(TMP, low);
2168 __ Xor(dst_low, lhs_low, TMP);
2169 }
2170 if (IsUint<16>(high)) {
2171 if (dst_high != lhs_high || high != 0) {
2172 __ Xori(dst_high, lhs_high, high);
2173 }
2174 } else {
2175 if (high != low) {
2176 __ LoadConst32(TMP, high);
2177 }
2178 __ Xor(dst_high, lhs_high, TMP);
2179 }
2180 } else if (instruction->IsAnd()) {
2181 uint32_t low = Low32Bits(value);
2182 uint32_t high = High32Bits(value);
2183 if (IsUint<16>(low)) {
2184 __ Andi(dst_low, lhs_low, low);
2185 } else if (low != 0xFFFFFFFF) {
2186 __ LoadConst32(TMP, low);
2187 __ And(dst_low, lhs_low, TMP);
2188 } else if (dst_low != lhs_low) {
2189 __ Move(dst_low, lhs_low);
2190 }
2191 if (IsUint<16>(high)) {
2192 __ Andi(dst_high, lhs_high, high);
2193 } else if (high != 0xFFFFFFFF) {
2194 if (high != low) {
2195 __ LoadConst32(TMP, high);
2196 }
2197 __ And(dst_high, lhs_high, TMP);
2198 } else if (dst_high != lhs_high) {
2199 __ Move(dst_high, lhs_high);
2200 }
2201 } else {
2202 if (instruction->IsSub()) {
2203 value = -value;
2204 } else {
2205 DCHECK(instruction->IsAdd());
2206 }
2207 int32_t low = Low32Bits(value);
2208 int32_t high = High32Bits(value);
2209 if (IsInt<16>(low)) {
2210 if (dst_low != lhs_low || low != 0) {
2211 __ Addiu(dst_low, lhs_low, low);
2212 }
2213 if (low != 0) {
2214 __ Sltiu(AT, dst_low, low);
2215 }
2216 } else {
2217 __ LoadConst32(TMP, low);
2218 __ Addu(dst_low, lhs_low, TMP);
2219 __ Sltu(AT, dst_low, TMP);
2220 }
2221 if (IsInt<16>(high)) {
2222 if (dst_high != lhs_high || high != 0) {
2223 __ Addiu(dst_high, lhs_high, high);
2224 }
2225 } else {
2226 if (high != low) {
2227 __ LoadConst32(TMP, high);
2228 }
2229 __ Addu(dst_high, lhs_high, TMP);
2230 }
2231 if (low != 0) {
2232 __ Addu(dst_high, dst_high, AT);
2233 }
2234 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002235 }
2236 break;
2237 }
2238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 case DataType::Type::kFloat32:
2240 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2242 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2243 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2244 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002246 __ AddS(dst, lhs, rhs);
2247 } else {
2248 __ AddD(dst, lhs, rhs);
2249 }
2250 } else {
2251 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002253 __ SubS(dst, lhs, rhs);
2254 } else {
2255 __ SubD(dst, lhs, rhs);
2256 }
2257 }
2258 break;
2259 }
2260
2261 default:
2262 LOG(FATAL) << "Unexpected binary operation type " << type;
2263 }
2264}
2265
2266void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002267 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268
Vladimir Markoca6fff82017-10-03 14:49:14 +01002269 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002273 locations->SetInAt(0, Location::RequiresRegister());
2274 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2276 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278 locations->SetInAt(0, Location::RequiresRegister());
2279 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2280 locations->SetOut(Location::RequiresRegister());
2281 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002282 default:
2283 LOG(FATAL) << "Unexpected shift type " << type;
2284 }
2285}
2286
2287static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2288
2289void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002290 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002291 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002293
2294 Location rhs_location = locations->InAt(1);
2295 bool use_imm = rhs_location.IsConstant();
2296 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2297 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002298 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002300 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002301 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2302 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303
2304 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002305 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306 Register dst = locations->Out().AsRegister<Register>();
2307 Register lhs = locations->InAt(0).AsRegister<Register>();
2308 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002309 if (shift_value == 0) {
2310 if (dst != lhs) {
2311 __ Move(dst, lhs);
2312 }
2313 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 __ Sll(dst, lhs, shift_value);
2315 } else if (instr->IsShr()) {
2316 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002317 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002318 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002319 } else {
2320 if (has_ins_rotr) {
2321 __ Rotr(dst, lhs, shift_value);
2322 } else {
2323 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2324 __ Srl(dst, lhs, shift_value);
2325 __ Or(dst, dst, TMP);
2326 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 }
2328 } else {
2329 if (instr->IsShl()) {
2330 __ Sllv(dst, lhs, rhs_reg);
2331 } else if (instr->IsShr()) {
2332 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002333 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002335 } else {
2336 if (has_ins_rotr) {
2337 __ Rotrv(dst, lhs, rhs_reg);
2338 } else {
2339 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002340 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2341 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2342 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2343 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2344 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 __ Sllv(TMP, lhs, TMP);
2346 __ Srlv(dst, lhs, rhs_reg);
2347 __ Or(dst, dst, TMP);
2348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002349 }
2350 }
2351 break;
2352 }
2353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2356 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2357 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2358 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2359 if (use_imm) {
2360 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002361 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002363 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002364 if (instr->IsShl()) {
2365 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2366 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2367 __ Sll(dst_low, lhs_low, shift_value);
2368 } else if (instr->IsShr()) {
2369 __ Srl(dst_low, lhs_low, shift_value);
2370 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2371 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002372 } else if (instr->IsUShr()) {
2373 __ Srl(dst_low, lhs_low, shift_value);
2374 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2375 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002376 } else {
2377 __ Srl(dst_low, lhs_low, shift_value);
2378 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2379 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002380 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002381 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002383 if (instr->IsShl()) {
2384 __ Sll(dst_low, lhs_low, shift_value);
2385 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2386 __ Sll(dst_high, lhs_high, shift_value);
2387 __ Or(dst_high, dst_high, TMP);
2388 } else if (instr->IsShr()) {
2389 __ Sra(dst_high, lhs_high, shift_value);
2390 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2391 __ Srl(dst_low, lhs_low, shift_value);
2392 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002393 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002394 __ Srl(dst_high, lhs_high, shift_value);
2395 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2396 __ Srl(dst_low, lhs_low, shift_value);
2397 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002398 } else {
2399 __ Srl(TMP, lhs_low, shift_value);
2400 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2401 __ Or(dst_low, dst_low, TMP);
2402 __ Srl(TMP, lhs_high, shift_value);
2403 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2404 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002405 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002406 }
2407 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002408 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002409 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002410 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 __ Move(dst_low, ZERO);
2412 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002413 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002414 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002415 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002416 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002417 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002418 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002419 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002420 // 64-bit rotation by 32 is just a swap.
2421 __ Move(dst_low, lhs_high);
2422 __ Move(dst_high, lhs_low);
2423 } else {
2424 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002425 __ Srl(dst_low, lhs_high, shift_value_high);
2426 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2427 __ Srl(dst_high, lhs_low, shift_value_high);
2428 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002430 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2431 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002432 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002433 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2434 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002435 __ Or(dst_high, dst_high, TMP);
2436 }
2437 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 }
2439 }
2440 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002441 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 MipsLabel done;
2443 if (instr->IsShl()) {
2444 __ Sllv(dst_low, lhs_low, rhs_reg);
2445 __ Nor(AT, ZERO, rhs_reg);
2446 __ Srl(TMP, lhs_low, 1);
2447 __ Srlv(TMP, TMP, AT);
2448 __ Sllv(dst_high, lhs_high, rhs_reg);
2449 __ Or(dst_high, dst_high, TMP);
2450 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002451 if (isR6) {
2452 __ Beqzc(TMP, &done, /* is_bare */ true);
2453 __ Move(dst_high, dst_low);
2454 __ Move(dst_low, ZERO);
2455 } else {
2456 __ Movn(dst_high, dst_low, TMP);
2457 __ Movn(dst_low, ZERO, TMP);
2458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002459 } else if (instr->IsShr()) {
2460 __ Srav(dst_high, lhs_high, rhs_reg);
2461 __ Nor(AT, ZERO, rhs_reg);
2462 __ Sll(TMP, lhs_high, 1);
2463 __ Sllv(TMP, TMP, AT);
2464 __ Srlv(dst_low, lhs_low, rhs_reg);
2465 __ Or(dst_low, dst_low, TMP);
2466 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002467 if (isR6) {
2468 __ Beqzc(TMP, &done, /* is_bare */ true);
2469 __ Move(dst_low, dst_high);
2470 __ Sra(dst_high, dst_high, 31);
2471 } else {
2472 __ Sra(AT, dst_high, 31);
2473 __ Movn(dst_low, dst_high, TMP);
2474 __ Movn(dst_high, AT, TMP);
2475 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002476 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002477 __ Srlv(dst_high, lhs_high, rhs_reg);
2478 __ Nor(AT, ZERO, rhs_reg);
2479 __ Sll(TMP, lhs_high, 1);
2480 __ Sllv(TMP, TMP, AT);
2481 __ Srlv(dst_low, lhs_low, rhs_reg);
2482 __ Or(dst_low, dst_low, TMP);
2483 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002484 if (isR6) {
2485 __ Beqzc(TMP, &done, /* is_bare */ true);
2486 __ Move(dst_low, dst_high);
2487 __ Move(dst_high, ZERO);
2488 } else {
2489 __ Movn(dst_low, dst_high, TMP);
2490 __ Movn(dst_high, ZERO, TMP);
2491 }
2492 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002493 __ Nor(AT, ZERO, rhs_reg);
2494 __ Srlv(TMP, lhs_low, rhs_reg);
2495 __ Sll(dst_low, lhs_high, 1);
2496 __ Sllv(dst_low, dst_low, AT);
2497 __ Or(dst_low, dst_low, TMP);
2498 __ Srlv(TMP, lhs_high, rhs_reg);
2499 __ Sll(dst_high, lhs_low, 1);
2500 __ Sllv(dst_high, dst_high, AT);
2501 __ Or(dst_high, dst_high, TMP);
2502 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002503 if (isR6) {
2504 __ Beqzc(TMP, &done, /* is_bare */ true);
2505 __ Move(TMP, dst_high);
2506 __ Move(dst_high, dst_low);
2507 __ Move(dst_low, TMP);
2508 } else {
2509 __ Movn(AT, dst_high, TMP);
2510 __ Movn(dst_high, dst_low, TMP);
2511 __ Movn(dst_low, AT, TMP);
2512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 }
2514 __ Bind(&done);
2515 }
2516 break;
2517 }
2518
2519 default:
2520 LOG(FATAL) << "Unexpected shift operation type " << type;
2521 }
2522}
2523
2524void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2525 HandleBinaryOp(instruction);
2526}
2527
2528void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2529 HandleBinaryOp(instruction);
2530}
2531
2532void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2533 HandleBinaryOp(instruction);
2534}
2535
2536void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2537 HandleBinaryOp(instruction);
2538}
2539
2540void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002542 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002543 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002545 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2546 object_array_get_with_read_barrier
2547 ? LocationSummary::kCallOnSlowPath
2548 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002549 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2550 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2551 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002552 locations->SetInAt(0, Location::RequiresRegister());
2553 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2556 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002557 // The output overlaps in the case of an object array get with
2558 // read barriers enabled: we do not want the move to overwrite the
2559 // array's location, as we need it to emit the read barrier.
2560 locations->SetOut(Location::RequiresRegister(),
2561 object_array_get_with_read_barrier
2562 ? Location::kOutputOverlap
2563 : Location::kNoOutputOverlap);
2564 }
2565 // We need a temporary register for the read barrier marking slow
2566 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2567 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002568 bool temp_needed = instruction->GetIndex()->IsConstant()
2569 ? !kBakerReadBarrierThunksEnableForFields
2570 : !kBakerReadBarrierThunksEnableForArrays;
2571 if (temp_needed) {
2572 locations->AddTemp(Location::RequiresRegister());
2573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 }
2575}
2576
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002577static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2578 auto null_checker = [codegen, instruction]() {
2579 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002580 };
2581 return null_checker;
2582}
2583
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2585 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002586 Location obj_loc = locations->InAt(0);
2587 Register obj = obj_loc.AsRegister<Register>();
2588 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002590 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002591 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002594 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2595 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002597 case DataType::Type::kBool:
2598 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002599 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 if (index.IsConstant()) {
2601 size_t offset =
2602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002603 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 } else {
2605 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002606 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 }
2608 break;
2609 }
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002612 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 if (index.IsConstant()) {
2614 size_t offset =
2615 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002616 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 } else {
2618 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002619 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 }
2621 break;
2622 }
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002625 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002626 if (maybe_compressed_char_at) {
2627 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2628 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2629 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2630 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2631 "Expecting 0=compressed, 1=uncompressed");
2632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002634 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2635 if (maybe_compressed_char_at) {
2636 MipsLabel uncompressed_load, done;
2637 __ Bnez(TMP, &uncompressed_load);
2638 __ LoadFromOffset(kLoadUnsignedByte,
2639 out,
2640 obj,
2641 data_offset + (const_index << TIMES_1));
2642 __ B(&done);
2643 __ Bind(&uncompressed_load);
2644 __ LoadFromOffset(kLoadUnsignedHalfword,
2645 out,
2646 obj,
2647 data_offset + (const_index << TIMES_2));
2648 __ Bind(&done);
2649 } else {
2650 __ LoadFromOffset(kLoadUnsignedHalfword,
2651 out,
2652 obj,
2653 data_offset + (const_index << TIMES_2),
2654 null_checker);
2655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002657 Register index_reg = index.AsRegister<Register>();
2658 if (maybe_compressed_char_at) {
2659 MipsLabel uncompressed_load, done;
2660 __ Bnez(TMP, &uncompressed_load);
2661 __ Addu(TMP, obj, index_reg);
2662 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2663 __ B(&done);
2664 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002665 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002666 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2667 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002668 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2669 __ Addu(TMP, index_reg, obj);
2670 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002671 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002672 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002673 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2674 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 }
2676 break;
2677 }
2678
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002679 case DataType::Type::kInt16: {
2680 Register out = out_loc.AsRegister<Register>();
2681 if (index.IsConstant()) {
2682 size_t offset =
2683 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2684 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002685 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2686 __ Addu(TMP, index.AsRegister<Register>(), obj);
2687 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002688 } else {
2689 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2690 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2691 }
2692 break;
2693 }
2694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002697 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698 if (index.IsConstant()) {
2699 size_t offset =
2700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002701 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002702 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2703 __ Addu(TMP, index.AsRegister<Register>(), obj);
2704 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002706 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002707 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002708 }
2709 break;
2710 }
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002713 static_assert(
2714 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2715 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2716 // /* HeapReference<Object> */ out =
2717 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2718 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002719 bool temp_needed = index.IsConstant()
2720 ? !kBakerReadBarrierThunksEnableForFields
2721 : !kBakerReadBarrierThunksEnableForArrays;
2722 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002723 // Note that a potential implicit null check is handled in this
2724 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002725 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2726 if (index.IsConstant()) {
2727 // Array load with a constant index can be treated as a field load.
2728 size_t offset =
2729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2730 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2731 out_loc,
2732 obj,
2733 offset,
2734 temp,
2735 /* needs_null_check */ false);
2736 } else {
2737 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2738 out_loc,
2739 obj,
2740 data_offset,
2741 index,
2742 temp,
2743 /* needs_null_check */ false);
2744 }
Alexey Frunze15958152017-02-09 19:08:30 -08002745 } else {
2746 Register out = out_loc.AsRegister<Register>();
2747 if (index.IsConstant()) {
2748 size_t offset =
2749 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2750 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2751 // If read barriers are enabled, emit read barriers other than
2752 // Baker's using a slow path (and also unpoison the loaded
2753 // reference, if heap poisoning is enabled).
2754 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2755 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002756 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002757 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2758 // If read barriers are enabled, emit read barriers other than
2759 // Baker's using a slow path (and also unpoison the loaded
2760 // reference, if heap poisoning is enabled).
2761 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2762 out_loc,
2763 out_loc,
2764 obj_loc,
2765 data_offset,
2766 index);
2767 }
2768 }
2769 break;
2770 }
2771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002772 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002773 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002774 if (index.IsConstant()) {
2775 size_t offset =
2776 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002777 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002778 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2779 __ Addu(TMP, index.AsRegister<Register>(), obj);
2780 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002782 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002783 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002784 }
2785 break;
2786 }
2787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002789 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002790 if (index.IsConstant()) {
2791 size_t offset =
2792 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002793 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002794 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2795 __ Addu(TMP, index.AsRegister<Register>(), obj);
2796 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002798 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002799 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 }
2801 break;
2802 }
2803
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002805 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002806 if (index.IsConstant()) {
2807 size_t offset =
2808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002809 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002810 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2811 __ Addu(TMP, index.AsRegister<Register>(), obj);
2812 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002814 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002815 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002816 }
2817 break;
2818 }
2819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002821 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2822 UNREACHABLE();
2823 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824}
2825
2826void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002827 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828 locations->SetInAt(0, Location::RequiresRegister());
2829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2830}
2831
2832void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2833 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002834 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002835 Register obj = locations->InAt(0).AsRegister<Register>();
2836 Register out = locations->Out().AsRegister<Register>();
2837 __ LoadFromOffset(kLoadWord, out, obj, offset);
2838 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002839 // Mask out compression flag from String's array length.
2840 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2841 __ Srl(out, out, 1u);
2842 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002843}
2844
Alexey Frunzef58b2482016-09-02 22:14:06 -07002845Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2846 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2847 ? Location::ConstantLocation(instruction->AsConstant())
2848 : Location::RequiresRegister();
2849}
2850
2851Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2852 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2853 // We can store a non-zero float or double constant without first loading it into the FPU,
2854 // but we should only prefer this if the constant has a single use.
2855 if (instruction->IsConstant() &&
2856 (instruction->AsConstant()->IsZeroBitPattern() ||
2857 instruction->GetUses().HasExactlyOneElement())) {
2858 return Location::ConstantLocation(instruction->AsConstant());
2859 // Otherwise fall through and require an FPU register for the constant.
2860 }
2861 return Location::RequiresFpuRegister();
2862}
2863
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002866
2867 bool needs_write_barrier =
2868 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2869 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2870
Vladimir Markoca6fff82017-10-03 14:49:14 +01002871 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002872 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002873 may_need_runtime_call_for_type_check ?
2874 LocationSummary::kCallOnSlowPath :
2875 LocationSummary::kNoCall);
2876
2877 locations->SetInAt(0, Location::RequiresRegister());
2878 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002880 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002882 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2883 }
2884 if (needs_write_barrier) {
2885 // Temporary register for the write barrier.
2886 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002887 }
2888}
2889
2890void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2891 LocationSummary* locations = instruction->GetLocations();
2892 Register obj = locations->InAt(0).AsRegister<Register>();
2893 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002894 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002896 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897 bool needs_write_barrier =
2898 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002899 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002900 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002901
2902 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002904 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002907 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002908 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002910 __ Addu(base_reg, obj, index.AsRegister<Register>());
2911 }
2912 if (value_location.IsConstant()) {
2913 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2914 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2915 } else {
2916 Register value = value_location.AsRegister<Register>();
2917 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 }
2919 break;
2920 }
2921
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002922 case DataType::Type::kUint16:
2923 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002925 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002926 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002927 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2928 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002930 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002931 }
2932 if (value_location.IsConstant()) {
2933 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2934 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2935 } else {
2936 Register value = value_location.AsRegister<Register>();
2937 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 }
2939 break;
2940 }
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2944 if (index.IsConstant()) {
2945 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002946 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2947 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002948 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002949 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002950 }
2951 if (value_location.IsConstant()) {
2952 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2953 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2954 } else {
2955 Register value = value_location.AsRegister<Register>();
2956 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2957 }
2958 break;
2959 }
2960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002962 if (value_location.IsConstant()) {
2963 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002966 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002967 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002968 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002969 }
Alexey Frunze15958152017-02-09 19:08:30 -08002970 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2971 DCHECK_EQ(value, 0);
2972 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2973 DCHECK(!needs_write_barrier);
2974 DCHECK(!may_need_runtime_call_for_type_check);
2975 break;
2976 }
2977
2978 DCHECK(needs_write_barrier);
2979 Register value = value_location.AsRegister<Register>();
2980 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2981 Register temp2 = TMP; // Doesn't need to survive slow path.
2982 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2983 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2984 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2985 MipsLabel done;
2986 SlowPathCodeMIPS* slow_path = nullptr;
2987
2988 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002989 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002990 codegen_->AddSlowPath(slow_path);
2991 if (instruction->GetValueCanBeNull()) {
2992 MipsLabel non_zero;
2993 __ Bnez(value, &non_zero);
2994 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2995 if (index.IsConstant()) {
2996 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002997 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2998 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002999 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003000 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003001 }
Alexey Frunze15958152017-02-09 19:08:30 -08003002 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3003 __ B(&done);
3004 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003005 }
Alexey Frunze15958152017-02-09 19:08:30 -08003006
3007 // Note that when read barriers are enabled, the type checks
3008 // are performed without read barriers. This is fine, even in
3009 // the case where a class object is in the from-space after
3010 // the flip, as a comparison involving such a type would not
3011 // produce a false positive; it may of course produce a false
3012 // negative, in which case we would take the ArraySet slow
3013 // path.
3014
3015 // /* HeapReference<Class> */ temp1 = obj->klass_
3016 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3017 __ MaybeUnpoisonHeapReference(temp1);
3018
3019 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3020 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3021 // /* HeapReference<Class> */ temp2 = value->klass_
3022 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3023 // If heap poisoning is enabled, no need to unpoison `temp1`
3024 // nor `temp2`, as we are comparing two poisoned references.
3025
3026 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3027 MipsLabel do_put;
3028 __ Beq(temp1, temp2, &do_put);
3029 // If heap poisoning is enabled, the `temp1` reference has
3030 // not been unpoisoned yet; unpoison it now.
3031 __ MaybeUnpoisonHeapReference(temp1);
3032
3033 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3034 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3035 // If heap poisoning is enabled, no need to unpoison
3036 // `temp1`, as we are comparing against null below.
3037 __ Bnez(temp1, slow_path->GetEntryLabel());
3038 __ Bind(&do_put);
3039 } else {
3040 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3041 }
3042 }
3043
3044 Register source = value;
3045 if (kPoisonHeapReferences) {
3046 // Note that in the case where `value` is a null reference,
3047 // we do not enter this block, as a null reference does not
3048 // need poisoning.
3049 __ Move(temp1, value);
3050 __ PoisonHeapReference(temp1);
3051 source = temp1;
3052 }
3053
3054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3055 if (index.IsConstant()) {
3056 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003057 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003058 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003059 }
3060 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3061
3062 if (!may_need_runtime_call_for_type_check) {
3063 codegen_->MaybeRecordImplicitNullCheck(instruction);
3064 }
3065
3066 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3067
3068 if (done.IsLinked()) {
3069 __ Bind(&done);
3070 }
3071
3072 if (slow_path != nullptr) {
3073 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003074 }
3075 break;
3076 }
3077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003081 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003082 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3083 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003085 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003086 }
3087 if (value_location.IsConstant()) {
3088 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3089 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3090 } else {
3091 Register value = value_location.AsRegisterPairLow<Register>();
3092 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 }
3094 break;
3095 }
3096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003097 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003100 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003101 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3102 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003103 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003104 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003105 }
3106 if (value_location.IsConstant()) {
3107 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3108 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3109 } else {
3110 FRegister value = value_location.AsFpuRegister<FRegister>();
3111 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 }
3113 break;
3114 }
3115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003117 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003118 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003119 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003120 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3121 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003122 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003123 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003124 }
3125 if (value_location.IsConstant()) {
3126 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3127 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3128 } else {
3129 FRegister value = value_location.AsFpuRegister<FRegister>();
3130 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 }
3132 break;
3133 }
3134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003136 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3137 UNREACHABLE();
3138 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003139}
3140
Lena Djokica2901602017-09-21 13:50:52 +02003141void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3142 HIntermediateArrayAddressIndex* instruction) {
3143 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003144 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003145
3146 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3147
3148 locations->SetInAt(0, Location::RequiresRegister());
3149 locations->SetInAt(1, Location::ConstantLocation(shift));
3150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3151}
3152
3153void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3154 HIntermediateArrayAddressIndex* instruction) {
3155 LocationSummary* locations = instruction->GetLocations();
3156 Register index_reg = locations->InAt(0).AsRegister<Register>();
3157 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3158 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3159}
3160
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003161void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003162 RegisterSet caller_saves = RegisterSet::Empty();
3163 InvokeRuntimeCallingConvention calling_convention;
3164 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3165 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3166 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003167
3168 HInstruction* index = instruction->InputAt(0);
3169 HInstruction* length = instruction->InputAt(1);
3170
3171 bool const_index = false;
3172 bool const_length = false;
3173
3174 if (index->IsConstant()) {
3175 if (length->IsConstant()) {
3176 const_index = true;
3177 const_length = true;
3178 } else {
3179 int32_t index_value = index->AsIntConstant()->GetValue();
3180 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3181 const_index = true;
3182 }
3183 }
3184 } else if (length->IsConstant()) {
3185 int32_t length_value = length->AsIntConstant()->GetValue();
3186 if (IsUint<15>(length_value)) {
3187 const_length = true;
3188 }
3189 }
3190
3191 locations->SetInAt(0, const_index
3192 ? Location::ConstantLocation(index->AsConstant())
3193 : Location::RequiresRegister());
3194 locations->SetInAt(1, const_length
3195 ? Location::ConstantLocation(length->AsConstant())
3196 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003197}
3198
3199void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3200 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003201 Location index_loc = locations->InAt(0);
3202 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003203
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003204 if (length_loc.IsConstant()) {
3205 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3206 if (index_loc.IsConstant()) {
3207 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3208 if (index < 0 || index >= length) {
3209 BoundsCheckSlowPathMIPS* slow_path =
3210 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3211 codegen_->AddSlowPath(slow_path);
3212 __ B(slow_path->GetEntryLabel());
3213 } else {
3214 // Nothing to be done.
3215 }
3216 return;
3217 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003218
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003219 BoundsCheckSlowPathMIPS* slow_path =
3220 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3221 codegen_->AddSlowPath(slow_path);
3222 Register index = index_loc.AsRegister<Register>();
3223 if (length == 0) {
3224 __ B(slow_path->GetEntryLabel());
3225 } else if (length == 1) {
3226 __ Bnez(index, slow_path->GetEntryLabel());
3227 } else {
3228 DCHECK(IsUint<15>(length)) << length;
3229 __ Sltiu(TMP, index, length);
3230 __ Beqz(TMP, slow_path->GetEntryLabel());
3231 }
3232 } else {
3233 Register length = length_loc.AsRegister<Register>();
3234 BoundsCheckSlowPathMIPS* slow_path =
3235 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3236 codegen_->AddSlowPath(slow_path);
3237 if (index_loc.IsConstant()) {
3238 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3239 if (index < 0) {
3240 __ B(slow_path->GetEntryLabel());
3241 } else if (index == 0) {
3242 __ Blez(length, slow_path->GetEntryLabel());
3243 } else {
3244 DCHECK(IsInt<16>(index + 1)) << index;
3245 __ Sltiu(TMP, length, index + 1);
3246 __ Bnez(TMP, slow_path->GetEntryLabel());
3247 }
3248 } else {
3249 Register index = index_loc.AsRegister<Register>();
3250 __ Bgeu(index, length, slow_path->GetEntryLabel());
3251 }
3252 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003253}
3254
Alexey Frunze15958152017-02-09 19:08:30 -08003255// Temp is used for read barrier.
3256static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3257 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003258 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003259 (kUseBakerReadBarrier ||
3260 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3261 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3262 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3263 return 1;
3264 }
3265 return 0;
3266}
3267
3268// Extra temp is used for read barrier.
3269static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3270 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3271}
3272
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003273void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003274 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003275 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003276 LocationSummary* locations =
3277 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003278 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003279 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003280 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003281}
3282
3283void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003284 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003285 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003286 Location obj_loc = locations->InAt(0);
3287 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003288 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003289 Location temp_loc = locations->GetTemp(0);
3290 Register temp = temp_loc.AsRegister<Register>();
3291 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3292 DCHECK_LE(num_temps, 2u);
3293 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003294 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3295 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3296 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3297 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3298 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3299 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3300 const uint32_t object_array_data_offset =
3301 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3302 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003303
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003304 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003305 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003306 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3307 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003308 codegen_->AddSlowPath(slow_path);
3309
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003310 // Avoid this check if we know `obj` is not null.
3311 if (instruction->MustDoNullCheck()) {
3312 __ Beqz(obj, &done);
3313 }
3314
3315 switch (type_check_kind) {
3316 case TypeCheckKind::kExactCheck:
3317 case TypeCheckKind::kArrayCheck: {
3318 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003319 GenerateReferenceLoadTwoRegisters(instruction,
3320 temp_loc,
3321 obj_loc,
3322 class_offset,
3323 maybe_temp2_loc,
3324 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003325 // Jump to slow path for throwing the exception or doing a
3326 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003327 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003328 break;
3329 }
3330
3331 case TypeCheckKind::kAbstractClassCheck: {
3332 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003333 GenerateReferenceLoadTwoRegisters(instruction,
3334 temp_loc,
3335 obj_loc,
3336 class_offset,
3337 maybe_temp2_loc,
3338 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003339 // If the class is abstract, we eagerly fetch the super class of the
3340 // object to avoid doing a comparison we know will fail.
3341 MipsLabel loop;
3342 __ Bind(&loop);
3343 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003344 GenerateReferenceLoadOneRegister(instruction,
3345 temp_loc,
3346 super_offset,
3347 maybe_temp2_loc,
3348 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003349 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3350 // exception.
3351 __ Beqz(temp, slow_path->GetEntryLabel());
3352 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003353 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003354 break;
3355 }
3356
3357 case TypeCheckKind::kClassHierarchyCheck: {
3358 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003359 GenerateReferenceLoadTwoRegisters(instruction,
3360 temp_loc,
3361 obj_loc,
3362 class_offset,
3363 maybe_temp2_loc,
3364 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003365 // Walk over the class hierarchy to find a match.
3366 MipsLabel loop;
3367 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003368 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003369 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003370 GenerateReferenceLoadOneRegister(instruction,
3371 temp_loc,
3372 super_offset,
3373 maybe_temp2_loc,
3374 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003375 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3376 // exception. Otherwise, jump to the beginning of the loop.
3377 __ Bnez(temp, &loop);
3378 __ B(slow_path->GetEntryLabel());
3379 break;
3380 }
3381
3382 case TypeCheckKind::kArrayObjectCheck: {
3383 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003384 GenerateReferenceLoadTwoRegisters(instruction,
3385 temp_loc,
3386 obj_loc,
3387 class_offset,
3388 maybe_temp2_loc,
3389 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003390 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003391 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003392 // Otherwise, we need to check that the object's class is a non-primitive array.
3393 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003394 GenerateReferenceLoadOneRegister(instruction,
3395 temp_loc,
3396 component_offset,
3397 maybe_temp2_loc,
3398 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003399 // If the component type is null, jump to the slow path to throw the exception.
3400 __ Beqz(temp, slow_path->GetEntryLabel());
3401 // Otherwise, the object is indeed an array, further check that this component
3402 // type is not a primitive type.
3403 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3404 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3405 __ Bnez(temp, slow_path->GetEntryLabel());
3406 break;
3407 }
3408
3409 case TypeCheckKind::kUnresolvedCheck:
3410 // We always go into the type check slow path for the unresolved check case.
3411 // We cannot directly call the CheckCast runtime entry point
3412 // without resorting to a type checking slow path here (i.e. by
3413 // calling InvokeRuntime directly), as it would require to
3414 // assign fixed registers for the inputs of this HInstanceOf
3415 // instruction (following the runtime calling convention), which
3416 // might be cluttered by the potential first read barrier
3417 // emission at the beginning of this method.
3418 __ B(slow_path->GetEntryLabel());
3419 break;
3420
3421 case TypeCheckKind::kInterfaceCheck: {
3422 // Avoid read barriers to improve performance of the fast path. We can not get false
3423 // positives by doing this.
3424 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003425 GenerateReferenceLoadTwoRegisters(instruction,
3426 temp_loc,
3427 obj_loc,
3428 class_offset,
3429 maybe_temp2_loc,
3430 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003431 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003432 GenerateReferenceLoadTwoRegisters(instruction,
3433 temp_loc,
3434 temp_loc,
3435 iftable_offset,
3436 maybe_temp2_loc,
3437 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003438 // Iftable is never null.
3439 __ Lw(TMP, temp, array_length_offset);
3440 // Loop through the iftable and check if any class matches.
3441 MipsLabel loop;
3442 __ Bind(&loop);
3443 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3444 __ Beqz(TMP, slow_path->GetEntryLabel());
3445 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3446 __ MaybeUnpoisonHeapReference(AT);
3447 // Go to next interface.
3448 __ Addiu(TMP, TMP, -2);
3449 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003450 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003451 break;
3452 }
3453 }
3454
3455 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003456 __ Bind(slow_path->GetExitLabel());
3457}
3458
3459void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3460 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003461 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003462 locations->SetInAt(0, Location::RequiresRegister());
3463 if (check->HasUses()) {
3464 locations->SetOut(Location::SameAsFirstInput());
3465 }
3466}
3467
3468void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3469 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003470 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003471 check->GetLoadClass(),
3472 check,
3473 check->GetDexPc(),
3474 true);
3475 codegen_->AddSlowPath(slow_path);
3476 GenerateClassInitializationCheck(slow_path,
3477 check->GetLocations()->InAt(0).AsRegister<Register>());
3478}
3479
3480void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003481 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003483 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003484 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003485
3486 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003487 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003488 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003491 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003492 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003493 locations->SetInAt(0, Location::RequiresRegister());
3494 locations->SetInAt(1, Location::RequiresRegister());
3495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3496 break;
3497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003498 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003499 locations->SetInAt(0, Location::RequiresRegister());
3500 locations->SetInAt(1, Location::RequiresRegister());
3501 // Output overlaps because it is written before doing the low comparison.
3502 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3503 break;
3504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003505 case DataType::Type::kFloat32:
3506 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003507 locations->SetInAt(0, Location::RequiresFpuRegister());
3508 locations->SetInAt(1, Location::RequiresFpuRegister());
3509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003510 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003511
3512 default:
3513 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3514 }
3515}
3516
3517void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3518 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003519 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003520 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003521 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522
3523 // 0 if: left == right
3524 // 1 if: left > right
3525 // -1 if: left < right
3526 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003527 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003528 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003529 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003530 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003531 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003532 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003533 Register lhs = locations->InAt(0).AsRegister<Register>();
3534 Register rhs = locations->InAt(1).AsRegister<Register>();
3535 __ Slt(TMP, lhs, rhs);
3536 __ Slt(res, rhs, lhs);
3537 __ Subu(res, res, TMP);
3538 break;
3539 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003541 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003542 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3543 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3544 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3545 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3546 // TODO: more efficient (direct) comparison with a constant.
3547 __ Slt(TMP, lhs_high, rhs_high);
3548 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3549 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3550 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3551 __ Sltu(TMP, lhs_low, rhs_low);
3552 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3553 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3554 __ Bind(&done);
3555 break;
3556 }
3557
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003558 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003559 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003560 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3561 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3562 MipsLabel done;
3563 if (isR6) {
3564 __ CmpEqS(FTMP, lhs, rhs);
3565 __ LoadConst32(res, 0);
3566 __ Bc1nez(FTMP, &done);
3567 if (gt_bias) {
3568 __ CmpLtS(FTMP, lhs, rhs);
3569 __ LoadConst32(res, -1);
3570 __ Bc1nez(FTMP, &done);
3571 __ LoadConst32(res, 1);
3572 } else {
3573 __ CmpLtS(FTMP, rhs, lhs);
3574 __ LoadConst32(res, 1);
3575 __ Bc1nez(FTMP, &done);
3576 __ LoadConst32(res, -1);
3577 }
3578 } else {
3579 if (gt_bias) {
3580 __ ColtS(0, lhs, rhs);
3581 __ LoadConst32(res, -1);
3582 __ Bc1t(0, &done);
3583 __ CeqS(0, lhs, rhs);
3584 __ LoadConst32(res, 1);
3585 __ Movt(res, ZERO, 0);
3586 } else {
3587 __ ColtS(0, rhs, lhs);
3588 __ LoadConst32(res, 1);
3589 __ Bc1t(0, &done);
3590 __ CeqS(0, lhs, rhs);
3591 __ LoadConst32(res, -1);
3592 __ Movt(res, ZERO, 0);
3593 }
3594 }
3595 __ Bind(&done);
3596 break;
3597 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003598 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003599 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003600 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3601 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3602 MipsLabel done;
3603 if (isR6) {
3604 __ CmpEqD(FTMP, lhs, rhs);
3605 __ LoadConst32(res, 0);
3606 __ Bc1nez(FTMP, &done);
3607 if (gt_bias) {
3608 __ CmpLtD(FTMP, lhs, rhs);
3609 __ LoadConst32(res, -1);
3610 __ Bc1nez(FTMP, &done);
3611 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003612 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003613 __ CmpLtD(FTMP, rhs, lhs);
3614 __ LoadConst32(res, 1);
3615 __ Bc1nez(FTMP, &done);
3616 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003617 }
3618 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003619 if (gt_bias) {
3620 __ ColtD(0, lhs, rhs);
3621 __ LoadConst32(res, -1);
3622 __ Bc1t(0, &done);
3623 __ CeqD(0, lhs, rhs);
3624 __ LoadConst32(res, 1);
3625 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003627 __ ColtD(0, rhs, lhs);
3628 __ LoadConst32(res, 1);
3629 __ Bc1t(0, &done);
3630 __ CeqD(0, lhs, rhs);
3631 __ LoadConst32(res, -1);
3632 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633 }
3634 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003635 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003636 break;
3637 }
3638
3639 default:
3640 LOG(FATAL) << "Unimplemented compare type " << in_type;
3641 }
3642}
3643
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003644void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003645 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003646 switch (instruction->InputAt(0)->GetType()) {
3647 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003648 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003649 locations->SetInAt(0, Location::RequiresRegister());
3650 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3651 break;
3652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003653 case DataType::Type::kFloat32:
3654 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003655 locations->SetInAt(0, Location::RequiresFpuRegister());
3656 locations->SetInAt(1, Location::RequiresFpuRegister());
3657 break;
3658 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003659 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3661 }
3662}
3663
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003664void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003665 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 return;
3667 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003669 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003670 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003671
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003672 switch (type) {
3673 default:
3674 // Integer case.
3675 GenerateIntCompare(instruction->GetCondition(), locations);
3676 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003679 GenerateLongCompare(instruction->GetCondition(), locations);
3680 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003682 case DataType::Type::kFloat32:
3683 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003684 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3685 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 }
3687}
3688
Alexey Frunze7e99e052015-11-24 19:28:01 -08003689void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3690 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003691 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003692
3693 LocationSummary* locations = instruction->GetLocations();
3694 Location second = locations->InAt(1);
3695 DCHECK(second.IsConstant());
3696
3697 Register out = locations->Out().AsRegister<Register>();
3698 Register dividend = locations->InAt(0).AsRegister<Register>();
3699 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3700 DCHECK(imm == 1 || imm == -1);
3701
3702 if (instruction->IsRem()) {
3703 __ Move(out, ZERO);
3704 } else {
3705 if (imm == -1) {
3706 __ Subu(out, ZERO, dividend);
3707 } else if (out != dividend) {
3708 __ Move(out, dividend);
3709 }
3710 }
3711}
3712
3713void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3714 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003715 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003716
3717 LocationSummary* locations = instruction->GetLocations();
3718 Location second = locations->InAt(1);
3719 DCHECK(second.IsConstant());
3720
3721 Register out = locations->Out().AsRegister<Register>();
3722 Register dividend = locations->InAt(0).AsRegister<Register>();
3723 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003724 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003725 int ctz_imm = CTZ(abs_imm);
3726
3727 if (instruction->IsDiv()) {
3728 if (ctz_imm == 1) {
3729 // Fast path for division by +/-2, which is very common.
3730 __ Srl(TMP, dividend, 31);
3731 } else {
3732 __ Sra(TMP, dividend, 31);
3733 __ Srl(TMP, TMP, 32 - ctz_imm);
3734 }
3735 __ Addu(out, dividend, TMP);
3736 __ Sra(out, out, ctz_imm);
3737 if (imm < 0) {
3738 __ Subu(out, ZERO, out);
3739 }
3740 } else {
3741 if (ctz_imm == 1) {
3742 // Fast path for modulo +/-2, which is very common.
3743 __ Sra(TMP, dividend, 31);
3744 __ Subu(out, dividend, TMP);
3745 __ Andi(out, out, 1);
3746 __ Addu(out, out, TMP);
3747 } else {
3748 __ Sra(TMP, dividend, 31);
3749 __ Srl(TMP, TMP, 32 - ctz_imm);
3750 __ Addu(out, dividend, TMP);
3751 if (IsUint<16>(abs_imm - 1)) {
3752 __ Andi(out, out, abs_imm - 1);
3753 } else {
Lena Djokica556e6b2017-12-13 12:09:42 +01003754 if (codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2()) {
3755 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3756 } else {
3757 __ Sll(out, out, 32 - ctz_imm);
3758 __ Srl(out, out, 32 - ctz_imm);
3759 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003760 }
3761 __ Subu(out, out, TMP);
3762 }
3763 }
3764}
3765
3766void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3767 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003768 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003769
3770 LocationSummary* locations = instruction->GetLocations();
3771 Location second = locations->InAt(1);
3772 DCHECK(second.IsConstant());
3773
3774 Register out = locations->Out().AsRegister<Register>();
3775 Register dividend = locations->InAt(0).AsRegister<Register>();
3776 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3777
3778 int64_t magic;
3779 int shift;
3780 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3781
3782 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3783
3784 __ LoadConst32(TMP, magic);
3785 if (isR6) {
3786 __ MuhR6(TMP, dividend, TMP);
3787 } else {
3788 __ MultR2(dividend, TMP);
3789 __ Mfhi(TMP);
3790 }
3791 if (imm > 0 && magic < 0) {
3792 __ Addu(TMP, TMP, dividend);
3793 } else if (imm < 0 && magic > 0) {
3794 __ Subu(TMP, TMP, dividend);
3795 }
3796
3797 if (shift != 0) {
3798 __ Sra(TMP, TMP, shift);
3799 }
3800
3801 if (instruction->IsDiv()) {
3802 __ Sra(out, TMP, 31);
3803 __ Subu(out, TMP, out);
3804 } else {
3805 __ Sra(AT, TMP, 31);
3806 __ Subu(AT, TMP, AT);
3807 __ LoadConst32(TMP, imm);
3808 if (isR6) {
3809 __ MulR6(TMP, AT, TMP);
3810 } else {
3811 __ MulR2(TMP, AT, TMP);
3812 }
3813 __ Subu(out, dividend, TMP);
3814 }
3815}
3816
3817void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3818 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003819 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003820
3821 LocationSummary* locations = instruction->GetLocations();
3822 Register out = locations->Out().AsRegister<Register>();
3823 Location second = locations->InAt(1);
3824
3825 if (second.IsConstant()) {
3826 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3827 if (imm == 0) {
3828 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3829 } else if (imm == 1 || imm == -1) {
3830 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003831 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003832 DivRemByPowerOfTwo(instruction);
3833 } else {
3834 DCHECK(imm <= -2 || imm >= 2);
3835 GenerateDivRemWithAnyConstant(instruction);
3836 }
3837 } else {
3838 Register dividend = locations->InAt(0).AsRegister<Register>();
3839 Register divisor = second.AsRegister<Register>();
3840 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3841 if (instruction->IsDiv()) {
3842 if (isR6) {
3843 __ DivR6(out, dividend, divisor);
3844 } else {
3845 __ DivR2(out, dividend, divisor);
3846 }
3847 } else {
3848 if (isR6) {
3849 __ ModR6(out, dividend, divisor);
3850 } else {
3851 __ ModR2(out, dividend, divisor);
3852 }
3853 }
3854 }
3855}
3856
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003857void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 DataType::Type type = div->GetResultType();
3859 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003860 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861 : LocationSummary::kNoCall;
3862
Vladimir Markoca6fff82017-10-03 14:49:14 +01003863 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003864
3865 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003867 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003868 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3870 break;
3871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003873 InvokeRuntimeCallingConvention calling_convention;
3874 locations->SetInAt(0, Location::RegisterPairLocation(
3875 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3876 locations->SetInAt(1, Location::RegisterPairLocation(
3877 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3878 locations->SetOut(calling_convention.GetReturnLocation(type));
3879 break;
3880 }
3881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003882 case DataType::Type::kFloat32:
3883 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003884 locations->SetInAt(0, Location::RequiresFpuRegister());
3885 locations->SetInAt(1, Location::RequiresFpuRegister());
3886 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3887 break;
3888
3889 default:
3890 LOG(FATAL) << "Unexpected div type " << type;
3891 }
3892}
3893
3894void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003897
3898 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003900 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003901 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003902 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003903 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003904 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3905 break;
3906 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003907 case DataType::Type::kFloat32:
3908 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3910 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3911 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003912 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003913 __ DivS(dst, lhs, rhs);
3914 } else {
3915 __ DivD(dst, lhs, rhs);
3916 }
3917 break;
3918 }
3919 default:
3920 LOG(FATAL) << "Unexpected div type " << type;
3921 }
3922}
3923
3924void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003925 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003926 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003927}
3928
3929void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003930 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003931 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003932 codegen_->AddSlowPath(slow_path);
3933 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003935
3936 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003938 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003939 case DataType::Type::kInt8:
3940 case DataType::Type::kUint16:
3941 case DataType::Type::kInt16:
3942 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003943 if (value.IsConstant()) {
3944 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3945 __ B(slow_path->GetEntryLabel());
3946 } else {
3947 // A division by a non-null constant is valid. We don't need to perform
3948 // any check, so simply fall through.
3949 }
3950 } else {
3951 DCHECK(value.IsRegister()) << value;
3952 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3953 }
3954 break;
3955 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003956 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003957 if (value.IsConstant()) {
3958 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3959 __ B(slow_path->GetEntryLabel());
3960 } else {
3961 // A division by a non-null constant is valid. We don't need to perform
3962 // any check, so simply fall through.
3963 }
3964 } else {
3965 DCHECK(value.IsRegisterPair()) << value;
3966 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3967 __ Beqz(TMP, slow_path->GetEntryLabel());
3968 }
3969 break;
3970 }
3971 default:
3972 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3973 }
3974}
3975
3976void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3977 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003978 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003979 locations->SetOut(Location::ConstantLocation(constant));
3980}
3981
3982void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3983 // Will be generated at use site.
3984}
3985
3986void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3987 exit->SetLocations(nullptr);
3988}
3989
3990void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3991}
3992
3993void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3994 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003995 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003996 locations->SetOut(Location::ConstantLocation(constant));
3997}
3998
3999void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4000 // Will be generated at use site.
4001}
4002
4003void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4004 got->SetLocations(nullptr);
4005}
4006
4007void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004008 if (successor->IsExitBlock()) {
4009 DCHECK(got->GetPrevious()->AlwaysThrows());
4010 return; // no code needed
4011 }
4012
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004013 HBasicBlock* block = got->GetBlock();
4014 HInstruction* previous = got->GetPrevious();
4015 HLoopInformation* info = block->GetLoopInformation();
4016
4017 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004018 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4019 return;
4020 }
4021 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4022 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4023 }
4024 if (!codegen_->GoesToNextBlock(block, successor)) {
4025 __ B(codegen_->GetLabelOf(successor));
4026 }
4027}
4028
4029void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4030 HandleGoto(got, got->GetSuccessor());
4031}
4032
4033void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4034 try_boundary->SetLocations(nullptr);
4035}
4036
4037void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4038 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4039 if (!successor->IsExitBlock()) {
4040 HandleGoto(try_boundary, successor);
4041 }
4042}
4043
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004044void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4045 LocationSummary* locations) {
4046 Register dst = locations->Out().AsRegister<Register>();
4047 Register lhs = locations->InAt(0).AsRegister<Register>();
4048 Location rhs_location = locations->InAt(1);
4049 Register rhs_reg = ZERO;
4050 int64_t rhs_imm = 0;
4051 bool use_imm = rhs_location.IsConstant();
4052 if (use_imm) {
4053 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4054 } else {
4055 rhs_reg = rhs_location.AsRegister<Register>();
4056 }
4057
4058 switch (cond) {
4059 case kCondEQ:
4060 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004061 if (use_imm && IsInt<16>(-rhs_imm)) {
4062 if (rhs_imm == 0) {
4063 if (cond == kCondEQ) {
4064 __ Sltiu(dst, lhs, 1);
4065 } else {
4066 __ Sltu(dst, ZERO, lhs);
4067 }
4068 } else {
4069 __ Addiu(dst, lhs, -rhs_imm);
4070 if (cond == kCondEQ) {
4071 __ Sltiu(dst, dst, 1);
4072 } else {
4073 __ Sltu(dst, ZERO, dst);
4074 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004075 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004076 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004077 if (use_imm && IsUint<16>(rhs_imm)) {
4078 __ Xori(dst, lhs, rhs_imm);
4079 } else {
4080 if (use_imm) {
4081 rhs_reg = TMP;
4082 __ LoadConst32(rhs_reg, rhs_imm);
4083 }
4084 __ Xor(dst, lhs, rhs_reg);
4085 }
4086 if (cond == kCondEQ) {
4087 __ Sltiu(dst, dst, 1);
4088 } else {
4089 __ Sltu(dst, ZERO, dst);
4090 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004091 }
4092 break;
4093
4094 case kCondLT:
4095 case kCondGE:
4096 if (use_imm && IsInt<16>(rhs_imm)) {
4097 __ Slti(dst, lhs, rhs_imm);
4098 } else {
4099 if (use_imm) {
4100 rhs_reg = TMP;
4101 __ LoadConst32(rhs_reg, rhs_imm);
4102 }
4103 __ Slt(dst, lhs, rhs_reg);
4104 }
4105 if (cond == kCondGE) {
4106 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4107 // only the slt instruction but no sge.
4108 __ Xori(dst, dst, 1);
4109 }
4110 break;
4111
4112 case kCondLE:
4113 case kCondGT:
4114 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4115 // Simulate lhs <= rhs via lhs < rhs + 1.
4116 __ Slti(dst, lhs, rhs_imm + 1);
4117 if (cond == kCondGT) {
4118 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4119 // only the slti instruction but no sgti.
4120 __ Xori(dst, dst, 1);
4121 }
4122 } else {
4123 if (use_imm) {
4124 rhs_reg = TMP;
4125 __ LoadConst32(rhs_reg, rhs_imm);
4126 }
4127 __ Slt(dst, rhs_reg, lhs);
4128 if (cond == kCondLE) {
4129 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4130 // only the slt instruction but no sle.
4131 __ Xori(dst, dst, 1);
4132 }
4133 }
4134 break;
4135
4136 case kCondB:
4137 case kCondAE:
4138 if (use_imm && IsInt<16>(rhs_imm)) {
4139 // Sltiu sign-extends its 16-bit immediate operand before
4140 // the comparison and thus lets us compare directly with
4141 // unsigned values in the ranges [0, 0x7fff] and
4142 // [0xffff8000, 0xffffffff].
4143 __ Sltiu(dst, lhs, rhs_imm);
4144 } else {
4145 if (use_imm) {
4146 rhs_reg = TMP;
4147 __ LoadConst32(rhs_reg, rhs_imm);
4148 }
4149 __ Sltu(dst, lhs, rhs_reg);
4150 }
4151 if (cond == kCondAE) {
4152 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4153 // only the sltu instruction but no sgeu.
4154 __ Xori(dst, dst, 1);
4155 }
4156 break;
4157
4158 case kCondBE:
4159 case kCondA:
4160 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4161 // Simulate lhs <= rhs via lhs < rhs + 1.
4162 // Note that this only works if rhs + 1 does not overflow
4163 // to 0, hence the check above.
4164 // Sltiu sign-extends its 16-bit immediate operand before
4165 // the comparison and thus lets us compare directly with
4166 // unsigned values in the ranges [0, 0x7fff] and
4167 // [0xffff8000, 0xffffffff].
4168 __ Sltiu(dst, lhs, rhs_imm + 1);
4169 if (cond == kCondA) {
4170 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4171 // only the sltiu instruction but no sgtiu.
4172 __ Xori(dst, dst, 1);
4173 }
4174 } else {
4175 if (use_imm) {
4176 rhs_reg = TMP;
4177 __ LoadConst32(rhs_reg, rhs_imm);
4178 }
4179 __ Sltu(dst, rhs_reg, lhs);
4180 if (cond == kCondBE) {
4181 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4182 // only the sltu instruction but no sleu.
4183 __ Xori(dst, dst, 1);
4184 }
4185 }
4186 break;
4187 }
4188}
4189
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004190bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4191 LocationSummary* input_locations,
4192 Register dst) {
4193 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4194 Location rhs_location = input_locations->InAt(1);
4195 Register rhs_reg = ZERO;
4196 int64_t rhs_imm = 0;
4197 bool use_imm = rhs_location.IsConstant();
4198 if (use_imm) {
4199 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4200 } else {
4201 rhs_reg = rhs_location.AsRegister<Register>();
4202 }
4203
4204 switch (cond) {
4205 case kCondEQ:
4206 case kCondNE:
4207 if (use_imm && IsInt<16>(-rhs_imm)) {
4208 __ Addiu(dst, lhs, -rhs_imm);
4209 } else if (use_imm && IsUint<16>(rhs_imm)) {
4210 __ Xori(dst, lhs, rhs_imm);
4211 } else {
4212 if (use_imm) {
4213 rhs_reg = TMP;
4214 __ LoadConst32(rhs_reg, rhs_imm);
4215 }
4216 __ Xor(dst, lhs, rhs_reg);
4217 }
4218 return (cond == kCondEQ);
4219
4220 case kCondLT:
4221 case kCondGE:
4222 if (use_imm && IsInt<16>(rhs_imm)) {
4223 __ Slti(dst, lhs, rhs_imm);
4224 } else {
4225 if (use_imm) {
4226 rhs_reg = TMP;
4227 __ LoadConst32(rhs_reg, rhs_imm);
4228 }
4229 __ Slt(dst, lhs, rhs_reg);
4230 }
4231 return (cond == kCondGE);
4232
4233 case kCondLE:
4234 case kCondGT:
4235 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4236 // Simulate lhs <= rhs via lhs < rhs + 1.
4237 __ Slti(dst, lhs, rhs_imm + 1);
4238 return (cond == kCondGT);
4239 } else {
4240 if (use_imm) {
4241 rhs_reg = TMP;
4242 __ LoadConst32(rhs_reg, rhs_imm);
4243 }
4244 __ Slt(dst, rhs_reg, lhs);
4245 return (cond == kCondLE);
4246 }
4247
4248 case kCondB:
4249 case kCondAE:
4250 if (use_imm && IsInt<16>(rhs_imm)) {
4251 // Sltiu sign-extends its 16-bit immediate operand before
4252 // the comparison and thus lets us compare directly with
4253 // unsigned values in the ranges [0, 0x7fff] and
4254 // [0xffff8000, 0xffffffff].
4255 __ Sltiu(dst, lhs, rhs_imm);
4256 } else {
4257 if (use_imm) {
4258 rhs_reg = TMP;
4259 __ LoadConst32(rhs_reg, rhs_imm);
4260 }
4261 __ Sltu(dst, lhs, rhs_reg);
4262 }
4263 return (cond == kCondAE);
4264
4265 case kCondBE:
4266 case kCondA:
4267 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4268 // Simulate lhs <= rhs via lhs < rhs + 1.
4269 // Note that this only works if rhs + 1 does not overflow
4270 // to 0, hence the check above.
4271 // Sltiu sign-extends its 16-bit immediate operand before
4272 // the comparison and thus lets us compare directly with
4273 // unsigned values in the ranges [0, 0x7fff] and
4274 // [0xffff8000, 0xffffffff].
4275 __ Sltiu(dst, lhs, rhs_imm + 1);
4276 return (cond == kCondA);
4277 } else {
4278 if (use_imm) {
4279 rhs_reg = TMP;
4280 __ LoadConst32(rhs_reg, rhs_imm);
4281 }
4282 __ Sltu(dst, rhs_reg, lhs);
4283 return (cond == kCondBE);
4284 }
4285 }
4286}
4287
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004288void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4289 LocationSummary* locations,
4290 MipsLabel* label) {
4291 Register lhs = locations->InAt(0).AsRegister<Register>();
4292 Location rhs_location = locations->InAt(1);
4293 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004294 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004295 bool use_imm = rhs_location.IsConstant();
4296 if (use_imm) {
4297 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4298 } else {
4299 rhs_reg = rhs_location.AsRegister<Register>();
4300 }
4301
4302 if (use_imm && rhs_imm == 0) {
4303 switch (cond) {
4304 case kCondEQ:
4305 case kCondBE: // <= 0 if zero
4306 __ Beqz(lhs, label);
4307 break;
4308 case kCondNE:
4309 case kCondA: // > 0 if non-zero
4310 __ Bnez(lhs, label);
4311 break;
4312 case kCondLT:
4313 __ Bltz(lhs, label);
4314 break;
4315 case kCondGE:
4316 __ Bgez(lhs, label);
4317 break;
4318 case kCondLE:
4319 __ Blez(lhs, label);
4320 break;
4321 case kCondGT:
4322 __ Bgtz(lhs, label);
4323 break;
4324 case kCondB: // always false
4325 break;
4326 case kCondAE: // always true
4327 __ B(label);
4328 break;
4329 }
4330 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004331 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4332 if (isR6 || !use_imm) {
4333 if (use_imm) {
4334 rhs_reg = TMP;
4335 __ LoadConst32(rhs_reg, rhs_imm);
4336 }
4337 switch (cond) {
4338 case kCondEQ:
4339 __ Beq(lhs, rhs_reg, label);
4340 break;
4341 case kCondNE:
4342 __ Bne(lhs, rhs_reg, label);
4343 break;
4344 case kCondLT:
4345 __ Blt(lhs, rhs_reg, label);
4346 break;
4347 case kCondGE:
4348 __ Bge(lhs, rhs_reg, label);
4349 break;
4350 case kCondLE:
4351 __ Bge(rhs_reg, lhs, label);
4352 break;
4353 case kCondGT:
4354 __ Blt(rhs_reg, lhs, label);
4355 break;
4356 case kCondB:
4357 __ Bltu(lhs, rhs_reg, label);
4358 break;
4359 case kCondAE:
4360 __ Bgeu(lhs, rhs_reg, label);
4361 break;
4362 case kCondBE:
4363 __ Bgeu(rhs_reg, lhs, label);
4364 break;
4365 case kCondA:
4366 __ Bltu(rhs_reg, lhs, label);
4367 break;
4368 }
4369 } else {
4370 // Special cases for more efficient comparison with constants on R2.
4371 switch (cond) {
4372 case kCondEQ:
4373 __ LoadConst32(TMP, rhs_imm);
4374 __ Beq(lhs, TMP, label);
4375 break;
4376 case kCondNE:
4377 __ LoadConst32(TMP, rhs_imm);
4378 __ Bne(lhs, TMP, label);
4379 break;
4380 case kCondLT:
4381 if (IsInt<16>(rhs_imm)) {
4382 __ Slti(TMP, lhs, rhs_imm);
4383 __ Bnez(TMP, label);
4384 } else {
4385 __ LoadConst32(TMP, rhs_imm);
4386 __ Blt(lhs, TMP, label);
4387 }
4388 break;
4389 case kCondGE:
4390 if (IsInt<16>(rhs_imm)) {
4391 __ Slti(TMP, lhs, rhs_imm);
4392 __ Beqz(TMP, label);
4393 } else {
4394 __ LoadConst32(TMP, rhs_imm);
4395 __ Bge(lhs, TMP, label);
4396 }
4397 break;
4398 case kCondLE:
4399 if (IsInt<16>(rhs_imm + 1)) {
4400 // Simulate lhs <= rhs via lhs < rhs + 1.
4401 __ Slti(TMP, lhs, rhs_imm + 1);
4402 __ Bnez(TMP, label);
4403 } else {
4404 __ LoadConst32(TMP, rhs_imm);
4405 __ Bge(TMP, lhs, label);
4406 }
4407 break;
4408 case kCondGT:
4409 if (IsInt<16>(rhs_imm + 1)) {
4410 // Simulate lhs > rhs via !(lhs < rhs + 1).
4411 __ Slti(TMP, lhs, rhs_imm + 1);
4412 __ Beqz(TMP, label);
4413 } else {
4414 __ LoadConst32(TMP, rhs_imm);
4415 __ Blt(TMP, lhs, label);
4416 }
4417 break;
4418 case kCondB:
4419 if (IsInt<16>(rhs_imm)) {
4420 __ Sltiu(TMP, lhs, rhs_imm);
4421 __ Bnez(TMP, label);
4422 } else {
4423 __ LoadConst32(TMP, rhs_imm);
4424 __ Bltu(lhs, TMP, label);
4425 }
4426 break;
4427 case kCondAE:
4428 if (IsInt<16>(rhs_imm)) {
4429 __ Sltiu(TMP, lhs, rhs_imm);
4430 __ Beqz(TMP, label);
4431 } else {
4432 __ LoadConst32(TMP, rhs_imm);
4433 __ Bgeu(lhs, TMP, label);
4434 }
4435 break;
4436 case kCondBE:
4437 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4438 // Simulate lhs <= rhs via lhs < rhs + 1.
4439 // Note that this only works if rhs + 1 does not overflow
4440 // to 0, hence the check above.
4441 __ Sltiu(TMP, lhs, rhs_imm + 1);
4442 __ Bnez(TMP, label);
4443 } else {
4444 __ LoadConst32(TMP, rhs_imm);
4445 __ Bgeu(TMP, lhs, label);
4446 }
4447 break;
4448 case kCondA:
4449 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4450 // Simulate lhs > rhs via !(lhs < rhs + 1).
4451 // Note that this only works if rhs + 1 does not overflow
4452 // to 0, hence the check above.
4453 __ Sltiu(TMP, lhs, rhs_imm + 1);
4454 __ Beqz(TMP, label);
4455 } else {
4456 __ LoadConst32(TMP, rhs_imm);
4457 __ Bltu(TMP, lhs, label);
4458 }
4459 break;
4460 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004461 }
4462 }
4463}
4464
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004465void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4466 LocationSummary* locations) {
4467 Register dst = locations->Out().AsRegister<Register>();
4468 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4469 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4470 Location rhs_location = locations->InAt(1);
4471 Register rhs_high = ZERO;
4472 Register rhs_low = ZERO;
4473 int64_t imm = 0;
4474 uint32_t imm_high = 0;
4475 uint32_t imm_low = 0;
4476 bool use_imm = rhs_location.IsConstant();
4477 if (use_imm) {
4478 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4479 imm_high = High32Bits(imm);
4480 imm_low = Low32Bits(imm);
4481 } else {
4482 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4483 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4484 }
4485 if (use_imm && imm == 0) {
4486 switch (cond) {
4487 case kCondEQ:
4488 case kCondBE: // <= 0 if zero
4489 __ Or(dst, lhs_high, lhs_low);
4490 __ Sltiu(dst, dst, 1);
4491 break;
4492 case kCondNE:
4493 case kCondA: // > 0 if non-zero
4494 __ Or(dst, lhs_high, lhs_low);
4495 __ Sltu(dst, ZERO, dst);
4496 break;
4497 case kCondLT:
4498 __ Slt(dst, lhs_high, ZERO);
4499 break;
4500 case kCondGE:
4501 __ Slt(dst, lhs_high, ZERO);
4502 __ Xori(dst, dst, 1);
4503 break;
4504 case kCondLE:
4505 __ Or(TMP, lhs_high, lhs_low);
4506 __ Sra(AT, lhs_high, 31);
4507 __ Sltu(dst, AT, TMP);
4508 __ Xori(dst, dst, 1);
4509 break;
4510 case kCondGT:
4511 __ Or(TMP, lhs_high, lhs_low);
4512 __ Sra(AT, lhs_high, 31);
4513 __ Sltu(dst, AT, TMP);
4514 break;
4515 case kCondB: // always false
4516 __ Andi(dst, dst, 0);
4517 break;
4518 case kCondAE: // always true
4519 __ Ori(dst, ZERO, 1);
4520 break;
4521 }
4522 } else if (use_imm) {
4523 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4524 switch (cond) {
4525 case kCondEQ:
4526 __ LoadConst32(TMP, imm_high);
4527 __ Xor(TMP, TMP, lhs_high);
4528 __ LoadConst32(AT, imm_low);
4529 __ Xor(AT, AT, lhs_low);
4530 __ Or(dst, TMP, AT);
4531 __ Sltiu(dst, dst, 1);
4532 break;
4533 case kCondNE:
4534 __ LoadConst32(TMP, imm_high);
4535 __ Xor(TMP, TMP, lhs_high);
4536 __ LoadConst32(AT, imm_low);
4537 __ Xor(AT, AT, lhs_low);
4538 __ Or(dst, TMP, AT);
4539 __ Sltu(dst, ZERO, dst);
4540 break;
4541 case kCondLT:
4542 case kCondGE:
4543 if (dst == lhs_low) {
4544 __ LoadConst32(TMP, imm_low);
4545 __ Sltu(dst, lhs_low, TMP);
4546 }
4547 __ LoadConst32(TMP, imm_high);
4548 __ Slt(AT, lhs_high, TMP);
4549 __ Slt(TMP, TMP, lhs_high);
4550 if (dst != lhs_low) {
4551 __ LoadConst32(dst, imm_low);
4552 __ Sltu(dst, lhs_low, dst);
4553 }
4554 __ Slt(dst, TMP, dst);
4555 __ Or(dst, dst, AT);
4556 if (cond == kCondGE) {
4557 __ Xori(dst, dst, 1);
4558 }
4559 break;
4560 case kCondGT:
4561 case kCondLE:
4562 if (dst == lhs_low) {
4563 __ LoadConst32(TMP, imm_low);
4564 __ Sltu(dst, TMP, lhs_low);
4565 }
4566 __ LoadConst32(TMP, imm_high);
4567 __ Slt(AT, TMP, lhs_high);
4568 __ Slt(TMP, lhs_high, TMP);
4569 if (dst != lhs_low) {
4570 __ LoadConst32(dst, imm_low);
4571 __ Sltu(dst, dst, lhs_low);
4572 }
4573 __ Slt(dst, TMP, dst);
4574 __ Or(dst, dst, AT);
4575 if (cond == kCondLE) {
4576 __ Xori(dst, dst, 1);
4577 }
4578 break;
4579 case kCondB:
4580 case kCondAE:
4581 if (dst == lhs_low) {
4582 __ LoadConst32(TMP, imm_low);
4583 __ Sltu(dst, lhs_low, TMP);
4584 }
4585 __ LoadConst32(TMP, imm_high);
4586 __ Sltu(AT, lhs_high, TMP);
4587 __ Sltu(TMP, TMP, lhs_high);
4588 if (dst != lhs_low) {
4589 __ LoadConst32(dst, imm_low);
4590 __ Sltu(dst, lhs_low, dst);
4591 }
4592 __ Slt(dst, TMP, dst);
4593 __ Or(dst, dst, AT);
4594 if (cond == kCondAE) {
4595 __ Xori(dst, dst, 1);
4596 }
4597 break;
4598 case kCondA:
4599 case kCondBE:
4600 if (dst == lhs_low) {
4601 __ LoadConst32(TMP, imm_low);
4602 __ Sltu(dst, TMP, lhs_low);
4603 }
4604 __ LoadConst32(TMP, imm_high);
4605 __ Sltu(AT, TMP, lhs_high);
4606 __ Sltu(TMP, lhs_high, TMP);
4607 if (dst != lhs_low) {
4608 __ LoadConst32(dst, imm_low);
4609 __ Sltu(dst, dst, lhs_low);
4610 }
4611 __ Slt(dst, TMP, dst);
4612 __ Or(dst, dst, AT);
4613 if (cond == kCondBE) {
4614 __ Xori(dst, dst, 1);
4615 }
4616 break;
4617 }
4618 } else {
4619 switch (cond) {
4620 case kCondEQ:
4621 __ Xor(TMP, lhs_high, rhs_high);
4622 __ Xor(AT, lhs_low, rhs_low);
4623 __ Or(dst, TMP, AT);
4624 __ Sltiu(dst, dst, 1);
4625 break;
4626 case kCondNE:
4627 __ Xor(TMP, lhs_high, rhs_high);
4628 __ Xor(AT, lhs_low, rhs_low);
4629 __ Or(dst, TMP, AT);
4630 __ Sltu(dst, ZERO, dst);
4631 break;
4632 case kCondLT:
4633 case kCondGE:
4634 __ Slt(TMP, rhs_high, lhs_high);
4635 __ Sltu(AT, lhs_low, rhs_low);
4636 __ Slt(TMP, TMP, AT);
4637 __ Slt(AT, lhs_high, rhs_high);
4638 __ Or(dst, AT, TMP);
4639 if (cond == kCondGE) {
4640 __ Xori(dst, dst, 1);
4641 }
4642 break;
4643 case kCondGT:
4644 case kCondLE:
4645 __ Slt(TMP, lhs_high, rhs_high);
4646 __ Sltu(AT, rhs_low, lhs_low);
4647 __ Slt(TMP, TMP, AT);
4648 __ Slt(AT, rhs_high, lhs_high);
4649 __ Or(dst, AT, TMP);
4650 if (cond == kCondLE) {
4651 __ Xori(dst, dst, 1);
4652 }
4653 break;
4654 case kCondB:
4655 case kCondAE:
4656 __ Sltu(TMP, rhs_high, lhs_high);
4657 __ Sltu(AT, lhs_low, rhs_low);
4658 __ Slt(TMP, TMP, AT);
4659 __ Sltu(AT, lhs_high, rhs_high);
4660 __ Or(dst, AT, TMP);
4661 if (cond == kCondAE) {
4662 __ Xori(dst, dst, 1);
4663 }
4664 break;
4665 case kCondA:
4666 case kCondBE:
4667 __ Sltu(TMP, lhs_high, rhs_high);
4668 __ Sltu(AT, rhs_low, lhs_low);
4669 __ Slt(TMP, TMP, AT);
4670 __ Sltu(AT, rhs_high, lhs_high);
4671 __ Or(dst, AT, TMP);
4672 if (cond == kCondBE) {
4673 __ Xori(dst, dst, 1);
4674 }
4675 break;
4676 }
4677 }
4678}
4679
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004680void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4681 LocationSummary* locations,
4682 MipsLabel* label) {
4683 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4684 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4685 Location rhs_location = locations->InAt(1);
4686 Register rhs_high = ZERO;
4687 Register rhs_low = ZERO;
4688 int64_t imm = 0;
4689 uint32_t imm_high = 0;
4690 uint32_t imm_low = 0;
4691 bool use_imm = rhs_location.IsConstant();
4692 if (use_imm) {
4693 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4694 imm_high = High32Bits(imm);
4695 imm_low = Low32Bits(imm);
4696 } else {
4697 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4698 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4699 }
4700
4701 if (use_imm && imm == 0) {
4702 switch (cond) {
4703 case kCondEQ:
4704 case kCondBE: // <= 0 if zero
4705 __ Or(TMP, lhs_high, lhs_low);
4706 __ Beqz(TMP, label);
4707 break;
4708 case kCondNE:
4709 case kCondA: // > 0 if non-zero
4710 __ Or(TMP, lhs_high, lhs_low);
4711 __ Bnez(TMP, label);
4712 break;
4713 case kCondLT:
4714 __ Bltz(lhs_high, label);
4715 break;
4716 case kCondGE:
4717 __ Bgez(lhs_high, label);
4718 break;
4719 case kCondLE:
4720 __ Or(TMP, lhs_high, lhs_low);
4721 __ Sra(AT, lhs_high, 31);
4722 __ Bgeu(AT, TMP, label);
4723 break;
4724 case kCondGT:
4725 __ Or(TMP, lhs_high, lhs_low);
4726 __ Sra(AT, lhs_high, 31);
4727 __ Bltu(AT, TMP, label);
4728 break;
4729 case kCondB: // always false
4730 break;
4731 case kCondAE: // always true
4732 __ B(label);
4733 break;
4734 }
4735 } else if (use_imm) {
4736 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4737 switch (cond) {
4738 case kCondEQ:
4739 __ LoadConst32(TMP, imm_high);
4740 __ Xor(TMP, TMP, lhs_high);
4741 __ LoadConst32(AT, imm_low);
4742 __ Xor(AT, AT, lhs_low);
4743 __ Or(TMP, TMP, AT);
4744 __ Beqz(TMP, label);
4745 break;
4746 case kCondNE:
4747 __ LoadConst32(TMP, imm_high);
4748 __ Xor(TMP, TMP, lhs_high);
4749 __ LoadConst32(AT, imm_low);
4750 __ Xor(AT, AT, lhs_low);
4751 __ Or(TMP, TMP, AT);
4752 __ Bnez(TMP, label);
4753 break;
4754 case kCondLT:
4755 __ LoadConst32(TMP, imm_high);
4756 __ Blt(lhs_high, TMP, label);
4757 __ Slt(TMP, TMP, lhs_high);
4758 __ LoadConst32(AT, imm_low);
4759 __ Sltu(AT, lhs_low, AT);
4760 __ Blt(TMP, AT, label);
4761 break;
4762 case kCondGE:
4763 __ LoadConst32(TMP, imm_high);
4764 __ Blt(TMP, lhs_high, label);
4765 __ Slt(TMP, lhs_high, TMP);
4766 __ LoadConst32(AT, imm_low);
4767 __ Sltu(AT, lhs_low, AT);
4768 __ Or(TMP, TMP, AT);
4769 __ Beqz(TMP, label);
4770 break;
4771 case kCondLE:
4772 __ LoadConst32(TMP, imm_high);
4773 __ Blt(lhs_high, TMP, label);
4774 __ Slt(TMP, TMP, lhs_high);
4775 __ LoadConst32(AT, imm_low);
4776 __ Sltu(AT, AT, lhs_low);
4777 __ Or(TMP, TMP, AT);
4778 __ Beqz(TMP, label);
4779 break;
4780 case kCondGT:
4781 __ LoadConst32(TMP, imm_high);
4782 __ Blt(TMP, lhs_high, label);
4783 __ Slt(TMP, lhs_high, TMP);
4784 __ LoadConst32(AT, imm_low);
4785 __ Sltu(AT, AT, lhs_low);
4786 __ Blt(TMP, AT, label);
4787 break;
4788 case kCondB:
4789 __ LoadConst32(TMP, imm_high);
4790 __ Bltu(lhs_high, TMP, label);
4791 __ Sltu(TMP, TMP, lhs_high);
4792 __ LoadConst32(AT, imm_low);
4793 __ Sltu(AT, lhs_low, AT);
4794 __ Blt(TMP, AT, label);
4795 break;
4796 case kCondAE:
4797 __ LoadConst32(TMP, imm_high);
4798 __ Bltu(TMP, lhs_high, label);
4799 __ Sltu(TMP, lhs_high, TMP);
4800 __ LoadConst32(AT, imm_low);
4801 __ Sltu(AT, lhs_low, AT);
4802 __ Or(TMP, TMP, AT);
4803 __ Beqz(TMP, label);
4804 break;
4805 case kCondBE:
4806 __ LoadConst32(TMP, imm_high);
4807 __ Bltu(lhs_high, TMP, label);
4808 __ Sltu(TMP, TMP, lhs_high);
4809 __ LoadConst32(AT, imm_low);
4810 __ Sltu(AT, AT, lhs_low);
4811 __ Or(TMP, TMP, AT);
4812 __ Beqz(TMP, label);
4813 break;
4814 case kCondA:
4815 __ LoadConst32(TMP, imm_high);
4816 __ Bltu(TMP, lhs_high, label);
4817 __ Sltu(TMP, lhs_high, TMP);
4818 __ LoadConst32(AT, imm_low);
4819 __ Sltu(AT, AT, lhs_low);
4820 __ Blt(TMP, AT, label);
4821 break;
4822 }
4823 } else {
4824 switch (cond) {
4825 case kCondEQ:
4826 __ Xor(TMP, lhs_high, rhs_high);
4827 __ Xor(AT, lhs_low, rhs_low);
4828 __ Or(TMP, TMP, AT);
4829 __ Beqz(TMP, label);
4830 break;
4831 case kCondNE:
4832 __ Xor(TMP, lhs_high, rhs_high);
4833 __ Xor(AT, lhs_low, rhs_low);
4834 __ Or(TMP, TMP, AT);
4835 __ Bnez(TMP, label);
4836 break;
4837 case kCondLT:
4838 __ Blt(lhs_high, rhs_high, label);
4839 __ Slt(TMP, rhs_high, lhs_high);
4840 __ Sltu(AT, lhs_low, rhs_low);
4841 __ Blt(TMP, AT, label);
4842 break;
4843 case kCondGE:
4844 __ Blt(rhs_high, lhs_high, label);
4845 __ Slt(TMP, lhs_high, rhs_high);
4846 __ Sltu(AT, lhs_low, rhs_low);
4847 __ Or(TMP, TMP, AT);
4848 __ Beqz(TMP, label);
4849 break;
4850 case kCondLE:
4851 __ Blt(lhs_high, rhs_high, label);
4852 __ Slt(TMP, rhs_high, lhs_high);
4853 __ Sltu(AT, rhs_low, lhs_low);
4854 __ Or(TMP, TMP, AT);
4855 __ Beqz(TMP, label);
4856 break;
4857 case kCondGT:
4858 __ Blt(rhs_high, lhs_high, label);
4859 __ Slt(TMP, lhs_high, rhs_high);
4860 __ Sltu(AT, rhs_low, lhs_low);
4861 __ Blt(TMP, AT, label);
4862 break;
4863 case kCondB:
4864 __ Bltu(lhs_high, rhs_high, label);
4865 __ Sltu(TMP, rhs_high, lhs_high);
4866 __ Sltu(AT, lhs_low, rhs_low);
4867 __ Blt(TMP, AT, label);
4868 break;
4869 case kCondAE:
4870 __ Bltu(rhs_high, lhs_high, label);
4871 __ Sltu(TMP, lhs_high, rhs_high);
4872 __ Sltu(AT, lhs_low, rhs_low);
4873 __ Or(TMP, TMP, AT);
4874 __ Beqz(TMP, label);
4875 break;
4876 case kCondBE:
4877 __ Bltu(lhs_high, rhs_high, label);
4878 __ Sltu(TMP, rhs_high, lhs_high);
4879 __ Sltu(AT, rhs_low, lhs_low);
4880 __ Or(TMP, TMP, AT);
4881 __ Beqz(TMP, label);
4882 break;
4883 case kCondA:
4884 __ Bltu(rhs_high, lhs_high, label);
4885 __ Sltu(TMP, lhs_high, rhs_high);
4886 __ Sltu(AT, rhs_low, lhs_low);
4887 __ Blt(TMP, AT, label);
4888 break;
4889 }
4890 }
4891}
4892
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004893void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4894 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004895 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004896 LocationSummary* locations) {
4897 Register dst = locations->Out().AsRegister<Register>();
4898 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4899 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4900 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004901 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004902 if (isR6) {
4903 switch (cond) {
4904 case kCondEQ:
4905 __ CmpEqS(FTMP, lhs, rhs);
4906 __ Mfc1(dst, FTMP);
4907 __ Andi(dst, dst, 1);
4908 break;
4909 case kCondNE:
4910 __ CmpEqS(FTMP, lhs, rhs);
4911 __ Mfc1(dst, FTMP);
4912 __ Addiu(dst, dst, 1);
4913 break;
4914 case kCondLT:
4915 if (gt_bias) {
4916 __ CmpLtS(FTMP, lhs, rhs);
4917 } else {
4918 __ CmpUltS(FTMP, lhs, rhs);
4919 }
4920 __ Mfc1(dst, FTMP);
4921 __ Andi(dst, dst, 1);
4922 break;
4923 case kCondLE:
4924 if (gt_bias) {
4925 __ CmpLeS(FTMP, lhs, rhs);
4926 } else {
4927 __ CmpUleS(FTMP, lhs, rhs);
4928 }
4929 __ Mfc1(dst, FTMP);
4930 __ Andi(dst, dst, 1);
4931 break;
4932 case kCondGT:
4933 if (gt_bias) {
4934 __ CmpUltS(FTMP, rhs, lhs);
4935 } else {
4936 __ CmpLtS(FTMP, rhs, lhs);
4937 }
4938 __ Mfc1(dst, FTMP);
4939 __ Andi(dst, dst, 1);
4940 break;
4941 case kCondGE:
4942 if (gt_bias) {
4943 __ CmpUleS(FTMP, rhs, lhs);
4944 } else {
4945 __ CmpLeS(FTMP, rhs, lhs);
4946 }
4947 __ Mfc1(dst, FTMP);
4948 __ Andi(dst, dst, 1);
4949 break;
4950 default:
4951 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4952 UNREACHABLE();
4953 }
4954 } else {
4955 switch (cond) {
4956 case kCondEQ:
4957 __ CeqS(0, lhs, rhs);
4958 __ LoadConst32(dst, 1);
4959 __ Movf(dst, ZERO, 0);
4960 break;
4961 case kCondNE:
4962 __ CeqS(0, lhs, rhs);
4963 __ LoadConst32(dst, 1);
4964 __ Movt(dst, ZERO, 0);
4965 break;
4966 case kCondLT:
4967 if (gt_bias) {
4968 __ ColtS(0, lhs, rhs);
4969 } else {
4970 __ CultS(0, lhs, rhs);
4971 }
4972 __ LoadConst32(dst, 1);
4973 __ Movf(dst, ZERO, 0);
4974 break;
4975 case kCondLE:
4976 if (gt_bias) {
4977 __ ColeS(0, lhs, rhs);
4978 } else {
4979 __ CuleS(0, lhs, rhs);
4980 }
4981 __ LoadConst32(dst, 1);
4982 __ Movf(dst, ZERO, 0);
4983 break;
4984 case kCondGT:
4985 if (gt_bias) {
4986 __ CultS(0, rhs, lhs);
4987 } else {
4988 __ ColtS(0, rhs, lhs);
4989 }
4990 __ LoadConst32(dst, 1);
4991 __ Movf(dst, ZERO, 0);
4992 break;
4993 case kCondGE:
4994 if (gt_bias) {
4995 __ CuleS(0, rhs, lhs);
4996 } else {
4997 __ ColeS(0, rhs, lhs);
4998 }
4999 __ LoadConst32(dst, 1);
5000 __ Movf(dst, ZERO, 0);
5001 break;
5002 default:
5003 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5004 UNREACHABLE();
5005 }
5006 }
5007 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005008 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005009 if (isR6) {
5010 switch (cond) {
5011 case kCondEQ:
5012 __ CmpEqD(FTMP, lhs, rhs);
5013 __ Mfc1(dst, FTMP);
5014 __ Andi(dst, dst, 1);
5015 break;
5016 case kCondNE:
5017 __ CmpEqD(FTMP, lhs, rhs);
5018 __ Mfc1(dst, FTMP);
5019 __ Addiu(dst, dst, 1);
5020 break;
5021 case kCondLT:
5022 if (gt_bias) {
5023 __ CmpLtD(FTMP, lhs, rhs);
5024 } else {
5025 __ CmpUltD(FTMP, lhs, rhs);
5026 }
5027 __ Mfc1(dst, FTMP);
5028 __ Andi(dst, dst, 1);
5029 break;
5030 case kCondLE:
5031 if (gt_bias) {
5032 __ CmpLeD(FTMP, lhs, rhs);
5033 } else {
5034 __ CmpUleD(FTMP, lhs, rhs);
5035 }
5036 __ Mfc1(dst, FTMP);
5037 __ Andi(dst, dst, 1);
5038 break;
5039 case kCondGT:
5040 if (gt_bias) {
5041 __ CmpUltD(FTMP, rhs, lhs);
5042 } else {
5043 __ CmpLtD(FTMP, rhs, lhs);
5044 }
5045 __ Mfc1(dst, FTMP);
5046 __ Andi(dst, dst, 1);
5047 break;
5048 case kCondGE:
5049 if (gt_bias) {
5050 __ CmpUleD(FTMP, rhs, lhs);
5051 } else {
5052 __ CmpLeD(FTMP, rhs, lhs);
5053 }
5054 __ Mfc1(dst, FTMP);
5055 __ Andi(dst, dst, 1);
5056 break;
5057 default:
5058 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5059 UNREACHABLE();
5060 }
5061 } else {
5062 switch (cond) {
5063 case kCondEQ:
5064 __ CeqD(0, lhs, rhs);
5065 __ LoadConst32(dst, 1);
5066 __ Movf(dst, ZERO, 0);
5067 break;
5068 case kCondNE:
5069 __ CeqD(0, lhs, rhs);
5070 __ LoadConst32(dst, 1);
5071 __ Movt(dst, ZERO, 0);
5072 break;
5073 case kCondLT:
5074 if (gt_bias) {
5075 __ ColtD(0, lhs, rhs);
5076 } else {
5077 __ CultD(0, lhs, rhs);
5078 }
5079 __ LoadConst32(dst, 1);
5080 __ Movf(dst, ZERO, 0);
5081 break;
5082 case kCondLE:
5083 if (gt_bias) {
5084 __ ColeD(0, lhs, rhs);
5085 } else {
5086 __ CuleD(0, lhs, rhs);
5087 }
5088 __ LoadConst32(dst, 1);
5089 __ Movf(dst, ZERO, 0);
5090 break;
5091 case kCondGT:
5092 if (gt_bias) {
5093 __ CultD(0, rhs, lhs);
5094 } else {
5095 __ ColtD(0, rhs, lhs);
5096 }
5097 __ LoadConst32(dst, 1);
5098 __ Movf(dst, ZERO, 0);
5099 break;
5100 case kCondGE:
5101 if (gt_bias) {
5102 __ CuleD(0, rhs, lhs);
5103 } else {
5104 __ ColeD(0, rhs, lhs);
5105 }
5106 __ LoadConst32(dst, 1);
5107 __ Movf(dst, ZERO, 0);
5108 break;
5109 default:
5110 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5111 UNREACHABLE();
5112 }
5113 }
5114 }
5115}
5116
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005117bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5118 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005119 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005120 LocationSummary* input_locations,
5121 int cc) {
5122 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5123 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5124 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005125 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005126 switch (cond) {
5127 case kCondEQ:
5128 __ CeqS(cc, lhs, rhs);
5129 return false;
5130 case kCondNE:
5131 __ CeqS(cc, lhs, rhs);
5132 return true;
5133 case kCondLT:
5134 if (gt_bias) {
5135 __ ColtS(cc, lhs, rhs);
5136 } else {
5137 __ CultS(cc, lhs, rhs);
5138 }
5139 return false;
5140 case kCondLE:
5141 if (gt_bias) {
5142 __ ColeS(cc, lhs, rhs);
5143 } else {
5144 __ CuleS(cc, lhs, rhs);
5145 }
5146 return false;
5147 case kCondGT:
5148 if (gt_bias) {
5149 __ CultS(cc, rhs, lhs);
5150 } else {
5151 __ ColtS(cc, rhs, lhs);
5152 }
5153 return false;
5154 case kCondGE:
5155 if (gt_bias) {
5156 __ CuleS(cc, rhs, lhs);
5157 } else {
5158 __ ColeS(cc, rhs, lhs);
5159 }
5160 return false;
5161 default:
5162 LOG(FATAL) << "Unexpected non-floating-point condition";
5163 UNREACHABLE();
5164 }
5165 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005166 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005167 switch (cond) {
5168 case kCondEQ:
5169 __ CeqD(cc, lhs, rhs);
5170 return false;
5171 case kCondNE:
5172 __ CeqD(cc, lhs, rhs);
5173 return true;
5174 case kCondLT:
5175 if (gt_bias) {
5176 __ ColtD(cc, lhs, rhs);
5177 } else {
5178 __ CultD(cc, lhs, rhs);
5179 }
5180 return false;
5181 case kCondLE:
5182 if (gt_bias) {
5183 __ ColeD(cc, lhs, rhs);
5184 } else {
5185 __ CuleD(cc, lhs, rhs);
5186 }
5187 return false;
5188 case kCondGT:
5189 if (gt_bias) {
5190 __ CultD(cc, rhs, lhs);
5191 } else {
5192 __ ColtD(cc, rhs, lhs);
5193 }
5194 return false;
5195 case kCondGE:
5196 if (gt_bias) {
5197 __ CuleD(cc, rhs, lhs);
5198 } else {
5199 __ ColeD(cc, rhs, lhs);
5200 }
5201 return false;
5202 default:
5203 LOG(FATAL) << "Unexpected non-floating-point condition";
5204 UNREACHABLE();
5205 }
5206 }
5207}
5208
5209bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5210 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005211 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005212 LocationSummary* input_locations,
5213 FRegister dst) {
5214 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5215 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5216 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005218 switch (cond) {
5219 case kCondEQ:
5220 __ CmpEqS(dst, lhs, rhs);
5221 return false;
5222 case kCondNE:
5223 __ CmpEqS(dst, lhs, rhs);
5224 return true;
5225 case kCondLT:
5226 if (gt_bias) {
5227 __ CmpLtS(dst, lhs, rhs);
5228 } else {
5229 __ CmpUltS(dst, lhs, rhs);
5230 }
5231 return false;
5232 case kCondLE:
5233 if (gt_bias) {
5234 __ CmpLeS(dst, lhs, rhs);
5235 } else {
5236 __ CmpUleS(dst, lhs, rhs);
5237 }
5238 return false;
5239 case kCondGT:
5240 if (gt_bias) {
5241 __ CmpUltS(dst, rhs, lhs);
5242 } else {
5243 __ CmpLtS(dst, rhs, lhs);
5244 }
5245 return false;
5246 case kCondGE:
5247 if (gt_bias) {
5248 __ CmpUleS(dst, rhs, lhs);
5249 } else {
5250 __ CmpLeS(dst, rhs, lhs);
5251 }
5252 return false;
5253 default:
5254 LOG(FATAL) << "Unexpected non-floating-point condition";
5255 UNREACHABLE();
5256 }
5257 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005258 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005259 switch (cond) {
5260 case kCondEQ:
5261 __ CmpEqD(dst, lhs, rhs);
5262 return false;
5263 case kCondNE:
5264 __ CmpEqD(dst, lhs, rhs);
5265 return true;
5266 case kCondLT:
5267 if (gt_bias) {
5268 __ CmpLtD(dst, lhs, rhs);
5269 } else {
5270 __ CmpUltD(dst, lhs, rhs);
5271 }
5272 return false;
5273 case kCondLE:
5274 if (gt_bias) {
5275 __ CmpLeD(dst, lhs, rhs);
5276 } else {
5277 __ CmpUleD(dst, lhs, rhs);
5278 }
5279 return false;
5280 case kCondGT:
5281 if (gt_bias) {
5282 __ CmpUltD(dst, rhs, lhs);
5283 } else {
5284 __ CmpLtD(dst, rhs, lhs);
5285 }
5286 return false;
5287 case kCondGE:
5288 if (gt_bias) {
5289 __ CmpUleD(dst, rhs, lhs);
5290 } else {
5291 __ CmpLeD(dst, rhs, lhs);
5292 }
5293 return false;
5294 default:
5295 LOG(FATAL) << "Unexpected non-floating-point condition";
5296 UNREACHABLE();
5297 }
5298 }
5299}
5300
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005301void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5302 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005303 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005304 LocationSummary* locations,
5305 MipsLabel* label) {
5306 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5307 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5308 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005309 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005310 if (isR6) {
5311 switch (cond) {
5312 case kCondEQ:
5313 __ CmpEqS(FTMP, lhs, rhs);
5314 __ Bc1nez(FTMP, label);
5315 break;
5316 case kCondNE:
5317 __ CmpEqS(FTMP, lhs, rhs);
5318 __ Bc1eqz(FTMP, label);
5319 break;
5320 case kCondLT:
5321 if (gt_bias) {
5322 __ CmpLtS(FTMP, lhs, rhs);
5323 } else {
5324 __ CmpUltS(FTMP, lhs, rhs);
5325 }
5326 __ Bc1nez(FTMP, label);
5327 break;
5328 case kCondLE:
5329 if (gt_bias) {
5330 __ CmpLeS(FTMP, lhs, rhs);
5331 } else {
5332 __ CmpUleS(FTMP, lhs, rhs);
5333 }
5334 __ Bc1nez(FTMP, label);
5335 break;
5336 case kCondGT:
5337 if (gt_bias) {
5338 __ CmpUltS(FTMP, rhs, lhs);
5339 } else {
5340 __ CmpLtS(FTMP, rhs, lhs);
5341 }
5342 __ Bc1nez(FTMP, label);
5343 break;
5344 case kCondGE:
5345 if (gt_bias) {
5346 __ CmpUleS(FTMP, rhs, lhs);
5347 } else {
5348 __ CmpLeS(FTMP, rhs, lhs);
5349 }
5350 __ Bc1nez(FTMP, label);
5351 break;
5352 default:
5353 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005354 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005355 }
5356 } else {
5357 switch (cond) {
5358 case kCondEQ:
5359 __ CeqS(0, lhs, rhs);
5360 __ Bc1t(0, label);
5361 break;
5362 case kCondNE:
5363 __ CeqS(0, lhs, rhs);
5364 __ Bc1f(0, label);
5365 break;
5366 case kCondLT:
5367 if (gt_bias) {
5368 __ ColtS(0, lhs, rhs);
5369 } else {
5370 __ CultS(0, lhs, rhs);
5371 }
5372 __ Bc1t(0, label);
5373 break;
5374 case kCondLE:
5375 if (gt_bias) {
5376 __ ColeS(0, lhs, rhs);
5377 } else {
5378 __ CuleS(0, lhs, rhs);
5379 }
5380 __ Bc1t(0, label);
5381 break;
5382 case kCondGT:
5383 if (gt_bias) {
5384 __ CultS(0, rhs, lhs);
5385 } else {
5386 __ ColtS(0, rhs, lhs);
5387 }
5388 __ Bc1t(0, label);
5389 break;
5390 case kCondGE:
5391 if (gt_bias) {
5392 __ CuleS(0, rhs, lhs);
5393 } else {
5394 __ ColeS(0, rhs, lhs);
5395 }
5396 __ Bc1t(0, label);
5397 break;
5398 default:
5399 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005400 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005401 }
5402 }
5403 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005404 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005405 if (isR6) {
5406 switch (cond) {
5407 case kCondEQ:
5408 __ CmpEqD(FTMP, lhs, rhs);
5409 __ Bc1nez(FTMP, label);
5410 break;
5411 case kCondNE:
5412 __ CmpEqD(FTMP, lhs, rhs);
5413 __ Bc1eqz(FTMP, label);
5414 break;
5415 case kCondLT:
5416 if (gt_bias) {
5417 __ CmpLtD(FTMP, lhs, rhs);
5418 } else {
5419 __ CmpUltD(FTMP, lhs, rhs);
5420 }
5421 __ Bc1nez(FTMP, label);
5422 break;
5423 case kCondLE:
5424 if (gt_bias) {
5425 __ CmpLeD(FTMP, lhs, rhs);
5426 } else {
5427 __ CmpUleD(FTMP, lhs, rhs);
5428 }
5429 __ Bc1nez(FTMP, label);
5430 break;
5431 case kCondGT:
5432 if (gt_bias) {
5433 __ CmpUltD(FTMP, rhs, lhs);
5434 } else {
5435 __ CmpLtD(FTMP, rhs, lhs);
5436 }
5437 __ Bc1nez(FTMP, label);
5438 break;
5439 case kCondGE:
5440 if (gt_bias) {
5441 __ CmpUleD(FTMP, rhs, lhs);
5442 } else {
5443 __ CmpLeD(FTMP, rhs, lhs);
5444 }
5445 __ Bc1nez(FTMP, label);
5446 break;
5447 default:
5448 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005449 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005450 }
5451 } else {
5452 switch (cond) {
5453 case kCondEQ:
5454 __ CeqD(0, lhs, rhs);
5455 __ Bc1t(0, label);
5456 break;
5457 case kCondNE:
5458 __ CeqD(0, lhs, rhs);
5459 __ Bc1f(0, label);
5460 break;
5461 case kCondLT:
5462 if (gt_bias) {
5463 __ ColtD(0, lhs, rhs);
5464 } else {
5465 __ CultD(0, lhs, rhs);
5466 }
5467 __ Bc1t(0, label);
5468 break;
5469 case kCondLE:
5470 if (gt_bias) {
5471 __ ColeD(0, lhs, rhs);
5472 } else {
5473 __ CuleD(0, lhs, rhs);
5474 }
5475 __ Bc1t(0, label);
5476 break;
5477 case kCondGT:
5478 if (gt_bias) {
5479 __ CultD(0, rhs, lhs);
5480 } else {
5481 __ ColtD(0, rhs, lhs);
5482 }
5483 __ Bc1t(0, label);
5484 break;
5485 case kCondGE:
5486 if (gt_bias) {
5487 __ CuleD(0, rhs, lhs);
5488 } else {
5489 __ ColeD(0, rhs, lhs);
5490 }
5491 __ Bc1t(0, label);
5492 break;
5493 default:
5494 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005495 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005496 }
5497 }
5498 }
5499}
5500
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005501void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005502 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005503 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005504 MipsLabel* false_target) {
5505 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005506
David Brazdil0debae72015-11-12 18:37:00 +00005507 if (true_target == nullptr && false_target == nullptr) {
5508 // Nothing to do. The code always falls through.
5509 return;
5510 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005511 // Constant condition, statically compared against "true" (integer value 1).
5512 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005513 if (true_target != nullptr) {
5514 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005515 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005516 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005517 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005518 if (false_target != nullptr) {
5519 __ B(false_target);
5520 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005521 }
David Brazdil0debae72015-11-12 18:37:00 +00005522 return;
5523 }
5524
5525 // The following code generates these patterns:
5526 // (1) true_target == nullptr && false_target != nullptr
5527 // - opposite condition true => branch to false_target
5528 // (2) true_target != nullptr && false_target == nullptr
5529 // - condition true => branch to true_target
5530 // (3) true_target != nullptr && false_target != nullptr
5531 // - condition true => branch to true_target
5532 // - branch to false_target
5533 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005534 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005535 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005537 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005538 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5539 } else {
5540 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5541 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005542 } else {
5543 // The condition instruction has not been materialized, use its inputs as
5544 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005545 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005547 LocationSummary* locations = cond->GetLocations();
5548 IfCondition if_cond = condition->GetCondition();
5549 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005550
David Brazdil0debae72015-11-12 18:37:00 +00005551 if (true_target == nullptr) {
5552 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005553 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005554 }
5555
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005556 switch (type) {
5557 default:
5558 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5559 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005560 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005561 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5562 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 case DataType::Type::kFloat32:
5564 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005565 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5566 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005567 }
5568 }
David Brazdil0debae72015-11-12 18:37:00 +00005569
5570 // If neither branch falls through (case 3), the conditional branch to `true_target`
5571 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5572 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005573 __ B(false_target);
5574 }
5575}
5576
5577void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005578 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005579 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580 locations->SetInAt(0, Location::RequiresRegister());
5581 }
5582}
5583
5584void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005585 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5586 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5587 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5588 nullptr : codegen_->GetLabelOf(true_successor);
5589 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5590 nullptr : codegen_->GetLabelOf(false_successor);
5591 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005592}
5593
5594void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005595 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005596 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005597 InvokeRuntimeCallingConvention calling_convention;
5598 RegisterSet caller_saves = RegisterSet::Empty();
5599 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5600 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005601 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005602 locations->SetInAt(0, Location::RequiresRegister());
5603 }
5604}
5605
5606void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005607 SlowPathCodeMIPS* slow_path =
5608 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005609 GenerateTestAndBranch(deoptimize,
5610 /* condition_input_index */ 0,
5611 slow_path->GetEntryLabel(),
5612 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005613}
5614
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005615// This function returns true if a conditional move can be generated for HSelect.
5616// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5617// branches and regular moves.
5618//
5619// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5620//
5621// While determining feasibility of a conditional move and setting inputs/outputs
5622// are two distinct tasks, this function does both because they share quite a bit
5623// of common logic.
5624static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5625 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5626 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5627 HCondition* condition = cond->AsCondition();
5628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 DataType::Type cond_type =
5630 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5631 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005632
5633 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5634 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5635 bool is_true_value_zero_constant =
5636 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5637 bool is_false_value_zero_constant =
5638 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5639
5640 bool can_move_conditionally = false;
5641 bool use_const_for_false_in = false;
5642 bool use_const_for_true_in = false;
5643
5644 if (!cond->IsConstant()) {
5645 switch (cond_type) {
5646 default:
5647 switch (dst_type) {
5648 default:
5649 // Moving int on int condition.
5650 if (is_r6) {
5651 if (is_true_value_zero_constant) {
5652 // seleqz out_reg, false_reg, cond_reg
5653 can_move_conditionally = true;
5654 use_const_for_true_in = true;
5655 } else if (is_false_value_zero_constant) {
5656 // selnez out_reg, true_reg, cond_reg
5657 can_move_conditionally = true;
5658 use_const_for_false_in = true;
5659 } else if (materialized) {
5660 // Not materializing unmaterialized int conditions
5661 // to keep the instruction count low.
5662 // selnez AT, true_reg, cond_reg
5663 // seleqz TMP, false_reg, cond_reg
5664 // or out_reg, AT, TMP
5665 can_move_conditionally = true;
5666 }
5667 } else {
5668 // movn out_reg, true_reg/ZERO, cond_reg
5669 can_move_conditionally = true;
5670 use_const_for_true_in = is_true_value_zero_constant;
5671 }
5672 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005673 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005674 // Moving long on int condition.
5675 if (is_r6) {
5676 if (is_true_value_zero_constant) {
5677 // seleqz out_reg_lo, false_reg_lo, cond_reg
5678 // seleqz out_reg_hi, false_reg_hi, cond_reg
5679 can_move_conditionally = true;
5680 use_const_for_true_in = true;
5681 } else if (is_false_value_zero_constant) {
5682 // selnez out_reg_lo, true_reg_lo, cond_reg
5683 // selnez out_reg_hi, true_reg_hi, cond_reg
5684 can_move_conditionally = true;
5685 use_const_for_false_in = true;
5686 }
5687 // Other long conditional moves would generate 6+ instructions,
5688 // which is too many.
5689 } else {
5690 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5691 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5692 can_move_conditionally = true;
5693 use_const_for_true_in = is_true_value_zero_constant;
5694 }
5695 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005696 case DataType::Type::kFloat32:
5697 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005698 // Moving float/double on int condition.
5699 if (is_r6) {
5700 if (materialized) {
5701 // Not materializing unmaterialized int conditions
5702 // to keep the instruction count low.
5703 can_move_conditionally = true;
5704 if (is_true_value_zero_constant) {
5705 // sltu TMP, ZERO, cond_reg
5706 // mtc1 TMP, temp_cond_reg
5707 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5708 use_const_for_true_in = true;
5709 } else if (is_false_value_zero_constant) {
5710 // sltu TMP, ZERO, cond_reg
5711 // mtc1 TMP, temp_cond_reg
5712 // selnez.fmt out_reg, true_reg, temp_cond_reg
5713 use_const_for_false_in = true;
5714 } else {
5715 // sltu TMP, ZERO, cond_reg
5716 // mtc1 TMP, temp_cond_reg
5717 // sel.fmt temp_cond_reg, false_reg, true_reg
5718 // mov.fmt out_reg, temp_cond_reg
5719 }
5720 }
5721 } else {
5722 // movn.fmt out_reg, true_reg, cond_reg
5723 can_move_conditionally = true;
5724 }
5725 break;
5726 }
5727 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005729 // We don't materialize long comparison now
5730 // and use conditional branches instead.
5731 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005732 case DataType::Type::kFloat32:
5733 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005734 switch (dst_type) {
5735 default:
5736 // Moving int on float/double condition.
5737 if (is_r6) {
5738 if (is_true_value_zero_constant) {
5739 // mfc1 TMP, temp_cond_reg
5740 // seleqz out_reg, false_reg, TMP
5741 can_move_conditionally = true;
5742 use_const_for_true_in = true;
5743 } else if (is_false_value_zero_constant) {
5744 // mfc1 TMP, temp_cond_reg
5745 // selnez out_reg, true_reg, TMP
5746 can_move_conditionally = true;
5747 use_const_for_false_in = true;
5748 } else {
5749 // mfc1 TMP, temp_cond_reg
5750 // selnez AT, true_reg, TMP
5751 // seleqz TMP, false_reg, TMP
5752 // or out_reg, AT, TMP
5753 can_move_conditionally = true;
5754 }
5755 } else {
5756 // movt out_reg, true_reg/ZERO, cc
5757 can_move_conditionally = true;
5758 use_const_for_true_in = is_true_value_zero_constant;
5759 }
5760 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005761 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005762 // Moving long on float/double condition.
5763 if (is_r6) {
5764 if (is_true_value_zero_constant) {
5765 // mfc1 TMP, temp_cond_reg
5766 // seleqz out_reg_lo, false_reg_lo, TMP
5767 // seleqz out_reg_hi, false_reg_hi, TMP
5768 can_move_conditionally = true;
5769 use_const_for_true_in = true;
5770 } else if (is_false_value_zero_constant) {
5771 // mfc1 TMP, temp_cond_reg
5772 // selnez out_reg_lo, true_reg_lo, TMP
5773 // selnez out_reg_hi, true_reg_hi, TMP
5774 can_move_conditionally = true;
5775 use_const_for_false_in = true;
5776 }
5777 // Other long conditional moves would generate 6+ instructions,
5778 // which is too many.
5779 } else {
5780 // movt out_reg_lo, true_reg_lo/ZERO, cc
5781 // movt out_reg_hi, true_reg_hi/ZERO, cc
5782 can_move_conditionally = true;
5783 use_const_for_true_in = is_true_value_zero_constant;
5784 }
5785 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005786 case DataType::Type::kFloat32:
5787 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005788 // Moving float/double on float/double condition.
5789 if (is_r6) {
5790 can_move_conditionally = true;
5791 if (is_true_value_zero_constant) {
5792 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5793 use_const_for_true_in = true;
5794 } else if (is_false_value_zero_constant) {
5795 // selnez.fmt out_reg, true_reg, temp_cond_reg
5796 use_const_for_false_in = true;
5797 } else {
5798 // sel.fmt temp_cond_reg, false_reg, true_reg
5799 // mov.fmt out_reg, temp_cond_reg
5800 }
5801 } else {
5802 // movt.fmt out_reg, true_reg, cc
5803 can_move_conditionally = true;
5804 }
5805 break;
5806 }
5807 break;
5808 }
5809 }
5810
5811 if (can_move_conditionally) {
5812 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5813 } else {
5814 DCHECK(!use_const_for_false_in);
5815 DCHECK(!use_const_for_true_in);
5816 }
5817
5818 if (locations_to_set != nullptr) {
5819 if (use_const_for_false_in) {
5820 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5821 } else {
5822 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005823 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005824 ? Location::RequiresFpuRegister()
5825 : Location::RequiresRegister());
5826 }
5827 if (use_const_for_true_in) {
5828 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5829 } else {
5830 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005831 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005832 ? Location::RequiresFpuRegister()
5833 : Location::RequiresRegister());
5834 }
5835 if (materialized) {
5836 locations_to_set->SetInAt(2, Location::RequiresRegister());
5837 }
5838 // On R6 we don't require the output to be the same as the
5839 // first input for conditional moves unlike on R2.
5840 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5841 if (is_out_same_as_first_in) {
5842 locations_to_set->SetOut(Location::SameAsFirstInput());
5843 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005844 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005845 ? Location::RequiresFpuRegister()
5846 : Location::RequiresRegister());
5847 }
5848 }
5849
5850 return can_move_conditionally;
5851}
5852
5853void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5854 LocationSummary* locations = select->GetLocations();
5855 Location dst = locations->Out();
5856 Location src = locations->InAt(1);
5857 Register src_reg = ZERO;
5858 Register src_reg_high = ZERO;
5859 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5860 Register cond_reg = TMP;
5861 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005862 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005863 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005865
5866 if (IsBooleanValueOrMaterializedCondition(cond)) {
5867 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5868 } else {
5869 HCondition* condition = cond->AsCondition();
5870 LocationSummary* cond_locations = cond->GetLocations();
5871 IfCondition if_cond = condition->GetCondition();
5872 cond_type = condition->InputAt(0)->GetType();
5873 switch (cond_type) {
5874 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005875 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005876 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5877 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005878 case DataType::Type::kFloat32:
5879 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005880 cond_inverted = MaterializeFpCompareR2(if_cond,
5881 condition->IsGtBias(),
5882 cond_type,
5883 cond_locations,
5884 cond_cc);
5885 break;
5886 }
5887 }
5888
5889 DCHECK(dst.Equals(locations->InAt(0)));
5890 if (src.IsRegister()) {
5891 src_reg = src.AsRegister<Register>();
5892 } else if (src.IsRegisterPair()) {
5893 src_reg = src.AsRegisterPairLow<Register>();
5894 src_reg_high = src.AsRegisterPairHigh<Register>();
5895 } else if (src.IsConstant()) {
5896 DCHECK(src.GetConstant()->IsZeroBitPattern());
5897 }
5898
5899 switch (cond_type) {
5900 default:
5901 switch (dst_type) {
5902 default:
5903 if (cond_inverted) {
5904 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5905 } else {
5906 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5907 }
5908 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005909 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005910 if (cond_inverted) {
5911 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5912 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5913 } else {
5914 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5915 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5916 }
5917 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005918 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005919 if (cond_inverted) {
5920 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5921 } else {
5922 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5923 }
5924 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005925 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005926 if (cond_inverted) {
5927 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5928 } else {
5929 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5930 }
5931 break;
5932 }
5933 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005934 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005935 LOG(FATAL) << "Unreachable";
5936 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 case DataType::Type::kFloat32:
5938 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005939 switch (dst_type) {
5940 default:
5941 if (cond_inverted) {
5942 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5943 } else {
5944 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5945 }
5946 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005948 if (cond_inverted) {
5949 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5950 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5951 } else {
5952 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5953 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5954 }
5955 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005957 if (cond_inverted) {
5958 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5959 } else {
5960 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5961 }
5962 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005963 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005964 if (cond_inverted) {
5965 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5966 } else {
5967 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5968 }
5969 break;
5970 }
5971 break;
5972 }
5973}
5974
5975void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5976 LocationSummary* locations = select->GetLocations();
5977 Location dst = locations->Out();
5978 Location false_src = locations->InAt(0);
5979 Location true_src = locations->InAt(1);
5980 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5981 Register cond_reg = TMP;
5982 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005984 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005986
5987 if (IsBooleanValueOrMaterializedCondition(cond)) {
5988 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5989 } else {
5990 HCondition* condition = cond->AsCondition();
5991 LocationSummary* cond_locations = cond->GetLocations();
5992 IfCondition if_cond = condition->GetCondition();
5993 cond_type = condition->InputAt(0)->GetType();
5994 switch (cond_type) {
5995 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005996 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005997 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5998 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 case DataType::Type::kFloat32:
6000 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006001 cond_inverted = MaterializeFpCompareR6(if_cond,
6002 condition->IsGtBias(),
6003 cond_type,
6004 cond_locations,
6005 fcond_reg);
6006 break;
6007 }
6008 }
6009
6010 if (true_src.IsConstant()) {
6011 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6012 }
6013 if (false_src.IsConstant()) {
6014 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6015 }
6016
6017 switch (dst_type) {
6018 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006020 __ Mfc1(cond_reg, fcond_reg);
6021 }
6022 if (true_src.IsConstant()) {
6023 if (cond_inverted) {
6024 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6025 } else {
6026 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6027 }
6028 } else if (false_src.IsConstant()) {
6029 if (cond_inverted) {
6030 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6031 } else {
6032 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6033 }
6034 } else {
6035 DCHECK_NE(cond_reg, AT);
6036 if (cond_inverted) {
6037 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6038 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6039 } else {
6040 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6041 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6042 }
6043 __ Or(dst.AsRegister<Register>(), AT, TMP);
6044 }
6045 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006046 case DataType::Type::kInt64: {
6047 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006048 __ Mfc1(cond_reg, fcond_reg);
6049 }
6050 Register dst_lo = dst.AsRegisterPairLow<Register>();
6051 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6052 if (true_src.IsConstant()) {
6053 Register src_lo = false_src.AsRegisterPairLow<Register>();
6054 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6055 if (cond_inverted) {
6056 __ Selnez(dst_lo, src_lo, cond_reg);
6057 __ Selnez(dst_hi, src_hi, cond_reg);
6058 } else {
6059 __ Seleqz(dst_lo, src_lo, cond_reg);
6060 __ Seleqz(dst_hi, src_hi, cond_reg);
6061 }
6062 } else {
6063 DCHECK(false_src.IsConstant());
6064 Register src_lo = true_src.AsRegisterPairLow<Register>();
6065 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6066 if (cond_inverted) {
6067 __ Seleqz(dst_lo, src_lo, cond_reg);
6068 __ Seleqz(dst_hi, src_hi, cond_reg);
6069 } else {
6070 __ Selnez(dst_lo, src_lo, cond_reg);
6071 __ Selnez(dst_hi, src_hi, cond_reg);
6072 }
6073 }
6074 break;
6075 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 case DataType::Type::kFloat32: {
6077 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006078 // sel*.fmt tests bit 0 of the condition register, account for that.
6079 __ Sltu(TMP, ZERO, cond_reg);
6080 __ Mtc1(TMP, fcond_reg);
6081 }
6082 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6083 if (true_src.IsConstant()) {
6084 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6085 if (cond_inverted) {
6086 __ SelnezS(dst_reg, src_reg, fcond_reg);
6087 } else {
6088 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6089 }
6090 } else if (false_src.IsConstant()) {
6091 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6092 if (cond_inverted) {
6093 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6094 } else {
6095 __ SelnezS(dst_reg, src_reg, fcond_reg);
6096 }
6097 } else {
6098 if (cond_inverted) {
6099 __ SelS(fcond_reg,
6100 true_src.AsFpuRegister<FRegister>(),
6101 false_src.AsFpuRegister<FRegister>());
6102 } else {
6103 __ SelS(fcond_reg,
6104 false_src.AsFpuRegister<FRegister>(),
6105 true_src.AsFpuRegister<FRegister>());
6106 }
6107 __ MovS(dst_reg, fcond_reg);
6108 }
6109 break;
6110 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 case DataType::Type::kFloat64: {
6112 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006113 // sel*.fmt tests bit 0 of the condition register, account for that.
6114 __ Sltu(TMP, ZERO, cond_reg);
6115 __ Mtc1(TMP, fcond_reg);
6116 }
6117 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6118 if (true_src.IsConstant()) {
6119 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6120 if (cond_inverted) {
6121 __ SelnezD(dst_reg, src_reg, fcond_reg);
6122 } else {
6123 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6124 }
6125 } else if (false_src.IsConstant()) {
6126 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6127 if (cond_inverted) {
6128 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6129 } else {
6130 __ SelnezD(dst_reg, src_reg, fcond_reg);
6131 }
6132 } else {
6133 if (cond_inverted) {
6134 __ SelD(fcond_reg,
6135 true_src.AsFpuRegister<FRegister>(),
6136 false_src.AsFpuRegister<FRegister>());
6137 } else {
6138 __ SelD(fcond_reg,
6139 false_src.AsFpuRegister<FRegister>(),
6140 true_src.AsFpuRegister<FRegister>());
6141 }
6142 __ MovD(dst_reg, fcond_reg);
6143 }
6144 break;
6145 }
6146 }
6147}
6148
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006149void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006150 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006151 LocationSummary(flag, LocationSummary::kNoCall);
6152 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006153}
6154
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006155void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6156 __ LoadFromOffset(kLoadWord,
6157 flag->GetLocations()->Out().AsRegister<Register>(),
6158 SP,
6159 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006160}
6161
David Brazdil74eb1b22015-12-14 11:44:01 +00006162void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006163 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006164 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006165}
6166
6167void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006168 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6169 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6170 if (is_r6) {
6171 GenConditionalMoveR6(select);
6172 } else {
6173 GenConditionalMoveR2(select);
6174 }
6175 } else {
6176 LocationSummary* locations = select->GetLocations();
6177 MipsLabel false_target;
6178 GenerateTestAndBranch(select,
6179 /* condition_input_index */ 2,
6180 /* true_target */ nullptr,
6181 &false_target);
6182 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6183 __ Bind(&false_target);
6184 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006185}
6186
David Srbecky0cf44932015-12-09 14:09:59 +00006187void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006188 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006189}
6190
David Srbeckyd28f4a02016-03-14 17:14:24 +00006191void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6192 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006193}
6194
6195void CodeGeneratorMIPS::GenerateNop() {
6196 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006197}
6198
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006199void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006200 DataType::Type field_type = field_info.GetFieldType();
6201 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006202 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006203 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006204 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006205 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006206 instruction,
6207 generate_volatile
6208 ? LocationSummary::kCallOnMainOnly
6209 : (object_field_get_with_read_barrier
6210 ? LocationSummary::kCallOnSlowPath
6211 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006212
Alexey Frunzec61c0762017-04-10 13:54:23 -07006213 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6214 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6215 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006216 locations->SetInAt(0, Location::RequiresRegister());
6217 if (generate_volatile) {
6218 InvokeRuntimeCallingConvention calling_convention;
6219 // need A0 to hold base + offset
6220 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006221 if (field_type == DataType::Type::kInt64) {
6222 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006223 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006224 // Use Location::Any() to prevent situations when running out of available fp registers.
6225 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006226 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006228 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6229 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6230 }
6231 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006232 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006233 locations->SetOut(Location::RequiresFpuRegister());
6234 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006235 // The output overlaps in the case of an object field get with
6236 // read barriers enabled: we do not want the move to overwrite the
6237 // object's location, as we need it to emit the read barrier.
6238 locations->SetOut(Location::RequiresRegister(),
6239 object_field_get_with_read_barrier
6240 ? Location::kOutputOverlap
6241 : Location::kNoOutputOverlap);
6242 }
6243 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6244 // We need a temporary register for the read barrier marking slow
6245 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006246 if (!kBakerReadBarrierThunksEnableForFields) {
6247 locations->AddTemp(Location::RequiresRegister());
6248 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 }
6250 }
6251}
6252
6253void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6254 const FieldInfo& field_info,
6255 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006256 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6257 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006258 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006259 Location obj_loc = locations->InAt(0);
6260 Register obj = obj_loc.AsRegister<Register>();
6261 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006262 LoadOperandType load_type = kLoadUnsignedByte;
6263 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006264 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006265 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266
6267 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006268 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006269 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006270 load_type = kLoadUnsignedByte;
6271 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006273 load_type = kLoadSignedByte;
6274 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006275 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276 load_type = kLoadUnsignedHalfword;
6277 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006278 case DataType::Type::kInt16:
6279 load_type = kLoadSignedHalfword;
6280 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006281 case DataType::Type::kInt32:
6282 case DataType::Type::kFloat32:
6283 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006284 load_type = kLoadWord;
6285 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006286 case DataType::Type::kInt64:
6287 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006288 load_type = kLoadDoubleword;
6289 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006290 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006291 LOG(FATAL) << "Unreachable type " << type;
6292 UNREACHABLE();
6293 }
6294
6295 if (is_volatile && load_type == kLoadDoubleword) {
6296 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006297 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006298 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006299 __ LoadFromOffset(kLoadWord,
6300 ZERO,
6301 locations->GetTemp(0).AsRegister<Register>(),
6302 0,
6303 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006304 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006305 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006307 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006308 if (dst_loc.IsFpuRegister()) {
6309 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006310 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006311 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006312 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006313 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006314 __ StoreToOffset(kStoreWord,
6315 locations->GetTemp(1).AsRegister<Register>(),
6316 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006317 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006318 __ StoreToOffset(kStoreWord,
6319 locations->GetTemp(2).AsRegister<Register>(),
6320 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006321 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006322 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006323 }
6324 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006325 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006326 // /* HeapReference<Object> */ dst = *(obj + offset)
6327 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006328 Location temp_loc =
6329 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006330 // Note that a potential implicit null check is handled in this
6331 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6332 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6333 dst_loc,
6334 obj,
6335 offset,
6336 temp_loc,
6337 /* needs_null_check */ true);
6338 if (is_volatile) {
6339 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6340 }
6341 } else {
6342 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6343 if (is_volatile) {
6344 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6345 }
6346 // If read barriers are enabled, emit read barriers other than
6347 // Baker's using a slow path (and also unpoison the loaded
6348 // reference, if heap poisoning is enabled).
6349 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6350 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006351 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006352 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006353 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006354 DCHECK(dst_loc.IsRegisterPair());
6355 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006356 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006357 DCHECK(dst_loc.IsRegister());
6358 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006359 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006360 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006361 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006362 DCHECK(dst_loc.IsFpuRegister());
6363 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006365 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006366 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006367 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006368 }
6369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 }
6371
Alexey Frunze15958152017-02-09 19:08:30 -08006372 // Memory barriers, in the case of references, are handled in the
6373 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006374 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006375 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6376 }
6377}
6378
6379void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006380 DataType::Type field_type = field_info.GetFieldType();
6381 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006382 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006383 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006384 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385
6386 locations->SetInAt(0, Location::RequiresRegister());
6387 if (generate_volatile) {
6388 InvokeRuntimeCallingConvention calling_convention;
6389 // need A0 to hold base + offset
6390 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 locations->SetInAt(1, Location::RegisterPairLocation(
6393 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6394 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006395 // Use Location::Any() to prevent situations when running out of available fp registers.
6396 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006397 // Pass FP parameters in core registers.
6398 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6399 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6400 }
6401 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006403 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006404 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006405 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006406 }
6407 }
6408}
6409
6410void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6411 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006412 uint32_t dex_pc,
6413 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006415 LocationSummary* locations = instruction->GetLocations();
6416 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006417 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006418 StoreOperandType store_type = kStoreByte;
6419 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006420 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006421 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006422 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006423
6424 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006425 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006426 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006427 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006428 store_type = kStoreByte;
6429 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006430 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006431 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006432 store_type = kStoreHalfword;
6433 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006434 case DataType::Type::kInt32:
6435 case DataType::Type::kFloat32:
6436 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 store_type = kStoreWord;
6438 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 case DataType::Type::kInt64:
6440 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006441 store_type = kStoreDoubleword;
6442 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006443 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006444 LOG(FATAL) << "Unreachable type " << type;
6445 UNREACHABLE();
6446 }
6447
6448 if (is_volatile) {
6449 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6450 }
6451
6452 if (is_volatile && store_type == kStoreDoubleword) {
6453 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006454 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006455 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006456 __ LoadFromOffset(kLoadWord,
6457 ZERO,
6458 locations->GetTemp(0).AsRegister<Register>(),
6459 0,
6460 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006461 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006462 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006463 if (value_location.IsFpuRegister()) {
6464 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6465 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006466 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006467 value_location.AsFpuRegister<FRegister>());
6468 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006469 __ LoadFromOffset(kLoadWord,
6470 locations->GetTemp(1).AsRegister<Register>(),
6471 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006472 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006473 __ LoadFromOffset(kLoadWord,
6474 locations->GetTemp(2).AsRegister<Register>(),
6475 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006476 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006477 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006478 DCHECK(value_location.IsConstant());
6479 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6480 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006481 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6482 locations->GetTemp(1).AsRegister<Register>(),
6483 value);
6484 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006485 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006486 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006487 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6488 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006489 if (value_location.IsConstant()) {
6490 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6491 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006492 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006493 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006494 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006495 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006496 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006497 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006498 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006499 if (kPoisonHeapReferences && needs_write_barrier) {
6500 // Note that in the case where `value` is a null reference,
6501 // we do not enter this block, as a null reference does not
6502 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006503 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006504 __ PoisonHeapReference(TMP, src);
6505 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6506 } else {
6507 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006509 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006510 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006511 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006512 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006513 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006514 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006515 }
6516 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006517 }
6518
Alexey Frunzec061de12017-02-14 13:27:23 -08006519 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006520 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006521 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522 }
6523
6524 if (is_volatile) {
6525 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6526 }
6527}
6528
6529void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6530 HandleFieldGet(instruction, instruction->GetFieldInfo());
6531}
6532
6533void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6534 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6535}
6536
6537void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6538 HandleFieldSet(instruction, instruction->GetFieldInfo());
6539}
6540
6541void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006542 HandleFieldSet(instruction,
6543 instruction->GetFieldInfo(),
6544 instruction->GetDexPc(),
6545 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546}
6547
Alexey Frunze15958152017-02-09 19:08:30 -08006548void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6549 HInstruction* instruction,
6550 Location out,
6551 uint32_t offset,
6552 Location maybe_temp,
6553 ReadBarrierOption read_barrier_option) {
6554 Register out_reg = out.AsRegister<Register>();
6555 if (read_barrier_option == kWithReadBarrier) {
6556 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006557 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6558 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6559 }
Alexey Frunze15958152017-02-09 19:08:30 -08006560 if (kUseBakerReadBarrier) {
6561 // Load with fast path based Baker's read barrier.
6562 // /* HeapReference<Object> */ out = *(out + offset)
6563 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6564 out,
6565 out_reg,
6566 offset,
6567 maybe_temp,
6568 /* needs_null_check */ false);
6569 } else {
6570 // Load with slow path based read barrier.
6571 // Save the value of `out` into `maybe_temp` before overwriting it
6572 // in the following move operation, as we will need it for the
6573 // read barrier below.
6574 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6575 // /* HeapReference<Object> */ out = *(out + offset)
6576 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6577 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6578 }
6579 } else {
6580 // Plain load with no read barrier.
6581 // /* HeapReference<Object> */ out = *(out + offset)
6582 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6583 __ MaybeUnpoisonHeapReference(out_reg);
6584 }
6585}
6586
6587void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6588 HInstruction* instruction,
6589 Location out,
6590 Location obj,
6591 uint32_t offset,
6592 Location maybe_temp,
6593 ReadBarrierOption read_barrier_option) {
6594 Register out_reg = out.AsRegister<Register>();
6595 Register obj_reg = obj.AsRegister<Register>();
6596 if (read_barrier_option == kWithReadBarrier) {
6597 CHECK(kEmitCompilerReadBarrier);
6598 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006599 if (!kBakerReadBarrierThunksEnableForFields) {
6600 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6601 }
Alexey Frunze15958152017-02-09 19:08:30 -08006602 // Load with fast path based Baker's read barrier.
6603 // /* HeapReference<Object> */ out = *(obj + offset)
6604 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6605 out,
6606 obj_reg,
6607 offset,
6608 maybe_temp,
6609 /* needs_null_check */ false);
6610 } else {
6611 // Load with slow path based read barrier.
6612 // /* HeapReference<Object> */ out = *(obj + offset)
6613 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6614 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6615 }
6616 } else {
6617 // Plain load with no read barrier.
6618 // /* HeapReference<Object> */ out = *(obj + offset)
6619 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6620 __ MaybeUnpoisonHeapReference(out_reg);
6621 }
6622}
6623
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006624static inline int GetBakerMarkThunkNumber(Register reg) {
6625 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6626 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6627 return reg - V0;
6628 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6629 return 14 + (reg - S2);
6630 } else if (reg == FP) { // One more.
6631 return 20;
6632 }
6633 LOG(FATAL) << "Unexpected register " << reg;
6634 UNREACHABLE();
6635}
6636
6637static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6638 int num = GetBakerMarkThunkNumber(reg) +
6639 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6640 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6641}
6642
6643static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6644 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6645 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6646}
6647
Alexey Frunze15958152017-02-09 19:08:30 -08006648void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6649 Location root,
6650 Register obj,
6651 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006652 ReadBarrierOption read_barrier_option,
6653 MipsLabel* label_low) {
6654 bool reordering;
6655 if (label_low != nullptr) {
6656 DCHECK_EQ(offset, 0x5678u);
6657 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006658 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006659 if (read_barrier_option == kWithReadBarrier) {
6660 DCHECK(kEmitCompilerReadBarrier);
6661 if (kUseBakerReadBarrier) {
6662 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6663 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006664 if (kBakerReadBarrierThunksEnableForGcRoots) {
6665 // Note that we do not actually check the value of `GetIsGcMarking()`
6666 // to decide whether to mark the loaded GC root or not. Instead, we
6667 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6668 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6669 // vice versa.
6670 //
6671 // We use thunks for the slow path. That thunk checks the reference
6672 // and jumps to the entrypoint if needed.
6673 //
6674 // temp = Thread::Current()->pReadBarrierMarkReg00
6675 // // AKA &art_quick_read_barrier_mark_introspection.
6676 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6677 // if (temp != nullptr) {
6678 // temp = &gc_root_thunk<root_reg>
6679 // root = temp(root)
6680 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006681
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006682 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6683 const int32_t entry_point_offset =
6684 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6685 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6686 int16_t offset_low = Low16Bits(offset);
6687 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6688 // extension in lw.
6689 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6690 Register base = short_offset ? obj : TMP;
6691 // Loading the entrypoint does not require a load acquire since it is only changed when
6692 // threads are suspended or running a checkpoint.
6693 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6694 reordering = __ SetReorder(false);
6695 if (!short_offset) {
6696 DCHECK(!label_low);
6697 __ AddUpper(base, obj, offset_high);
6698 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006699 MipsLabel skip_call;
6700 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006701 if (label_low != nullptr) {
6702 DCHECK(short_offset);
6703 __ Bind(label_low);
6704 }
6705 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6706 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6707 // in delay slot.
6708 if (isR6) {
6709 __ Jialc(T9, thunk_disp);
6710 } else {
6711 __ Addiu(T9, T9, thunk_disp);
6712 __ Jalr(T9);
6713 __ Nop();
6714 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006715 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006716 __ SetReorder(reordering);
6717 } else {
6718 // Note that we do not actually check the value of `GetIsGcMarking()`
6719 // to decide whether to mark the loaded GC root or not. Instead, we
6720 // load into `temp` (T9) the read barrier mark entry point corresponding
6721 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6722 // is false, and vice versa.
6723 //
6724 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6725 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6726 // if (temp != null) {
6727 // root = temp(root)
6728 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006729
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006730 if (label_low != nullptr) {
6731 reordering = __ SetReorder(false);
6732 __ Bind(label_low);
6733 }
6734 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6735 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6736 if (label_low != nullptr) {
6737 __ SetReorder(reordering);
6738 }
6739 static_assert(
6740 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6741 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6742 "have different sizes.");
6743 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6744 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6745 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006746
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006747 // Slow path marking the GC root `root`.
6748 Location temp = Location::RegisterLocation(T9);
6749 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006750 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006751 instruction,
6752 root,
6753 /*entrypoint*/ temp);
6754 codegen_->AddSlowPath(slow_path);
6755
6756 const int32_t entry_point_offset =
6757 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6758 // Loading the entrypoint does not require a load acquire since it is only changed when
6759 // threads are suspended or running a checkpoint.
6760 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6761 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6762 __ Bind(slow_path->GetExitLabel());
6763 }
Alexey Frunze15958152017-02-09 19:08:30 -08006764 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006765 if (label_low != nullptr) {
6766 reordering = __ SetReorder(false);
6767 __ Bind(label_low);
6768 }
Alexey Frunze15958152017-02-09 19:08:30 -08006769 // GC root loaded through a slow path for read barriers other
6770 // than Baker's.
6771 // /* GcRoot<mirror::Object>* */ root = obj + offset
6772 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006773 if (label_low != nullptr) {
6774 __ SetReorder(reordering);
6775 }
Alexey Frunze15958152017-02-09 19:08:30 -08006776 // /* mirror::Object* */ root = root->Read()
6777 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6778 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006779 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006780 if (label_low != nullptr) {
6781 reordering = __ SetReorder(false);
6782 __ Bind(label_low);
6783 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006784 // Plain GC root load with no read barrier.
6785 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6786 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6787 // Note that GC roots are not affected by heap poisoning, thus we
6788 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006789 if (label_low != nullptr) {
6790 __ SetReorder(reordering);
6791 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006792 }
6793}
6794
Alexey Frunze15958152017-02-09 19:08:30 -08006795void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6796 Location ref,
6797 Register obj,
6798 uint32_t offset,
6799 Location temp,
6800 bool needs_null_check) {
6801 DCHECK(kEmitCompilerReadBarrier);
6802 DCHECK(kUseBakerReadBarrier);
6803
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006804 if (kBakerReadBarrierThunksEnableForFields) {
6805 // Note that we do not actually check the value of `GetIsGcMarking()`
6806 // to decide whether to mark the loaded reference or not. Instead, we
6807 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6808 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6809 // vice versa.
6810 //
6811 // We use thunks for the slow path. That thunk checks the reference
6812 // and jumps to the entrypoint if needed. If the holder is not gray,
6813 // it issues a load-load memory barrier and returns to the original
6814 // reference load.
6815 //
6816 // temp = Thread::Current()->pReadBarrierMarkReg00
6817 // // AKA &art_quick_read_barrier_mark_introspection.
6818 // if (temp != nullptr) {
6819 // temp = &field_array_thunk<holder_reg>
6820 // temp()
6821 // }
6822 // not_gray_return_address:
6823 // // If the offset is too large to fit into the lw instruction, we
6824 // // use an adjusted base register (TMP) here. This register
6825 // // receives bits 16 ... 31 of the offset before the thunk invocation
6826 // // and the thunk benefits from it.
6827 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6828 // gray_return_address:
6829
6830 DCHECK(temp.IsInvalid());
6831 bool isR6 = GetInstructionSetFeatures().IsR6();
6832 int16_t offset_low = Low16Bits(offset);
6833 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6834 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6835 bool reordering = __ SetReorder(false);
6836 const int32_t entry_point_offset =
6837 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6838 // There may have or may have not been a null check if the field offset is smaller than
6839 // the page size.
6840 // There must've been a null check in case it's actually a load from an array.
6841 // We will, however, perform an explicit null check in the thunk as it's easier to
6842 // do it than not.
6843 if (instruction->IsArrayGet()) {
6844 DCHECK(!needs_null_check);
6845 }
6846 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6847 // Loading the entrypoint does not require a load acquire since it is only changed when
6848 // threads are suspended or running a checkpoint.
6849 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6850 Register ref_reg = ref.AsRegister<Register>();
6851 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006852 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006853 if (short_offset) {
6854 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006855 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006856 __ Nop(); // In forbidden slot.
6857 __ Jialc(T9, thunk_disp);
6858 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006859 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006860 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6861 __ Jalr(T9);
6862 __ Nop(); // In delay slot.
6863 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006864 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006865 } else {
6866 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006867 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006868 __ Aui(base, obj, offset_high); // In delay slot.
6869 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006870 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006871 } else {
6872 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006873 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006874 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6875 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006876 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006877 __ Addu(base, base, obj); // In delay slot.
6878 }
6879 }
6880 // /* HeapReference<Object> */ ref = *(obj + offset)
6881 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6882 if (needs_null_check) {
6883 MaybeRecordImplicitNullCheck(instruction);
6884 }
6885 __ MaybeUnpoisonHeapReference(ref_reg);
6886 __ SetReorder(reordering);
6887 return;
6888 }
6889
Alexey Frunze15958152017-02-09 19:08:30 -08006890 // /* HeapReference<Object> */ ref = *(obj + offset)
6891 Location no_index = Location::NoLocation();
6892 ScaleFactor no_scale_factor = TIMES_1;
6893 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6894 ref,
6895 obj,
6896 offset,
6897 no_index,
6898 no_scale_factor,
6899 temp,
6900 needs_null_check);
6901}
6902
6903void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6904 Location ref,
6905 Register obj,
6906 uint32_t data_offset,
6907 Location index,
6908 Location temp,
6909 bool needs_null_check) {
6910 DCHECK(kEmitCompilerReadBarrier);
6911 DCHECK(kUseBakerReadBarrier);
6912
6913 static_assert(
6914 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6915 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006916 ScaleFactor scale_factor = TIMES_4;
6917
6918 if (kBakerReadBarrierThunksEnableForArrays) {
6919 // Note that we do not actually check the value of `GetIsGcMarking()`
6920 // to decide whether to mark the loaded reference or not. Instead, we
6921 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6922 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6923 // vice versa.
6924 //
6925 // We use thunks for the slow path. That thunk checks the reference
6926 // and jumps to the entrypoint if needed. If the holder is not gray,
6927 // it issues a load-load memory barrier and returns to the original
6928 // reference load.
6929 //
6930 // temp = Thread::Current()->pReadBarrierMarkReg00
6931 // // AKA &art_quick_read_barrier_mark_introspection.
6932 // if (temp != nullptr) {
6933 // temp = &field_array_thunk<holder_reg>
6934 // temp()
6935 // }
6936 // not_gray_return_address:
6937 // // The element address is pre-calculated in the TMP register before the
6938 // // thunk invocation and the thunk benefits from it.
6939 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6940 // gray_return_address:
6941
6942 DCHECK(temp.IsInvalid());
6943 DCHECK(index.IsValid());
6944 bool reordering = __ SetReorder(false);
6945 const int32_t entry_point_offset =
6946 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6947 // We will not do the explicit null check in the thunk as some form of a null check
6948 // must've been done earlier.
6949 DCHECK(!needs_null_check);
6950 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6951 // Loading the entrypoint does not require a load acquire since it is only changed when
6952 // threads are suspended or running a checkpoint.
6953 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6954 Register ref_reg = ref.AsRegister<Register>();
6955 Register index_reg = index.IsRegisterPair()
6956 ? index.AsRegisterPairLow<Register>()
6957 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006958 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006959 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006960 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006961 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6962 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006963 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006964 } else {
6965 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006966 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006967 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6968 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006969 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006970 __ Addu(TMP, TMP, obj); // In delay slot.
6971 }
6972 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6973 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6974 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6975 __ MaybeUnpoisonHeapReference(ref_reg);
6976 __ SetReorder(reordering);
6977 return;
6978 }
6979
Alexey Frunze15958152017-02-09 19:08:30 -08006980 // /* HeapReference<Object> */ ref =
6981 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006982 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6983 ref,
6984 obj,
6985 data_offset,
6986 index,
6987 scale_factor,
6988 temp,
6989 needs_null_check);
6990}
6991
6992void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6993 Location ref,
6994 Register obj,
6995 uint32_t offset,
6996 Location index,
6997 ScaleFactor scale_factor,
6998 Location temp,
6999 bool needs_null_check,
7000 bool always_update_field) {
7001 DCHECK(kEmitCompilerReadBarrier);
7002 DCHECK(kUseBakerReadBarrier);
7003
7004 // In slow path based read barriers, the read barrier call is
7005 // inserted after the original load. However, in fast path based
7006 // Baker's read barriers, we need to perform the load of
7007 // mirror::Object::monitor_ *before* the original reference load.
7008 // This load-load ordering is required by the read barrier.
7009 // The fast path/slow path (for Baker's algorithm) should look like:
7010 //
7011 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7012 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7013 // HeapReference<Object> ref = *src; // Original reference load.
7014 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7015 // if (is_gray) {
7016 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7017 // }
7018 //
7019 // Note: the original implementation in ReadBarrier::Barrier is
7020 // slightly more complex as it performs additional checks that we do
7021 // not do here for performance reasons.
7022
7023 Register ref_reg = ref.AsRegister<Register>();
7024 Register temp_reg = temp.AsRegister<Register>();
7025 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7026
7027 // /* int32_t */ monitor = obj->monitor_
7028 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7029 if (needs_null_check) {
7030 MaybeRecordImplicitNullCheck(instruction);
7031 }
7032 // /* LockWord */ lock_word = LockWord(monitor)
7033 static_assert(sizeof(LockWord) == sizeof(int32_t),
7034 "art::LockWord and int32_t have different sizes.");
7035
7036 __ Sync(0); // Barrier to prevent load-load reordering.
7037
7038 // The actual reference load.
7039 if (index.IsValid()) {
7040 // Load types involving an "index": ArrayGet,
7041 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7042 // intrinsics.
7043 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7044 if (index.IsConstant()) {
7045 size_t computed_offset =
7046 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7047 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7048 } else {
7049 // Handle the special case of the
7050 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7051 // intrinsics, which use a register pair as index ("long
7052 // offset"), of which only the low part contains data.
7053 Register index_reg = index.IsRegisterPair()
7054 ? index.AsRegisterPairLow<Register>()
7055 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007056 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007057 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7058 }
7059 } else {
7060 // /* HeapReference<Object> */ ref = *(obj + offset)
7061 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7062 }
7063
7064 // Object* ref = ref_addr->AsMirrorPtr()
7065 __ MaybeUnpoisonHeapReference(ref_reg);
7066
7067 // Slow path marking the object `ref` when it is gray.
7068 SlowPathCodeMIPS* slow_path;
7069 if (always_update_field) {
7070 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7071 // of the form `obj + field_offset`, where `obj` is a register and
7072 // `field_offset` is a register pair (of which only the lower half
7073 // is used). Thus `offset` and `scale_factor` above are expected
7074 // to be null in this code path.
7075 DCHECK_EQ(offset, 0u);
7076 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007077 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007078 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7079 ref,
7080 obj,
7081 /* field_offset */ index,
7082 temp_reg);
7083 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007084 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007085 }
7086 AddSlowPath(slow_path);
7087
7088 // if (rb_state == ReadBarrier::GrayState())
7089 // ref = ReadBarrier::Mark(ref);
7090 // Given the numeric representation, it's enough to check the low bit of the
7091 // rb_state. We do that by shifting the bit into the sign bit (31) and
7092 // performing a branch on less than zero.
7093 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7094 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7095 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7096 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7097 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7098 __ Bind(slow_path->GetExitLabel());
7099}
7100
7101void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7102 Location out,
7103 Location ref,
7104 Location obj,
7105 uint32_t offset,
7106 Location index) {
7107 DCHECK(kEmitCompilerReadBarrier);
7108
7109 // Insert a slow path based read barrier *after* the reference load.
7110 //
7111 // If heap poisoning is enabled, the unpoisoning of the loaded
7112 // reference will be carried out by the runtime within the slow
7113 // path.
7114 //
7115 // Note that `ref` currently does not get unpoisoned (when heap
7116 // poisoning is enabled), which is alright as the `ref` argument is
7117 // not used by the artReadBarrierSlow entry point.
7118 //
7119 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007120 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007121 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7122 AddSlowPath(slow_path);
7123
7124 __ B(slow_path->GetEntryLabel());
7125 __ Bind(slow_path->GetExitLabel());
7126}
7127
7128void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7129 Location out,
7130 Location ref,
7131 Location obj,
7132 uint32_t offset,
7133 Location index) {
7134 if (kEmitCompilerReadBarrier) {
7135 // Baker's read barriers shall be handled by the fast path
7136 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7137 DCHECK(!kUseBakerReadBarrier);
7138 // If heap poisoning is enabled, unpoisoning will be taken care of
7139 // by the runtime within the slow path.
7140 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7141 } else if (kPoisonHeapReferences) {
7142 __ UnpoisonHeapReference(out.AsRegister<Register>());
7143 }
7144}
7145
7146void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7147 Location out,
7148 Location root) {
7149 DCHECK(kEmitCompilerReadBarrier);
7150
7151 // Insert a slow path based read barrier *after* the GC root load.
7152 //
7153 // Note that GC roots are not affected by heap poisoning, so we do
7154 // not need to do anything special for this here.
7155 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007156 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007157 AddSlowPath(slow_path);
7158
7159 __ B(slow_path->GetEntryLabel());
7160 __ Bind(slow_path->GetExitLabel());
7161}
7162
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007163void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007164 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7165 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007166 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007167 switch (type_check_kind) {
7168 case TypeCheckKind::kExactCheck:
7169 case TypeCheckKind::kAbstractClassCheck:
7170 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007171 case TypeCheckKind::kArrayObjectCheck: {
7172 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7173 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7174 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007175 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007176 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007177 case TypeCheckKind::kArrayCheck:
7178 case TypeCheckKind::kUnresolvedCheck:
7179 case TypeCheckKind::kInterfaceCheck:
7180 call_kind = LocationSummary::kCallOnSlowPath;
7181 break;
7182 }
7183
Vladimir Markoca6fff82017-10-03 14:49:14 +01007184 LocationSummary* locations =
7185 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007186 if (baker_read_barrier_slow_path) {
7187 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7188 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007189 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007190 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007191 // The output does overlap inputs.
7192 // Note that TypeCheckSlowPathMIPS uses this register too.
7193 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007194 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007195}
7196
7197void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007198 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007200 Location obj_loc = locations->InAt(0);
7201 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007202 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007203 Location out_loc = locations->Out();
7204 Register out = out_loc.AsRegister<Register>();
7205 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7206 DCHECK_LE(num_temps, 1u);
7207 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007208 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7209 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7210 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7211 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007212 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007213 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007214
7215 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007216 // Avoid this check if we know `obj` is not null.
7217 if (instruction->MustDoNullCheck()) {
7218 __ Move(out, ZERO);
7219 __ Beqz(obj, &done);
7220 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007221
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007222 switch (type_check_kind) {
7223 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007224 ReadBarrierOption read_barrier_option =
7225 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007226 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007227 GenerateReferenceLoadTwoRegisters(instruction,
7228 out_loc,
7229 obj_loc,
7230 class_offset,
7231 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007232 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007233 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007234 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007235 __ Sltiu(out, out, 1);
7236 break;
7237 }
7238
7239 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007240 ReadBarrierOption read_barrier_option =
7241 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007242 // /* 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,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007248 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007249 // If the class is abstract, we eagerly fetch the super class of the
7250 // object to avoid doing a comparison we know will fail.
7251 MipsLabel loop;
7252 __ Bind(&loop);
7253 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007254 GenerateReferenceLoadOneRegister(instruction,
7255 out_loc,
7256 super_offset,
7257 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007258 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007259 // If `out` is null, we use it for the result, and jump to `done`.
7260 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007261 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007262 __ LoadConst32(out, 1);
7263 break;
7264 }
7265
7266 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007267 ReadBarrierOption read_barrier_option =
7268 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007269 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007270 GenerateReferenceLoadTwoRegisters(instruction,
7271 out_loc,
7272 obj_loc,
7273 class_offset,
7274 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007275 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007276 // Walk over the class hierarchy to find a match.
7277 MipsLabel loop, success;
7278 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007279 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007280 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007281 GenerateReferenceLoadOneRegister(instruction,
7282 out_loc,
7283 super_offset,
7284 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007285 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007286 __ Bnez(out, &loop);
7287 // If `out` is null, we use it for the result, and jump to `done`.
7288 __ B(&done);
7289 __ Bind(&success);
7290 __ LoadConst32(out, 1);
7291 break;
7292 }
7293
7294 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007295 ReadBarrierOption read_barrier_option =
7296 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007297 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007298 GenerateReferenceLoadTwoRegisters(instruction,
7299 out_loc,
7300 obj_loc,
7301 class_offset,
7302 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007303 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007304 // Do an exact check.
7305 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007306 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007307 // Otherwise, we need to check that the object's class is a non-primitive array.
7308 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007309 GenerateReferenceLoadOneRegister(instruction,
7310 out_loc,
7311 component_offset,
7312 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007313 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007314 // If `out` is null, we use it for the result, and jump to `done`.
7315 __ Beqz(out, &done);
7316 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7317 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7318 __ Sltiu(out, out, 1);
7319 __ B(&done);
7320 __ Bind(&success);
7321 __ LoadConst32(out, 1);
7322 break;
7323 }
7324
7325 case TypeCheckKind::kArrayCheck: {
7326 // No read barrier since the slow path will retry upon failure.
7327 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007328 GenerateReferenceLoadTwoRegisters(instruction,
7329 out_loc,
7330 obj_loc,
7331 class_offset,
7332 maybe_temp_loc,
7333 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007334 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007335 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7336 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007337 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007338 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007339 __ LoadConst32(out, 1);
7340 break;
7341 }
7342
7343 case TypeCheckKind::kUnresolvedCheck:
7344 case TypeCheckKind::kInterfaceCheck: {
7345 // Note that we indeed only call on slow path, but we always go
7346 // into the slow path for the unresolved and interface check
7347 // cases.
7348 //
7349 // We cannot directly call the InstanceofNonTrivial runtime
7350 // entry point without resorting to a type checking slow path
7351 // here (i.e. by calling InvokeRuntime directly), as it would
7352 // require to assign fixed registers for the inputs of this
7353 // HInstanceOf instruction (following the runtime calling
7354 // convention), which might be cluttered by the potential first
7355 // read barrier emission at the beginning of this method.
7356 //
7357 // TODO: Introduce a new runtime entry point taking the object
7358 // to test (instead of its class) as argument, and let it deal
7359 // with the read barrier issues. This will let us refactor this
7360 // case of the `switch` code as it was previously (with a direct
7361 // call to the runtime not using a type checking slow path).
7362 // This should also be beneficial for the other cases above.
7363 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007364 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7365 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007366 codegen_->AddSlowPath(slow_path);
7367 __ B(slow_path->GetEntryLabel());
7368 break;
7369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007370 }
7371
7372 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007373
7374 if (slow_path != nullptr) {
7375 __ Bind(slow_path->GetExitLabel());
7376 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007377}
7378
7379void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007380 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007381 locations->SetOut(Location::ConstantLocation(constant));
7382}
7383
7384void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7385 // Will be generated at use site.
7386}
7387
7388void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007389 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007390 locations->SetOut(Location::ConstantLocation(constant));
7391}
7392
7393void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7394 // Will be generated at use site.
7395}
7396
7397void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7398 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7399 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7400}
7401
7402void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7403 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007404 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007405 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007406 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007407}
7408
7409void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7410 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7411 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007412 Location receiver = invoke->GetLocations()->InAt(0);
7413 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007414 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007415
7416 // Set the hidden argument.
7417 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7418 invoke->GetDexMethodIndex());
7419
7420 // temp = object->GetClass();
7421 if (receiver.IsStackSlot()) {
7422 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7423 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7424 } else {
7425 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7426 }
7427 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007428 // Instead of simply (possibly) unpoisoning `temp` here, we should
7429 // emit a read barrier for the previous class reference load.
7430 // However this is not required in practice, as this is an
7431 // intermediate/temporary reference and because the current
7432 // concurrent copying collector keeps the from-space memory
7433 // intact/accessible until the end of the marking phase (the
7434 // concurrent copying collector may not in the future).
7435 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007436 __ LoadFromOffset(kLoadWord, temp, temp,
7437 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7438 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007439 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007440 // temp = temp->GetImtEntryAt(method_offset);
7441 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7442 // T9 = temp->GetEntryPoint();
7443 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7444 // T9();
7445 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007446 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007447 DCHECK(!codegen_->IsLeafMethod());
7448 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7449}
7450
7451void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007452 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7453 if (intrinsic.TryDispatch(invoke)) {
7454 return;
7455 }
7456
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007457 HandleInvoke(invoke);
7458}
7459
7460void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007461 // Explicit clinit checks triggered by static invokes must have been pruned by
7462 // art::PrepareForRegisterAllocation.
7463 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007464
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007465 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007466 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7467 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007468
Chris Larsen701566a2015-10-27 15:29:13 -07007469 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7470 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007471 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7472 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7473 }
Chris Larsen701566a2015-10-27 15:29:13 -07007474 return;
7475 }
7476
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007477 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007478
7479 // Add the extra input register if either the dex cache array base register
7480 // or the PC-relative base register for accessing literals is needed.
7481 if (has_extra_input) {
7482 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7483 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007484}
7485
Orion Hodsonac141392017-01-13 11:53:47 +00007486void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7487 HandleInvoke(invoke);
7488}
7489
7490void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7491 codegen_->GenerateInvokePolymorphicCall(invoke);
7492}
7493
Chris Larsen701566a2015-10-27 15:29:13 -07007494static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007495 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007496 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7497 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007498 return true;
7499 }
7500 return false;
7501}
7502
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007503HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007504 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007505 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007506 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007507 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007508 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007509 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007510 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007511 case HLoadString::LoadKind::kJitTableAddress:
7512 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007513 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007514 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007515 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007516 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007517 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007518 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007519}
7520
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007521HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7522 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007523 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007524 case HLoadClass::LoadKind::kInvalid:
7525 LOG(FATAL) << "UNREACHABLE";
7526 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007527 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007528 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007529 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007530 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007531 case HLoadClass::LoadKind::kBssEntry:
7532 DCHECK(!Runtime::Current()->UseJitCompilation());
7533 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007534 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007535 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007536 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007537 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007538 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007539 break;
7540 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007541 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007542}
7543
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007544Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7545 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007546 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007547 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007548 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7549 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7550 if (!invoke->GetLocations()->Intrinsified()) {
7551 return location.AsRegister<Register>();
7552 }
7553 // For intrinsics we allow any location, so it may be on the stack.
7554 if (!location.IsRegister()) {
7555 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7556 return temp;
7557 }
7558 // For register locations, check if the register was saved. If so, get it from the stack.
7559 // Note: There is a chance that the register was saved but not overwritten, so we could
7560 // save one load. However, since this is just an intrinsic slow path we prefer this
7561 // simple and more robust approach rather that trying to determine if that's the case.
7562 SlowPathCode* slow_path = GetCurrentSlowPath();
7563 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7564 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7565 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7566 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7567 return temp;
7568 }
7569 return location.AsRegister<Register>();
7570}
7571
Vladimir Markodc151b22015-10-15 18:02:30 +01007572HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7573 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007574 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007575 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007576}
7577
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007578void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7579 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007580 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007581 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007582 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7583 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007584 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007585 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7586 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007587 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7588 : ZERO;
7589
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007590 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007591 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007592 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007593 uint32_t offset =
7594 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007595 __ LoadFromOffset(kLoadWord,
7596 temp.AsRegister<Register>(),
7597 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007598 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007599 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007600 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007601 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007602 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007603 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007604 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7605 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007606 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7607 PcRelativePatchInfo* info_low =
7608 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007609 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007610 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7611 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007612 break;
7613 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007614 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7615 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7616 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007617 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007618 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007619 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007620 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7621 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007622 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007623 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7624 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007625 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007626 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007627 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7628 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7629 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007630 }
7631 }
7632
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007633 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007634 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007635 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007636 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007637 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7638 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007639 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640 T9,
7641 callee_method.AsRegister<Register>(),
7642 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007643 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007644 // T9()
7645 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007646 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007647 break;
7648 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007649 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7650
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007651 DCHECK(!IsLeafMethod());
7652}
7653
7654void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007655 // Explicit clinit checks triggered by static invokes must have been pruned by
7656 // art::PrepareForRegisterAllocation.
7657 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007658
7659 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7660 return;
7661 }
7662
7663 LocationSummary* locations = invoke->GetLocations();
7664 codegen_->GenerateStaticOrDirectCall(invoke,
7665 locations->HasTemps()
7666 ? locations->GetTemp(0)
7667 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668}
7669
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007670void CodeGeneratorMIPS::GenerateVirtualCall(
7671 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007672 // Use the calling convention instead of the location of the receiver, as
7673 // intrinsics may have put the receiver in a different register. In the intrinsics
7674 // slow path, the arguments have been moved to the right place, so here we are
7675 // guaranteed that the receiver is the first register of the calling convention.
7676 InvokeDexCallingConvention calling_convention;
7677 Register receiver = calling_convention.GetRegisterAt(0);
7678
Chris Larsen3acee732015-11-18 13:31:08 -08007679 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007680 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7681 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7682 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007683 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007684
7685 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007686 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007687 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007688 // Instead of simply (possibly) unpoisoning `temp` here, we should
7689 // emit a read barrier for the previous class reference load.
7690 // However this is not required in practice, as this is an
7691 // intermediate/temporary reference and because the current
7692 // concurrent copying collector keeps the from-space memory
7693 // intact/accessible until the end of the marking phase (the
7694 // concurrent copying collector may not in the future).
7695 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007696 // temp = temp->GetMethodAt(method_offset);
7697 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7698 // T9 = temp->GetEntryPoint();
7699 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7700 // T9();
7701 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007702 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007703 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007704}
7705
7706void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7707 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7708 return;
7709 }
7710
7711 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007712 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007713}
7714
7715void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007716 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007717 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007718 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007719 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7720 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007721 return;
7722 }
Vladimir Marko41559982017-01-06 14:04:23 +00007723 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007724 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007725 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007726 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7727 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007728 ? LocationSummary::kCallOnSlowPath
7729 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007730 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007731 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7732 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7733 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007734 switch (load_kind) {
7735 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007736 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007737 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007738 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007739 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007740 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007741 break;
7742 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007743 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007744 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7745 codegen_->ClobberRA();
7746 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007747 break;
7748 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007749 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007750 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007751 locations->SetInAt(0, Location::RequiresRegister());
7752 break;
7753 default:
7754 break;
7755 }
7756 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007757 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7758 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7759 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007760 RegisterSet caller_saves = RegisterSet::Empty();
7761 InvokeRuntimeCallingConvention calling_convention;
7762 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7763 locations->SetCustomSlowPathCallerSaves(caller_saves);
7764 } else {
7765 // For non-Baker read barriers we have a temp-clobbering call.
7766 }
7767 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007768}
7769
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007770// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7771// move.
7772void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007773 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007774 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007775 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007776 return;
7777 }
Vladimir Marko41559982017-01-06 14:04:23 +00007778 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007779
Vladimir Marko41559982017-01-06 14:04:23 +00007780 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007781 Location out_loc = locations->Out();
7782 Register out = out_loc.AsRegister<Register>();
7783 Register base_or_current_method_reg;
7784 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007785 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007786 switch (load_kind) {
7787 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007788 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007789 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007790 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007791 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007792 base_or_current_method_reg =
7793 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007794 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007795 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007796 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007797 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7798 break;
7799 default:
7800 base_or_current_method_reg = ZERO;
7801 break;
7802 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007803
Alexey Frunze15958152017-02-09 19:08:30 -08007804 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7805 ? kWithoutReadBarrier
7806 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007807 bool generate_null_check = false;
7808 switch (load_kind) {
7809 case HLoadClass::LoadKind::kReferrersClass: {
7810 DCHECK(!cls->CanCallRuntime());
7811 DCHECK(!cls->MustGenerateClinitCheck());
7812 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7813 GenerateGcRootFieldLoad(cls,
7814 out_loc,
7815 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007816 ArtMethod::DeclaringClassOffset().Int32Value(),
7817 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007818 break;
7819 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007820 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007821 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007822 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007823 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007824 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007825 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7826 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007827 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7828 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007829 base_or_current_method_reg);
7830 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007831 break;
7832 }
7833 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007834 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007835 uint32_t address = dchecked_integral_cast<uint32_t>(
7836 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7837 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007838 if (isR6 || !has_irreducible_loops) {
7839 __ LoadLiteral(out,
7840 base_or_current_method_reg,
7841 codegen_->DeduplicateBootImageAddressLiteral(address));
7842 } else {
7843 __ LoadConst32(out, address);
7844 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007845 break;
7846 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007847 case HLoadClass::LoadKind::kBootImageClassTable: {
7848 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7849 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7850 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7851 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7852 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7853 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7854 out,
7855 base_or_current_method_reg);
7856 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7857 // Extract the reference from the slot data, i.e. clear the hash bits.
7858 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7859 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7860 if (masked_hash != 0) {
7861 __ Addiu(out, out, -masked_hash);
7862 }
7863 break;
7864 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007865 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007866 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7867 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007868 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7869 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007870 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007871 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007872 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007873 GenerateGcRootFieldLoad(cls,
7874 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007875 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007876 /* placeholder */ 0x5678,
7877 read_barrier_option,
7878 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007879 generate_null_check = true;
7880 break;
7881 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007882 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007883 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7884 cls->GetTypeIndex(),
7885 cls->GetClass());
7886 bool reordering = __ SetReorder(false);
7887 __ Bind(&info->high_label);
7888 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007889 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007890 GenerateGcRootFieldLoad(cls,
7891 out_loc,
7892 out,
7893 /* placeholder */ 0x5678,
7894 read_barrier_option,
7895 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007896 break;
7897 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007898 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007899 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007900 LOG(FATAL) << "UNREACHABLE";
7901 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007902 }
7903
7904 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7905 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007906 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007907 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007908 codegen_->AddSlowPath(slow_path);
7909 if (generate_null_check) {
7910 __ Beqz(out, slow_path->GetEntryLabel());
7911 }
7912 if (cls->MustGenerateClinitCheck()) {
7913 GenerateClassInitializationCheck(slow_path, out);
7914 } else {
7915 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007916 }
7917 }
7918}
7919
7920static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007921 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007922}
7923
7924void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7925 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007926 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007927 locations->SetOut(Location::RequiresRegister());
7928}
7929
7930void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7931 Register out = load->GetLocations()->Out().AsRegister<Register>();
7932 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7933}
7934
7935void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007936 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007937}
7938
7939void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7940 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7941}
7942
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007943void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007944 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007945 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007946 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007947 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007948 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007949 switch (load_kind) {
7950 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007951 case HLoadString::LoadKind::kBootImageAddress:
7952 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007953 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007954 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007955 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007956 break;
7957 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007958 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007959 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7960 codegen_->ClobberRA();
7961 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007962 break;
7963 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007964 FALLTHROUGH_INTENDED;
7965 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007966 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007967 locations->SetInAt(0, Location::RequiresRegister());
7968 break;
7969 default:
7970 break;
7971 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007972 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007973 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007974 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007975 } else {
7976 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007977 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7978 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7979 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007980 RegisterSet caller_saves = RegisterSet::Empty();
7981 InvokeRuntimeCallingConvention calling_convention;
7982 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7983 locations->SetCustomSlowPathCallerSaves(caller_saves);
7984 } else {
7985 // For non-Baker read barriers we have a temp-clobbering call.
7986 }
7987 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007988 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007989}
7990
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007991// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7992// move.
7993void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007994 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007995 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007996 Location out_loc = locations->Out();
7997 Register out = out_loc.AsRegister<Register>();
7998 Register base_or_current_method_reg;
7999 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008000 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008001 switch (load_kind) {
8002 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008003 case HLoadString::LoadKind::kBootImageAddress:
8004 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008005 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008006 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008007 base_or_current_method_reg =
8008 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008009 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008010 default:
8011 base_or_current_method_reg = ZERO;
8012 break;
8013 }
8014
8015 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008016 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008017 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008018 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008019 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008020 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8021 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008022 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8023 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008024 base_or_current_method_reg);
8025 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008026 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008027 }
8028 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008029 uint32_t address = dchecked_integral_cast<uint32_t>(
8030 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8031 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008032 if (isR6 || !has_irreducible_loops) {
8033 __ LoadLiteral(out,
8034 base_or_current_method_reg,
8035 codegen_->DeduplicateBootImageAddressLiteral(address));
8036 } else {
8037 __ LoadConst32(out, address);
8038 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008039 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008040 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008041 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008042 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008043 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008044 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008045 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8046 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008047 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8048 out,
8049 base_or_current_method_reg);
8050 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8051 return;
8052 }
8053 case HLoadString::LoadKind::kBssEntry: {
8054 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8055 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8056 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8057 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8058 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008059 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008060 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008061 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008062 GenerateGcRootFieldLoad(load,
8063 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008064 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008065 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008066 kCompilerReadBarrierOption,
8067 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008068 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008069 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008070 codegen_->AddSlowPath(slow_path);
8071 __ Beqz(out, slow_path->GetEntryLabel());
8072 __ Bind(slow_path->GetExitLabel());
8073 return;
8074 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008075 case HLoadString::LoadKind::kJitTableAddress: {
8076 CodeGeneratorMIPS::JitPatchInfo* info =
8077 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8078 load->GetStringIndex(),
8079 load->GetString());
8080 bool reordering = __ SetReorder(false);
8081 __ Bind(&info->high_label);
8082 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008083 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008084 GenerateGcRootFieldLoad(load,
8085 out_loc,
8086 out,
8087 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008088 kCompilerReadBarrierOption,
8089 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008090 return;
8091 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008092 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008093 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008094 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008095
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008096 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008097 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008098 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008099 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008100 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008101 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8102 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008103}
8104
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008105void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008106 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008107 locations->SetOut(Location::ConstantLocation(constant));
8108}
8109
8110void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8111 // Will be generated at use site.
8112}
8113
8114void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008115 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8116 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008117 InvokeRuntimeCallingConvention calling_convention;
8118 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8119}
8120
8121void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8122 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008123 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008124 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8125 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008126 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008127 }
8128 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8129}
8130
8131void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8132 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008133 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008134 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008135 case DataType::Type::kInt32:
8136 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008137 locations->SetInAt(0, Location::RequiresRegister());
8138 locations->SetInAt(1, Location::RequiresRegister());
8139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8140 break;
8141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008142 case DataType::Type::kFloat32:
8143 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008144 locations->SetInAt(0, Location::RequiresFpuRegister());
8145 locations->SetInAt(1, Location::RequiresFpuRegister());
8146 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8147 break;
8148
8149 default:
8150 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8151 }
8152}
8153
8154void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008155 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008156 LocationSummary* locations = instruction->GetLocations();
8157 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8158
8159 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008160 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008161 Register dst = locations->Out().AsRegister<Register>();
8162 Register lhs = locations->InAt(0).AsRegister<Register>();
8163 Register rhs = locations->InAt(1).AsRegister<Register>();
8164
8165 if (isR6) {
8166 __ MulR6(dst, lhs, rhs);
8167 } else {
8168 __ MulR2(dst, lhs, rhs);
8169 }
8170 break;
8171 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008172 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008173 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8174 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8175 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8176 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8177 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8178 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8179
8180 // Extra checks to protect caused by the existance of A1_A2.
8181 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8182 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8183 DCHECK_NE(dst_high, lhs_low);
8184 DCHECK_NE(dst_high, rhs_low);
8185
8186 // A_B * C_D
8187 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8188 // dst_lo: [ low(B*D) ]
8189 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8190
8191 if (isR6) {
8192 __ MulR6(TMP, lhs_high, rhs_low);
8193 __ MulR6(dst_high, lhs_low, rhs_high);
8194 __ Addu(dst_high, dst_high, TMP);
8195 __ MuhuR6(TMP, lhs_low, rhs_low);
8196 __ Addu(dst_high, dst_high, TMP);
8197 __ MulR6(dst_low, lhs_low, rhs_low);
8198 } else {
8199 __ MulR2(TMP, lhs_high, rhs_low);
8200 __ MulR2(dst_high, lhs_low, rhs_high);
8201 __ Addu(dst_high, dst_high, TMP);
8202 __ MultuR2(lhs_low, rhs_low);
8203 __ Mfhi(TMP);
8204 __ Addu(dst_high, dst_high, TMP);
8205 __ Mflo(dst_low);
8206 }
8207 break;
8208 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008209 case DataType::Type::kFloat32:
8210 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008211 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8212 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8213 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008214 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008215 __ MulS(dst, lhs, rhs);
8216 } else {
8217 __ MulD(dst, lhs, rhs);
8218 }
8219 break;
8220 }
8221 default:
8222 LOG(FATAL) << "Unexpected mul type " << type;
8223 }
8224}
8225
8226void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8227 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008228 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008229 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008230 case DataType::Type::kInt32:
8231 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008232 locations->SetInAt(0, Location::RequiresRegister());
8233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8234 break;
8235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008236 case DataType::Type::kFloat32:
8237 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008238 locations->SetInAt(0, Location::RequiresFpuRegister());
8239 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8240 break;
8241
8242 default:
8243 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8244 }
8245}
8246
8247void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008248 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008249 LocationSummary* locations = instruction->GetLocations();
8250
8251 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008252 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008253 Register dst = locations->Out().AsRegister<Register>();
8254 Register src = locations->InAt(0).AsRegister<Register>();
8255 __ Subu(dst, ZERO, src);
8256 break;
8257 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008258 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008259 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8260 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8261 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8262 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8263 __ Subu(dst_low, ZERO, src_low);
8264 __ Sltu(TMP, ZERO, dst_low);
8265 __ Subu(dst_high, ZERO, src_high);
8266 __ Subu(dst_high, dst_high, TMP);
8267 break;
8268 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008269 case DataType::Type::kFloat32:
8270 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008271 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8272 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008273 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008274 __ NegS(dst, src);
8275 } else {
8276 __ NegD(dst, src);
8277 }
8278 break;
8279 }
8280 default:
8281 LOG(FATAL) << "Unexpected neg type " << type;
8282 }
8283}
8284
8285void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008286 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8287 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008288 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008289 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8291 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008292}
8293
8294void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008295 // Note: if heap poisoning is enabled, the entry point takes care
8296 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008297 QuickEntrypointEnum entrypoint =
8298 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8299 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008300 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008301 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008302}
8303
8304void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008305 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8306 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008307 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008308 if (instruction->IsStringAlloc()) {
8309 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8310 } else {
8311 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008312 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008313 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008314}
8315
8316void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008317 // Note: if heap poisoning is enabled, the entry point takes care
8318 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008319 if (instruction->IsStringAlloc()) {
8320 // String is allocated through StringFactory. Call NewEmptyString entry point.
8321 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008322 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008323 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8324 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8325 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008326 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008327 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8328 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008329 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008330 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008331 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008332}
8333
8334void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008335 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008336 locations->SetInAt(0, Location::RequiresRegister());
8337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8338}
8339
8340void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008341 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342 LocationSummary* locations = instruction->GetLocations();
8343
8344 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008345 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008346 Register dst = locations->Out().AsRegister<Register>();
8347 Register src = locations->InAt(0).AsRegister<Register>();
8348 __ Nor(dst, src, ZERO);
8349 break;
8350 }
8351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008352 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008353 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8354 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8355 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8356 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8357 __ Nor(dst_high, src_high, ZERO);
8358 __ Nor(dst_low, src_low, ZERO);
8359 break;
8360 }
8361
8362 default:
8363 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8364 }
8365}
8366
8367void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008368 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008369 locations->SetInAt(0, Location::RequiresRegister());
8370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8371}
8372
8373void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8374 LocationSummary* locations = instruction->GetLocations();
8375 __ Xori(locations->Out().AsRegister<Register>(),
8376 locations->InAt(0).AsRegister<Register>(),
8377 1);
8378}
8379
8380void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008381 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8382 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008383}
8384
Calin Juravle2ae48182016-03-16 14:05:09 +00008385void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8386 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008387 return;
8388 }
8389 Location obj = instruction->GetLocations()->InAt(0);
8390
8391 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008392 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008393}
8394
Calin Juravle2ae48182016-03-16 14:05:09 +00008395void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008396 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008397 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008398
8399 Location obj = instruction->GetLocations()->InAt(0);
8400
8401 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8402}
8403
8404void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008405 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008406}
8407
8408void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8409 HandleBinaryOp(instruction);
8410}
8411
8412void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8413 HandleBinaryOp(instruction);
8414}
8415
8416void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8417 LOG(FATAL) << "Unreachable";
8418}
8419
8420void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008421 if (instruction->GetNext()->IsSuspendCheck() &&
8422 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8423 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8424 // The back edge will generate the suspend check.
8425 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8426 }
8427
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008428 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8429}
8430
8431void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008432 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008433 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8434 if (location.IsStackSlot()) {
8435 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8436 } else if (location.IsDoubleStackSlot()) {
8437 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8438 }
8439 locations->SetOut(location);
8440}
8441
8442void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8443 ATTRIBUTE_UNUSED) {
8444 // Nothing to do, the parameter is already at its location.
8445}
8446
8447void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008449 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008450 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8451}
8452
8453void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8454 ATTRIBUTE_UNUSED) {
8455 // Nothing to do, the method is already at its location.
8456}
8457
8458void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008459 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008460 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008461 locations->SetInAt(i, Location::Any());
8462 }
8463 locations->SetOut(Location::Any());
8464}
8465
8466void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8467 LOG(FATAL) << "Unreachable";
8468}
8469
8470void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008471 DataType::Type type = rem->GetResultType();
8472 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8473 ? LocationSummary::kNoCall
8474 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008475 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008476
8477 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008478 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008479 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008480 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8482 break;
8483
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008484 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485 InvokeRuntimeCallingConvention calling_convention;
8486 locations->SetInAt(0, Location::RegisterPairLocation(
8487 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8488 locations->SetInAt(1, Location::RegisterPairLocation(
8489 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8490 locations->SetOut(calling_convention.GetReturnLocation(type));
8491 break;
8492 }
8493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 case DataType::Type::kFloat32:
8495 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496 InvokeRuntimeCallingConvention calling_convention;
8497 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8498 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8499 locations->SetOut(calling_convention.GetReturnLocation(type));
8500 break;
8501 }
8502
8503 default:
8504 LOG(FATAL) << "Unexpected rem type " << type;
8505 }
8506}
8507
8508void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008509 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008510
8511 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008512 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008513 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008514 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008515 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008516 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008517 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8518 break;
8519 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008520 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008521 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008522 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008523 break;
8524 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008525 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008526 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008527 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008528 break;
8529 }
8530 default:
8531 LOG(FATAL) << "Unexpected rem type " << type;
8532 }
8533}
8534
Igor Murashkind01745e2017-04-05 16:40:31 -07008535void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8536 constructor_fence->SetLocations(nullptr);
8537}
8538
8539void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8540 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8541 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8542}
8543
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008544void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8545 memory_barrier->SetLocations(nullptr);
8546}
8547
8548void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8549 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8550}
8551
8552void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008553 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008554 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008555 locations->SetInAt(0, MipsReturnLocation(return_type));
8556}
8557
8558void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8559 codegen_->GenerateFrameExit();
8560}
8561
8562void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8563 ret->SetLocations(nullptr);
8564}
8565
8566void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8567 codegen_->GenerateFrameExit();
8568}
8569
Alexey Frunze92d90602015-12-18 18:16:36 -08008570void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8571 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008572}
8573
Alexey Frunze92d90602015-12-18 18:16:36 -08008574void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8575 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008576}
8577
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008578void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8579 HandleShift(shl);
8580}
8581
8582void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8583 HandleShift(shl);
8584}
8585
8586void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8587 HandleShift(shr);
8588}
8589
8590void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8591 HandleShift(shr);
8592}
8593
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008594void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8595 HandleBinaryOp(instruction);
8596}
8597
8598void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8599 HandleBinaryOp(instruction);
8600}
8601
8602void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8603 HandleFieldGet(instruction, instruction->GetFieldInfo());
8604}
8605
8606void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8607 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8608}
8609
8610void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8611 HandleFieldSet(instruction, instruction->GetFieldInfo());
8612}
8613
8614void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008615 HandleFieldSet(instruction,
8616 instruction->GetFieldInfo(),
8617 instruction->GetDexPc(),
8618 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008619}
8620
8621void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8622 HUnresolvedInstanceFieldGet* instruction) {
8623 FieldAccessCallingConventionMIPS calling_convention;
8624 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8625 instruction->GetFieldType(),
8626 calling_convention);
8627}
8628
8629void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8630 HUnresolvedInstanceFieldGet* instruction) {
8631 FieldAccessCallingConventionMIPS calling_convention;
8632 codegen_->GenerateUnresolvedFieldAccess(instruction,
8633 instruction->GetFieldType(),
8634 instruction->GetFieldIndex(),
8635 instruction->GetDexPc(),
8636 calling_convention);
8637}
8638
8639void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8640 HUnresolvedInstanceFieldSet* instruction) {
8641 FieldAccessCallingConventionMIPS calling_convention;
8642 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8643 instruction->GetFieldType(),
8644 calling_convention);
8645}
8646
8647void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8648 HUnresolvedInstanceFieldSet* instruction) {
8649 FieldAccessCallingConventionMIPS calling_convention;
8650 codegen_->GenerateUnresolvedFieldAccess(instruction,
8651 instruction->GetFieldType(),
8652 instruction->GetFieldIndex(),
8653 instruction->GetDexPc(),
8654 calling_convention);
8655}
8656
8657void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8658 HUnresolvedStaticFieldGet* instruction) {
8659 FieldAccessCallingConventionMIPS calling_convention;
8660 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8661 instruction->GetFieldType(),
8662 calling_convention);
8663}
8664
8665void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8666 HUnresolvedStaticFieldGet* instruction) {
8667 FieldAccessCallingConventionMIPS calling_convention;
8668 codegen_->GenerateUnresolvedFieldAccess(instruction,
8669 instruction->GetFieldType(),
8670 instruction->GetFieldIndex(),
8671 instruction->GetDexPc(),
8672 calling_convention);
8673}
8674
8675void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8676 HUnresolvedStaticFieldSet* instruction) {
8677 FieldAccessCallingConventionMIPS calling_convention;
8678 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8679 instruction->GetFieldType(),
8680 calling_convention);
8681}
8682
8683void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8684 HUnresolvedStaticFieldSet* instruction) {
8685 FieldAccessCallingConventionMIPS calling_convention;
8686 codegen_->GenerateUnresolvedFieldAccess(instruction,
8687 instruction->GetFieldType(),
8688 instruction->GetFieldIndex(),
8689 instruction->GetDexPc(),
8690 calling_convention);
8691}
8692
8693void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008694 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8695 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008696 // In suspend check slow path, usually there are no caller-save registers at all.
8697 // If SIMD instructions are present, however, we force spilling all live SIMD
8698 // registers in full width (since the runtime only saves/restores lower part).
8699 locations->SetCustomSlowPathCallerSaves(
8700 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008701}
8702
8703void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8704 HBasicBlock* block = instruction->GetBlock();
8705 if (block->GetLoopInformation() != nullptr) {
8706 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8707 // The back edge will generate the suspend check.
8708 return;
8709 }
8710 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8711 // The goto will generate the suspend check.
8712 return;
8713 }
8714 GenerateSuspendCheck(instruction, nullptr);
8715}
8716
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008717void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008718 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8719 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008720 InvokeRuntimeCallingConvention calling_convention;
8721 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8722}
8723
8724void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008725 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8727}
8728
8729void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008730 DataType::Type input_type = conversion->GetInputType();
8731 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008732 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8733 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008734 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008736 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8737 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008738 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8739 }
8740
8741 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008742 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008743 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8744 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008745 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746 }
8747
Vladimir Markoca6fff82017-10-03 14:49:14 +01008748 LocationSummary* locations =
8749 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750
8751 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008752 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008753 locations->SetInAt(0, Location::RequiresFpuRegister());
8754 } else {
8755 locations->SetInAt(0, Location::RequiresRegister());
8756 }
8757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008758 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008759 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8760 } else {
8761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8762 }
8763 } else {
8764 InvokeRuntimeCallingConvention calling_convention;
8765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008766 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008767 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8768 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008769 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770 locations->SetInAt(0, Location::RegisterPairLocation(
8771 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8772 }
8773
8774 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8775 }
8776}
8777
8778void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8779 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008780 DataType::Type result_type = conversion->GetResultType();
8781 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008782 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008783 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008784
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008785 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8786 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008788 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008789 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8790 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8791 Register src = locations->InAt(0).AsRegister<Register>();
8792
Alexey Frunzea871ef12016-06-27 15:20:11 -07008793 if (dst_low != src) {
8794 __ Move(dst_low, src);
8795 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008796 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008797 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008798 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008799 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008800 ? locations->InAt(0).AsRegisterPairLow<Register>()
8801 : locations->InAt(0).AsRegister<Register>();
8802
8803 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008804 case DataType::Type::kUint8:
8805 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008807 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008808 if (has_sign_extension) {
8809 __ Seb(dst, src);
8810 } else {
8811 __ Sll(dst, src, 24);
8812 __ Sra(dst, dst, 24);
8813 }
8814 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008815 case DataType::Type::kUint16:
8816 __ Andi(dst, src, 0xFFFF);
8817 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008818 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008819 if (has_sign_extension) {
8820 __ Seh(dst, src);
8821 } else {
8822 __ Sll(dst, src, 16);
8823 __ Sra(dst, dst, 16);
8824 }
8825 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008826 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008827 if (dst != src) {
8828 __ Move(dst, src);
8829 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008830 break;
8831
8832 default:
8833 LOG(FATAL) << "Unexpected type conversion from " << input_type
8834 << " to " << result_type;
8835 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008836 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8837 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008838 if (isR6) {
8839 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8840 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8841 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8842 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8843 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8844 __ Mtc1(src_low, FTMP);
8845 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008846 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008847 __ Cvtsl(dst, FTMP);
8848 } else {
8849 __ Cvtdl(dst, FTMP);
8850 }
8851 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008852 QuickEntrypointEnum entrypoint =
8853 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008854 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008855 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008856 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8857 } else {
8858 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8859 }
8860 }
8861 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008862 Register src = locations->InAt(0).AsRegister<Register>();
8863 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8864 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008865 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008866 __ Cvtsw(dst, FTMP);
8867 } else {
8868 __ Cvtdw(dst, FTMP);
8869 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008870 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008871 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8872 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008873
8874 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8875 // value of the output type if the input is outside of the range after the truncation or
8876 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8877 // results. This matches the desired float/double-to-int/long conversion exactly.
8878 //
8879 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8880 // value when the input is either a NaN or is outside of the range of the output type
8881 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8882 // the same result.
8883 //
8884 // The code takes care of the different behaviors by first comparing the input to the
8885 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8886 // If the input is greater than or equal to the minimum, it procedes to the truncate
8887 // instruction, which will handle such an input the same way irrespective of NAN2008.
8888 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8889 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008890 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008891 if (isR6) {
8892 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8893 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8894 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8895 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8896 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008898 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008899 __ TruncLS(FTMP, src);
8900 } else {
8901 __ TruncLD(FTMP, src);
8902 }
8903 __ Mfc1(dst_low, FTMP);
8904 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008905 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008906 QuickEntrypointEnum entrypoint =
8907 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008908 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008909 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008910 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8911 } else {
8912 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8913 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008914 }
8915 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008916 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8917 Register dst = locations->Out().AsRegister<Register>();
8918 MipsLabel truncate;
8919 MipsLabel done;
8920
Lena Djokicf4e23a82017-05-09 15:43:45 +02008921 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008922 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008923 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8924 __ LoadConst32(TMP, min_val);
8925 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008926 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008927 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8928 __ LoadConst32(TMP, High32Bits(min_val));
8929 __ Mtc1(ZERO, FTMP);
8930 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008931 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008933 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008934 __ ColeS(0, FTMP, src);
8935 } else {
8936 __ ColeD(0, FTMP, src);
8937 }
8938 __ Bc1t(0, &truncate);
8939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008940 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008941 __ CeqS(0, src, src);
8942 } else {
8943 __ CeqD(0, src, src);
8944 }
8945 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8946 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008947
8948 __ B(&done);
8949
8950 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008951 }
8952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008953 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008954 __ TruncWS(FTMP, src);
8955 } else {
8956 __ TruncWD(FTMP, src);
8957 }
8958 __ Mfc1(dst, FTMP);
8959
Lena Djokicf4e23a82017-05-09 15:43:45 +02008960 if (!isR6) {
8961 __ Bind(&done);
8962 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008963 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008964 } else if (DataType::IsFloatingPointType(result_type) &&
8965 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008966 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8967 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008968 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008969 __ Cvtsd(dst, src);
8970 } else {
8971 __ Cvtds(dst, src);
8972 }
8973 } else {
8974 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8975 << " to " << result_type;
8976 }
8977}
8978
8979void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8980 HandleShift(ushr);
8981}
8982
8983void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8984 HandleShift(ushr);
8985}
8986
8987void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8988 HandleBinaryOp(instruction);
8989}
8990
8991void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8992 HandleBinaryOp(instruction);
8993}
8994
8995void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8996 // Nothing to do, this should be removed during prepare for register allocator.
8997 LOG(FATAL) << "Unreachable";
8998}
8999
9000void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9001 // Nothing to do, this should be removed during prepare for register allocator.
9002 LOG(FATAL) << "Unreachable";
9003}
9004
9005void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009006 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009007}
9008
9009void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009010 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009011}
9012
9013void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009014 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009015}
9016
9017void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009018 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009019}
9020
9021void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009022 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009023}
9024
9025void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009026 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009027}
9028
9029void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009030 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009031}
9032
9033void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009034 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009035}
9036
9037void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009038 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009039}
9040
9041void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009042 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009043}
9044
9045void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009046 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009047}
9048
9049void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009050 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009051}
9052
9053void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009054 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009055}
9056
9057void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009058 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009059}
9060
9061void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009062 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009063}
9064
9065void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009066 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009067}
9068
9069void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009070 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009071}
9072
9073void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009074 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009075}
9076
9077void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009078 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009079}
9080
9081void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009082 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009083}
9084
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009085void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9086 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009087 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009088 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009089 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9090 uint32_t num_entries = switch_instr->GetNumEntries();
9091 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9092 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9093 // instruction to simulate PC-relative addressing when accessing the jump table.
9094 // NAL clobbers RA. Make sure RA is preserved.
9095 codegen_->ClobberRA();
9096 }
9097 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009098}
9099
Alexey Frunze96b66822016-09-10 02:32:44 -07009100void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9101 int32_t lower_bound,
9102 uint32_t num_entries,
9103 HBasicBlock* switch_block,
9104 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009105 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009106 Register temp_reg = TMP;
9107 __ Addiu32(temp_reg, value_reg, -lower_bound);
9108 // Jump to default if index is negative
9109 // Note: We don't check the case that index is positive while value < lower_bound, because in
9110 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9111 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9112
Alexey Frunze96b66822016-09-10 02:32:44 -07009113 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009114 // Jump to successors[0] if value == lower_bound.
9115 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9116 int32_t last_index = 0;
9117 for (; num_entries - last_index > 2; last_index += 2) {
9118 __ Addiu(temp_reg, temp_reg, -2);
9119 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9120 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9121 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9122 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9123 }
9124 if (num_entries - last_index == 2) {
9125 // The last missing case_value.
9126 __ Addiu(temp_reg, temp_reg, -1);
9127 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009128 }
9129
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009130 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009131 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009132 __ B(codegen_->GetLabelOf(default_block));
9133 }
9134}
9135
Alexey Frunze96b66822016-09-10 02:32:44 -07009136void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9137 Register constant_area,
9138 int32_t lower_bound,
9139 uint32_t num_entries,
9140 HBasicBlock* switch_block,
9141 HBasicBlock* default_block) {
9142 // Create a jump table.
9143 std::vector<MipsLabel*> labels(num_entries);
9144 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9145 for (uint32_t i = 0; i < num_entries; i++) {
9146 labels[i] = codegen_->GetLabelOf(successors[i]);
9147 }
9148 JumpTable* table = __ CreateJumpTable(std::move(labels));
9149
9150 // Is the value in range?
9151 __ Addiu32(TMP, value_reg, -lower_bound);
9152 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9153 __ Sltiu(AT, TMP, num_entries);
9154 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9155 } else {
9156 __ LoadConst32(AT, num_entries);
9157 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9158 }
9159
9160 // We are in the range of the table.
9161 // Load the target address from the jump table, indexing by the value.
9162 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009163 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009164 __ Lw(TMP, TMP, 0);
9165 // Compute the absolute target address by adding the table start address
9166 // (the table contains offsets to targets relative to its start).
9167 __ Addu(TMP, TMP, AT);
9168 // And jump.
9169 __ Jr(TMP);
9170 __ NopIfNoReordering();
9171}
9172
9173void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9174 int32_t lower_bound = switch_instr->GetStartValue();
9175 uint32_t num_entries = switch_instr->GetNumEntries();
9176 LocationSummary* locations = switch_instr->GetLocations();
9177 Register value_reg = locations->InAt(0).AsRegister<Register>();
9178 HBasicBlock* switch_block = switch_instr->GetBlock();
9179 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9180
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009181 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009182 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009183 //
9184 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9185 // to access the jump table and it is implemented by changing HPackedSwitch to
9186 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9187 // VisitMipsPackedSwitch()).
9188 //
9189 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9190 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9191 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009192 GenTableBasedPackedSwitch(value_reg,
9193 ZERO,
9194 lower_bound,
9195 num_entries,
9196 switch_block,
9197 default_block);
9198 } else {
9199 GenPackedSwitchWithCompares(value_reg,
9200 lower_bound,
9201 num_entries,
9202 switch_block,
9203 default_block);
9204 }
9205}
9206
9207void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9208 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009209 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009210 locations->SetInAt(0, Location::RequiresRegister());
9211 // Constant area pointer (HMipsComputeBaseMethodAddress).
9212 locations->SetInAt(1, Location::RequiresRegister());
9213}
9214
9215void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9216 int32_t lower_bound = switch_instr->GetStartValue();
9217 uint32_t num_entries = switch_instr->GetNumEntries();
9218 LocationSummary* locations = switch_instr->GetLocations();
9219 Register value_reg = locations->InAt(0).AsRegister<Register>();
9220 Register constant_area = locations->InAt(1).AsRegister<Register>();
9221 HBasicBlock* switch_block = switch_instr->GetBlock();
9222 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9223
9224 // This is an R2-only path. HPackedSwitch has been changed to
9225 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9226 // required to address the jump table relative to PC.
9227 GenTableBasedPackedSwitch(value_reg,
9228 constant_area,
9229 lower_bound,
9230 num_entries,
9231 switch_block,
9232 default_block);
9233}
9234
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009235void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9236 HMipsComputeBaseMethodAddress* insn) {
9237 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009238 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009239 locations->SetOut(Location::RequiresRegister());
9240}
9241
9242void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9243 HMipsComputeBaseMethodAddress* insn) {
9244 LocationSummary* locations = insn->GetLocations();
9245 Register reg = locations->Out().AsRegister<Register>();
9246
9247 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9248
9249 // Generate a dummy PC-relative call to obtain PC.
9250 __ Nal();
9251 // Grab the return address off RA.
9252 __ Move(reg, RA);
9253
9254 // Remember this offset (the obtained PC value) for later use with constant area.
9255 __ BindPcRelBaseLabel();
9256}
9257
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009258void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9259 // The trampoline uses the same calling convention as dex calling conventions,
9260 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9261 // the method_idx.
9262 HandleInvoke(invoke);
9263}
9264
9265void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9266 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9267}
9268
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009269void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009271 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009272 locations->SetInAt(0, Location::RequiresRegister());
9273 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009274}
9275
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009276void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9277 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009278 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009279 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009280 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009281 __ LoadFromOffset(kLoadWord,
9282 locations->Out().AsRegister<Register>(),
9283 locations->InAt(0).AsRegister<Register>(),
9284 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009285 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009286 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009287 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009288 __ LoadFromOffset(kLoadWord,
9289 locations->Out().AsRegister<Register>(),
9290 locations->InAt(0).AsRegister<Register>(),
9291 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009292 __ LoadFromOffset(kLoadWord,
9293 locations->Out().AsRegister<Register>(),
9294 locations->Out().AsRegister<Register>(),
9295 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009296 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009297}
9298
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009299void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9300 ATTRIBUTE_UNUSED) {
9301 LOG(FATAL) << "Unreachable";
9302}
9303
9304void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9305 ATTRIBUTE_UNUSED) {
9306 LOG(FATAL) << "Unreachable";
9307}
9308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009309#undef __
9310#undef QUICK_ENTRY_POINT
9311
9312} // namespace mips
9313} // namespace art