blob: 815e32cf547a7d12f16509a2ac9fdc55d0c93b00 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
61 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020062 return Location::RegisterLocation(V0);
63
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010064 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020065 return Location::RegisterPairLocation(V0, V1);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kFloat32:
68 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020069 return Location::FpuRegisterLocation(F0);
70
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010071 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location();
73 }
74 UNREACHABLE();
75}
76
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010077Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020078 return MipsReturnLocation(type);
79}
80
81Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
82 return Location::RegisterLocation(kMethodRegisterArgument);
83}
84
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020086 Location next_location;
87
88 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010089 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kInt8:
93 case DataType::Type::kUint16:
94 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010095 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020096 uint32_t gp_index = gp_index_++;
97 if (gp_index < calling_convention.GetNumberOfRegisters()) {
98 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
99 } else {
100 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
101 next_location = Location::StackSlot(stack_offset);
102 }
103 break;
104 }
105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200107 uint32_t gp_index = gp_index_;
108 gp_index_ += 2;
109 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800110 Register reg = calling_convention.GetRegisterAt(gp_index);
111 if (reg == A1 || reg == A3) {
112 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200113 gp_index++;
114 }
115 Register low_even = calling_convention.GetRegisterAt(gp_index);
116 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
117 DCHECK_EQ(low_even + 1, high_odd);
118 next_location = Location::RegisterPairLocation(low_even, high_odd);
119 } else {
120 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
121 next_location = Location::DoubleStackSlot(stack_offset);
122 }
123 break;
124 }
125
126 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
127 // will take up the even/odd pair, while floats are stored in even regs only.
128 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 case DataType::Type::kFloat32:
130 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200131 uint32_t float_index = float_index_++;
132 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
133 next_location = Location::FpuRegisterLocation(
134 calling_convention.GetFpuRegisterAt(float_index));
135 } else {
136 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
138 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200139 }
140 break;
141 }
142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200144 LOG(FATAL) << "Unexpected parameter type " << type;
145 break;
146 }
147
148 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 return next_location;
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155 return MipsReturnLocation(type);
156}
157
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100158// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
159#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700160#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
163 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000164 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
167 LocationSummary* locations = instruction_->GetLocations();
168 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
169 __ Bind(GetEntryLabel());
170 if (instruction_->CanThrowIntoCatchBlock()) {
171 // Live registers will be restored in the catch block if caught.
172 SaveLiveRegisters(codegen, instruction_->GetLocations());
173 }
174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
176 InvokeRuntimeCallingConvention calling_convention;
177 codegen->EmitParallelMoves(locations->InAt(0),
178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100179 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 locations->InAt(1),
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100183 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
184 ? kQuickThrowStringBounds
185 : kQuickThrowArrayBounds;
186 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100187 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
189 }
190
191 bool IsFatal() const OVERRIDE { return true; }
192
193 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
194
195 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
197};
198
199class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
200 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202
203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
204 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
205 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100206 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
208 }
209
210 bool IsFatal() const OVERRIDE { return true; }
211
212 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
213
214 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200215 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
216};
217
218class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
219 public:
220 LoadClassSlowPathMIPS(HLoadClass* cls,
221 HInstruction* at,
222 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000223 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700224 : SlowPathCodeMIPS(at),
225 cls_(cls),
226 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000232 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700233 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 InvokeRuntimeCallingConvention calling_convention;
236 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200237 __ Bind(GetEntryLabel());
238 SaveLiveRegisters(codegen, locations);
239
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 dex::TypeIndex type_index = cls_->GetTypeIndex();
241 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100242 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
243 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200245 if (do_clinit_) {
246 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
247 } else {
248 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
249 }
250
251 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200252 if (out.IsValid()) {
253 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100254 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700255 mips_codegen->MoveLocation(out,
256 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
257 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200270 // The dex PC of `at_`.
271 const uint32_t dex_pc_;
272
273 // Whether to initialize the class.
274 const bool do_clinit_;
275
276 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
277};
278
279class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
280 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000281 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
282 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283
284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700285 DCHECK(instruction_->IsLoadString());
286 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287 LocationSummary* locations = instruction_->GetLocations();
288 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000289 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100296 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200297 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100299 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200300 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700301 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000304
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200305 __ B(GetExitLabel());
306 }
307
308 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
309
310 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200311 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
312};
313
314class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
315 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000316 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
319 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
320 __ Bind(GetEntryLabel());
321 if (instruction_->CanThrowIntoCatchBlock()) {
322 // Live registers will be restored in the catch block if caught.
323 SaveLiveRegisters(codegen, instruction_->GetLocations());
324 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100325 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200326 instruction_,
327 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100328 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
330 }
331
332 bool IsFatal() const OVERRIDE { return true; }
333
334 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
335
336 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
338};
339
340class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
341 public:
342 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000343 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344
345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200346 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200349 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100350 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200352 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200353 if (successor_ == nullptr) {
354 __ B(GetReturnLabel());
355 } else {
356 __ B(mips_codegen->GetLabelOf(successor_));
357 }
358 }
359
360 MipsLabel* GetReturnLabel() {
361 DCHECK(successor_ == nullptr);
362 return &return_label_;
363 }
364
365 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
366
Chris Larsena2045912017-11-02 12:39:54 -0700367 HBasicBlock* GetSuccessor() const {
368 return successor_;
369 }
370
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200371 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200372 // If not null, the block to branch to after the suspend check.
373 HBasicBlock* const successor_;
374
375 // If `successor_` is null, the label to branch to after the suspend check.
376 MipsLabel return_label_;
377
378 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
379};
380
381class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
382 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
384 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200388 uint32_t dex_pc = instruction_->GetDexPc();
389 DCHECK(instruction_->IsCheckCast()
390 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
391 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
392
393 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800394 if (!is_fatal_) {
395 SaveLiveRegisters(codegen, locations);
396 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397
398 // We're moving two locations to locations that could overlap, so we need a parallel
399 // move resolver.
400 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800401 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100403 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800404 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100406 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100408 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
412 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 } else {
414 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800415 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
416 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 }
418
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800419 if (!is_fatal_) {
420 RestoreLiveRegisters(codegen, locations);
421 __ B(GetExitLabel());
422 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 }
424
425 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
426
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800427 bool IsFatal() const OVERRIDE { return is_fatal_; }
428
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200429 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800430 const bool is_fatal_;
431
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200432 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
433};
434
435class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
436 public:
Aart Bik42249c32016-01-07 15:33:50 -0800437 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800441 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100443 LocationSummary* locations = instruction_->GetLocations();
444 SaveLiveRegisters(codegen, locations);
445 InvokeRuntimeCallingConvention calling_convention;
446 __ LoadConst32(calling_convention.GetRegisterAt(0),
447 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100448 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100449 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 }
451
452 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
453
454 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
456};
457
Alexey Frunze15958152017-02-09 19:08:30 -0800458class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
459 public:
460 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
461
462 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
463 LocationSummary* locations = instruction_->GetLocations();
464 __ Bind(GetEntryLabel());
465 SaveLiveRegisters(codegen, locations);
466
467 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100468 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800469 parallel_move.AddMove(
470 locations->InAt(0),
471 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100472 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800473 nullptr);
474 parallel_move.AddMove(
475 locations->InAt(1),
476 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100477 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800478 nullptr);
479 parallel_move.AddMove(
480 locations->InAt(2),
481 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800483 nullptr);
484 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
485
486 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
487 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
488 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
489 RestoreLiveRegisters(codegen, locations);
490 __ B(GetExitLabel());
491 }
492
493 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
494
495 private:
496 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
497};
498
499// Slow path marking an object reference `ref` during a read
500// barrier. The field `obj.field` in the object `obj` holding this
501// reference does not get updated by this slow path after marking (see
502// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
503//
504// This means that after the execution of this slow path, `ref` will
505// always be up-to-date, but `obj.field` may not; i.e., after the
506// flip, `ref` will be a to-space reference, but `obj.field` will
507// probably still be a from-space reference (unless it gets updated by
508// another thread, or if another thread installed another object
509// reference (different from `ref`) in `obj.field`).
510//
511// If `entrypoint` is a valid location it is assumed to already be
512// holding the entrypoint. The case where the entrypoint is passed in
513// is for the GcRoot read barrier.
514class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
515 public:
516 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
517 Location ref,
518 Location entrypoint = Location::NoLocation())
519 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
520 DCHECK(kEmitCompilerReadBarrier);
521 }
522
523 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
526 LocationSummary* locations = instruction_->GetLocations();
527 Register ref_reg = ref_.AsRegister<Register>();
528 DCHECK(locations->CanCall());
529 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
530 DCHECK(instruction_->IsInstanceFieldGet() ||
531 instruction_->IsStaticFieldGet() ||
532 instruction_->IsArrayGet() ||
533 instruction_->IsArraySet() ||
534 instruction_->IsLoadClass() ||
535 instruction_->IsLoadString() ||
536 instruction_->IsInstanceOf() ||
537 instruction_->IsCheckCast() ||
538 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
539 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
540 << "Unexpected instruction in read barrier marking slow path: "
541 << instruction_->DebugName();
542
543 __ Bind(GetEntryLabel());
544 // No need to save live registers; it's taken care of by the
545 // entrypoint. Also, there is no need to update the stack mask,
546 // as this runtime call will not trigger a garbage collection.
547 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
548 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
549 (S2 <= ref_reg && ref_reg <= S7) ||
550 (ref_reg == FP)) << ref_reg;
551 // "Compact" slow path, saving two moves.
552 //
553 // Instead of using the standard runtime calling convention (input
554 // and output in A0 and V0 respectively):
555 //
556 // A0 <- ref
557 // V0 <- ReadBarrierMark(A0)
558 // ref <- V0
559 //
560 // we just use rX (the register containing `ref`) as input and output
561 // of a dedicated entrypoint:
562 //
563 // rX <- ReadBarrierMarkRegX(rX)
564 //
565 if (entrypoint_.IsValid()) {
566 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
567 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
568 __ Jalr(entrypoint_.AsRegister<Register>());
569 __ NopIfNoReordering();
570 } else {
571 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100572 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800573 // This runtime call does not require a stack map.
574 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
575 instruction_,
576 this,
577 /* direct */ false);
578 }
579 __ B(GetExitLabel());
580 }
581
582 private:
583 // The location (register) of the marked object reference.
584 const Location ref_;
585
586 // The location of the entrypoint if already loaded.
587 const Location entrypoint_;
588
589 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
590};
591
592// Slow path marking an object reference `ref` during a read barrier,
593// and if needed, atomically updating the field `obj.field` in the
594// object `obj` holding this reference after marking (contrary to
595// ReadBarrierMarkSlowPathMIPS above, which never tries to update
596// `obj.field`).
597//
598// This means that after the execution of this slow path, both `ref`
599// and `obj.field` will be up-to-date; i.e., after the flip, both will
600// hold the same to-space reference (unless another thread installed
601// another object reference (different from `ref`) in `obj.field`).
602class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
603 public:
604 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
605 Location ref,
606 Register obj,
607 Location field_offset,
608 Register temp1)
609 : SlowPathCodeMIPS(instruction),
610 ref_(ref),
611 obj_(obj),
612 field_offset_(field_offset),
613 temp1_(temp1) {
614 DCHECK(kEmitCompilerReadBarrier);
615 }
616
617 const char* GetDescription() const OVERRIDE {
618 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
619 }
620
621 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
622 LocationSummary* locations = instruction_->GetLocations();
623 Register ref_reg = ref_.AsRegister<Register>();
624 DCHECK(locations->CanCall());
625 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
626 // This slow path is only used by the UnsafeCASObject intrinsic.
627 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
628 << "Unexpected instruction in read barrier marking and field updating slow path: "
629 << instruction_->DebugName();
630 DCHECK(instruction_->GetLocations()->Intrinsified());
631 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
632 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
633
634 __ Bind(GetEntryLabel());
635
636 // Save the old reference.
637 // Note that we cannot use AT or TMP to save the old reference, as those
638 // are used by the code that follows, but we need the old reference after
639 // the call to the ReadBarrierMarkRegX entry point.
640 DCHECK_NE(temp1_, AT);
641 DCHECK_NE(temp1_, TMP);
642 __ Move(temp1_, ref_reg);
643
644 // No need to save live registers; it's taken care of by the
645 // entrypoint. Also, there is no need to update the stack mask,
646 // as this runtime call will not trigger a garbage collection.
647 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
648 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
649 (S2 <= ref_reg && ref_reg <= S7) ||
650 (ref_reg == FP)) << ref_reg;
651 // "Compact" slow path, saving two moves.
652 //
653 // Instead of using the standard runtime calling convention (input
654 // and output in A0 and V0 respectively):
655 //
656 // A0 <- ref
657 // V0 <- ReadBarrierMark(A0)
658 // ref <- V0
659 //
660 // we just use rX (the register containing `ref`) as input and output
661 // of a dedicated entrypoint:
662 //
663 // rX <- ReadBarrierMarkRegX(rX)
664 //
665 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100666 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800667 // This runtime call does not require a stack map.
668 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
669 instruction_,
670 this,
671 /* direct */ false);
672
673 // If the new reference is different from the old reference,
674 // update the field in the holder (`*(obj_ + field_offset_)`).
675 //
676 // Note that this field could also hold a different object, if
677 // another thread had concurrently changed it. In that case, the
678 // the compare-and-set (CAS) loop below would abort, leaving the
679 // field as-is.
680 MipsLabel done;
681 __ Beq(temp1_, ref_reg, &done);
682
683 // Update the the holder's field atomically. This may fail if
684 // mutator updates before us, but it's OK. This is achieved
685 // using a strong compare-and-set (CAS) operation with relaxed
686 // memory synchronization ordering, where the expected value is
687 // the old reference and the desired value is the new reference.
688
689 // Convenience aliases.
690 Register base = obj_;
691 // The UnsafeCASObject intrinsic uses a register pair as field
692 // offset ("long offset"), of which only the low part contains
693 // data.
694 Register offset = field_offset_.AsRegisterPairLow<Register>();
695 Register expected = temp1_;
696 Register value = ref_reg;
697 Register tmp_ptr = TMP; // Pointer to actual memory.
698 Register tmp = AT; // Value in memory.
699
700 __ Addu(tmp_ptr, base, offset);
701
702 if (kPoisonHeapReferences) {
703 __ PoisonHeapReference(expected);
704 // Do not poison `value` if it is the same register as
705 // `expected`, which has just been poisoned.
706 if (value != expected) {
707 __ PoisonHeapReference(value);
708 }
709 }
710
711 // do {
712 // tmp = [r_ptr] - expected;
713 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
714
715 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
716 MipsLabel loop_head, exit_loop;
717 __ Bind(&loop_head);
718 if (is_r6) {
719 __ LlR6(tmp, tmp_ptr);
720 } else {
721 __ LlR2(tmp, tmp_ptr);
722 }
723 __ Bne(tmp, expected, &exit_loop);
724 __ Move(tmp, value);
725 if (is_r6) {
726 __ ScR6(tmp, tmp_ptr);
727 } else {
728 __ ScR2(tmp, tmp_ptr);
729 }
730 __ Beqz(tmp, &loop_head);
731 __ Bind(&exit_loop);
732
733 if (kPoisonHeapReferences) {
734 __ UnpoisonHeapReference(expected);
735 // Do not unpoison `value` if it is the same register as
736 // `expected`, which has just been unpoisoned.
737 if (value != expected) {
738 __ UnpoisonHeapReference(value);
739 }
740 }
741
742 __ Bind(&done);
743 __ B(GetExitLabel());
744 }
745
746 private:
747 // The location (register) of the marked object reference.
748 const Location ref_;
749 // The register containing the object holding the marked object reference field.
750 const Register obj_;
751 // The location of the offset of the marked reference field within `obj_`.
752 Location field_offset_;
753
754 const Register temp1_;
755
756 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
757};
758
759// Slow path generating a read barrier for a heap reference.
760class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
761 public:
762 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
763 Location out,
764 Location ref,
765 Location obj,
766 uint32_t offset,
767 Location index)
768 : SlowPathCodeMIPS(instruction),
769 out_(out),
770 ref_(ref),
771 obj_(obj),
772 offset_(offset),
773 index_(index) {
774 DCHECK(kEmitCompilerReadBarrier);
775 // If `obj` is equal to `out` or `ref`, it means the initial object
776 // has been overwritten by (or after) the heap object reference load
777 // to be instrumented, e.g.:
778 //
779 // __ LoadFromOffset(kLoadWord, out, out, offset);
780 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
781 //
782 // In that case, we have lost the information about the original
783 // object, and the emitted read barrier cannot work properly.
784 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
785 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
786 }
787
788 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
789 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
790 LocationSummary* locations = instruction_->GetLocations();
791 Register reg_out = out_.AsRegister<Register>();
792 DCHECK(locations->CanCall());
793 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
794 DCHECK(instruction_->IsInstanceFieldGet() ||
795 instruction_->IsStaticFieldGet() ||
796 instruction_->IsArrayGet() ||
797 instruction_->IsInstanceOf() ||
798 instruction_->IsCheckCast() ||
799 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
800 << "Unexpected instruction in read barrier for heap reference slow path: "
801 << instruction_->DebugName();
802
803 __ Bind(GetEntryLabel());
804 SaveLiveRegisters(codegen, locations);
805
806 // We may have to change the index's value, but as `index_` is a
807 // constant member (like other "inputs" of this slow path),
808 // introduce a copy of it, `index`.
809 Location index = index_;
810 if (index_.IsValid()) {
811 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
812 if (instruction_->IsArrayGet()) {
813 // Compute the actual memory offset and store it in `index`.
814 Register index_reg = index_.AsRegister<Register>();
815 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
816 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
817 // We are about to change the value of `index_reg` (see the
818 // calls to art::mips::MipsAssembler::Sll and
819 // art::mips::MipsAssembler::Addiu32 below), but it has
820 // not been saved by the previous call to
821 // art::SlowPathCode::SaveLiveRegisters, as it is a
822 // callee-save register --
823 // art::SlowPathCode::SaveLiveRegisters does not consider
824 // callee-save registers, as it has been designed with the
825 // assumption that callee-save registers are supposed to be
826 // handled by the called function. So, as a callee-save
827 // register, `index_reg` _would_ eventually be saved onto
828 // the stack, but it would be too late: we would have
829 // changed its value earlier. Therefore, we manually save
830 // it here into another freely available register,
831 // `free_reg`, chosen of course among the caller-save
832 // registers (as a callee-save `free_reg` register would
833 // exhibit the same problem).
834 //
835 // Note we could have requested a temporary register from
836 // the register allocator instead; but we prefer not to, as
837 // this is a slow path, and we know we can find a
838 // caller-save register that is available.
839 Register free_reg = FindAvailableCallerSaveRegister(codegen);
840 __ Move(free_reg, index_reg);
841 index_reg = free_reg;
842 index = Location::RegisterLocation(index_reg);
843 } else {
844 // The initial register stored in `index_` has already been
845 // saved in the call to art::SlowPathCode::SaveLiveRegisters
846 // (as it is not a callee-save register), so we can freely
847 // use it.
848 }
849 // Shifting the index value contained in `index_reg` by the scale
850 // factor (2) cannot overflow in practice, as the runtime is
851 // unable to allocate object arrays with a size larger than
852 // 2^26 - 1 (that is, 2^28 - 4 bytes).
853 __ Sll(index_reg, index_reg, TIMES_4);
854 static_assert(
855 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
856 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
857 __ Addiu32(index_reg, index_reg, offset_);
858 } else {
859 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
860 // intrinsics, `index_` is not shifted by a scale factor of 2
861 // (as in the case of ArrayGet), as it is actually an offset
862 // to an object field within an object.
863 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
864 DCHECK(instruction_->GetLocations()->Intrinsified());
865 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
866 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
867 << instruction_->AsInvoke()->GetIntrinsic();
868 DCHECK_EQ(offset_, 0U);
869 DCHECK(index_.IsRegisterPair());
870 // UnsafeGet's offset location is a register pair, the low
871 // part contains the correct offset.
872 index = index_.ToLow();
873 }
874 }
875
876 // We're moving two or three locations to locations that could
877 // overlap, so we need a parallel move resolver.
878 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100879 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800880 parallel_move.AddMove(ref_,
881 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100882 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800883 nullptr);
884 parallel_move.AddMove(obj_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 if (index.IsValid()) {
889 parallel_move.AddMove(index,
890 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100891 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800892 nullptr);
893 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
894 } else {
895 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
896 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
897 }
898 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
899 instruction_,
900 instruction_->GetDexPc(),
901 this);
902 CheckEntrypointTypes<
903 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200904 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 calling_convention.GetReturnLocation(DataType::Type::kReference),
906 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800907
908 RestoreLiveRegisters(codegen, locations);
909 __ B(GetExitLabel());
910 }
911
912 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
913
914 private:
915 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
916 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
917 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
918 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
919 if (i != ref &&
920 i != obj &&
921 !codegen->IsCoreCalleeSaveRegister(i) &&
922 !codegen->IsBlockedCoreRegister(i)) {
923 return static_cast<Register>(i);
924 }
925 }
926 // We shall never fail to find a free caller-save register, as
927 // there are more than two core caller-save registers on MIPS
928 // (meaning it is possible to find one which is different from
929 // `ref` and `obj`).
930 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
931 LOG(FATAL) << "Could not find a free caller-save register";
932 UNREACHABLE();
933 }
934
935 const Location out_;
936 const Location ref_;
937 const Location obj_;
938 const uint32_t offset_;
939 // An additional location containing an index to an array.
940 // Only used for HArrayGet and the UnsafeGetObject &
941 // UnsafeGetObjectVolatile intrinsics.
942 const Location index_;
943
944 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
945};
946
947// Slow path generating a read barrier for a GC root.
948class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
949 public:
950 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
951 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
952 DCHECK(kEmitCompilerReadBarrier);
953 }
954
955 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
956 LocationSummary* locations = instruction_->GetLocations();
957 Register reg_out = out_.AsRegister<Register>();
958 DCHECK(locations->CanCall());
959 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
960 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
961 << "Unexpected instruction in read barrier for GC root slow path: "
962 << instruction_->DebugName();
963
964 __ Bind(GetEntryLabel());
965 SaveLiveRegisters(codegen, locations);
966
967 InvokeRuntimeCallingConvention calling_convention;
968 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200969 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
970 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100971 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800972 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
973 instruction_,
974 instruction_->GetDexPc(),
975 this);
976 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200977 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100978 calling_convention.GetReturnLocation(DataType::Type::kReference),
979 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800980
981 RestoreLiveRegisters(codegen, locations);
982 __ B(GetExitLabel());
983 }
984
985 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
986
987 private:
988 const Location out_;
989 const Location root_;
990
991 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
992};
993
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200994CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
995 const MipsInstructionSetFeatures& isa_features,
996 const CompilerOptions& compiler_options,
997 OptimizingCompilerStats* stats)
998 : CodeGenerator(graph,
999 kNumberOfCoreRegisters,
1000 kNumberOfFRegisters,
1001 kNumberOfRegisterPairs,
1002 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1003 arraysize(kCoreCalleeSaves)),
1004 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1005 arraysize(kFpuCalleeSaves)),
1006 compiler_options,
1007 stats),
1008 block_labels_(nullptr),
1009 location_builder_(graph, this),
1010 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001011 move_resolver_(graph->GetAllocator(), this),
1012 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001014 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1016 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1017 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1018 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1019 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1020 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1021 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1022 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1023 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001024 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001025 // Save RA (containing the return address) to mimic Quick.
1026 AddAllocatedRegister(Location::RegisterLocation(RA));
1027}
1028
1029#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001030// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1031#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001032#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001033
1034void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1035 // Ensure that we fix up branches.
1036 __ FinalizeCode();
1037
1038 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001039 StackMapStream* stack_map_stream = GetStackMapStream();
1040 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001041 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001042 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001043 uint32_t new_position = __ GetAdjustedPosition(old_position);
1044 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001045 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001046 }
1047
1048 // Adjust pc offsets for the disassembly information.
1049 if (disasm_info_ != nullptr) {
1050 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1051 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1052 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1053 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1054 it.second.start = __ GetAdjustedPosition(it.second.start);
1055 it.second.end = __ GetAdjustedPosition(it.second.end);
1056 }
1057 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1058 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1059 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1060 }
1061 }
1062
1063 CodeGenerator::Finalize(allocator);
1064}
1065
1066MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1067 return codegen_->GetAssembler();
1068}
1069
1070void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1071 DCHECK_LT(index, moves_.size());
1072 MoveOperands* move = moves_[index];
1073 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1074}
1075
1076void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1077 DCHECK_LT(index, moves_.size());
1078 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001079 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001080 Location loc1 = move->GetDestination();
1081 Location loc2 = move->GetSource();
1082
1083 DCHECK(!loc1.IsConstant());
1084 DCHECK(!loc2.IsConstant());
1085
1086 if (loc1.Equals(loc2)) {
1087 return;
1088 }
1089
1090 if (loc1.IsRegister() && loc2.IsRegister()) {
1091 // Swap 2 GPRs.
1092 Register r1 = loc1.AsRegister<Register>();
1093 Register r2 = loc2.AsRegister<Register>();
1094 __ Move(TMP, r2);
1095 __ Move(r2, r1);
1096 __ Move(r1, TMP);
1097 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001098 if (codegen_->GetGraph()->HasSIMD()) {
1099 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1100 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1101 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001102 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001103 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1104 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1105 if (type == DataType::Type::kFloat32) {
1106 __ MovS(FTMP, f2);
1107 __ MovS(f2, f1);
1108 __ MovS(f1, FTMP);
1109 } else {
1110 DCHECK_EQ(type, DataType::Type::kFloat64);
1111 __ MovD(FTMP, f2);
1112 __ MovD(f2, f1);
1113 __ MovD(f1, FTMP);
1114 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115 }
1116 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1117 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1118 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001119 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001120 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1121 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001122 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123 __ Move(TMP, r2);
1124 __ Mfc1(r2, f1);
1125 __ Mtc1(TMP, f1);
1126 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1127 // Swap 2 GPR register pairs.
1128 Register r1 = loc1.AsRegisterPairLow<Register>();
1129 Register r2 = loc2.AsRegisterPairLow<Register>();
1130 __ Move(TMP, r2);
1131 __ Move(r2, r1);
1132 __ Move(r1, TMP);
1133 r1 = loc1.AsRegisterPairHigh<Register>();
1134 r2 = loc2.AsRegisterPairHigh<Register>();
1135 __ Move(TMP, r2);
1136 __ Move(r2, r1);
1137 __ Move(r1, TMP);
1138 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1139 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1140 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1143 : loc2.AsFpuRegister<FRegister>();
1144 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1145 : loc2.AsRegisterPairLow<Register>();
1146 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1147 : loc2.AsRegisterPairHigh<Register>();
1148 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1149 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1150 // unpredictable and the following mfch1 will fail.
1151 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001152 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001153 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001154 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001155 __ Move(r2_l, TMP);
1156 __ Move(r2_h, AT);
1157 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1158 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1159 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1160 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001161 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1162 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001163 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1164 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001165 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1166 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 __ Move(TMP, reg);
1168 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1169 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1170 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1171 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1172 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1173 : loc2.AsRegisterPairLow<Register>();
1174 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1175 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001176 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001177 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1178 : loc2.GetHighStackIndex(kMipsWordSize);
1179 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001180 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001182 __ Move(TMP, reg_h);
1183 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1184 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001185 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1186 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1187 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1188 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1189 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1190 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1191 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001192 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1193 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1194 : loc2.AsFpuRegister<FRegister>();
1195 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001197 __ MovS(FTMP, reg);
1198 __ LoadSFromOffset(reg, SP, offset);
1199 __ StoreSToOffset(FTMP, SP, offset);
1200 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001201 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001202 __ MovD(FTMP, reg);
1203 __ LoadDFromOffset(reg, SP, offset);
1204 __ StoreDToOffset(FTMP, SP, offset);
1205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 } else {
1207 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1208 }
1209}
1210
1211void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1212 __ Pop(static_cast<Register>(reg));
1213}
1214
1215void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1216 __ Push(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1220 // Allocate a scratch register other than TMP, if available.
1221 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1222 // automatically unspilled when the scratch scope object is destroyed).
1223 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1224 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001225 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001226 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1227 __ LoadFromOffset(kLoadWord,
1228 Register(ensure_scratch.GetRegister()),
1229 SP,
1230 index1 + stack_offset);
1231 __ LoadFromOffset(kLoadWord,
1232 TMP,
1233 SP,
1234 index2 + stack_offset);
1235 __ StoreToOffset(kStoreWord,
1236 Register(ensure_scratch.GetRegister()),
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1240 }
1241}
1242
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001243void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1244 __ LoadQFromOffset(FTMP, SP, index1);
1245 __ LoadQFromOffset(FTMP2, SP, index2);
1246 __ StoreQToOffset(FTMP, SP, index2);
1247 __ StoreQToOffset(FTMP2, SP, index1);
1248}
1249
Alexey Frunze73296a72016-06-03 22:51:46 -07001250void CodeGeneratorMIPS::ComputeSpillMask() {
1251 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1252 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1253 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1254 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1255 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1256 // within the stack frame.
1257 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1258 core_spill_mask_ |= (1 << ZERO);
1259 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001260}
1261
1262bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001263 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1265 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1266 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001267 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001268}
1269
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001270static dwarf::Reg DWARFReg(Register reg) {
1271 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1272}
1273
1274// TODO: mapping of floating-point registers to DWARF.
1275
1276void CodeGeneratorMIPS::GenerateFrameEntry() {
1277 __ Bind(&frame_entry_label_);
1278
Vladimir Marko33bff252017-11-01 14:35:42 +00001279 bool do_overflow_check =
1280 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281
1282 if (do_overflow_check) {
1283 __ LoadFromOffset(kLoadWord,
1284 ZERO,
1285 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001286 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001287 RecordPcInfo(nullptr, 0);
1288 }
1289
1290 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001291 CHECK_EQ(fpu_spill_mask_, 0u);
1292 CHECK_EQ(core_spill_mask_, 1u << RA);
1293 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001294 return;
1295 }
1296
1297 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001298 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1299 LOG(FATAL) << "Stack frame larger than "
1300 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001301 }
1302
1303 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304
Alexey Frunze73296a72016-06-03 22:51:46 -07001305 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001306 __ IncreaseFrameSize(ofs);
1307
Alexey Frunze73296a72016-06-03 22:51:46 -07001308 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1309 Register reg = static_cast<Register>(MostSignificantBit(mask));
1310 mask ^= 1u << reg;
1311 ofs -= kMipsWordSize;
1312 // The ZERO register is only included for alignment.
1313 if (reg != ZERO) {
1314 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 __ cfi().RelOffset(DWARFReg(reg), ofs);
1316 }
1317 }
1318
Alexey Frunze73296a72016-06-03 22:51:46 -07001319 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1320 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1321 mask ^= 1u << reg;
1322 ofs -= kMipsDoublewordSize;
1323 __ StoreDToOffset(reg, SP, ofs);
1324 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 }
1326
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001327 // Save the current method if we need it. Note that we do not
1328 // do this in HCurrentMethod, as the instruction might have been removed
1329 // in the SSA graph.
1330 if (RequiresCurrentMethod()) {
1331 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1332 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001333
1334 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1335 // Initialize should deoptimize flag to 0.
1336 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001338}
1339
1340void CodeGeneratorMIPS::GenerateFrameExit() {
1341 __ cfi().RememberState();
1342
1343 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001344 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345
Alexey Frunze73296a72016-06-03 22:51:46 -07001346 // For better instruction scheduling restore RA before other registers.
1347 uint32_t ofs = GetFrameSize();
1348 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1349 Register reg = static_cast<Register>(MostSignificantBit(mask));
1350 mask ^= 1u << reg;
1351 ofs -= kMipsWordSize;
1352 // The ZERO register is only included for alignment.
1353 if (reg != ZERO) {
1354 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355 __ cfi().Restore(DWARFReg(reg));
1356 }
1357 }
1358
Alexey Frunze73296a72016-06-03 22:51:46 -07001359 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1360 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1361 mask ^= 1u << reg;
1362 ofs -= kMipsDoublewordSize;
1363 __ LoadDFromOffset(reg, SP, ofs);
1364 // TODO: __ cfi().Restore(DWARFReg(reg));
1365 }
1366
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001367 size_t frame_size = GetFrameSize();
1368 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1369 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1370 bool reordering = __ SetReorder(false);
1371 if (exchange) {
1372 __ Jr(RA);
1373 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1374 } else {
1375 __ DecreaseFrameSize(frame_size);
1376 __ Jr(RA);
1377 __ Nop(); // In delay slot.
1378 }
1379 __ SetReorder(reordering);
1380 } else {
1381 __ Jr(RA);
1382 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001383 }
1384
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001385 __ cfi().RestoreState();
1386 __ cfi().DefCFAOffset(GetFrameSize());
1387}
1388
1389void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1390 __ Bind(GetLabelOf(block));
1391}
1392
Lena Djokicca8c2952017-05-29 11:31:46 +02001393VectorRegister VectorRegisterFrom(Location location) {
1394 DCHECK(location.IsFpuRegister());
1395 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1396}
1397
Lena Djokic8098da92017-06-28 12:07:50 +02001398void CodeGeneratorMIPS::MoveLocation(Location destination,
1399 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001400 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001401 if (source.Equals(destination)) {
1402 return;
1403 }
1404
Lena Djokic8098da92017-06-28 12:07:50 +02001405 if (source.IsConstant()) {
1406 MoveConstant(destination, source.GetConstant());
1407 } else {
1408 if (destination.IsRegister()) {
1409 if (source.IsRegister()) {
1410 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1411 } else if (source.IsFpuRegister()) {
1412 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1413 } else {
1414 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001416 }
1417 } else if (destination.IsRegisterPair()) {
1418 if (source.IsRegisterPair()) {
1419 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1420 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 Register dst_high = destination.AsRegisterPairHigh<Register>();
1423 Register dst_low = destination.AsRegisterPairLow<Register>();
1424 FRegister src = source.AsFpuRegister<FRegister>();
1425 __ Mfc1(dst_low, src);
1426 __ MoveFromFpuHigh(dst_high, src);
1427 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001428 DCHECK(source.IsDoubleStackSlot())
1429 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001430 int32_t off = source.GetStackIndex();
1431 Register r = destination.AsRegisterPairLow<Register>();
1432 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1433 }
1434 } else if (destination.IsFpuRegister()) {
1435 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001436 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001437 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1438 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001439 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001440 FRegister dst = destination.AsFpuRegister<FRegister>();
1441 Register src_high = source.AsRegisterPairHigh<Register>();
1442 Register src_low = source.AsRegisterPairLow<Register>();
1443 __ Mtc1(src_low, dst);
1444 __ MoveToFpuHigh(src_high, dst);
1445 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001446 if (GetGraph()->HasSIMD()) {
1447 __ MoveV(VectorRegisterFrom(destination),
1448 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001449 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001451 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1452 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001453 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001454 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1455 }
Lena Djokic8098da92017-06-28 12:07:50 +02001456 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001457 } else if (source.IsSIMDStackSlot()) {
1458 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001461 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001464 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1465 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (destination.IsSIMDStackSlot()) {
1468 if (source.IsFpuRegister()) {
1469 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1470 } else {
1471 DCHECK(source.IsSIMDStackSlot());
1472 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1473 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1474 }
Lena Djokic8098da92017-06-28 12:07:50 +02001475 } else if (destination.IsDoubleStackSlot()) {
1476 int32_t dst_offset = destination.GetStackIndex();
1477 if (source.IsRegisterPair()) {
1478 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1479 } else if (source.IsFpuRegister()) {
1480 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1481 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001482 DCHECK(source.IsDoubleStackSlot())
1483 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001484 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1485 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1486 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1487 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001489 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001490 DCHECK(destination.IsStackSlot()) << destination;
1491 int32_t dst_offset = destination.GetStackIndex();
1492 if (source.IsRegister()) {
1493 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1494 } else if (source.IsFpuRegister()) {
1495 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1496 } else {
1497 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1498 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1499 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1500 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001501 }
1502 }
1503}
1504
1505void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1506 if (c->IsIntConstant() || c->IsNullConstant()) {
1507 // Move 32 bit constant.
1508 int32_t value = GetInt32ValueOf(c);
1509 if (destination.IsRegister()) {
1510 Register dst = destination.AsRegister<Register>();
1511 __ LoadConst32(dst, value);
1512 } else {
1513 DCHECK(destination.IsStackSlot())
1514 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001515 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001516 }
1517 } else if (c->IsLongConstant()) {
1518 // Move 64 bit constant.
1519 int64_t value = GetInt64ValueOf(c);
1520 if (destination.IsRegisterPair()) {
1521 Register r_h = destination.AsRegisterPairHigh<Register>();
1522 Register r_l = destination.AsRegisterPairLow<Register>();
1523 __ LoadConst64(r_h, r_l, value);
1524 } else {
1525 DCHECK(destination.IsDoubleStackSlot())
1526 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001527 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001528 }
1529 } else if (c->IsFloatConstant()) {
1530 // Move 32 bit float constant.
1531 int32_t value = GetInt32ValueOf(c);
1532 if (destination.IsFpuRegister()) {
1533 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1534 } else {
1535 DCHECK(destination.IsStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else {
1540 // Move 64 bit double constant.
1541 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1542 int64_t value = GetInt64ValueOf(c);
1543 if (destination.IsFpuRegister()) {
1544 FRegister fd = destination.AsFpuRegister<FRegister>();
1545 __ LoadDConst64(fd, value, TMP);
1546 } else {
1547 DCHECK(destination.IsDoubleStackSlot())
1548 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001549 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001550 }
1551 }
1552}
1553
1554void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1555 DCHECK(destination.IsRegister());
1556 Register dst = destination.AsRegister<Register>();
1557 __ LoadConst32(dst, value);
1558}
1559
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1561 if (location.IsRegister()) {
1562 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001563 } else if (location.IsRegisterPair()) {
1564 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1565 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001566 } else {
1567 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1568 }
1569}
1570
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001571template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001572inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1573 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001574 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001575 for (const PcRelativePatchInfo& info : infos) {
1576 const DexFile& dex_file = info.target_dex_file;
1577 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001578 DCHECK(info.label.IsBound());
1579 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001580 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1581 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001582 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1583 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1584 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001586 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 }
1588}
1589
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001590void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001591 DCHECK(linker_patches->empty());
1592 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001593 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001594 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001595 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001596 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001597 pc_relative_string_patches_.size() +
1598 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001599 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001600 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001601 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1602 pc_relative_method_patches_, linker_patches);
1603 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1604 pc_relative_type_patches_, linker_patches);
1605 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1606 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001607 } else {
1608 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001609 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1610 pc_relative_type_patches_, linker_patches);
1611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1612 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001613 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001614 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1615 method_bss_entry_patches_, linker_patches);
1616 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1617 type_bss_entry_patches_, linker_patches);
1618 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1619 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001620 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001621}
1622
Vladimir Marko65979462017-05-19 17:25:12 +01001623CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001624 MethodReference target_method,
1625 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001626 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001627 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001628 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001629 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001630}
1631
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001632CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001633 MethodReference target_method,
1634 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001635 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001636 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001637 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001638 &method_bss_entry_patches_);
1639}
1640
Alexey Frunze06a46c42016-07-19 15:00:40 -07001641CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001642 const DexFile& dex_file,
1643 dex::TypeIndex type_index,
1644 const PcRelativePatchInfo* info_high) {
1645 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001646}
1647
Vladimir Marko1998cd02017-01-13 13:02:58 +00001648CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001649 const DexFile& dex_file,
1650 dex::TypeIndex type_index,
1651 const PcRelativePatchInfo* info_high) {
1652 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001653}
1654
Vladimir Marko65979462017-05-19 17:25:12 +01001655CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001656 const DexFile& dex_file,
1657 dex::StringIndex string_index,
1658 const PcRelativePatchInfo* info_high) {
1659 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001660}
1661
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001662CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1663 const DexFile& dex_file,
1664 dex::StringIndex string_index,
1665 const PcRelativePatchInfo* info_high) {
1666 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1667}
1668
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001669CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001670 const DexFile& dex_file,
1671 uint32_t offset_or_index,
1672 const PcRelativePatchInfo* info_high,
1673 ArenaDeque<PcRelativePatchInfo>* patches) {
1674 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001675 return &patches->back();
1676}
1677
Alexey Frunze06a46c42016-07-19 15:00:40 -07001678Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1679 return map->GetOrCreate(
1680 value,
1681 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1682}
1683
Alexey Frunze06a46c42016-07-19 15:00:40 -07001684Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001685 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001686}
1687
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001689 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001690 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001691 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001692 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001693 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001694 if (GetInstructionSetFeatures().IsR6()) {
1695 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001696 __ Bind(&info_high->label);
1697 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001698 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001699 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001700 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001701 } else {
1702 // If base is ZERO, emit NAL to obtain the actual base.
1703 if (base == ZERO) {
1704 // Generate a dummy PC-relative call to obtain PC.
1705 __ Nal();
1706 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001707 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001708 __ Lui(out, /* placeholder */ 0x1234);
1709 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1710 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1711 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001713 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001714 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001715 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001716 __ Addu(out, out, (base == ZERO) ? RA : base);
1717 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001718 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001719 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720}
1721
Alexey Frunze627c1a02017-01-30 19:28:14 -08001722CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1723 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001724 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001725 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001726 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1727 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001728 return &jit_string_patches_.back();
1729}
1730
1731CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1732 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001733 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001734 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001735 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1736 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001737 return &jit_class_patches_.back();
1738}
1739
1740void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1741 const uint8_t* roots_data,
1742 const CodeGeneratorMIPS::JitPatchInfo& info,
1743 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001744 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1745 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001746 uintptr_t address =
1747 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1748 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1749 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001750 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1751 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1752 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1753 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001754 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001755 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1756 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001757 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001759 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1760 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001761 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001762 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1763 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001764}
1765
1766void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1767 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001768 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1769 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001770 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001771 }
1772 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001773 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1774 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001775 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001776 }
1777}
1778
Goran Jakovljevice114da22016-12-26 14:21:43 +01001779void CodeGeneratorMIPS::MarkGCCard(Register object,
1780 Register value,
1781 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001782 MipsLabel done;
1783 Register card = AT;
1784 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001785 if (value_can_be_null) {
1786 __ Beqz(value, &done);
1787 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001788 __ LoadFromOffset(kLoadWord,
1789 card,
1790 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001791 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001792 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1793 __ Addu(temp, card, temp);
1794 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001795 if (value_can_be_null) {
1796 __ Bind(&done);
1797 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001798}
1799
David Brazdil58282f42016-01-14 12:45:10 +00001800void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001801 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1802 blocked_core_registers_[ZERO] = true;
1803 blocked_core_registers_[K0] = true;
1804 blocked_core_registers_[K1] = true;
1805 blocked_core_registers_[GP] = true;
1806 blocked_core_registers_[SP] = true;
1807 blocked_core_registers_[RA] = true;
1808
1809 // AT and TMP(T8) are used as temporary/scratch registers
1810 // (similar to how AT is used by MIPS assemblers).
1811 blocked_core_registers_[AT] = true;
1812 blocked_core_registers_[TMP] = true;
1813 blocked_fpu_registers_[FTMP] = true;
1814
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001815 if (GetInstructionSetFeatures().HasMsa()) {
1816 // To be used just for MSA instructions.
1817 blocked_fpu_registers_[FTMP2] = true;
1818 }
1819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001820 // Reserve suspend and thread registers.
1821 blocked_core_registers_[S0] = true;
1822 blocked_core_registers_[TR] = true;
1823
1824 // Reserve T9 for function calls
1825 blocked_core_registers_[T9] = true;
1826
1827 // Reserve odd-numbered FPU registers.
1828 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1829 blocked_fpu_registers_[i] = true;
1830 }
1831
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001832 if (GetGraph()->IsDebuggable()) {
1833 // Stubs do not save callee-save floating point registers. If the graph
1834 // is debuggable, we need to deal with these registers differently. For
1835 // now, just block them.
1836 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1837 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1838 }
1839 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840}
1841
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001842size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1843 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1844 return kMipsWordSize;
1845}
1846
1847size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1848 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1849 return kMipsWordSize;
1850}
1851
1852size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001853 if (GetGraph()->HasSIMD()) {
1854 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1855 } else {
1856 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1857 }
1858 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001859}
1860
1861size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001862 if (GetGraph()->HasSIMD()) {
1863 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1864 } else {
1865 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1866 }
1867 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868}
1869
1870void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001871 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872}
1873
1874void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001875 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876}
1877
Serban Constantinescufca16662016-07-14 09:21:59 +01001878constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1879
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1881 HInstruction* instruction,
1882 uint32_t dex_pc,
1883 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001884 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001885 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1886 IsDirectEntrypoint(entrypoint));
1887 if (EntrypointRequiresStackMap(entrypoint)) {
1888 RecordPcInfo(instruction, dex_pc, slow_path);
1889 }
1890}
1891
1892void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1893 HInstruction* instruction,
1894 SlowPathCode* slow_path,
1895 bool direct) {
1896 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1897 GenerateInvokeRuntime(entry_point_offset, direct);
1898}
1899
1900void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001901 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001902 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001903 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001904 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 // Reserve argument space on stack (for $a0-$a3) for
1906 // entrypoints that directly reference native implementations.
1907 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001908 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001910 } else {
1911 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001913 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914}
1915
1916void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1917 Register class_reg) {
Vladimir 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
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00001932void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1933 Register temp) {
1934 uint32_t path_to_root = check->GetBitstringPathToRoot();
1935 uint32_t mask = check->GetBitstringMask();
1936 DCHECK(IsPowerOfTwo(mask + 1));
1937 size_t mask_bits = WhichPowerOf2(mask + 1);
1938
1939 if (mask_bits == 16u) {
1940 // Load only the bitstring part of the status word.
1941 __ LoadFromOffset(
1942 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1943 // Compare the bitstring bits using XOR.
1944 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1945 } else {
1946 // /* uint32_t */ temp = temp->status_
1947 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1948 // Compare the bitstring bits using XOR.
1949 if (IsUint<16>(path_to_root)) {
1950 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1951 } else {
1952 __ LoadConst32(TMP, path_to_root);
1953 __ Xor(temp, temp, TMP);
1954 }
1955 // Shift out bits that do not contribute to the comparison.
1956 __ Sll(temp, temp, 32 - mask_bits);
1957 }
1958}
1959
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1961 __ Sync(0); // Only stype 0 is supported.
1962}
1963
1964void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1965 HBasicBlock* successor) {
1966 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001967 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1968
1969 if (slow_path == nullptr) {
1970 slow_path =
1971 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1972 instruction->SetSlowPath(slow_path);
1973 codegen_->AddSlowPath(slow_path);
1974 if (successor != nullptr) {
1975 DCHECK(successor->IsLoopHeader());
1976 }
1977 } else {
1978 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1979 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980
1981 __ LoadFromOffset(kLoadUnsignedHalfword,
1982 TMP,
1983 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001984 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985 if (successor == nullptr) {
1986 __ Bnez(TMP, slow_path->GetEntryLabel());
1987 __ Bind(slow_path->GetReturnLabel());
1988 } else {
1989 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1990 __ B(slow_path->GetEntryLabel());
1991 // slow_path will return to GetLabelOf(successor).
1992 }
1993}
1994
1995InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1996 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001997 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001998 assembler_(codegen->GetAssembler()),
1999 codegen_(codegen) {}
2000
2001void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2002 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002003 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002005 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 locations->SetInAt(0, Location::RequiresRegister());
2009 HInstruction* right = instruction->InputAt(1);
2010 bool can_use_imm = false;
2011 if (right->IsConstant()) {
2012 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2013 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2014 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002016 DCHECK(instruction->IsSub() || instruction->IsAdd());
2017 if (instruction->IsSub()) {
2018 imm = -imm;
2019 }
2020 if (isR6) {
2021 bool single_use = right->GetUses().HasExactlyOneElement();
2022 int16_t imm_high = High16Bits(imm);
2023 int16_t imm_low = Low16Bits(imm);
2024 if (imm_low < 0) {
2025 imm_high += 1;
2026 }
2027 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2028 } else {
2029 can_use_imm = IsInt<16>(imm);
2030 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031 }
2032 }
2033 if (can_use_imm)
2034 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2035 else
2036 locations->SetInAt(1, Location::RequiresRegister());
2037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2038 break;
2039 }
2040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002043 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045 break;
2046 }
2047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 case DataType::Type::kFloat32:
2049 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 DCHECK(instruction->IsAdd() || instruction->IsSub());
2051 locations->SetInAt(0, Location::RequiresFpuRegister());
2052 locations->SetInAt(1, Location::RequiresFpuRegister());
2053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2058 }
2059}
2060
2061void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002064 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002065
2066 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 Register dst = locations->Out().AsRegister<Register>();
2069 Register lhs = locations->InAt(0).AsRegister<Register>();
2070 Location rhs_location = locations->InAt(1);
2071
2072 Register rhs_reg = ZERO;
2073 int32_t rhs_imm = 0;
2074 bool use_imm = rhs_location.IsConstant();
2075 if (use_imm) {
2076 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2077 } else {
2078 rhs_reg = rhs_location.AsRegister<Register>();
2079 }
2080
2081 if (instruction->IsAnd()) {
2082 if (use_imm)
2083 __ Andi(dst, lhs, rhs_imm);
2084 else
2085 __ And(dst, lhs, rhs_reg);
2086 } else if (instruction->IsOr()) {
2087 if (use_imm)
2088 __ Ori(dst, lhs, rhs_imm);
2089 else
2090 __ Or(dst, lhs, rhs_reg);
2091 } else if (instruction->IsXor()) {
2092 if (use_imm)
2093 __ Xori(dst, lhs, rhs_imm);
2094 else
2095 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002096 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002097 DCHECK(instruction->IsAdd() || instruction->IsSub());
2098 if (use_imm) {
2099 if (instruction->IsSub()) {
2100 rhs_imm = -rhs_imm;
2101 }
2102 if (IsInt<16>(rhs_imm)) {
2103 __ Addiu(dst, lhs, rhs_imm);
2104 } else {
2105 DCHECK(isR6);
2106 int16_t rhs_imm_high = High16Bits(rhs_imm);
2107 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2108 if (rhs_imm_low < 0) {
2109 rhs_imm_high += 1;
2110 }
2111 __ Aui(dst, lhs, rhs_imm_high);
2112 if (rhs_imm_low != 0) {
2113 __ Addiu(dst, dst, rhs_imm_low);
2114 }
2115 }
2116 } else if (instruction->IsAdd()) {
2117 __ Addu(dst, lhs, rhs_reg);
2118 } else {
2119 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002120 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002121 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 }
2123 break;
2124 }
2125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002127 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2128 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2129 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2130 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002131 Location rhs_location = locations->InAt(1);
2132 bool use_imm = rhs_location.IsConstant();
2133 if (!use_imm) {
2134 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2135 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2136 if (instruction->IsAnd()) {
2137 __ And(dst_low, lhs_low, rhs_low);
2138 __ And(dst_high, lhs_high, rhs_high);
2139 } else if (instruction->IsOr()) {
2140 __ Or(dst_low, lhs_low, rhs_low);
2141 __ Or(dst_high, lhs_high, rhs_high);
2142 } else if (instruction->IsXor()) {
2143 __ Xor(dst_low, lhs_low, rhs_low);
2144 __ Xor(dst_high, lhs_high, rhs_high);
2145 } else if (instruction->IsAdd()) {
2146 if (lhs_low == rhs_low) {
2147 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2148 __ Slt(TMP, lhs_low, ZERO);
2149 __ Addu(dst_low, lhs_low, rhs_low);
2150 } else {
2151 __ Addu(dst_low, lhs_low, rhs_low);
2152 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2153 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2154 }
2155 __ Addu(dst_high, lhs_high, rhs_high);
2156 __ Addu(dst_high, dst_high, TMP);
2157 } else {
2158 DCHECK(instruction->IsSub());
2159 __ Sltu(TMP, lhs_low, rhs_low);
2160 __ Subu(dst_low, lhs_low, rhs_low);
2161 __ Subu(dst_high, lhs_high, rhs_high);
2162 __ Subu(dst_high, dst_high, TMP);
2163 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002164 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002165 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2166 if (instruction->IsOr()) {
2167 uint32_t low = Low32Bits(value);
2168 uint32_t high = High32Bits(value);
2169 if (IsUint<16>(low)) {
2170 if (dst_low != lhs_low || low != 0) {
2171 __ Ori(dst_low, lhs_low, low);
2172 }
2173 } else {
2174 __ LoadConst32(TMP, low);
2175 __ Or(dst_low, lhs_low, TMP);
2176 }
2177 if (IsUint<16>(high)) {
2178 if (dst_high != lhs_high || high != 0) {
2179 __ Ori(dst_high, lhs_high, high);
2180 }
2181 } else {
2182 if (high != low) {
2183 __ LoadConst32(TMP, high);
2184 }
2185 __ Or(dst_high, lhs_high, TMP);
2186 }
2187 } else if (instruction->IsXor()) {
2188 uint32_t low = Low32Bits(value);
2189 uint32_t high = High32Bits(value);
2190 if (IsUint<16>(low)) {
2191 if (dst_low != lhs_low || low != 0) {
2192 __ Xori(dst_low, lhs_low, low);
2193 }
2194 } else {
2195 __ LoadConst32(TMP, low);
2196 __ Xor(dst_low, lhs_low, TMP);
2197 }
2198 if (IsUint<16>(high)) {
2199 if (dst_high != lhs_high || high != 0) {
2200 __ Xori(dst_high, lhs_high, high);
2201 }
2202 } else {
2203 if (high != low) {
2204 __ LoadConst32(TMP, high);
2205 }
2206 __ Xor(dst_high, lhs_high, TMP);
2207 }
2208 } else if (instruction->IsAnd()) {
2209 uint32_t low = Low32Bits(value);
2210 uint32_t high = High32Bits(value);
2211 if (IsUint<16>(low)) {
2212 __ Andi(dst_low, lhs_low, low);
2213 } else if (low != 0xFFFFFFFF) {
2214 __ LoadConst32(TMP, low);
2215 __ And(dst_low, lhs_low, TMP);
2216 } else if (dst_low != lhs_low) {
2217 __ Move(dst_low, lhs_low);
2218 }
2219 if (IsUint<16>(high)) {
2220 __ Andi(dst_high, lhs_high, high);
2221 } else if (high != 0xFFFFFFFF) {
2222 if (high != low) {
2223 __ LoadConst32(TMP, high);
2224 }
2225 __ And(dst_high, lhs_high, TMP);
2226 } else if (dst_high != lhs_high) {
2227 __ Move(dst_high, lhs_high);
2228 }
2229 } else {
2230 if (instruction->IsSub()) {
2231 value = -value;
2232 } else {
2233 DCHECK(instruction->IsAdd());
2234 }
2235 int32_t low = Low32Bits(value);
2236 int32_t high = High32Bits(value);
2237 if (IsInt<16>(low)) {
2238 if (dst_low != lhs_low || low != 0) {
2239 __ Addiu(dst_low, lhs_low, low);
2240 }
2241 if (low != 0) {
2242 __ Sltiu(AT, dst_low, low);
2243 }
2244 } else {
2245 __ LoadConst32(TMP, low);
2246 __ Addu(dst_low, lhs_low, TMP);
2247 __ Sltu(AT, dst_low, TMP);
2248 }
2249 if (IsInt<16>(high)) {
2250 if (dst_high != lhs_high || high != 0) {
2251 __ Addiu(dst_high, lhs_high, high);
2252 }
2253 } else {
2254 if (high != low) {
2255 __ LoadConst32(TMP, high);
2256 }
2257 __ Addu(dst_high, lhs_high, TMP);
2258 }
2259 if (low != 0) {
2260 __ Addu(dst_high, dst_high, AT);
2261 }
2262 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002263 }
2264 break;
2265 }
2266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 case DataType::Type::kFloat32:
2268 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2270 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2271 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2272 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 __ AddS(dst, lhs, rhs);
2275 } else {
2276 __ AddD(dst, lhs, rhs);
2277 }
2278 } else {
2279 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002280 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 __ SubS(dst, lhs, rhs);
2282 } else {
2283 __ SubD(dst, lhs, rhs);
2284 }
2285 }
2286 break;
2287 }
2288
2289 default:
2290 LOG(FATAL) << "Unexpected binary operation type " << type;
2291 }
2292}
2293
2294void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002295 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002296
Vladimir Markoca6fff82017-10-03 14:49:14 +01002297 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002299 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2304 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002305 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306 locations->SetInAt(0, Location::RequiresRegister());
2307 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2308 locations->SetOut(Location::RequiresRegister());
2309 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310 default:
2311 LOG(FATAL) << "Unexpected shift type " << type;
2312 }
2313}
2314
2315static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2316
2317void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002318 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002319 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321
2322 Location rhs_location = locations->InAt(1);
2323 bool use_imm = rhs_location.IsConstant();
2324 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2325 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002326 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002328 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002329 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2330 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002331
2332 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 Register dst = locations->Out().AsRegister<Register>();
2335 Register lhs = locations->InAt(0).AsRegister<Register>();
2336 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 if (shift_value == 0) {
2338 if (dst != lhs) {
2339 __ Move(dst, lhs);
2340 }
2341 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002342 __ Sll(dst, lhs, shift_value);
2343 } else if (instr->IsShr()) {
2344 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002347 } else {
2348 if (has_ins_rotr) {
2349 __ Rotr(dst, lhs, shift_value);
2350 } else {
2351 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2352 __ Srl(dst, lhs, shift_value);
2353 __ Or(dst, dst, TMP);
2354 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 }
2356 } else {
2357 if (instr->IsShl()) {
2358 __ Sllv(dst, lhs, rhs_reg);
2359 } else if (instr->IsShr()) {
2360 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002361 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002363 } else {
2364 if (has_ins_rotr) {
2365 __ Rotrv(dst, lhs, rhs_reg);
2366 } else {
2367 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002368 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2369 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2370 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2371 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2372 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002373 __ Sllv(TMP, lhs, TMP);
2374 __ Srlv(dst, lhs, rhs_reg);
2375 __ Or(dst, dst, TMP);
2376 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002377 }
2378 }
2379 break;
2380 }
2381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002382 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2384 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2385 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2386 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2387 if (use_imm) {
2388 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002389 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002390 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002391 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002392 if (instr->IsShl()) {
2393 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2394 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2395 __ Sll(dst_low, lhs_low, shift_value);
2396 } else if (instr->IsShr()) {
2397 __ Srl(dst_low, lhs_low, shift_value);
2398 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2399 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002400 } else if (instr->IsUShr()) {
2401 __ Srl(dst_low, lhs_low, shift_value);
2402 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2403 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 } else {
2405 __ Srl(dst_low, lhs_low, shift_value);
2406 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2407 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002408 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002409 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002410 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002411 if (instr->IsShl()) {
2412 __ Sll(dst_low, lhs_low, shift_value);
2413 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2414 __ Sll(dst_high, lhs_high, shift_value);
2415 __ Or(dst_high, dst_high, TMP);
2416 } else if (instr->IsShr()) {
2417 __ Sra(dst_high, lhs_high, shift_value);
2418 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2419 __ Srl(dst_low, lhs_low, shift_value);
2420 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002421 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002422 __ Srl(dst_high, lhs_high, shift_value);
2423 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2424 __ Srl(dst_low, lhs_low, shift_value);
2425 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002426 } else {
2427 __ Srl(TMP, lhs_low, shift_value);
2428 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2429 __ Or(dst_low, dst_low, TMP);
2430 __ Srl(TMP, lhs_high, shift_value);
2431 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2432 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 }
2435 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002436 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002438 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 __ Move(dst_low, ZERO);
2440 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002441 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002443 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002444 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002446 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002447 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002448 // 64-bit rotation by 32 is just a swap.
2449 __ Move(dst_low, lhs_high);
2450 __ Move(dst_high, lhs_low);
2451 } else {
2452 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002453 __ Srl(dst_low, lhs_high, shift_value_high);
2454 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2455 __ Srl(dst_high, lhs_low, shift_value_high);
2456 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002457 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002458 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2459 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002460 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002461 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2462 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002463 __ Or(dst_high, dst_high, TMP);
2464 }
2465 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466 }
2467 }
2468 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002469 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002470 MipsLabel done;
2471 if (instr->IsShl()) {
2472 __ Sllv(dst_low, lhs_low, rhs_reg);
2473 __ Nor(AT, ZERO, rhs_reg);
2474 __ Srl(TMP, lhs_low, 1);
2475 __ Srlv(TMP, TMP, AT);
2476 __ Sllv(dst_high, lhs_high, rhs_reg);
2477 __ Or(dst_high, dst_high, TMP);
2478 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002479 if (isR6) {
2480 __ Beqzc(TMP, &done, /* is_bare */ true);
2481 __ Move(dst_high, dst_low);
2482 __ Move(dst_low, ZERO);
2483 } else {
2484 __ Movn(dst_high, dst_low, TMP);
2485 __ Movn(dst_low, ZERO, TMP);
2486 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002487 } else if (instr->IsShr()) {
2488 __ Srav(dst_high, lhs_high, rhs_reg);
2489 __ Nor(AT, ZERO, rhs_reg);
2490 __ Sll(TMP, lhs_high, 1);
2491 __ Sllv(TMP, TMP, AT);
2492 __ Srlv(dst_low, lhs_low, rhs_reg);
2493 __ Or(dst_low, dst_low, TMP);
2494 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002495 if (isR6) {
2496 __ Beqzc(TMP, &done, /* is_bare */ true);
2497 __ Move(dst_low, dst_high);
2498 __ Sra(dst_high, dst_high, 31);
2499 } else {
2500 __ Sra(AT, dst_high, 31);
2501 __ Movn(dst_low, dst_high, TMP);
2502 __ Movn(dst_high, AT, TMP);
2503 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002504 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002505 __ Srlv(dst_high, lhs_high, rhs_reg);
2506 __ Nor(AT, ZERO, rhs_reg);
2507 __ Sll(TMP, lhs_high, 1);
2508 __ Sllv(TMP, TMP, AT);
2509 __ Srlv(dst_low, lhs_low, rhs_reg);
2510 __ Or(dst_low, dst_low, TMP);
2511 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002512 if (isR6) {
2513 __ Beqzc(TMP, &done, /* is_bare */ true);
2514 __ Move(dst_low, dst_high);
2515 __ Move(dst_high, ZERO);
2516 } else {
2517 __ Movn(dst_low, dst_high, TMP);
2518 __ Movn(dst_high, ZERO, TMP);
2519 }
2520 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002521 __ Nor(AT, ZERO, rhs_reg);
2522 __ Srlv(TMP, lhs_low, rhs_reg);
2523 __ Sll(dst_low, lhs_high, 1);
2524 __ Sllv(dst_low, dst_low, AT);
2525 __ Or(dst_low, dst_low, TMP);
2526 __ Srlv(TMP, lhs_high, rhs_reg);
2527 __ Sll(dst_high, lhs_low, 1);
2528 __ Sllv(dst_high, dst_high, AT);
2529 __ Or(dst_high, dst_high, TMP);
2530 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002531 if (isR6) {
2532 __ Beqzc(TMP, &done, /* is_bare */ true);
2533 __ Move(TMP, dst_high);
2534 __ Move(dst_high, dst_low);
2535 __ Move(dst_low, TMP);
2536 } else {
2537 __ Movn(AT, dst_high, TMP);
2538 __ Movn(dst_high, dst_low, TMP);
2539 __ Movn(dst_low, AT, TMP);
2540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002541 }
2542 __ Bind(&done);
2543 }
2544 break;
2545 }
2546
2547 default:
2548 LOG(FATAL) << "Unexpected shift operation type " << type;
2549 }
2550}
2551
2552void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2553 HandleBinaryOp(instruction);
2554}
2555
2556void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2557 HandleBinaryOp(instruction);
2558}
2559
2560void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2561 HandleBinaryOp(instruction);
2562}
2563
2564void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2565 HandleBinaryOp(instruction);
2566}
2567
2568void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002569 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002570 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002572 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002573 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2574 object_array_get_with_read_barrier
2575 ? LocationSummary::kCallOnSlowPath
2576 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002577 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2578 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2579 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580 locations->SetInAt(0, Location::RequiresRegister());
2581 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002583 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2584 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002585 // The output overlaps in the case of an object array get with
2586 // read barriers enabled: we do not want the move to overwrite the
2587 // array's location, as we need it to emit the read barrier.
2588 locations->SetOut(Location::RequiresRegister(),
2589 object_array_get_with_read_barrier
2590 ? Location::kOutputOverlap
2591 : Location::kNoOutputOverlap);
2592 }
2593 // We need a temporary register for the read barrier marking slow
2594 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2595 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002596 bool temp_needed = instruction->GetIndex()->IsConstant()
2597 ? !kBakerReadBarrierThunksEnableForFields
2598 : !kBakerReadBarrierThunksEnableForArrays;
2599 if (temp_needed) {
2600 locations->AddTemp(Location::RequiresRegister());
2601 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002602 }
2603}
2604
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002605static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2606 auto null_checker = [codegen, instruction]() {
2607 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002608 };
2609 return null_checker;
2610}
2611
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002612void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2613 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002614 Location obj_loc = locations->InAt(0);
2615 Register obj = obj_loc.AsRegister<Register>();
2616 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002618 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002619 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002622 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2623 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002625 case DataType::Type::kBool:
2626 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002627 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 if (index.IsConstant()) {
2629 size_t offset =
2630 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002631 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002632 } else {
2633 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002634 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002635 }
2636 break;
2637 }
2638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002640 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002641 if (index.IsConstant()) {
2642 size_t offset =
2643 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002644 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002645 } else {
2646 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002647 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002648 }
2649 break;
2650 }
2651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002653 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002654 if (maybe_compressed_char_at) {
2655 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2656 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2657 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2658 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2659 "Expecting 0=compressed, 1=uncompressed");
2660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002661 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002662 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2663 if (maybe_compressed_char_at) {
2664 MipsLabel uncompressed_load, done;
2665 __ Bnez(TMP, &uncompressed_load);
2666 __ LoadFromOffset(kLoadUnsignedByte,
2667 out,
2668 obj,
2669 data_offset + (const_index << TIMES_1));
2670 __ B(&done);
2671 __ Bind(&uncompressed_load);
2672 __ LoadFromOffset(kLoadUnsignedHalfword,
2673 out,
2674 obj,
2675 data_offset + (const_index << TIMES_2));
2676 __ Bind(&done);
2677 } else {
2678 __ LoadFromOffset(kLoadUnsignedHalfword,
2679 out,
2680 obj,
2681 data_offset + (const_index << TIMES_2),
2682 null_checker);
2683 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002684 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002685 Register index_reg = index.AsRegister<Register>();
2686 if (maybe_compressed_char_at) {
2687 MipsLabel uncompressed_load, done;
2688 __ Bnez(TMP, &uncompressed_load);
2689 __ Addu(TMP, obj, index_reg);
2690 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2691 __ B(&done);
2692 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002693 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002694 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2695 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002696 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2697 __ Addu(TMP, index_reg, obj);
2698 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002699 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002700 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002701 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2702 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 }
2704 break;
2705 }
2706
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002707 case DataType::Type::kInt16: {
2708 Register out = out_loc.AsRegister<Register>();
2709 if (index.IsConstant()) {
2710 size_t offset =
2711 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2712 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002713 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2714 __ Addu(TMP, index.AsRegister<Register>(), obj);
2715 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002716 } else {
2717 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2718 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2719 }
2720 break;
2721 }
2722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002724 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002725 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002726 if (index.IsConstant()) {
2727 size_t offset =
2728 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002729 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002730 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2731 __ Addu(TMP, index.AsRegister<Register>(), obj);
2732 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002733 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002734 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002735 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002736 }
2737 break;
2738 }
2739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002741 static_assert(
2742 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2743 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2744 // /* HeapReference<Object> */ out =
2745 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2746 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002747 bool temp_needed = index.IsConstant()
2748 ? !kBakerReadBarrierThunksEnableForFields
2749 : !kBakerReadBarrierThunksEnableForArrays;
2750 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002751 // Note that a potential implicit null check is handled in this
2752 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002753 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2754 if (index.IsConstant()) {
2755 // Array load with a constant index can be treated as a field load.
2756 size_t offset =
2757 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2758 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2759 out_loc,
2760 obj,
2761 offset,
2762 temp,
2763 /* needs_null_check */ false);
2764 } else {
2765 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2766 out_loc,
2767 obj,
2768 data_offset,
2769 index,
2770 temp,
2771 /* needs_null_check */ false);
2772 }
Alexey Frunze15958152017-02-09 19:08:30 -08002773 } else {
2774 Register out = out_loc.AsRegister<Register>();
2775 if (index.IsConstant()) {
2776 size_t offset =
2777 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2778 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2779 // If read barriers are enabled, emit read barriers other than
2780 // Baker's using a slow path (and also unpoison the loaded
2781 // reference, if heap poisoning is enabled).
2782 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2783 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002784 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002785 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2786 // If read barriers are enabled, emit read barriers other than
2787 // Baker's using a slow path (and also unpoison the loaded
2788 // reference, if heap poisoning is enabled).
2789 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2790 out_loc,
2791 out_loc,
2792 obj_loc,
2793 data_offset,
2794 index);
2795 }
2796 }
2797 break;
2798 }
2799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002801 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802 if (index.IsConstant()) {
2803 size_t offset =
2804 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002805 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002806 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2807 __ Addu(TMP, index.AsRegister<Register>(), obj);
2808 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002809 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002810 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002811 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 }
2813 break;
2814 }
2815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002817 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002818 if (index.IsConstant()) {
2819 size_t offset =
2820 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002821 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002822 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2823 __ Addu(TMP, index.AsRegister<Register>(), obj);
2824 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002825 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002826 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002827 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828 }
2829 break;
2830 }
2831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002833 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002834 if (index.IsConstant()) {
2835 size_t offset =
2836 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002837 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002838 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2839 __ Addu(TMP, index.AsRegister<Register>(), obj);
2840 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002841 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002842 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002843 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002844 }
2845 break;
2846 }
2847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002849 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2850 UNREACHABLE();
2851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002852}
2853
2854void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002855 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002856 locations->SetInAt(0, Location::RequiresRegister());
2857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2858}
2859
2860void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2861 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002862 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002863 Register obj = locations->InAt(0).AsRegister<Register>();
2864 Register out = locations->Out().AsRegister<Register>();
2865 __ LoadFromOffset(kLoadWord, out, obj, offset);
2866 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002867 // Mask out compression flag from String's array length.
2868 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2869 __ Srl(out, out, 1u);
2870 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002871}
2872
Alexey Frunzef58b2482016-09-02 22:14:06 -07002873Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2874 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2875 ? Location::ConstantLocation(instruction->AsConstant())
2876 : Location::RequiresRegister();
2877}
2878
2879Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2880 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2881 // We can store a non-zero float or double constant without first loading it into the FPU,
2882 // but we should only prefer this if the constant has a single use.
2883 if (instruction->IsConstant() &&
2884 (instruction->AsConstant()->IsZeroBitPattern() ||
2885 instruction->GetUses().HasExactlyOneElement())) {
2886 return Location::ConstantLocation(instruction->AsConstant());
2887 // Otherwise fall through and require an FPU register for the constant.
2888 }
2889 return Location::RequiresFpuRegister();
2890}
2891
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002892void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002894
2895 bool needs_write_barrier =
2896 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2897 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2898
Vladimir Markoca6fff82017-10-03 14:49:14 +01002899 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002901 may_need_runtime_call_for_type_check ?
2902 LocationSummary::kCallOnSlowPath :
2903 LocationSummary::kNoCall);
2904
2905 locations->SetInAt(0, Location::RequiresRegister());
2906 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002908 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002910 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2911 }
2912 if (needs_write_barrier) {
2913 // Temporary register for the write barrier.
2914 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002915 }
2916}
2917
2918void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2919 LocationSummary* locations = instruction->GetLocations();
2920 Register obj = locations->InAt(0).AsRegister<Register>();
2921 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002922 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002923 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002924 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002925 bool needs_write_barrier =
2926 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002927 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002928 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929
2930 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002931 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002932 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002935 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002936 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002938 __ Addu(base_reg, obj, index.AsRegister<Register>());
2939 }
2940 if (value_location.IsConstant()) {
2941 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2942 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2943 } else {
2944 Register value = value_location.AsRegister<Register>();
2945 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002946 }
2947 break;
2948 }
2949
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002950 case DataType::Type::kUint16:
2951 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002952 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002953 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002954 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002955 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2956 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002957 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002958 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002959 }
2960 if (value_location.IsConstant()) {
2961 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2962 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2963 } else {
2964 Register value = value_location.AsRegister<Register>();
2965 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002966 }
2967 break;
2968 }
2969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002971 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2972 if (index.IsConstant()) {
2973 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002974 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2975 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002976 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002977 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002978 }
2979 if (value_location.IsConstant()) {
2980 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2981 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2982 } else {
2983 Register value = value_location.AsRegister<Register>();
2984 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2985 }
2986 break;
2987 }
2988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002990 if (value_location.IsConstant()) {
2991 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002992 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002993 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002994 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002995 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002996 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002997 }
Alexey Frunze15958152017-02-09 19:08:30 -08002998 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2999 DCHECK_EQ(value, 0);
3000 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3001 DCHECK(!needs_write_barrier);
3002 DCHECK(!may_need_runtime_call_for_type_check);
3003 break;
3004 }
3005
3006 DCHECK(needs_write_barrier);
3007 Register value = value_location.AsRegister<Register>();
3008 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3009 Register temp2 = TMP; // Doesn't need to survive slow path.
3010 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3011 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3012 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3013 MipsLabel done;
3014 SlowPathCodeMIPS* slow_path = nullptr;
3015
3016 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003017 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003018 codegen_->AddSlowPath(slow_path);
3019 if (instruction->GetValueCanBeNull()) {
3020 MipsLabel non_zero;
3021 __ Bnez(value, &non_zero);
3022 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3023 if (index.IsConstant()) {
3024 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003025 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3026 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003027 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003028 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003029 }
Alexey Frunze15958152017-02-09 19:08:30 -08003030 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3031 __ B(&done);
3032 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003033 }
Alexey Frunze15958152017-02-09 19:08:30 -08003034
3035 // Note that when read barriers are enabled, the type checks
3036 // are performed without read barriers. This is fine, even in
3037 // the case where a class object is in the from-space after
3038 // the flip, as a comparison involving such a type would not
3039 // produce a false positive; it may of course produce a false
3040 // negative, in which case we would take the ArraySet slow
3041 // path.
3042
3043 // /* HeapReference<Class> */ temp1 = obj->klass_
3044 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3045 __ MaybeUnpoisonHeapReference(temp1);
3046
3047 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3048 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3049 // /* HeapReference<Class> */ temp2 = value->klass_
3050 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3051 // If heap poisoning is enabled, no need to unpoison `temp1`
3052 // nor `temp2`, as we are comparing two poisoned references.
3053
3054 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3055 MipsLabel do_put;
3056 __ Beq(temp1, temp2, &do_put);
3057 // If heap poisoning is enabled, the `temp1` reference has
3058 // not been unpoisoned yet; unpoison it now.
3059 __ MaybeUnpoisonHeapReference(temp1);
3060
3061 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3062 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3063 // If heap poisoning is enabled, no need to unpoison
3064 // `temp1`, as we are comparing against null below.
3065 __ Bnez(temp1, slow_path->GetEntryLabel());
3066 __ Bind(&do_put);
3067 } else {
3068 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3069 }
3070 }
3071
3072 Register source = value;
3073 if (kPoisonHeapReferences) {
3074 // Note that in the case where `value` is a null reference,
3075 // we do not enter this block, as a null reference does not
3076 // need poisoning.
3077 __ Move(temp1, value);
3078 __ PoisonHeapReference(temp1);
3079 source = temp1;
3080 }
3081
3082 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3083 if (index.IsConstant()) {
3084 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003085 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003086 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003087 }
3088 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3089
3090 if (!may_need_runtime_call_for_type_check) {
3091 codegen_->MaybeRecordImplicitNullCheck(instruction);
3092 }
3093
3094 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3095
3096 if (done.IsLinked()) {
3097 __ Bind(&done);
3098 }
3099
3100 if (slow_path != nullptr) {
3101 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 }
3103 break;
3104 }
3105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003107 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003109 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003110 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3111 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003113 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003114 }
3115 if (value_location.IsConstant()) {
3116 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3117 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3118 } else {
3119 Register value = value_location.AsRegisterPairLow<Register>();
3120 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003121 }
3122 break;
3123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003126 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003128 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003129 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3130 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003132 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003133 }
3134 if (value_location.IsConstant()) {
3135 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3136 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3137 } else {
3138 FRegister value = value_location.AsFpuRegister<FRegister>();
3139 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 }
3141 break;
3142 }
3143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003144 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003145 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003146 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003147 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003148 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3149 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003151 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003152 }
3153 if (value_location.IsConstant()) {
3154 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3155 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3156 } else {
3157 FRegister value = value_location.AsFpuRegister<FRegister>();
3158 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003159 }
3160 break;
3161 }
3162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003164 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3165 UNREACHABLE();
3166 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003167}
3168
Lena Djokica2901602017-09-21 13:50:52 +02003169void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3170 HIntermediateArrayAddressIndex* instruction) {
3171 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003172 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003173
3174 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3175
3176 locations->SetInAt(0, Location::RequiresRegister());
3177 locations->SetInAt(1, Location::ConstantLocation(shift));
3178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3179}
3180
3181void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3182 HIntermediateArrayAddressIndex* instruction) {
3183 LocationSummary* locations = instruction->GetLocations();
3184 Register index_reg = locations->InAt(0).AsRegister<Register>();
3185 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3186 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3187}
3188
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003189void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003190 RegisterSet caller_saves = RegisterSet::Empty();
3191 InvokeRuntimeCallingConvention calling_convention;
3192 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3193 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3194 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003195
3196 HInstruction* index = instruction->InputAt(0);
3197 HInstruction* length = instruction->InputAt(1);
3198
3199 bool const_index = false;
3200 bool const_length = false;
3201
3202 if (index->IsConstant()) {
3203 if (length->IsConstant()) {
3204 const_index = true;
3205 const_length = true;
3206 } else {
3207 int32_t index_value = index->AsIntConstant()->GetValue();
3208 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3209 const_index = true;
3210 }
3211 }
3212 } else if (length->IsConstant()) {
3213 int32_t length_value = length->AsIntConstant()->GetValue();
3214 if (IsUint<15>(length_value)) {
3215 const_length = true;
3216 }
3217 }
3218
3219 locations->SetInAt(0, const_index
3220 ? Location::ConstantLocation(index->AsConstant())
3221 : Location::RequiresRegister());
3222 locations->SetInAt(1, const_length
3223 ? Location::ConstantLocation(length->AsConstant())
3224 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003225}
3226
3227void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3228 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003229 Location index_loc = locations->InAt(0);
3230 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003231
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003232 if (length_loc.IsConstant()) {
3233 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3234 if (index_loc.IsConstant()) {
3235 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3236 if (index < 0 || index >= length) {
3237 BoundsCheckSlowPathMIPS* slow_path =
3238 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3239 codegen_->AddSlowPath(slow_path);
3240 __ B(slow_path->GetEntryLabel());
3241 } else {
3242 // Nothing to be done.
3243 }
3244 return;
3245 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003246
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003247 BoundsCheckSlowPathMIPS* slow_path =
3248 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3249 codegen_->AddSlowPath(slow_path);
3250 Register index = index_loc.AsRegister<Register>();
3251 if (length == 0) {
3252 __ B(slow_path->GetEntryLabel());
3253 } else if (length == 1) {
3254 __ Bnez(index, slow_path->GetEntryLabel());
3255 } else {
3256 DCHECK(IsUint<15>(length)) << length;
3257 __ Sltiu(TMP, index, length);
3258 __ Beqz(TMP, slow_path->GetEntryLabel());
3259 }
3260 } else {
3261 Register length = length_loc.AsRegister<Register>();
3262 BoundsCheckSlowPathMIPS* slow_path =
3263 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3264 codegen_->AddSlowPath(slow_path);
3265 if (index_loc.IsConstant()) {
3266 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3267 if (index < 0) {
3268 __ B(slow_path->GetEntryLabel());
3269 } else if (index == 0) {
3270 __ Blez(length, slow_path->GetEntryLabel());
3271 } else {
3272 DCHECK(IsInt<16>(index + 1)) << index;
3273 __ Sltiu(TMP, length, index + 1);
3274 __ Bnez(TMP, slow_path->GetEntryLabel());
3275 }
3276 } else {
3277 Register index = index_loc.AsRegister<Register>();
3278 __ Bgeu(index, length, slow_path->GetEntryLabel());
3279 }
3280 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003281}
3282
Alexey Frunze15958152017-02-09 19:08:30 -08003283// Temp is used for read barrier.
3284static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3285 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003286 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003287 (kUseBakerReadBarrier ||
3288 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3289 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3290 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3291 return 1;
3292 }
3293 return 0;
3294}
3295
3296// Extra temp is used for read barrier.
3297static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3298 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3299}
3300
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003301void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003302 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3303 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3304
3305 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3306 switch (type_check_kind) {
3307 case TypeCheckKind::kExactCheck:
3308 case TypeCheckKind::kAbstractClassCheck:
3309 case TypeCheckKind::kClassHierarchyCheck:
3310 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003311 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003312 ? LocationSummary::kCallOnSlowPath
3313 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3314 break;
3315 case TypeCheckKind::kArrayCheck:
3316 case TypeCheckKind::kUnresolvedCheck:
3317 case TypeCheckKind::kInterfaceCheck:
3318 call_kind = LocationSummary::kCallOnSlowPath;
3319 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003320 case TypeCheckKind::kBitstringCheck:
3321 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003322 }
3323
Vladimir Markoca6fff82017-10-03 14:49:14 +01003324 LocationSummary* locations =
3325 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003326 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003327 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3328 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3329 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3330 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3331 } else {
3332 locations->SetInAt(1, Location::RequiresRegister());
3333 }
Alexey Frunze15958152017-02-09 19:08:30 -08003334 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003335}
3336
3337void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003338 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003339 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003340 Location obj_loc = locations->InAt(0);
3341 Register obj = obj_loc.AsRegister<Register>();
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003342 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003343 Location temp_loc = locations->GetTemp(0);
3344 Register temp = temp_loc.AsRegister<Register>();
3345 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3346 DCHECK_LE(num_temps, 2u);
3347 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003348 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3349 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3350 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3351 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3352 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3353 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3354 const uint32_t object_array_data_offset =
3355 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3356 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003357
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003358 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3359 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3360 // read barriers is done for performance and code size reasons.
3361 bool is_type_check_slow_path_fatal = false;
3362 if (!kEmitCompilerReadBarrier) {
3363 is_type_check_slow_path_fatal =
3364 (type_check_kind == TypeCheckKind::kExactCheck ||
3365 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3366 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3367 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3368 !instruction->CanThrowIntoCatchBlock();
3369 }
3370 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003371 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3372 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003373 codegen_->AddSlowPath(slow_path);
3374
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003375 // Avoid this check if we know `obj` is not null.
3376 if (instruction->MustDoNullCheck()) {
3377 __ Beqz(obj, &done);
3378 }
3379
3380 switch (type_check_kind) {
3381 case TypeCheckKind::kExactCheck:
3382 case TypeCheckKind::kArrayCheck: {
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 // Jump to slow path for throwing the exception or doing a
3391 // more involved array check.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003392 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003393 break;
3394 }
3395
3396 case TypeCheckKind::kAbstractClassCheck: {
3397 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003398 GenerateReferenceLoadTwoRegisters(instruction,
3399 temp_loc,
3400 obj_loc,
3401 class_offset,
3402 maybe_temp2_loc,
3403 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003404 // If the class is abstract, we eagerly fetch the super class of the
3405 // object to avoid doing a comparison we know will fail.
3406 MipsLabel loop;
3407 __ Bind(&loop);
3408 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003409 GenerateReferenceLoadOneRegister(instruction,
3410 temp_loc,
3411 super_offset,
3412 maybe_temp2_loc,
3413 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003414 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3415 // exception.
3416 __ Beqz(temp, slow_path->GetEntryLabel());
3417 // Otherwise, compare the classes.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003418 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003419 break;
3420 }
3421
3422 case TypeCheckKind::kClassHierarchyCheck: {
3423 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003424 GenerateReferenceLoadTwoRegisters(instruction,
3425 temp_loc,
3426 obj_loc,
3427 class_offset,
3428 maybe_temp2_loc,
3429 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003430 // Walk over the class hierarchy to find a match.
3431 MipsLabel loop;
3432 __ Bind(&loop);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003433 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003434 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003435 GenerateReferenceLoadOneRegister(instruction,
3436 temp_loc,
3437 super_offset,
3438 maybe_temp2_loc,
3439 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003440 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3441 // exception. Otherwise, jump to the beginning of the loop.
3442 __ Bnez(temp, &loop);
3443 __ B(slow_path->GetEntryLabel());
3444 break;
3445 }
3446
3447 case TypeCheckKind::kArrayObjectCheck: {
3448 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003449 GenerateReferenceLoadTwoRegisters(instruction,
3450 temp_loc,
3451 obj_loc,
3452 class_offset,
3453 maybe_temp2_loc,
3454 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003455 // Do an exact check.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003456 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003457 // Otherwise, we need to check that the object's class is a non-primitive array.
3458 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003459 GenerateReferenceLoadOneRegister(instruction,
3460 temp_loc,
3461 component_offset,
3462 maybe_temp2_loc,
3463 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003464 // If the component type is null, jump to the slow path to throw the exception.
3465 __ Beqz(temp, slow_path->GetEntryLabel());
3466 // Otherwise, the object is indeed an array, further check that this component
3467 // type is not a primitive type.
3468 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3469 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3470 __ Bnez(temp, slow_path->GetEntryLabel());
3471 break;
3472 }
3473
3474 case TypeCheckKind::kUnresolvedCheck:
3475 // We always go into the type check slow path for the unresolved check case.
3476 // We cannot directly call the CheckCast runtime entry point
3477 // without resorting to a type checking slow path here (i.e. by
3478 // calling InvokeRuntime directly), as it would require to
3479 // assign fixed registers for the inputs of this HInstanceOf
3480 // instruction (following the runtime calling convention), which
3481 // might be cluttered by the potential first read barrier
3482 // emission at the beginning of this method.
3483 __ B(slow_path->GetEntryLabel());
3484 break;
3485
3486 case TypeCheckKind::kInterfaceCheck: {
3487 // Avoid read barriers to improve performance of the fast path. We can not get false
3488 // positives by doing this.
3489 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003490 GenerateReferenceLoadTwoRegisters(instruction,
3491 temp_loc,
3492 obj_loc,
3493 class_offset,
3494 maybe_temp2_loc,
3495 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003496 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003497 GenerateReferenceLoadTwoRegisters(instruction,
3498 temp_loc,
3499 temp_loc,
3500 iftable_offset,
3501 maybe_temp2_loc,
3502 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003503 // Iftable is never null.
3504 __ Lw(TMP, temp, array_length_offset);
3505 // Loop through the iftable and check if any class matches.
3506 MipsLabel loop;
3507 __ Bind(&loop);
3508 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3509 __ Beqz(TMP, slow_path->GetEntryLabel());
3510 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3511 __ MaybeUnpoisonHeapReference(AT);
3512 // Go to next interface.
3513 __ Addiu(TMP, TMP, -2);
3514 // Compare the classes and continue the loop if they do not match.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003515 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3516 break;
3517 }
3518
3519 case TypeCheckKind::kBitstringCheck: {
3520 // /* HeapReference<Class> */ temp = obj->klass_
3521 GenerateReferenceLoadTwoRegisters(instruction,
3522 temp_loc,
3523 obj_loc,
3524 class_offset,
3525 maybe_temp2_loc,
3526 kWithoutReadBarrier);
3527
3528 GenerateBitstringTypeCheckCompare(instruction, temp);
3529 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003530 break;
3531 }
3532 }
3533
3534 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 __ Bind(slow_path->GetExitLabel());
3536}
3537
3538void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3539 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003540 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003541 locations->SetInAt(0, Location::RequiresRegister());
3542 if (check->HasUses()) {
3543 locations->SetOut(Location::SameAsFirstInput());
3544 }
3545}
3546
3547void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3548 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003549 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003550 check->GetLoadClass(),
3551 check,
3552 check->GetDexPc(),
3553 true);
3554 codegen_->AddSlowPath(slow_path);
3555 GenerateClassInitializationCheck(slow_path,
3556 check->GetLocations()->InAt(0).AsRegister<Register>());
3557}
3558
3559void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003560 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003561
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003562 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003563 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003564
3565 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003566 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003567 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003570 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003571 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003572 locations->SetInAt(0, Location::RequiresRegister());
3573 locations->SetInAt(1, Location::RequiresRegister());
3574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3575 break;
3576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003577 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003578 locations->SetInAt(0, Location::RequiresRegister());
3579 locations->SetInAt(1, Location::RequiresRegister());
3580 // Output overlaps because it is written before doing the low comparison.
3581 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3582 break;
3583
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003584 case DataType::Type::kFloat32:
3585 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003586 locations->SetInAt(0, Location::RequiresFpuRegister());
3587 locations->SetInAt(1, Location::RequiresFpuRegister());
3588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003589 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003590
3591 default:
3592 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3593 }
3594}
3595
3596void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3597 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003598 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003599 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003600 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003601
3602 // 0 if: left == right
3603 // 1 if: left > right
3604 // -1 if: left < right
3605 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003606 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003607 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003608 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003610 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003611 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003612 Register lhs = locations->InAt(0).AsRegister<Register>();
3613 Register rhs = locations->InAt(1).AsRegister<Register>();
3614 __ Slt(TMP, lhs, rhs);
3615 __ Slt(res, rhs, lhs);
3616 __ Subu(res, res, TMP);
3617 break;
3618 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003619 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003620 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003621 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3622 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3623 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3624 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3625 // TODO: more efficient (direct) comparison with a constant.
3626 __ Slt(TMP, lhs_high, rhs_high);
3627 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3628 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3629 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3630 __ Sltu(TMP, lhs_low, rhs_low);
3631 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3632 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3633 __ Bind(&done);
3634 break;
3635 }
3636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003637 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003638 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003639 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3640 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3641 MipsLabel done;
3642 if (isR6) {
3643 __ CmpEqS(FTMP, lhs, rhs);
3644 __ LoadConst32(res, 0);
3645 __ Bc1nez(FTMP, &done);
3646 if (gt_bias) {
3647 __ CmpLtS(FTMP, lhs, rhs);
3648 __ LoadConst32(res, -1);
3649 __ Bc1nez(FTMP, &done);
3650 __ LoadConst32(res, 1);
3651 } else {
3652 __ CmpLtS(FTMP, rhs, lhs);
3653 __ LoadConst32(res, 1);
3654 __ Bc1nez(FTMP, &done);
3655 __ LoadConst32(res, -1);
3656 }
3657 } else {
3658 if (gt_bias) {
3659 __ ColtS(0, lhs, rhs);
3660 __ LoadConst32(res, -1);
3661 __ Bc1t(0, &done);
3662 __ CeqS(0, lhs, rhs);
3663 __ LoadConst32(res, 1);
3664 __ Movt(res, ZERO, 0);
3665 } else {
3666 __ ColtS(0, rhs, lhs);
3667 __ LoadConst32(res, 1);
3668 __ Bc1t(0, &done);
3669 __ CeqS(0, lhs, rhs);
3670 __ LoadConst32(res, -1);
3671 __ Movt(res, ZERO, 0);
3672 }
3673 }
3674 __ Bind(&done);
3675 break;
3676 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003677 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003678 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003679 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3680 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3681 MipsLabel done;
3682 if (isR6) {
3683 __ CmpEqD(FTMP, lhs, rhs);
3684 __ LoadConst32(res, 0);
3685 __ Bc1nez(FTMP, &done);
3686 if (gt_bias) {
3687 __ CmpLtD(FTMP, lhs, rhs);
3688 __ LoadConst32(res, -1);
3689 __ Bc1nez(FTMP, &done);
3690 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003692 __ CmpLtD(FTMP, rhs, lhs);
3693 __ LoadConst32(res, 1);
3694 __ Bc1nez(FTMP, &done);
3695 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003696 }
3697 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003698 if (gt_bias) {
3699 __ ColtD(0, lhs, rhs);
3700 __ LoadConst32(res, -1);
3701 __ Bc1t(0, &done);
3702 __ CeqD(0, lhs, rhs);
3703 __ LoadConst32(res, 1);
3704 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003705 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003706 __ ColtD(0, rhs, lhs);
3707 __ LoadConst32(res, 1);
3708 __ Bc1t(0, &done);
3709 __ CeqD(0, lhs, rhs);
3710 __ LoadConst32(res, -1);
3711 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003712 }
3713 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003714 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003715 break;
3716 }
3717
3718 default:
3719 LOG(FATAL) << "Unimplemented compare type " << in_type;
3720 }
3721}
3722
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003723void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003724 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003725 switch (instruction->InputAt(0)->GetType()) {
3726 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003728 locations->SetInAt(0, Location::RequiresRegister());
3729 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3730 break;
3731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003732 case DataType::Type::kFloat32:
3733 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003734 locations->SetInAt(0, Location::RequiresFpuRegister());
3735 locations->SetInAt(1, Location::RequiresFpuRegister());
3736 break;
3737 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003738 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3740 }
3741}
3742
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003743void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003744 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003745 return;
3746 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003749 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003750
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003751 switch (type) {
3752 default:
3753 // Integer case.
3754 GenerateIntCompare(instruction->GetCondition(), locations);
3755 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003758 GenerateLongCompare(instruction->GetCondition(), locations);
3759 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 case DataType::Type::kFloat32:
3762 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003763 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3764 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003765 }
3766}
3767
Alexey Frunze7e99e052015-11-24 19:28:01 -08003768void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3769 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003770
3771 LocationSummary* locations = instruction->GetLocations();
3772 Location second = locations->InAt(1);
3773 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003774 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003775 DCHECK(imm == 1 || imm == -1);
3776
Lena Djokic4b8025c2017-12-21 16:15:50 +01003777 if (instruction->GetResultType() == DataType::Type::kInt32) {
3778 Register out = locations->Out().AsRegister<Register>();
3779 Register dividend = locations->InAt(0).AsRegister<Register>();
3780
3781 if (instruction->IsRem()) {
3782 __ Move(out, ZERO);
3783 } else {
3784 if (imm == -1) {
3785 __ Subu(out, ZERO, dividend);
3786 } else if (out != dividend) {
3787 __ Move(out, dividend);
3788 }
3789 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003790 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003791 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3792 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3793 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3794 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3795 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3796
3797 if (instruction->IsRem()) {
3798 __ Move(out_high, ZERO);
3799 __ Move(out_low, ZERO);
3800 } else {
3801 if (imm == -1) {
3802 __ Subu(out_low, ZERO, in_low);
3803 __ Sltu(AT, ZERO, out_low);
3804 __ Subu(out_high, ZERO, in_high);
3805 __ Subu(out_high, out_high, AT);
3806 } else {
3807 __ Move(out_low, in_low);
3808 __ Move(out_high, in_high);
3809 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003810 }
3811 }
3812}
3813
3814void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3815 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003816
3817 LocationSummary* locations = instruction->GetLocations();
3818 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003819 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3820 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003821 DCHECK(second.IsConstant());
3822
Lena Djokic4b8025c2017-12-21 16:15:50 +01003823 if (instruction->GetResultType() == DataType::Type::kInt32) {
3824 Register out = locations->Out().AsRegister<Register>();
3825 Register dividend = locations->InAt(0).AsRegister<Register>();
3826 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3827 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3828 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003829
Lena Djokic4b8025c2017-12-21 16:15:50 +01003830 if (instruction->IsDiv()) {
3831 if (ctz_imm == 1) {
3832 // Fast path for division by +/-2, which is very common.
3833 __ Srl(TMP, dividend, 31);
3834 } else {
3835 __ Sra(TMP, dividend, 31);
3836 __ Srl(TMP, TMP, 32 - ctz_imm);
3837 }
3838 __ Addu(out, dividend, TMP);
3839 __ Sra(out, out, ctz_imm);
3840 if (imm < 0) {
3841 __ Subu(out, ZERO, out);
3842 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003843 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003844 if (ctz_imm == 1) {
3845 // Fast path for modulo +/-2, which is very common.
3846 __ Sra(TMP, dividend, 31);
3847 __ Subu(out, dividend, TMP);
3848 __ Andi(out, out, 1);
3849 __ Addu(out, out, TMP);
3850 } else {
3851 __ Sra(TMP, dividend, 31);
3852 __ Srl(TMP, TMP, 32 - ctz_imm);
3853 __ Addu(out, dividend, TMP);
3854 if (IsUint<16>(abs_imm - 1)) {
3855 __ Andi(out, out, abs_imm - 1);
3856 } else {
3857 if (is_r2_or_newer) {
3858 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3859 } else {
3860 __ Sll(out, out, 32 - ctz_imm);
3861 __ Srl(out, out, 32 - ctz_imm);
3862 }
3863 }
3864 __ Subu(out, out, TMP);
3865 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003866 }
3867 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003868 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3869 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3870 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3871 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3872 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3873 int64_t imm = Int64FromConstant(second.GetConstant());
3874 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3875 int ctz_imm = CTZ(abs_imm);
3876
3877 if (instruction->IsDiv()) {
3878 if (ctz_imm < 32) {
3879 if (ctz_imm == 1) {
3880 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003881 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003882 __ Sra(AT, in_high, 31);
3883 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003884 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003885 __ Addu(AT, AT, in_low);
3886 __ Sltu(TMP, AT, in_low);
3887 __ Addu(out_high, in_high, TMP);
3888 __ Srl(out_low, AT, ctz_imm);
3889 if (is_r2_or_newer) {
3890 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3891 __ Sra(out_high, out_high, ctz_imm);
3892 } else {
3893 __ Sll(AT, out_high, 32 - ctz_imm);
3894 __ Sra(out_high, out_high, ctz_imm);
3895 __ Or(out_low, out_low, AT);
3896 }
3897 if (imm < 0) {
3898 __ Subu(out_low, ZERO, out_low);
3899 __ Sltu(AT, ZERO, out_low);
3900 __ Subu(out_high, ZERO, out_high);
3901 __ Subu(out_high, out_high, AT);
3902 }
3903 } else if (ctz_imm == 32) {
3904 __ Sra(AT, in_high, 31);
3905 __ Addu(AT, AT, in_low);
3906 __ Sltu(AT, AT, in_low);
3907 __ Addu(out_low, in_high, AT);
3908 if (imm < 0) {
3909 __ Srl(TMP, out_low, 31);
3910 __ Subu(out_low, ZERO, out_low);
3911 __ Sltu(AT, ZERO, out_low);
3912 __ Subu(out_high, TMP, AT);
3913 } else {
3914 __ Sra(out_high, out_low, 31);
3915 }
3916 } else if (ctz_imm < 63) {
3917 __ Sra(AT, in_high, 31);
3918 __ Srl(TMP, AT, 64 - ctz_imm);
3919 __ Addu(AT, AT, in_low);
3920 __ Sltu(AT, AT, in_low);
3921 __ Addu(out_low, in_high, AT);
3922 __ Addu(out_low, out_low, TMP);
3923 __ Sra(out_low, out_low, ctz_imm - 32);
3924 if (imm < 0) {
3925 __ Subu(out_low, ZERO, out_low);
3926 }
3927 __ Sra(out_high, out_low, 31);
3928 } else {
3929 DCHECK_LT(imm, 0);
3930 if (is_r6) {
3931 __ Aui(AT, in_high, 0x8000);
3932 } else {
3933 __ Lui(AT, 0x8000);
3934 __ Xor(AT, AT, in_high);
3935 }
3936 __ Or(AT, AT, in_low);
3937 __ Sltiu(out_low, AT, 1);
3938 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003939 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003940 } else {
3941 if ((ctz_imm == 1) && !is_r6) {
3942 __ Andi(AT, in_low, 1);
3943 __ Sll(TMP, in_low, 31);
3944 __ And(TMP, in_high, TMP);
3945 __ Sra(out_high, TMP, 31);
3946 __ Or(out_low, out_high, AT);
3947 } else if (ctz_imm < 32) {
3948 __ Sra(AT, in_high, 31);
3949 if (ctz_imm <= 16) {
3950 __ Andi(out_low, in_low, abs_imm - 1);
3951 } else if (is_r2_or_newer) {
3952 __ Ext(out_low, in_low, 0, ctz_imm);
3953 } else {
3954 __ Sll(out_low, in_low, 32 - ctz_imm);
3955 __ Srl(out_low, out_low, 32 - ctz_imm);
3956 }
3957 if (is_r6) {
3958 __ Selnez(out_high, AT, out_low);
3959 } else {
3960 __ Movz(AT, ZERO, out_low);
3961 __ Move(out_high, AT);
3962 }
3963 if (is_r2_or_newer) {
3964 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3965 } else {
3966 __ Sll(AT, out_high, ctz_imm);
3967 __ Or(out_low, out_low, AT);
3968 }
3969 } else if (ctz_imm == 32) {
3970 __ Sra(AT, in_high, 31);
3971 __ Move(out_low, in_low);
3972 if (is_r6) {
3973 __ Selnez(out_high, AT, out_low);
3974 } else {
3975 __ Movz(AT, ZERO, out_low);
3976 __ Move(out_high, AT);
3977 }
3978 } else if (ctz_imm < 63) {
3979 __ Sra(AT, in_high, 31);
3980 __ Move(TMP, in_low);
3981 if (ctz_imm - 32 <= 16) {
3982 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3983 } else if (is_r2_or_newer) {
3984 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3985 } else {
3986 __ Sll(out_high, in_high, 64 - ctz_imm);
3987 __ Srl(out_high, out_high, 64 - ctz_imm);
3988 }
3989 __ Move(out_low, TMP);
3990 __ Or(TMP, TMP, out_high);
3991 if (is_r6) {
3992 __ Selnez(AT, AT, TMP);
3993 } else {
3994 __ Movz(AT, ZERO, TMP);
3995 }
3996 if (is_r2_or_newer) {
3997 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3998 } else {
3999 __ Sll(AT, AT, ctz_imm - 32);
4000 __ Or(out_high, out_high, AT);
4001 }
4002 } else {
4003 if (is_r6) {
4004 __ Aui(AT, in_high, 0x8000);
4005 } else {
4006 __ Lui(AT, 0x8000);
4007 __ Xor(AT, AT, in_high);
4008 }
4009 __ Or(AT, AT, in_low);
4010 __ Sltiu(AT, AT, 1);
4011 __ Sll(AT, AT, 31);
4012 __ Move(out_low, in_low);
4013 __ Xor(out_high, in_high, AT);
4014 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004015 }
4016 }
4017}
4018
4019void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4020 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004022
4023 LocationSummary* locations = instruction->GetLocations();
4024 Location second = locations->InAt(1);
4025 DCHECK(second.IsConstant());
4026
4027 Register out = locations->Out().AsRegister<Register>();
4028 Register dividend = locations->InAt(0).AsRegister<Register>();
4029 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4030
4031 int64_t magic;
4032 int shift;
4033 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4034
4035 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4036
4037 __ LoadConst32(TMP, magic);
4038 if (isR6) {
4039 __ MuhR6(TMP, dividend, TMP);
4040 } else {
4041 __ MultR2(dividend, TMP);
4042 __ Mfhi(TMP);
4043 }
4044 if (imm > 0 && magic < 0) {
4045 __ Addu(TMP, TMP, dividend);
4046 } else if (imm < 0 && magic > 0) {
4047 __ Subu(TMP, TMP, dividend);
4048 }
4049
4050 if (shift != 0) {
4051 __ Sra(TMP, TMP, shift);
4052 }
4053
4054 if (instruction->IsDiv()) {
4055 __ Sra(out, TMP, 31);
4056 __ Subu(out, TMP, out);
4057 } else {
4058 __ Sra(AT, TMP, 31);
4059 __ Subu(AT, TMP, AT);
4060 __ LoadConst32(TMP, imm);
4061 if (isR6) {
4062 __ MulR6(TMP, AT, TMP);
4063 } else {
4064 __ MulR2(TMP, AT, TMP);
4065 }
4066 __ Subu(out, dividend, TMP);
4067 }
4068}
4069
4070void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4071 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004072 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004073
4074 LocationSummary* locations = instruction->GetLocations();
4075 Register out = locations->Out().AsRegister<Register>();
4076 Location second = locations->InAt(1);
4077
4078 if (second.IsConstant()) {
4079 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4080 if (imm == 0) {
4081 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4082 } else if (imm == 1 || imm == -1) {
4083 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004084 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004085 DivRemByPowerOfTwo(instruction);
4086 } else {
4087 DCHECK(imm <= -2 || imm >= 2);
4088 GenerateDivRemWithAnyConstant(instruction);
4089 }
4090 } else {
4091 Register dividend = locations->InAt(0).AsRegister<Register>();
4092 Register divisor = second.AsRegister<Register>();
4093 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4094 if (instruction->IsDiv()) {
4095 if (isR6) {
4096 __ DivR6(out, dividend, divisor);
4097 } else {
4098 __ DivR2(out, dividend, divisor);
4099 }
4100 } else {
4101 if (isR6) {
4102 __ ModR6(out, dividend, divisor);
4103 } else {
4104 __ ModR2(out, dividend, divisor);
4105 }
4106 }
4107 }
4108}
4109
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004110void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004112 bool call_long_div = false;
4113 if (type == DataType::Type::kInt64) {
4114 if (div->InputAt(1)->IsConstant()) {
4115 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4116 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4117 } else {
4118 call_long_div = true;
4119 }
4120 }
4121 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004122 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004123 : LocationSummary::kNoCall;
4124
Vladimir Markoca6fff82017-10-03 14:49:14 +01004125 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004126
4127 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004129 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004130 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4132 break;
4133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004134 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004135 if (call_long_div) {
4136 InvokeRuntimeCallingConvention calling_convention;
4137 locations->SetInAt(0, Location::RegisterPairLocation(
4138 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4139 locations->SetInAt(1, Location::RegisterPairLocation(
4140 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4141 locations->SetOut(calling_convention.GetReturnLocation(type));
4142 } else {
4143 locations->SetInAt(0, Location::RequiresRegister());
4144 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4145 locations->SetOut(Location::RequiresRegister());
4146 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004147 break;
4148 }
4149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 case DataType::Type::kFloat32:
4151 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004152 locations->SetInAt(0, Location::RequiresFpuRegister());
4153 locations->SetInAt(1, Location::RequiresFpuRegister());
4154 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4155 break;
4156
4157 default:
4158 LOG(FATAL) << "Unexpected div type " << type;
4159 }
4160}
4161
4162void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004164 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004165
4166 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004167 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004168 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004169 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004171 if (locations->InAt(1).IsConstant()) {
4172 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4173 if (imm == 0) {
4174 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4175 } else if (imm == 1 || imm == -1) {
4176 DivRemOneOrMinusOne(instruction);
4177 } else {
4178 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4179 DivRemByPowerOfTwo(instruction);
4180 }
4181 } else {
4182 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4183 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4184 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004185 break;
4186 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004187 case DataType::Type::kFloat32:
4188 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004189 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4190 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4191 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004192 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004193 __ DivS(dst, lhs, rhs);
4194 } else {
4195 __ DivD(dst, lhs, rhs);
4196 }
4197 break;
4198 }
4199 default:
4200 LOG(FATAL) << "Unexpected div type " << type;
4201 }
4202}
4203
4204void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004205 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004207}
4208
4209void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004210 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004211 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004212 codegen_->AddSlowPath(slow_path);
4213 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004214 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004215
4216 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004217 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004218 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt8:
4220 case DataType::Type::kUint16:
4221 case DataType::Type::kInt16:
4222 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004223 if (value.IsConstant()) {
4224 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4225 __ B(slow_path->GetEntryLabel());
4226 } else {
4227 // A division by a non-null constant is valid. We don't need to perform
4228 // any check, so simply fall through.
4229 }
4230 } else {
4231 DCHECK(value.IsRegister()) << value;
4232 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4233 }
4234 break;
4235 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004237 if (value.IsConstant()) {
4238 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4239 __ B(slow_path->GetEntryLabel());
4240 } else {
4241 // A division by a non-null constant is valid. We don't need to perform
4242 // any check, so simply fall through.
4243 }
4244 } else {
4245 DCHECK(value.IsRegisterPair()) << value;
4246 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4247 __ Beqz(TMP, slow_path->GetEntryLabel());
4248 }
4249 break;
4250 }
4251 default:
4252 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4253 }
4254}
4255
4256void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4257 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004258 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004259 locations->SetOut(Location::ConstantLocation(constant));
4260}
4261
4262void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4263 // Will be generated at use site.
4264}
4265
4266void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4267 exit->SetLocations(nullptr);
4268}
4269
4270void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4271}
4272
4273void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4274 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004275 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004276 locations->SetOut(Location::ConstantLocation(constant));
4277}
4278
4279void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4280 // Will be generated at use site.
4281}
4282
4283void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4284 got->SetLocations(nullptr);
4285}
4286
4287void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004288 if (successor->IsExitBlock()) {
4289 DCHECK(got->GetPrevious()->AlwaysThrows());
4290 return; // no code needed
4291 }
4292
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004293 HBasicBlock* block = got->GetBlock();
4294 HInstruction* previous = got->GetPrevious();
4295 HLoopInformation* info = block->GetLoopInformation();
4296
4297 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004298 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4299 return;
4300 }
4301 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4302 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4303 }
4304 if (!codegen_->GoesToNextBlock(block, successor)) {
4305 __ B(codegen_->GetLabelOf(successor));
4306 }
4307}
4308
4309void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4310 HandleGoto(got, got->GetSuccessor());
4311}
4312
4313void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4314 try_boundary->SetLocations(nullptr);
4315}
4316
4317void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4318 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4319 if (!successor->IsExitBlock()) {
4320 HandleGoto(try_boundary, successor);
4321 }
4322}
4323
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004324void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4325 LocationSummary* locations) {
4326 Register dst = locations->Out().AsRegister<Register>();
4327 Register lhs = locations->InAt(0).AsRegister<Register>();
4328 Location rhs_location = locations->InAt(1);
4329 Register rhs_reg = ZERO;
4330 int64_t rhs_imm = 0;
4331 bool use_imm = rhs_location.IsConstant();
4332 if (use_imm) {
4333 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4334 } else {
4335 rhs_reg = rhs_location.AsRegister<Register>();
4336 }
4337
4338 switch (cond) {
4339 case kCondEQ:
4340 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004341 if (use_imm && IsInt<16>(-rhs_imm)) {
4342 if (rhs_imm == 0) {
4343 if (cond == kCondEQ) {
4344 __ Sltiu(dst, lhs, 1);
4345 } else {
4346 __ Sltu(dst, ZERO, lhs);
4347 }
4348 } else {
4349 __ Addiu(dst, lhs, -rhs_imm);
4350 if (cond == kCondEQ) {
4351 __ Sltiu(dst, dst, 1);
4352 } else {
4353 __ Sltu(dst, ZERO, dst);
4354 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004355 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004356 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004357 if (use_imm && IsUint<16>(rhs_imm)) {
4358 __ Xori(dst, lhs, rhs_imm);
4359 } else {
4360 if (use_imm) {
4361 rhs_reg = TMP;
4362 __ LoadConst32(rhs_reg, rhs_imm);
4363 }
4364 __ Xor(dst, lhs, rhs_reg);
4365 }
4366 if (cond == kCondEQ) {
4367 __ Sltiu(dst, dst, 1);
4368 } else {
4369 __ Sltu(dst, ZERO, dst);
4370 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004371 }
4372 break;
4373
4374 case kCondLT:
4375 case kCondGE:
4376 if (use_imm && IsInt<16>(rhs_imm)) {
4377 __ Slti(dst, lhs, rhs_imm);
4378 } else {
4379 if (use_imm) {
4380 rhs_reg = TMP;
4381 __ LoadConst32(rhs_reg, rhs_imm);
4382 }
4383 __ Slt(dst, lhs, rhs_reg);
4384 }
4385 if (cond == kCondGE) {
4386 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4387 // only the slt instruction but no sge.
4388 __ Xori(dst, dst, 1);
4389 }
4390 break;
4391
4392 case kCondLE:
4393 case kCondGT:
4394 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4395 // Simulate lhs <= rhs via lhs < rhs + 1.
4396 __ Slti(dst, lhs, rhs_imm + 1);
4397 if (cond == kCondGT) {
4398 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4399 // only the slti instruction but no sgti.
4400 __ Xori(dst, dst, 1);
4401 }
4402 } else {
4403 if (use_imm) {
4404 rhs_reg = TMP;
4405 __ LoadConst32(rhs_reg, rhs_imm);
4406 }
4407 __ Slt(dst, rhs_reg, lhs);
4408 if (cond == kCondLE) {
4409 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4410 // only the slt instruction but no sle.
4411 __ Xori(dst, dst, 1);
4412 }
4413 }
4414 break;
4415
4416 case kCondB:
4417 case kCondAE:
4418 if (use_imm && IsInt<16>(rhs_imm)) {
4419 // Sltiu sign-extends its 16-bit immediate operand before
4420 // the comparison and thus lets us compare directly with
4421 // unsigned values in the ranges [0, 0x7fff] and
4422 // [0xffff8000, 0xffffffff].
4423 __ Sltiu(dst, lhs, rhs_imm);
4424 } else {
4425 if (use_imm) {
4426 rhs_reg = TMP;
4427 __ LoadConst32(rhs_reg, rhs_imm);
4428 }
4429 __ Sltu(dst, lhs, rhs_reg);
4430 }
4431 if (cond == kCondAE) {
4432 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4433 // only the sltu instruction but no sgeu.
4434 __ Xori(dst, dst, 1);
4435 }
4436 break;
4437
4438 case kCondBE:
4439 case kCondA:
4440 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4441 // Simulate lhs <= rhs via lhs < rhs + 1.
4442 // Note that this only works if rhs + 1 does not overflow
4443 // to 0, hence the check above.
4444 // Sltiu sign-extends its 16-bit immediate operand before
4445 // the comparison and thus lets us compare directly with
4446 // unsigned values in the ranges [0, 0x7fff] and
4447 // [0xffff8000, 0xffffffff].
4448 __ Sltiu(dst, lhs, rhs_imm + 1);
4449 if (cond == kCondA) {
4450 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4451 // only the sltiu instruction but no sgtiu.
4452 __ Xori(dst, dst, 1);
4453 }
4454 } else {
4455 if (use_imm) {
4456 rhs_reg = TMP;
4457 __ LoadConst32(rhs_reg, rhs_imm);
4458 }
4459 __ Sltu(dst, rhs_reg, lhs);
4460 if (cond == kCondBE) {
4461 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4462 // only the sltu instruction but no sleu.
4463 __ Xori(dst, dst, 1);
4464 }
4465 }
4466 break;
4467 }
4468}
4469
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004470bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4471 LocationSummary* input_locations,
4472 Register dst) {
4473 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4474 Location rhs_location = input_locations->InAt(1);
4475 Register rhs_reg = ZERO;
4476 int64_t rhs_imm = 0;
4477 bool use_imm = rhs_location.IsConstant();
4478 if (use_imm) {
4479 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4480 } else {
4481 rhs_reg = rhs_location.AsRegister<Register>();
4482 }
4483
4484 switch (cond) {
4485 case kCondEQ:
4486 case kCondNE:
4487 if (use_imm && IsInt<16>(-rhs_imm)) {
4488 __ Addiu(dst, lhs, -rhs_imm);
4489 } else if (use_imm && IsUint<16>(rhs_imm)) {
4490 __ Xori(dst, lhs, rhs_imm);
4491 } else {
4492 if (use_imm) {
4493 rhs_reg = TMP;
4494 __ LoadConst32(rhs_reg, rhs_imm);
4495 }
4496 __ Xor(dst, lhs, rhs_reg);
4497 }
4498 return (cond == kCondEQ);
4499
4500 case kCondLT:
4501 case kCondGE:
4502 if (use_imm && IsInt<16>(rhs_imm)) {
4503 __ Slti(dst, lhs, rhs_imm);
4504 } else {
4505 if (use_imm) {
4506 rhs_reg = TMP;
4507 __ LoadConst32(rhs_reg, rhs_imm);
4508 }
4509 __ Slt(dst, lhs, rhs_reg);
4510 }
4511 return (cond == kCondGE);
4512
4513 case kCondLE:
4514 case kCondGT:
4515 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4516 // Simulate lhs <= rhs via lhs < rhs + 1.
4517 __ Slti(dst, lhs, rhs_imm + 1);
4518 return (cond == kCondGT);
4519 } else {
4520 if (use_imm) {
4521 rhs_reg = TMP;
4522 __ LoadConst32(rhs_reg, rhs_imm);
4523 }
4524 __ Slt(dst, rhs_reg, lhs);
4525 return (cond == kCondLE);
4526 }
4527
4528 case kCondB:
4529 case kCondAE:
4530 if (use_imm && IsInt<16>(rhs_imm)) {
4531 // Sltiu sign-extends its 16-bit immediate operand before
4532 // the comparison and thus lets us compare directly with
4533 // unsigned values in the ranges [0, 0x7fff] and
4534 // [0xffff8000, 0xffffffff].
4535 __ Sltiu(dst, lhs, rhs_imm);
4536 } else {
4537 if (use_imm) {
4538 rhs_reg = TMP;
4539 __ LoadConst32(rhs_reg, rhs_imm);
4540 }
4541 __ Sltu(dst, lhs, rhs_reg);
4542 }
4543 return (cond == kCondAE);
4544
4545 case kCondBE:
4546 case kCondA:
4547 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4548 // Simulate lhs <= rhs via lhs < rhs + 1.
4549 // Note that this only works if rhs + 1 does not overflow
4550 // to 0, hence the check above.
4551 // Sltiu sign-extends its 16-bit immediate operand before
4552 // the comparison and thus lets us compare directly with
4553 // unsigned values in the ranges [0, 0x7fff] and
4554 // [0xffff8000, 0xffffffff].
4555 __ Sltiu(dst, lhs, rhs_imm + 1);
4556 return (cond == kCondA);
4557 } else {
4558 if (use_imm) {
4559 rhs_reg = TMP;
4560 __ LoadConst32(rhs_reg, rhs_imm);
4561 }
4562 __ Sltu(dst, rhs_reg, lhs);
4563 return (cond == kCondBE);
4564 }
4565 }
4566}
4567
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004568void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4569 LocationSummary* locations,
4570 MipsLabel* label) {
4571 Register lhs = locations->InAt(0).AsRegister<Register>();
4572 Location rhs_location = locations->InAt(1);
4573 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004574 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004575 bool use_imm = rhs_location.IsConstant();
4576 if (use_imm) {
4577 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4578 } else {
4579 rhs_reg = rhs_location.AsRegister<Register>();
4580 }
4581
4582 if (use_imm && rhs_imm == 0) {
4583 switch (cond) {
4584 case kCondEQ:
4585 case kCondBE: // <= 0 if zero
4586 __ Beqz(lhs, label);
4587 break;
4588 case kCondNE:
4589 case kCondA: // > 0 if non-zero
4590 __ Bnez(lhs, label);
4591 break;
4592 case kCondLT:
4593 __ Bltz(lhs, label);
4594 break;
4595 case kCondGE:
4596 __ Bgez(lhs, label);
4597 break;
4598 case kCondLE:
4599 __ Blez(lhs, label);
4600 break;
4601 case kCondGT:
4602 __ Bgtz(lhs, label);
4603 break;
4604 case kCondB: // always false
4605 break;
4606 case kCondAE: // always true
4607 __ B(label);
4608 break;
4609 }
4610 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004611 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4612 if (isR6 || !use_imm) {
4613 if (use_imm) {
4614 rhs_reg = TMP;
4615 __ LoadConst32(rhs_reg, rhs_imm);
4616 }
4617 switch (cond) {
4618 case kCondEQ:
4619 __ Beq(lhs, rhs_reg, label);
4620 break;
4621 case kCondNE:
4622 __ Bne(lhs, rhs_reg, label);
4623 break;
4624 case kCondLT:
4625 __ Blt(lhs, rhs_reg, label);
4626 break;
4627 case kCondGE:
4628 __ Bge(lhs, rhs_reg, label);
4629 break;
4630 case kCondLE:
4631 __ Bge(rhs_reg, lhs, label);
4632 break;
4633 case kCondGT:
4634 __ Blt(rhs_reg, lhs, label);
4635 break;
4636 case kCondB:
4637 __ Bltu(lhs, rhs_reg, label);
4638 break;
4639 case kCondAE:
4640 __ Bgeu(lhs, rhs_reg, label);
4641 break;
4642 case kCondBE:
4643 __ Bgeu(rhs_reg, lhs, label);
4644 break;
4645 case kCondA:
4646 __ Bltu(rhs_reg, lhs, label);
4647 break;
4648 }
4649 } else {
4650 // Special cases for more efficient comparison with constants on R2.
4651 switch (cond) {
4652 case kCondEQ:
4653 __ LoadConst32(TMP, rhs_imm);
4654 __ Beq(lhs, TMP, label);
4655 break;
4656 case kCondNE:
4657 __ LoadConst32(TMP, rhs_imm);
4658 __ Bne(lhs, TMP, label);
4659 break;
4660 case kCondLT:
4661 if (IsInt<16>(rhs_imm)) {
4662 __ Slti(TMP, lhs, rhs_imm);
4663 __ Bnez(TMP, label);
4664 } else {
4665 __ LoadConst32(TMP, rhs_imm);
4666 __ Blt(lhs, TMP, label);
4667 }
4668 break;
4669 case kCondGE:
4670 if (IsInt<16>(rhs_imm)) {
4671 __ Slti(TMP, lhs, rhs_imm);
4672 __ Beqz(TMP, label);
4673 } else {
4674 __ LoadConst32(TMP, rhs_imm);
4675 __ Bge(lhs, TMP, label);
4676 }
4677 break;
4678 case kCondLE:
4679 if (IsInt<16>(rhs_imm + 1)) {
4680 // Simulate lhs <= rhs via lhs < rhs + 1.
4681 __ Slti(TMP, lhs, rhs_imm + 1);
4682 __ Bnez(TMP, label);
4683 } else {
4684 __ LoadConst32(TMP, rhs_imm);
4685 __ Bge(TMP, lhs, label);
4686 }
4687 break;
4688 case kCondGT:
4689 if (IsInt<16>(rhs_imm + 1)) {
4690 // Simulate lhs > rhs via !(lhs < rhs + 1).
4691 __ Slti(TMP, lhs, rhs_imm + 1);
4692 __ Beqz(TMP, label);
4693 } else {
4694 __ LoadConst32(TMP, rhs_imm);
4695 __ Blt(TMP, lhs, label);
4696 }
4697 break;
4698 case kCondB:
4699 if (IsInt<16>(rhs_imm)) {
4700 __ Sltiu(TMP, lhs, rhs_imm);
4701 __ Bnez(TMP, label);
4702 } else {
4703 __ LoadConst32(TMP, rhs_imm);
4704 __ Bltu(lhs, TMP, label);
4705 }
4706 break;
4707 case kCondAE:
4708 if (IsInt<16>(rhs_imm)) {
4709 __ Sltiu(TMP, lhs, rhs_imm);
4710 __ Beqz(TMP, label);
4711 } else {
4712 __ LoadConst32(TMP, rhs_imm);
4713 __ Bgeu(lhs, TMP, label);
4714 }
4715 break;
4716 case kCondBE:
4717 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4718 // Simulate lhs <= rhs via lhs < rhs + 1.
4719 // Note that this only works if rhs + 1 does not overflow
4720 // to 0, hence the check above.
4721 __ Sltiu(TMP, lhs, rhs_imm + 1);
4722 __ Bnez(TMP, label);
4723 } else {
4724 __ LoadConst32(TMP, rhs_imm);
4725 __ Bgeu(TMP, lhs, label);
4726 }
4727 break;
4728 case kCondA:
4729 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4730 // Simulate lhs > rhs via !(lhs < rhs + 1).
4731 // Note that this only works if rhs + 1 does not overflow
4732 // to 0, hence the check above.
4733 __ Sltiu(TMP, lhs, rhs_imm + 1);
4734 __ Beqz(TMP, label);
4735 } else {
4736 __ LoadConst32(TMP, rhs_imm);
4737 __ Bltu(TMP, lhs, label);
4738 }
4739 break;
4740 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004741 }
4742 }
4743}
4744
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004745void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4746 LocationSummary* locations) {
4747 Register dst = locations->Out().AsRegister<Register>();
4748 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4749 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4750 Location rhs_location = locations->InAt(1);
4751 Register rhs_high = ZERO;
4752 Register rhs_low = ZERO;
4753 int64_t imm = 0;
4754 uint32_t imm_high = 0;
4755 uint32_t imm_low = 0;
4756 bool use_imm = rhs_location.IsConstant();
4757 if (use_imm) {
4758 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4759 imm_high = High32Bits(imm);
4760 imm_low = Low32Bits(imm);
4761 } else {
4762 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4763 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4764 }
4765 if (use_imm && imm == 0) {
4766 switch (cond) {
4767 case kCondEQ:
4768 case kCondBE: // <= 0 if zero
4769 __ Or(dst, lhs_high, lhs_low);
4770 __ Sltiu(dst, dst, 1);
4771 break;
4772 case kCondNE:
4773 case kCondA: // > 0 if non-zero
4774 __ Or(dst, lhs_high, lhs_low);
4775 __ Sltu(dst, ZERO, dst);
4776 break;
4777 case kCondLT:
4778 __ Slt(dst, lhs_high, ZERO);
4779 break;
4780 case kCondGE:
4781 __ Slt(dst, lhs_high, ZERO);
4782 __ Xori(dst, dst, 1);
4783 break;
4784 case kCondLE:
4785 __ Or(TMP, lhs_high, lhs_low);
4786 __ Sra(AT, lhs_high, 31);
4787 __ Sltu(dst, AT, TMP);
4788 __ Xori(dst, dst, 1);
4789 break;
4790 case kCondGT:
4791 __ Or(TMP, lhs_high, lhs_low);
4792 __ Sra(AT, lhs_high, 31);
4793 __ Sltu(dst, AT, TMP);
4794 break;
4795 case kCondB: // always false
4796 __ Andi(dst, dst, 0);
4797 break;
4798 case kCondAE: // always true
4799 __ Ori(dst, ZERO, 1);
4800 break;
4801 }
4802 } else if (use_imm) {
4803 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4804 switch (cond) {
4805 case kCondEQ:
4806 __ LoadConst32(TMP, imm_high);
4807 __ Xor(TMP, TMP, lhs_high);
4808 __ LoadConst32(AT, imm_low);
4809 __ Xor(AT, AT, lhs_low);
4810 __ Or(dst, TMP, AT);
4811 __ Sltiu(dst, dst, 1);
4812 break;
4813 case kCondNE:
4814 __ LoadConst32(TMP, imm_high);
4815 __ Xor(TMP, TMP, lhs_high);
4816 __ LoadConst32(AT, imm_low);
4817 __ Xor(AT, AT, lhs_low);
4818 __ Or(dst, TMP, AT);
4819 __ Sltu(dst, ZERO, dst);
4820 break;
4821 case kCondLT:
4822 case kCondGE:
4823 if (dst == lhs_low) {
4824 __ LoadConst32(TMP, imm_low);
4825 __ Sltu(dst, lhs_low, TMP);
4826 }
4827 __ LoadConst32(TMP, imm_high);
4828 __ Slt(AT, lhs_high, TMP);
4829 __ Slt(TMP, TMP, lhs_high);
4830 if (dst != lhs_low) {
4831 __ LoadConst32(dst, imm_low);
4832 __ Sltu(dst, lhs_low, dst);
4833 }
4834 __ Slt(dst, TMP, dst);
4835 __ Or(dst, dst, AT);
4836 if (cond == kCondGE) {
4837 __ Xori(dst, dst, 1);
4838 }
4839 break;
4840 case kCondGT:
4841 case kCondLE:
4842 if (dst == lhs_low) {
4843 __ LoadConst32(TMP, imm_low);
4844 __ Sltu(dst, TMP, lhs_low);
4845 }
4846 __ LoadConst32(TMP, imm_high);
4847 __ Slt(AT, TMP, lhs_high);
4848 __ Slt(TMP, lhs_high, TMP);
4849 if (dst != lhs_low) {
4850 __ LoadConst32(dst, imm_low);
4851 __ Sltu(dst, dst, lhs_low);
4852 }
4853 __ Slt(dst, TMP, dst);
4854 __ Or(dst, dst, AT);
4855 if (cond == kCondLE) {
4856 __ Xori(dst, dst, 1);
4857 }
4858 break;
4859 case kCondB:
4860 case kCondAE:
4861 if (dst == lhs_low) {
4862 __ LoadConst32(TMP, imm_low);
4863 __ Sltu(dst, lhs_low, TMP);
4864 }
4865 __ LoadConst32(TMP, imm_high);
4866 __ Sltu(AT, lhs_high, TMP);
4867 __ Sltu(TMP, TMP, lhs_high);
4868 if (dst != lhs_low) {
4869 __ LoadConst32(dst, imm_low);
4870 __ Sltu(dst, lhs_low, dst);
4871 }
4872 __ Slt(dst, TMP, dst);
4873 __ Or(dst, dst, AT);
4874 if (cond == kCondAE) {
4875 __ Xori(dst, dst, 1);
4876 }
4877 break;
4878 case kCondA:
4879 case kCondBE:
4880 if (dst == lhs_low) {
4881 __ LoadConst32(TMP, imm_low);
4882 __ Sltu(dst, TMP, lhs_low);
4883 }
4884 __ LoadConst32(TMP, imm_high);
4885 __ Sltu(AT, TMP, lhs_high);
4886 __ Sltu(TMP, lhs_high, TMP);
4887 if (dst != lhs_low) {
4888 __ LoadConst32(dst, imm_low);
4889 __ Sltu(dst, dst, lhs_low);
4890 }
4891 __ Slt(dst, TMP, dst);
4892 __ Or(dst, dst, AT);
4893 if (cond == kCondBE) {
4894 __ Xori(dst, dst, 1);
4895 }
4896 break;
4897 }
4898 } else {
4899 switch (cond) {
4900 case kCondEQ:
4901 __ Xor(TMP, lhs_high, rhs_high);
4902 __ Xor(AT, lhs_low, rhs_low);
4903 __ Or(dst, TMP, AT);
4904 __ Sltiu(dst, dst, 1);
4905 break;
4906 case kCondNE:
4907 __ Xor(TMP, lhs_high, rhs_high);
4908 __ Xor(AT, lhs_low, rhs_low);
4909 __ Or(dst, TMP, AT);
4910 __ Sltu(dst, ZERO, dst);
4911 break;
4912 case kCondLT:
4913 case kCondGE:
4914 __ Slt(TMP, rhs_high, lhs_high);
4915 __ Sltu(AT, lhs_low, rhs_low);
4916 __ Slt(TMP, TMP, AT);
4917 __ Slt(AT, lhs_high, rhs_high);
4918 __ Or(dst, AT, TMP);
4919 if (cond == kCondGE) {
4920 __ Xori(dst, dst, 1);
4921 }
4922 break;
4923 case kCondGT:
4924 case kCondLE:
4925 __ Slt(TMP, lhs_high, rhs_high);
4926 __ Sltu(AT, rhs_low, lhs_low);
4927 __ Slt(TMP, TMP, AT);
4928 __ Slt(AT, rhs_high, lhs_high);
4929 __ Or(dst, AT, TMP);
4930 if (cond == kCondLE) {
4931 __ Xori(dst, dst, 1);
4932 }
4933 break;
4934 case kCondB:
4935 case kCondAE:
4936 __ Sltu(TMP, rhs_high, lhs_high);
4937 __ Sltu(AT, lhs_low, rhs_low);
4938 __ Slt(TMP, TMP, AT);
4939 __ Sltu(AT, lhs_high, rhs_high);
4940 __ Or(dst, AT, TMP);
4941 if (cond == kCondAE) {
4942 __ Xori(dst, dst, 1);
4943 }
4944 break;
4945 case kCondA:
4946 case kCondBE:
4947 __ Sltu(TMP, lhs_high, rhs_high);
4948 __ Sltu(AT, rhs_low, lhs_low);
4949 __ Slt(TMP, TMP, AT);
4950 __ Sltu(AT, rhs_high, lhs_high);
4951 __ Or(dst, AT, TMP);
4952 if (cond == kCondBE) {
4953 __ Xori(dst, dst, 1);
4954 }
4955 break;
4956 }
4957 }
4958}
4959
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004960void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4961 LocationSummary* locations,
4962 MipsLabel* label) {
4963 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4964 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4965 Location rhs_location = locations->InAt(1);
4966 Register rhs_high = ZERO;
4967 Register rhs_low = ZERO;
4968 int64_t imm = 0;
4969 uint32_t imm_high = 0;
4970 uint32_t imm_low = 0;
4971 bool use_imm = rhs_location.IsConstant();
4972 if (use_imm) {
4973 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4974 imm_high = High32Bits(imm);
4975 imm_low = Low32Bits(imm);
4976 } else {
4977 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4978 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4979 }
4980
4981 if (use_imm && imm == 0) {
4982 switch (cond) {
4983 case kCondEQ:
4984 case kCondBE: // <= 0 if zero
4985 __ Or(TMP, lhs_high, lhs_low);
4986 __ Beqz(TMP, label);
4987 break;
4988 case kCondNE:
4989 case kCondA: // > 0 if non-zero
4990 __ Or(TMP, lhs_high, lhs_low);
4991 __ Bnez(TMP, label);
4992 break;
4993 case kCondLT:
4994 __ Bltz(lhs_high, label);
4995 break;
4996 case kCondGE:
4997 __ Bgez(lhs_high, label);
4998 break;
4999 case kCondLE:
5000 __ Or(TMP, lhs_high, lhs_low);
5001 __ Sra(AT, lhs_high, 31);
5002 __ Bgeu(AT, TMP, label);
5003 break;
5004 case kCondGT:
5005 __ Or(TMP, lhs_high, lhs_low);
5006 __ Sra(AT, lhs_high, 31);
5007 __ Bltu(AT, TMP, label);
5008 break;
5009 case kCondB: // always false
5010 break;
5011 case kCondAE: // always true
5012 __ B(label);
5013 break;
5014 }
5015 } else if (use_imm) {
5016 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5017 switch (cond) {
5018 case kCondEQ:
5019 __ LoadConst32(TMP, imm_high);
5020 __ Xor(TMP, TMP, lhs_high);
5021 __ LoadConst32(AT, imm_low);
5022 __ Xor(AT, AT, lhs_low);
5023 __ Or(TMP, TMP, AT);
5024 __ Beqz(TMP, label);
5025 break;
5026 case kCondNE:
5027 __ LoadConst32(TMP, imm_high);
5028 __ Xor(TMP, TMP, lhs_high);
5029 __ LoadConst32(AT, imm_low);
5030 __ Xor(AT, AT, lhs_low);
5031 __ Or(TMP, TMP, AT);
5032 __ Bnez(TMP, label);
5033 break;
5034 case kCondLT:
5035 __ LoadConst32(TMP, imm_high);
5036 __ Blt(lhs_high, TMP, label);
5037 __ Slt(TMP, TMP, lhs_high);
5038 __ LoadConst32(AT, imm_low);
5039 __ Sltu(AT, lhs_low, AT);
5040 __ Blt(TMP, AT, label);
5041 break;
5042 case kCondGE:
5043 __ LoadConst32(TMP, imm_high);
5044 __ Blt(TMP, lhs_high, label);
5045 __ Slt(TMP, lhs_high, TMP);
5046 __ LoadConst32(AT, imm_low);
5047 __ Sltu(AT, lhs_low, AT);
5048 __ Or(TMP, TMP, AT);
5049 __ Beqz(TMP, label);
5050 break;
5051 case kCondLE:
5052 __ LoadConst32(TMP, imm_high);
5053 __ Blt(lhs_high, TMP, label);
5054 __ Slt(TMP, TMP, lhs_high);
5055 __ LoadConst32(AT, imm_low);
5056 __ Sltu(AT, AT, lhs_low);
5057 __ Or(TMP, TMP, AT);
5058 __ Beqz(TMP, label);
5059 break;
5060 case kCondGT:
5061 __ LoadConst32(TMP, imm_high);
5062 __ Blt(TMP, lhs_high, label);
5063 __ Slt(TMP, lhs_high, TMP);
5064 __ LoadConst32(AT, imm_low);
5065 __ Sltu(AT, AT, lhs_low);
5066 __ Blt(TMP, AT, label);
5067 break;
5068 case kCondB:
5069 __ LoadConst32(TMP, imm_high);
5070 __ Bltu(lhs_high, TMP, label);
5071 __ Sltu(TMP, TMP, lhs_high);
5072 __ LoadConst32(AT, imm_low);
5073 __ Sltu(AT, lhs_low, AT);
5074 __ Blt(TMP, AT, label);
5075 break;
5076 case kCondAE:
5077 __ LoadConst32(TMP, imm_high);
5078 __ Bltu(TMP, lhs_high, label);
5079 __ Sltu(TMP, lhs_high, TMP);
5080 __ LoadConst32(AT, imm_low);
5081 __ Sltu(AT, lhs_low, AT);
5082 __ Or(TMP, TMP, AT);
5083 __ Beqz(TMP, label);
5084 break;
5085 case kCondBE:
5086 __ LoadConst32(TMP, imm_high);
5087 __ Bltu(lhs_high, TMP, label);
5088 __ Sltu(TMP, TMP, lhs_high);
5089 __ LoadConst32(AT, imm_low);
5090 __ Sltu(AT, AT, lhs_low);
5091 __ Or(TMP, TMP, AT);
5092 __ Beqz(TMP, label);
5093 break;
5094 case kCondA:
5095 __ LoadConst32(TMP, imm_high);
5096 __ Bltu(TMP, lhs_high, label);
5097 __ Sltu(TMP, lhs_high, TMP);
5098 __ LoadConst32(AT, imm_low);
5099 __ Sltu(AT, AT, lhs_low);
5100 __ Blt(TMP, AT, label);
5101 break;
5102 }
5103 } else {
5104 switch (cond) {
5105 case kCondEQ:
5106 __ Xor(TMP, lhs_high, rhs_high);
5107 __ Xor(AT, lhs_low, rhs_low);
5108 __ Or(TMP, TMP, AT);
5109 __ Beqz(TMP, label);
5110 break;
5111 case kCondNE:
5112 __ Xor(TMP, lhs_high, rhs_high);
5113 __ Xor(AT, lhs_low, rhs_low);
5114 __ Or(TMP, TMP, AT);
5115 __ Bnez(TMP, label);
5116 break;
5117 case kCondLT:
5118 __ Blt(lhs_high, rhs_high, label);
5119 __ Slt(TMP, rhs_high, lhs_high);
5120 __ Sltu(AT, lhs_low, rhs_low);
5121 __ Blt(TMP, AT, label);
5122 break;
5123 case kCondGE:
5124 __ Blt(rhs_high, lhs_high, label);
5125 __ Slt(TMP, lhs_high, rhs_high);
5126 __ Sltu(AT, lhs_low, rhs_low);
5127 __ Or(TMP, TMP, AT);
5128 __ Beqz(TMP, label);
5129 break;
5130 case kCondLE:
5131 __ Blt(lhs_high, rhs_high, label);
5132 __ Slt(TMP, rhs_high, lhs_high);
5133 __ Sltu(AT, rhs_low, lhs_low);
5134 __ Or(TMP, TMP, AT);
5135 __ Beqz(TMP, label);
5136 break;
5137 case kCondGT:
5138 __ Blt(rhs_high, lhs_high, label);
5139 __ Slt(TMP, lhs_high, rhs_high);
5140 __ Sltu(AT, rhs_low, lhs_low);
5141 __ Blt(TMP, AT, label);
5142 break;
5143 case kCondB:
5144 __ Bltu(lhs_high, rhs_high, label);
5145 __ Sltu(TMP, rhs_high, lhs_high);
5146 __ Sltu(AT, lhs_low, rhs_low);
5147 __ Blt(TMP, AT, label);
5148 break;
5149 case kCondAE:
5150 __ Bltu(rhs_high, lhs_high, label);
5151 __ Sltu(TMP, lhs_high, rhs_high);
5152 __ Sltu(AT, lhs_low, rhs_low);
5153 __ Or(TMP, TMP, AT);
5154 __ Beqz(TMP, label);
5155 break;
5156 case kCondBE:
5157 __ Bltu(lhs_high, rhs_high, label);
5158 __ Sltu(TMP, rhs_high, lhs_high);
5159 __ Sltu(AT, rhs_low, lhs_low);
5160 __ Or(TMP, TMP, AT);
5161 __ Beqz(TMP, label);
5162 break;
5163 case kCondA:
5164 __ Bltu(rhs_high, lhs_high, label);
5165 __ Sltu(TMP, lhs_high, rhs_high);
5166 __ Sltu(AT, rhs_low, lhs_low);
5167 __ Blt(TMP, AT, label);
5168 break;
5169 }
5170 }
5171}
5172
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005173void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5174 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005176 LocationSummary* locations) {
5177 Register dst = locations->Out().AsRegister<Register>();
5178 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5179 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5180 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005182 if (isR6) {
5183 switch (cond) {
5184 case kCondEQ:
5185 __ CmpEqS(FTMP, lhs, rhs);
5186 __ Mfc1(dst, FTMP);
5187 __ Andi(dst, dst, 1);
5188 break;
5189 case kCondNE:
5190 __ CmpEqS(FTMP, lhs, rhs);
5191 __ Mfc1(dst, FTMP);
5192 __ Addiu(dst, dst, 1);
5193 break;
5194 case kCondLT:
5195 if (gt_bias) {
5196 __ CmpLtS(FTMP, lhs, rhs);
5197 } else {
5198 __ CmpUltS(FTMP, lhs, rhs);
5199 }
5200 __ Mfc1(dst, FTMP);
5201 __ Andi(dst, dst, 1);
5202 break;
5203 case kCondLE:
5204 if (gt_bias) {
5205 __ CmpLeS(FTMP, lhs, rhs);
5206 } else {
5207 __ CmpUleS(FTMP, lhs, rhs);
5208 }
5209 __ Mfc1(dst, FTMP);
5210 __ Andi(dst, dst, 1);
5211 break;
5212 case kCondGT:
5213 if (gt_bias) {
5214 __ CmpUltS(FTMP, rhs, lhs);
5215 } else {
5216 __ CmpLtS(FTMP, rhs, lhs);
5217 }
5218 __ Mfc1(dst, FTMP);
5219 __ Andi(dst, dst, 1);
5220 break;
5221 case kCondGE:
5222 if (gt_bias) {
5223 __ CmpUleS(FTMP, rhs, lhs);
5224 } else {
5225 __ CmpLeS(FTMP, rhs, lhs);
5226 }
5227 __ Mfc1(dst, FTMP);
5228 __ Andi(dst, dst, 1);
5229 break;
5230 default:
5231 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5232 UNREACHABLE();
5233 }
5234 } else {
5235 switch (cond) {
5236 case kCondEQ:
5237 __ CeqS(0, lhs, rhs);
5238 __ LoadConst32(dst, 1);
5239 __ Movf(dst, ZERO, 0);
5240 break;
5241 case kCondNE:
5242 __ CeqS(0, lhs, rhs);
5243 __ LoadConst32(dst, 1);
5244 __ Movt(dst, ZERO, 0);
5245 break;
5246 case kCondLT:
5247 if (gt_bias) {
5248 __ ColtS(0, lhs, rhs);
5249 } else {
5250 __ CultS(0, lhs, rhs);
5251 }
5252 __ LoadConst32(dst, 1);
5253 __ Movf(dst, ZERO, 0);
5254 break;
5255 case kCondLE:
5256 if (gt_bias) {
5257 __ ColeS(0, lhs, rhs);
5258 } else {
5259 __ CuleS(0, lhs, rhs);
5260 }
5261 __ LoadConst32(dst, 1);
5262 __ Movf(dst, ZERO, 0);
5263 break;
5264 case kCondGT:
5265 if (gt_bias) {
5266 __ CultS(0, rhs, lhs);
5267 } else {
5268 __ ColtS(0, rhs, lhs);
5269 }
5270 __ LoadConst32(dst, 1);
5271 __ Movf(dst, ZERO, 0);
5272 break;
5273 case kCondGE:
5274 if (gt_bias) {
5275 __ CuleS(0, rhs, lhs);
5276 } else {
5277 __ ColeS(0, rhs, lhs);
5278 }
5279 __ LoadConst32(dst, 1);
5280 __ Movf(dst, ZERO, 0);
5281 break;
5282 default:
5283 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5284 UNREACHABLE();
5285 }
5286 }
5287 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005288 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005289 if (isR6) {
5290 switch (cond) {
5291 case kCondEQ:
5292 __ CmpEqD(FTMP, lhs, rhs);
5293 __ Mfc1(dst, FTMP);
5294 __ Andi(dst, dst, 1);
5295 break;
5296 case kCondNE:
5297 __ CmpEqD(FTMP, lhs, rhs);
5298 __ Mfc1(dst, FTMP);
5299 __ Addiu(dst, dst, 1);
5300 break;
5301 case kCondLT:
5302 if (gt_bias) {
5303 __ CmpLtD(FTMP, lhs, rhs);
5304 } else {
5305 __ CmpUltD(FTMP, lhs, rhs);
5306 }
5307 __ Mfc1(dst, FTMP);
5308 __ Andi(dst, dst, 1);
5309 break;
5310 case kCondLE:
5311 if (gt_bias) {
5312 __ CmpLeD(FTMP, lhs, rhs);
5313 } else {
5314 __ CmpUleD(FTMP, lhs, rhs);
5315 }
5316 __ Mfc1(dst, FTMP);
5317 __ Andi(dst, dst, 1);
5318 break;
5319 case kCondGT:
5320 if (gt_bias) {
5321 __ CmpUltD(FTMP, rhs, lhs);
5322 } else {
5323 __ CmpLtD(FTMP, rhs, lhs);
5324 }
5325 __ Mfc1(dst, FTMP);
5326 __ Andi(dst, dst, 1);
5327 break;
5328 case kCondGE:
5329 if (gt_bias) {
5330 __ CmpUleD(FTMP, rhs, lhs);
5331 } else {
5332 __ CmpLeD(FTMP, rhs, lhs);
5333 }
5334 __ Mfc1(dst, FTMP);
5335 __ Andi(dst, dst, 1);
5336 break;
5337 default:
5338 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5339 UNREACHABLE();
5340 }
5341 } else {
5342 switch (cond) {
5343 case kCondEQ:
5344 __ CeqD(0, lhs, rhs);
5345 __ LoadConst32(dst, 1);
5346 __ Movf(dst, ZERO, 0);
5347 break;
5348 case kCondNE:
5349 __ CeqD(0, lhs, rhs);
5350 __ LoadConst32(dst, 1);
5351 __ Movt(dst, ZERO, 0);
5352 break;
5353 case kCondLT:
5354 if (gt_bias) {
5355 __ ColtD(0, lhs, rhs);
5356 } else {
5357 __ CultD(0, lhs, rhs);
5358 }
5359 __ LoadConst32(dst, 1);
5360 __ Movf(dst, ZERO, 0);
5361 break;
5362 case kCondLE:
5363 if (gt_bias) {
5364 __ ColeD(0, lhs, rhs);
5365 } else {
5366 __ CuleD(0, lhs, rhs);
5367 }
5368 __ LoadConst32(dst, 1);
5369 __ Movf(dst, ZERO, 0);
5370 break;
5371 case kCondGT:
5372 if (gt_bias) {
5373 __ CultD(0, rhs, lhs);
5374 } else {
5375 __ ColtD(0, rhs, lhs);
5376 }
5377 __ LoadConst32(dst, 1);
5378 __ Movf(dst, ZERO, 0);
5379 break;
5380 case kCondGE:
5381 if (gt_bias) {
5382 __ CuleD(0, rhs, lhs);
5383 } else {
5384 __ ColeD(0, rhs, lhs);
5385 }
5386 __ LoadConst32(dst, 1);
5387 __ Movf(dst, ZERO, 0);
5388 break;
5389 default:
5390 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5391 UNREACHABLE();
5392 }
5393 }
5394 }
5395}
5396
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005397bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5398 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005400 LocationSummary* input_locations,
5401 int cc) {
5402 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5403 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5404 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005405 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005406 switch (cond) {
5407 case kCondEQ:
5408 __ CeqS(cc, lhs, rhs);
5409 return false;
5410 case kCondNE:
5411 __ CeqS(cc, lhs, rhs);
5412 return true;
5413 case kCondLT:
5414 if (gt_bias) {
5415 __ ColtS(cc, lhs, rhs);
5416 } else {
5417 __ CultS(cc, lhs, rhs);
5418 }
5419 return false;
5420 case kCondLE:
5421 if (gt_bias) {
5422 __ ColeS(cc, lhs, rhs);
5423 } else {
5424 __ CuleS(cc, lhs, rhs);
5425 }
5426 return false;
5427 case kCondGT:
5428 if (gt_bias) {
5429 __ CultS(cc, rhs, lhs);
5430 } else {
5431 __ ColtS(cc, rhs, lhs);
5432 }
5433 return false;
5434 case kCondGE:
5435 if (gt_bias) {
5436 __ CuleS(cc, rhs, lhs);
5437 } else {
5438 __ ColeS(cc, rhs, lhs);
5439 }
5440 return false;
5441 default:
5442 LOG(FATAL) << "Unexpected non-floating-point condition";
5443 UNREACHABLE();
5444 }
5445 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005446 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005447 switch (cond) {
5448 case kCondEQ:
5449 __ CeqD(cc, lhs, rhs);
5450 return false;
5451 case kCondNE:
5452 __ CeqD(cc, lhs, rhs);
5453 return true;
5454 case kCondLT:
5455 if (gt_bias) {
5456 __ ColtD(cc, lhs, rhs);
5457 } else {
5458 __ CultD(cc, lhs, rhs);
5459 }
5460 return false;
5461 case kCondLE:
5462 if (gt_bias) {
5463 __ ColeD(cc, lhs, rhs);
5464 } else {
5465 __ CuleD(cc, lhs, rhs);
5466 }
5467 return false;
5468 case kCondGT:
5469 if (gt_bias) {
5470 __ CultD(cc, rhs, lhs);
5471 } else {
5472 __ ColtD(cc, rhs, lhs);
5473 }
5474 return false;
5475 case kCondGE:
5476 if (gt_bias) {
5477 __ CuleD(cc, rhs, lhs);
5478 } else {
5479 __ ColeD(cc, rhs, lhs);
5480 }
5481 return false;
5482 default:
5483 LOG(FATAL) << "Unexpected non-floating-point condition";
5484 UNREACHABLE();
5485 }
5486 }
5487}
5488
5489bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5490 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005491 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005492 LocationSummary* input_locations,
5493 FRegister dst) {
5494 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5495 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5496 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005498 switch (cond) {
5499 case kCondEQ:
5500 __ CmpEqS(dst, lhs, rhs);
5501 return false;
5502 case kCondNE:
5503 __ CmpEqS(dst, lhs, rhs);
5504 return true;
5505 case kCondLT:
5506 if (gt_bias) {
5507 __ CmpLtS(dst, lhs, rhs);
5508 } else {
5509 __ CmpUltS(dst, lhs, rhs);
5510 }
5511 return false;
5512 case kCondLE:
5513 if (gt_bias) {
5514 __ CmpLeS(dst, lhs, rhs);
5515 } else {
5516 __ CmpUleS(dst, lhs, rhs);
5517 }
5518 return false;
5519 case kCondGT:
5520 if (gt_bias) {
5521 __ CmpUltS(dst, rhs, lhs);
5522 } else {
5523 __ CmpLtS(dst, rhs, lhs);
5524 }
5525 return false;
5526 case kCondGE:
5527 if (gt_bias) {
5528 __ CmpUleS(dst, rhs, lhs);
5529 } else {
5530 __ CmpLeS(dst, rhs, lhs);
5531 }
5532 return false;
5533 default:
5534 LOG(FATAL) << "Unexpected non-floating-point condition";
5535 UNREACHABLE();
5536 }
5537 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005539 switch (cond) {
5540 case kCondEQ:
5541 __ CmpEqD(dst, lhs, rhs);
5542 return false;
5543 case kCondNE:
5544 __ CmpEqD(dst, lhs, rhs);
5545 return true;
5546 case kCondLT:
5547 if (gt_bias) {
5548 __ CmpLtD(dst, lhs, rhs);
5549 } else {
5550 __ CmpUltD(dst, lhs, rhs);
5551 }
5552 return false;
5553 case kCondLE:
5554 if (gt_bias) {
5555 __ CmpLeD(dst, lhs, rhs);
5556 } else {
5557 __ CmpUleD(dst, lhs, rhs);
5558 }
5559 return false;
5560 case kCondGT:
5561 if (gt_bias) {
5562 __ CmpUltD(dst, rhs, lhs);
5563 } else {
5564 __ CmpLtD(dst, rhs, lhs);
5565 }
5566 return false;
5567 case kCondGE:
5568 if (gt_bias) {
5569 __ CmpUleD(dst, rhs, lhs);
5570 } else {
5571 __ CmpLeD(dst, rhs, lhs);
5572 }
5573 return false;
5574 default:
5575 LOG(FATAL) << "Unexpected non-floating-point condition";
5576 UNREACHABLE();
5577 }
5578 }
5579}
5580
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005581void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5582 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005584 LocationSummary* locations,
5585 MipsLabel* label) {
5586 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5587 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5588 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005590 if (isR6) {
5591 switch (cond) {
5592 case kCondEQ:
5593 __ CmpEqS(FTMP, lhs, rhs);
5594 __ Bc1nez(FTMP, label);
5595 break;
5596 case kCondNE:
5597 __ CmpEqS(FTMP, lhs, rhs);
5598 __ Bc1eqz(FTMP, label);
5599 break;
5600 case kCondLT:
5601 if (gt_bias) {
5602 __ CmpLtS(FTMP, lhs, rhs);
5603 } else {
5604 __ CmpUltS(FTMP, lhs, rhs);
5605 }
5606 __ Bc1nez(FTMP, label);
5607 break;
5608 case kCondLE:
5609 if (gt_bias) {
5610 __ CmpLeS(FTMP, lhs, rhs);
5611 } else {
5612 __ CmpUleS(FTMP, lhs, rhs);
5613 }
5614 __ Bc1nez(FTMP, label);
5615 break;
5616 case kCondGT:
5617 if (gt_bias) {
5618 __ CmpUltS(FTMP, rhs, lhs);
5619 } else {
5620 __ CmpLtS(FTMP, rhs, lhs);
5621 }
5622 __ Bc1nez(FTMP, label);
5623 break;
5624 case kCondGE:
5625 if (gt_bias) {
5626 __ CmpUleS(FTMP, rhs, lhs);
5627 } else {
5628 __ CmpLeS(FTMP, rhs, lhs);
5629 }
5630 __ Bc1nez(FTMP, label);
5631 break;
5632 default:
5633 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005634 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005635 }
5636 } else {
5637 switch (cond) {
5638 case kCondEQ:
5639 __ CeqS(0, lhs, rhs);
5640 __ Bc1t(0, label);
5641 break;
5642 case kCondNE:
5643 __ CeqS(0, lhs, rhs);
5644 __ Bc1f(0, label);
5645 break;
5646 case kCondLT:
5647 if (gt_bias) {
5648 __ ColtS(0, lhs, rhs);
5649 } else {
5650 __ CultS(0, lhs, rhs);
5651 }
5652 __ Bc1t(0, label);
5653 break;
5654 case kCondLE:
5655 if (gt_bias) {
5656 __ ColeS(0, lhs, rhs);
5657 } else {
5658 __ CuleS(0, lhs, rhs);
5659 }
5660 __ Bc1t(0, label);
5661 break;
5662 case kCondGT:
5663 if (gt_bias) {
5664 __ CultS(0, rhs, lhs);
5665 } else {
5666 __ ColtS(0, rhs, lhs);
5667 }
5668 __ Bc1t(0, label);
5669 break;
5670 case kCondGE:
5671 if (gt_bias) {
5672 __ CuleS(0, rhs, lhs);
5673 } else {
5674 __ ColeS(0, rhs, lhs);
5675 }
5676 __ Bc1t(0, label);
5677 break;
5678 default:
5679 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005680 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005681 }
5682 }
5683 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005684 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005685 if (isR6) {
5686 switch (cond) {
5687 case kCondEQ:
5688 __ CmpEqD(FTMP, lhs, rhs);
5689 __ Bc1nez(FTMP, label);
5690 break;
5691 case kCondNE:
5692 __ CmpEqD(FTMP, lhs, rhs);
5693 __ Bc1eqz(FTMP, label);
5694 break;
5695 case kCondLT:
5696 if (gt_bias) {
5697 __ CmpLtD(FTMP, lhs, rhs);
5698 } else {
5699 __ CmpUltD(FTMP, lhs, rhs);
5700 }
5701 __ Bc1nez(FTMP, label);
5702 break;
5703 case kCondLE:
5704 if (gt_bias) {
5705 __ CmpLeD(FTMP, lhs, rhs);
5706 } else {
5707 __ CmpUleD(FTMP, lhs, rhs);
5708 }
5709 __ Bc1nez(FTMP, label);
5710 break;
5711 case kCondGT:
5712 if (gt_bias) {
5713 __ CmpUltD(FTMP, rhs, lhs);
5714 } else {
5715 __ CmpLtD(FTMP, rhs, lhs);
5716 }
5717 __ Bc1nez(FTMP, label);
5718 break;
5719 case kCondGE:
5720 if (gt_bias) {
5721 __ CmpUleD(FTMP, rhs, lhs);
5722 } else {
5723 __ CmpLeD(FTMP, rhs, lhs);
5724 }
5725 __ Bc1nez(FTMP, label);
5726 break;
5727 default:
5728 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005729 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005730 }
5731 } else {
5732 switch (cond) {
5733 case kCondEQ:
5734 __ CeqD(0, lhs, rhs);
5735 __ Bc1t(0, label);
5736 break;
5737 case kCondNE:
5738 __ CeqD(0, lhs, rhs);
5739 __ Bc1f(0, label);
5740 break;
5741 case kCondLT:
5742 if (gt_bias) {
5743 __ ColtD(0, lhs, rhs);
5744 } else {
5745 __ CultD(0, lhs, rhs);
5746 }
5747 __ Bc1t(0, label);
5748 break;
5749 case kCondLE:
5750 if (gt_bias) {
5751 __ ColeD(0, lhs, rhs);
5752 } else {
5753 __ CuleD(0, lhs, rhs);
5754 }
5755 __ Bc1t(0, label);
5756 break;
5757 case kCondGT:
5758 if (gt_bias) {
5759 __ CultD(0, rhs, lhs);
5760 } else {
5761 __ ColtD(0, rhs, lhs);
5762 }
5763 __ Bc1t(0, label);
5764 break;
5765 case kCondGE:
5766 if (gt_bias) {
5767 __ CuleD(0, rhs, lhs);
5768 } else {
5769 __ ColeD(0, rhs, lhs);
5770 }
5771 __ Bc1t(0, label);
5772 break;
5773 default:
5774 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005775 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005776 }
5777 }
5778 }
5779}
5780
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005781void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005782 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005783 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005784 MipsLabel* false_target) {
5785 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005786
David Brazdil0debae72015-11-12 18:37:00 +00005787 if (true_target == nullptr && false_target == nullptr) {
5788 // Nothing to do. The code always falls through.
5789 return;
5790 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005791 // Constant condition, statically compared against "true" (integer value 1).
5792 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005793 if (true_target != nullptr) {
5794 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005795 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005797 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005798 if (false_target != nullptr) {
5799 __ B(false_target);
5800 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801 }
David Brazdil0debae72015-11-12 18:37:00 +00005802 return;
5803 }
5804
5805 // The following code generates these patterns:
5806 // (1) true_target == nullptr && false_target != nullptr
5807 // - opposite condition true => branch to false_target
5808 // (2) true_target != nullptr && false_target == nullptr
5809 // - condition true => branch to true_target
5810 // (3) true_target != nullptr && false_target != nullptr
5811 // - condition true => branch to true_target
5812 // - branch to false_target
5813 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005815 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005816 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005817 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005818 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5819 } else {
5820 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5821 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005822 } else {
5823 // The condition instruction has not been materialized, use its inputs as
5824 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005825 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005827 LocationSummary* locations = cond->GetLocations();
5828 IfCondition if_cond = condition->GetCondition();
5829 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005830
David Brazdil0debae72015-11-12 18:37:00 +00005831 if (true_target == nullptr) {
5832 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005833 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005834 }
5835
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005836 switch (type) {
5837 default:
5838 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5839 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005841 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5842 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 case DataType::Type::kFloat32:
5844 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005845 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5846 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005847 }
5848 }
David Brazdil0debae72015-11-12 18:37:00 +00005849
5850 // If neither branch falls through (case 3), the conditional branch to `true_target`
5851 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5852 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005853 __ B(false_target);
5854 }
5855}
5856
5857void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005858 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005859 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005860 locations->SetInAt(0, Location::RequiresRegister());
5861 }
5862}
5863
5864void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005865 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5866 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5867 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5868 nullptr : codegen_->GetLabelOf(true_successor);
5869 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5870 nullptr : codegen_->GetLabelOf(false_successor);
5871 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005872}
5873
5874void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005875 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005876 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005877 InvokeRuntimeCallingConvention calling_convention;
5878 RegisterSet caller_saves = RegisterSet::Empty();
5879 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5880 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005881 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005882 locations->SetInAt(0, Location::RequiresRegister());
5883 }
5884}
5885
5886void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005887 SlowPathCodeMIPS* slow_path =
5888 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005889 GenerateTestAndBranch(deoptimize,
5890 /* condition_input_index */ 0,
5891 slow_path->GetEntryLabel(),
5892 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005893}
5894
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005895// This function returns true if a conditional move can be generated for HSelect.
5896// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5897// branches and regular moves.
5898//
5899// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5900//
5901// While determining feasibility of a conditional move and setting inputs/outputs
5902// are two distinct tasks, this function does both because they share quite a bit
5903// of common logic.
5904static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5905 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5906 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5907 HCondition* condition = cond->AsCondition();
5908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005909 DataType::Type cond_type =
5910 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5911 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005912
5913 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5914 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5915 bool is_true_value_zero_constant =
5916 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5917 bool is_false_value_zero_constant =
5918 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5919
5920 bool can_move_conditionally = false;
5921 bool use_const_for_false_in = false;
5922 bool use_const_for_true_in = false;
5923
5924 if (!cond->IsConstant()) {
5925 switch (cond_type) {
5926 default:
5927 switch (dst_type) {
5928 default:
5929 // Moving int on int condition.
5930 if (is_r6) {
5931 if (is_true_value_zero_constant) {
5932 // seleqz out_reg, false_reg, cond_reg
5933 can_move_conditionally = true;
5934 use_const_for_true_in = true;
5935 } else if (is_false_value_zero_constant) {
5936 // selnez out_reg, true_reg, cond_reg
5937 can_move_conditionally = true;
5938 use_const_for_false_in = true;
5939 } else if (materialized) {
5940 // Not materializing unmaterialized int conditions
5941 // to keep the instruction count low.
5942 // selnez AT, true_reg, cond_reg
5943 // seleqz TMP, false_reg, cond_reg
5944 // or out_reg, AT, TMP
5945 can_move_conditionally = true;
5946 }
5947 } else {
5948 // movn out_reg, true_reg/ZERO, cond_reg
5949 can_move_conditionally = true;
5950 use_const_for_true_in = is_true_value_zero_constant;
5951 }
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005954 // Moving long on int condition.
5955 if (is_r6) {
5956 if (is_true_value_zero_constant) {
5957 // seleqz out_reg_lo, false_reg_lo, cond_reg
5958 // seleqz out_reg_hi, false_reg_hi, cond_reg
5959 can_move_conditionally = true;
5960 use_const_for_true_in = true;
5961 } else if (is_false_value_zero_constant) {
5962 // selnez out_reg_lo, true_reg_lo, cond_reg
5963 // selnez out_reg_hi, true_reg_hi, cond_reg
5964 can_move_conditionally = true;
5965 use_const_for_false_in = true;
5966 }
5967 // Other long conditional moves would generate 6+ instructions,
5968 // which is too many.
5969 } else {
5970 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5971 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5972 can_move_conditionally = true;
5973 use_const_for_true_in = is_true_value_zero_constant;
5974 }
5975 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 case DataType::Type::kFloat32:
5977 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005978 // Moving float/double on int condition.
5979 if (is_r6) {
5980 if (materialized) {
5981 // Not materializing unmaterialized int conditions
5982 // to keep the instruction count low.
5983 can_move_conditionally = true;
5984 if (is_true_value_zero_constant) {
5985 // sltu TMP, ZERO, cond_reg
5986 // mtc1 TMP, temp_cond_reg
5987 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5988 use_const_for_true_in = true;
5989 } else if (is_false_value_zero_constant) {
5990 // sltu TMP, ZERO, cond_reg
5991 // mtc1 TMP, temp_cond_reg
5992 // selnez.fmt out_reg, true_reg, temp_cond_reg
5993 use_const_for_false_in = true;
5994 } else {
5995 // sltu TMP, ZERO, cond_reg
5996 // mtc1 TMP, temp_cond_reg
5997 // sel.fmt temp_cond_reg, false_reg, true_reg
5998 // mov.fmt out_reg, temp_cond_reg
5999 }
6000 }
6001 } else {
6002 // movn.fmt out_reg, true_reg, cond_reg
6003 can_move_conditionally = true;
6004 }
6005 break;
6006 }
6007 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006009 // We don't materialize long comparison now
6010 // and use conditional branches instead.
6011 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006012 case DataType::Type::kFloat32:
6013 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006014 switch (dst_type) {
6015 default:
6016 // Moving int on float/double condition.
6017 if (is_r6) {
6018 if (is_true_value_zero_constant) {
6019 // mfc1 TMP, temp_cond_reg
6020 // seleqz out_reg, false_reg, TMP
6021 can_move_conditionally = true;
6022 use_const_for_true_in = true;
6023 } else if (is_false_value_zero_constant) {
6024 // mfc1 TMP, temp_cond_reg
6025 // selnez out_reg, true_reg, TMP
6026 can_move_conditionally = true;
6027 use_const_for_false_in = true;
6028 } else {
6029 // mfc1 TMP, temp_cond_reg
6030 // selnez AT, true_reg, TMP
6031 // seleqz TMP, false_reg, TMP
6032 // or out_reg, AT, TMP
6033 can_move_conditionally = true;
6034 }
6035 } else {
6036 // movt out_reg, true_reg/ZERO, cc
6037 can_move_conditionally = true;
6038 use_const_for_true_in = is_true_value_zero_constant;
6039 }
6040 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006042 // Moving long on float/double condition.
6043 if (is_r6) {
6044 if (is_true_value_zero_constant) {
6045 // mfc1 TMP, temp_cond_reg
6046 // seleqz out_reg_lo, false_reg_lo, TMP
6047 // seleqz out_reg_hi, false_reg_hi, TMP
6048 can_move_conditionally = true;
6049 use_const_for_true_in = true;
6050 } else if (is_false_value_zero_constant) {
6051 // mfc1 TMP, temp_cond_reg
6052 // selnez out_reg_lo, true_reg_lo, TMP
6053 // selnez out_reg_hi, true_reg_hi, TMP
6054 can_move_conditionally = true;
6055 use_const_for_false_in = true;
6056 }
6057 // Other long conditional moves would generate 6+ instructions,
6058 // which is too many.
6059 } else {
6060 // movt out_reg_lo, true_reg_lo/ZERO, cc
6061 // movt out_reg_hi, true_reg_hi/ZERO, cc
6062 can_move_conditionally = true;
6063 use_const_for_true_in = is_true_value_zero_constant;
6064 }
6065 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006066 case DataType::Type::kFloat32:
6067 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006068 // Moving float/double on float/double condition.
6069 if (is_r6) {
6070 can_move_conditionally = true;
6071 if (is_true_value_zero_constant) {
6072 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6073 use_const_for_true_in = true;
6074 } else if (is_false_value_zero_constant) {
6075 // selnez.fmt out_reg, true_reg, temp_cond_reg
6076 use_const_for_false_in = true;
6077 } else {
6078 // sel.fmt temp_cond_reg, false_reg, true_reg
6079 // mov.fmt out_reg, temp_cond_reg
6080 }
6081 } else {
6082 // movt.fmt out_reg, true_reg, cc
6083 can_move_conditionally = true;
6084 }
6085 break;
6086 }
6087 break;
6088 }
6089 }
6090
6091 if (can_move_conditionally) {
6092 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6093 } else {
6094 DCHECK(!use_const_for_false_in);
6095 DCHECK(!use_const_for_true_in);
6096 }
6097
6098 if (locations_to_set != nullptr) {
6099 if (use_const_for_false_in) {
6100 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6101 } else {
6102 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006104 ? Location::RequiresFpuRegister()
6105 : Location::RequiresRegister());
6106 }
6107 if (use_const_for_true_in) {
6108 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6109 } else {
6110 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006112 ? Location::RequiresFpuRegister()
6113 : Location::RequiresRegister());
6114 }
6115 if (materialized) {
6116 locations_to_set->SetInAt(2, Location::RequiresRegister());
6117 }
6118 // On R6 we don't require the output to be the same as the
6119 // first input for conditional moves unlike on R2.
6120 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6121 if (is_out_same_as_first_in) {
6122 locations_to_set->SetOut(Location::SameAsFirstInput());
6123 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006125 ? Location::RequiresFpuRegister()
6126 : Location::RequiresRegister());
6127 }
6128 }
6129
6130 return can_move_conditionally;
6131}
6132
6133void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6134 LocationSummary* locations = select->GetLocations();
6135 Location dst = locations->Out();
6136 Location src = locations->InAt(1);
6137 Register src_reg = ZERO;
6138 Register src_reg_high = ZERO;
6139 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6140 Register cond_reg = TMP;
6141 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006142 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006143 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006145
6146 if (IsBooleanValueOrMaterializedCondition(cond)) {
6147 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6148 } else {
6149 HCondition* condition = cond->AsCondition();
6150 LocationSummary* cond_locations = cond->GetLocations();
6151 IfCondition if_cond = condition->GetCondition();
6152 cond_type = condition->InputAt(0)->GetType();
6153 switch (cond_type) {
6154 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006156 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6157 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006158 case DataType::Type::kFloat32:
6159 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006160 cond_inverted = MaterializeFpCompareR2(if_cond,
6161 condition->IsGtBias(),
6162 cond_type,
6163 cond_locations,
6164 cond_cc);
6165 break;
6166 }
6167 }
6168
6169 DCHECK(dst.Equals(locations->InAt(0)));
6170 if (src.IsRegister()) {
6171 src_reg = src.AsRegister<Register>();
6172 } else if (src.IsRegisterPair()) {
6173 src_reg = src.AsRegisterPairLow<Register>();
6174 src_reg_high = src.AsRegisterPairHigh<Register>();
6175 } else if (src.IsConstant()) {
6176 DCHECK(src.GetConstant()->IsZeroBitPattern());
6177 }
6178
6179 switch (cond_type) {
6180 default:
6181 switch (dst_type) {
6182 default:
6183 if (cond_inverted) {
6184 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6185 } else {
6186 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6187 }
6188 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006189 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006190 if (cond_inverted) {
6191 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6192 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6193 } else {
6194 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6195 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6196 }
6197 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006199 if (cond_inverted) {
6200 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6201 } else {
6202 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6203 }
6204 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006205 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006206 if (cond_inverted) {
6207 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6208 } else {
6209 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6210 }
6211 break;
6212 }
6213 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006215 LOG(FATAL) << "Unreachable";
6216 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 case DataType::Type::kFloat32:
6218 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006219 switch (dst_type) {
6220 default:
6221 if (cond_inverted) {
6222 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6223 } else {
6224 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6225 }
6226 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006228 if (cond_inverted) {
6229 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6230 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6231 } else {
6232 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6233 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6234 }
6235 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006237 if (cond_inverted) {
6238 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6239 } else {
6240 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6241 }
6242 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006244 if (cond_inverted) {
6245 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6246 } else {
6247 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6248 }
6249 break;
6250 }
6251 break;
6252 }
6253}
6254
6255void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6256 LocationSummary* locations = select->GetLocations();
6257 Location dst = locations->Out();
6258 Location false_src = locations->InAt(0);
6259 Location true_src = locations->InAt(1);
6260 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6261 Register cond_reg = TMP;
6262 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006264 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006266
6267 if (IsBooleanValueOrMaterializedCondition(cond)) {
6268 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6269 } else {
6270 HCondition* condition = cond->AsCondition();
6271 LocationSummary* cond_locations = cond->GetLocations();
6272 IfCondition if_cond = condition->GetCondition();
6273 cond_type = condition->InputAt(0)->GetType();
6274 switch (cond_type) {
6275 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006276 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006277 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6278 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 case DataType::Type::kFloat32:
6280 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006281 cond_inverted = MaterializeFpCompareR6(if_cond,
6282 condition->IsGtBias(),
6283 cond_type,
6284 cond_locations,
6285 fcond_reg);
6286 break;
6287 }
6288 }
6289
6290 if (true_src.IsConstant()) {
6291 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6292 }
6293 if (false_src.IsConstant()) {
6294 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6295 }
6296
6297 switch (dst_type) {
6298 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006300 __ Mfc1(cond_reg, fcond_reg);
6301 }
6302 if (true_src.IsConstant()) {
6303 if (cond_inverted) {
6304 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6305 } else {
6306 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6307 }
6308 } else if (false_src.IsConstant()) {
6309 if (cond_inverted) {
6310 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6311 } else {
6312 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6313 }
6314 } else {
6315 DCHECK_NE(cond_reg, AT);
6316 if (cond_inverted) {
6317 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6318 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6319 } else {
6320 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6321 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6322 }
6323 __ Or(dst.AsRegister<Register>(), AT, TMP);
6324 }
6325 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006326 case DataType::Type::kInt64: {
6327 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006328 __ Mfc1(cond_reg, fcond_reg);
6329 }
6330 Register dst_lo = dst.AsRegisterPairLow<Register>();
6331 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6332 if (true_src.IsConstant()) {
6333 Register src_lo = false_src.AsRegisterPairLow<Register>();
6334 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6335 if (cond_inverted) {
6336 __ Selnez(dst_lo, src_lo, cond_reg);
6337 __ Selnez(dst_hi, src_hi, cond_reg);
6338 } else {
6339 __ Seleqz(dst_lo, src_lo, cond_reg);
6340 __ Seleqz(dst_hi, src_hi, cond_reg);
6341 }
6342 } else {
6343 DCHECK(false_src.IsConstant());
6344 Register src_lo = true_src.AsRegisterPairLow<Register>();
6345 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6346 if (cond_inverted) {
6347 __ Seleqz(dst_lo, src_lo, cond_reg);
6348 __ Seleqz(dst_hi, src_hi, cond_reg);
6349 } else {
6350 __ Selnez(dst_lo, src_lo, cond_reg);
6351 __ Selnez(dst_hi, src_hi, cond_reg);
6352 }
6353 }
6354 break;
6355 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 case DataType::Type::kFloat32: {
6357 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006358 // sel*.fmt tests bit 0 of the condition register, account for that.
6359 __ Sltu(TMP, ZERO, cond_reg);
6360 __ Mtc1(TMP, fcond_reg);
6361 }
6362 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6363 if (true_src.IsConstant()) {
6364 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6365 if (cond_inverted) {
6366 __ SelnezS(dst_reg, src_reg, fcond_reg);
6367 } else {
6368 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6369 }
6370 } else if (false_src.IsConstant()) {
6371 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6372 if (cond_inverted) {
6373 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6374 } else {
6375 __ SelnezS(dst_reg, src_reg, fcond_reg);
6376 }
6377 } else {
6378 if (cond_inverted) {
6379 __ SelS(fcond_reg,
6380 true_src.AsFpuRegister<FRegister>(),
6381 false_src.AsFpuRegister<FRegister>());
6382 } else {
6383 __ SelS(fcond_reg,
6384 false_src.AsFpuRegister<FRegister>(),
6385 true_src.AsFpuRegister<FRegister>());
6386 }
6387 __ MovS(dst_reg, fcond_reg);
6388 }
6389 break;
6390 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kFloat64: {
6392 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006393 // sel*.fmt tests bit 0 of the condition register, account for that.
6394 __ Sltu(TMP, ZERO, cond_reg);
6395 __ Mtc1(TMP, fcond_reg);
6396 }
6397 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6398 if (true_src.IsConstant()) {
6399 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6400 if (cond_inverted) {
6401 __ SelnezD(dst_reg, src_reg, fcond_reg);
6402 } else {
6403 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6404 }
6405 } else if (false_src.IsConstant()) {
6406 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6407 if (cond_inverted) {
6408 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6409 } else {
6410 __ SelnezD(dst_reg, src_reg, fcond_reg);
6411 }
6412 } else {
6413 if (cond_inverted) {
6414 __ SelD(fcond_reg,
6415 true_src.AsFpuRegister<FRegister>(),
6416 false_src.AsFpuRegister<FRegister>());
6417 } else {
6418 __ SelD(fcond_reg,
6419 false_src.AsFpuRegister<FRegister>(),
6420 true_src.AsFpuRegister<FRegister>());
6421 }
6422 __ MovD(dst_reg, fcond_reg);
6423 }
6424 break;
6425 }
6426 }
6427}
6428
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006429void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006430 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006431 LocationSummary(flag, LocationSummary::kNoCall);
6432 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006433}
6434
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006435void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6436 __ LoadFromOffset(kLoadWord,
6437 flag->GetLocations()->Out().AsRegister<Register>(),
6438 SP,
6439 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006440}
6441
David Brazdil74eb1b22015-12-14 11:44:01 +00006442void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006444 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006445}
6446
6447void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006448 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6449 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6450 if (is_r6) {
6451 GenConditionalMoveR6(select);
6452 } else {
6453 GenConditionalMoveR2(select);
6454 }
6455 } else {
6456 LocationSummary* locations = select->GetLocations();
6457 MipsLabel false_target;
6458 GenerateTestAndBranch(select,
6459 /* condition_input_index */ 2,
6460 /* true_target */ nullptr,
6461 &false_target);
6462 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6463 __ Bind(&false_target);
6464 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006465}
6466
David Srbecky0cf44932015-12-09 14:09:59 +00006467void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006468 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006469}
6470
David Srbeckyd28f4a02016-03-14 17:14:24 +00006471void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6472 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006473}
6474
6475void CodeGeneratorMIPS::GenerateNop() {
6476 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006477}
6478
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006479void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 DataType::Type field_type = field_info.GetFieldType();
6481 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006482 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006483 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006484 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006485 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006486 instruction,
6487 generate_volatile
6488 ? LocationSummary::kCallOnMainOnly
6489 : (object_field_get_with_read_barrier
6490 ? LocationSummary::kCallOnSlowPath
6491 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006492
Alexey Frunzec61c0762017-04-10 13:54:23 -07006493 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6494 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006496 locations->SetInAt(0, Location::RequiresRegister());
6497 if (generate_volatile) {
6498 InvokeRuntimeCallingConvention calling_convention;
6499 // need A0 to hold base + offset
6500 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006501 if (field_type == DataType::Type::kInt64) {
6502 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006503 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006504 // Use Location::Any() to prevent situations when running out of available fp registers.
6505 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006507 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006508 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6509 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6510 }
6511 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006512 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006513 locations->SetOut(Location::RequiresFpuRegister());
6514 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006515 // The output overlaps in the case of an object field get with
6516 // read barriers enabled: we do not want the move to overwrite the
6517 // object's location, as we need it to emit the read barrier.
6518 locations->SetOut(Location::RequiresRegister(),
6519 object_field_get_with_read_barrier
6520 ? Location::kOutputOverlap
6521 : Location::kNoOutputOverlap);
6522 }
6523 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6524 // We need a temporary register for the read barrier marking slow
6525 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006526 if (!kBakerReadBarrierThunksEnableForFields) {
6527 locations->AddTemp(Location::RequiresRegister());
6528 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006529 }
6530 }
6531}
6532
6533void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6534 const FieldInfo& field_info,
6535 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006536 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6537 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006538 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006539 Location obj_loc = locations->InAt(0);
6540 Register obj = obj_loc.AsRegister<Register>();
6541 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006542 LoadOperandType load_type = kLoadUnsignedByte;
6543 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006544 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006545 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546
6547 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006548 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006549 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006550 load_type = kLoadUnsignedByte;
6551 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006552 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006553 load_type = kLoadSignedByte;
6554 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006555 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006556 load_type = kLoadUnsignedHalfword;
6557 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006558 case DataType::Type::kInt16:
6559 load_type = kLoadSignedHalfword;
6560 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006561 case DataType::Type::kInt32:
6562 case DataType::Type::kFloat32:
6563 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006564 load_type = kLoadWord;
6565 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006566 case DataType::Type::kInt64:
6567 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006568 load_type = kLoadDoubleword;
6569 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006570 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006571 LOG(FATAL) << "Unreachable type " << type;
6572 UNREACHABLE();
6573 }
6574
6575 if (is_volatile && load_type == kLoadDoubleword) {
6576 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006577 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006578 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006579 __ LoadFromOffset(kLoadWord,
6580 ZERO,
6581 locations->GetTemp(0).AsRegister<Register>(),
6582 0,
6583 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006584 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006585 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006586 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006587 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006588 if (dst_loc.IsFpuRegister()) {
6589 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006590 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006591 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006592 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006593 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006594 __ StoreToOffset(kStoreWord,
6595 locations->GetTemp(1).AsRegister<Register>(),
6596 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006597 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006598 __ StoreToOffset(kStoreWord,
6599 locations->GetTemp(2).AsRegister<Register>(),
6600 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006601 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006602 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006603 }
6604 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006605 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006606 // /* HeapReference<Object> */ dst = *(obj + offset)
6607 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006608 Location temp_loc =
6609 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006610 // Note that a potential implicit null check is handled in this
6611 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6612 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6613 dst_loc,
6614 obj,
6615 offset,
6616 temp_loc,
6617 /* needs_null_check */ true);
6618 if (is_volatile) {
6619 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6620 }
6621 } else {
6622 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6623 if (is_volatile) {
6624 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6625 }
6626 // If read barriers are enabled, emit read barriers other than
6627 // Baker's using a slow path (and also unpoison the loaded
6628 // reference, if heap poisoning is enabled).
6629 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6630 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006632 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006633 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006634 DCHECK(dst_loc.IsRegisterPair());
6635 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006636 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006637 DCHECK(dst_loc.IsRegister());
6638 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006639 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006640 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006641 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006642 DCHECK(dst_loc.IsFpuRegister());
6643 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006644 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006645 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006646 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006647 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006648 }
6649 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006650 }
6651
Alexey Frunze15958152017-02-09 19:08:30 -08006652 // Memory barriers, in the case of references, are handled in the
6653 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006654 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006655 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6656 }
6657}
6658
6659void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006660 DataType::Type field_type = field_info.GetFieldType();
6661 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006662 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006663 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006664 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006665
6666 locations->SetInAt(0, Location::RequiresRegister());
6667 if (generate_volatile) {
6668 InvokeRuntimeCallingConvention calling_convention;
6669 // need A0 to hold base + offset
6670 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006671 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006672 locations->SetInAt(1, Location::RegisterPairLocation(
6673 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6674 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006675 // Use Location::Any() to prevent situations when running out of available fp registers.
6676 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006677 // Pass FP parameters in core registers.
6678 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6679 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6680 }
6681 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006682 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006683 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006685 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006686 }
6687 }
6688}
6689
6690void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6691 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006692 uint32_t dex_pc,
6693 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006694 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006695 LocationSummary* locations = instruction->GetLocations();
6696 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006697 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006698 StoreOperandType store_type = kStoreByte;
6699 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006700 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006701 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006702 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006703
6704 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006705 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006706 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006707 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006708 store_type = kStoreByte;
6709 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006710 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006711 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006712 store_type = kStoreHalfword;
6713 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006714 case DataType::Type::kInt32:
6715 case DataType::Type::kFloat32:
6716 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006717 store_type = kStoreWord;
6718 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006719 case DataType::Type::kInt64:
6720 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006721 store_type = kStoreDoubleword;
6722 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006723 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724 LOG(FATAL) << "Unreachable type " << type;
6725 UNREACHABLE();
6726 }
6727
6728 if (is_volatile) {
6729 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6730 }
6731
6732 if (is_volatile && store_type == kStoreDoubleword) {
6733 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006734 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006735 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006736 __ LoadFromOffset(kLoadWord,
6737 ZERO,
6738 locations->GetTemp(0).AsRegister<Register>(),
6739 0,
6740 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006741 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006742 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006743 if (value_location.IsFpuRegister()) {
6744 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6745 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006746 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006747 value_location.AsFpuRegister<FRegister>());
6748 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006749 __ LoadFromOffset(kLoadWord,
6750 locations->GetTemp(1).AsRegister<Register>(),
6751 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006752 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006753 __ LoadFromOffset(kLoadWord,
6754 locations->GetTemp(2).AsRegister<Register>(),
6755 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006756 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006757 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006758 DCHECK(value_location.IsConstant());
6759 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6760 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006761 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6762 locations->GetTemp(1).AsRegister<Register>(),
6763 value);
6764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006766 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006767 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6768 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006769 if (value_location.IsConstant()) {
6770 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6771 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006772 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006773 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006774 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006775 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006776 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006777 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006778 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006779 if (kPoisonHeapReferences && needs_write_barrier) {
6780 // Note that in the case where `value` is a null reference,
6781 // we do not enter this block, as a null reference does not
6782 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006783 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006784 __ PoisonHeapReference(TMP, src);
6785 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6786 } else {
6787 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6788 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006789 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006790 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006791 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006792 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006793 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006794 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006795 }
6796 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797 }
6798
Alexey Frunzec061de12017-02-14 13:27:23 -08006799 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006800 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006801 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006802 }
6803
6804 if (is_volatile) {
6805 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6806 }
6807}
6808
6809void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6810 HandleFieldGet(instruction, instruction->GetFieldInfo());
6811}
6812
6813void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6814 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6815}
6816
6817void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6818 HandleFieldSet(instruction, instruction->GetFieldInfo());
6819}
6820
6821void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006822 HandleFieldSet(instruction,
6823 instruction->GetFieldInfo(),
6824 instruction->GetDexPc(),
6825 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006826}
6827
Alexey Frunze15958152017-02-09 19:08:30 -08006828void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6829 HInstruction* instruction,
6830 Location out,
6831 uint32_t offset,
6832 Location maybe_temp,
6833 ReadBarrierOption read_barrier_option) {
6834 Register out_reg = out.AsRegister<Register>();
6835 if (read_barrier_option == kWithReadBarrier) {
6836 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006837 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6838 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6839 }
Alexey Frunze15958152017-02-09 19:08:30 -08006840 if (kUseBakerReadBarrier) {
6841 // Load with fast path based Baker's read barrier.
6842 // /* HeapReference<Object> */ out = *(out + offset)
6843 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6844 out,
6845 out_reg,
6846 offset,
6847 maybe_temp,
6848 /* needs_null_check */ false);
6849 } else {
6850 // Load with slow path based read barrier.
6851 // Save the value of `out` into `maybe_temp` before overwriting it
6852 // in the following move operation, as we will need it for the
6853 // read barrier below.
6854 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6855 // /* HeapReference<Object> */ out = *(out + offset)
6856 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6857 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6858 }
6859 } else {
6860 // Plain load with no read barrier.
6861 // /* HeapReference<Object> */ out = *(out + offset)
6862 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6863 __ MaybeUnpoisonHeapReference(out_reg);
6864 }
6865}
6866
6867void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6868 HInstruction* instruction,
6869 Location out,
6870 Location obj,
6871 uint32_t offset,
6872 Location maybe_temp,
6873 ReadBarrierOption read_barrier_option) {
6874 Register out_reg = out.AsRegister<Register>();
6875 Register obj_reg = obj.AsRegister<Register>();
6876 if (read_barrier_option == kWithReadBarrier) {
6877 CHECK(kEmitCompilerReadBarrier);
6878 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006879 if (!kBakerReadBarrierThunksEnableForFields) {
6880 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6881 }
Alexey Frunze15958152017-02-09 19:08:30 -08006882 // Load with fast path based Baker's read barrier.
6883 // /* HeapReference<Object> */ out = *(obj + offset)
6884 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6885 out,
6886 obj_reg,
6887 offset,
6888 maybe_temp,
6889 /* needs_null_check */ false);
6890 } else {
6891 // Load with slow path based read barrier.
6892 // /* HeapReference<Object> */ out = *(obj + offset)
6893 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6894 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6895 }
6896 } else {
6897 // Plain load with no read barrier.
6898 // /* HeapReference<Object> */ out = *(obj + offset)
6899 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6900 __ MaybeUnpoisonHeapReference(out_reg);
6901 }
6902}
6903
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904static inline int GetBakerMarkThunkNumber(Register reg) {
6905 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6906 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6907 return reg - V0;
6908 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6909 return 14 + (reg - S2);
6910 } else if (reg == FP) { // One more.
6911 return 20;
6912 }
6913 LOG(FATAL) << "Unexpected register " << reg;
6914 UNREACHABLE();
6915}
6916
6917static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6918 int num = GetBakerMarkThunkNumber(reg) +
6919 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6920 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6921}
6922
6923static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6924 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6925 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6926}
6927
Alexey Frunze15958152017-02-09 19:08:30 -08006928void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6929 Location root,
6930 Register obj,
6931 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006932 ReadBarrierOption read_barrier_option,
6933 MipsLabel* label_low) {
6934 bool reordering;
6935 if (label_low != nullptr) {
6936 DCHECK_EQ(offset, 0x5678u);
6937 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006938 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006939 if (read_barrier_option == kWithReadBarrier) {
6940 DCHECK(kEmitCompilerReadBarrier);
6941 if (kUseBakerReadBarrier) {
6942 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6943 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006944 if (kBakerReadBarrierThunksEnableForGcRoots) {
6945 // Note that we do not actually check the value of `GetIsGcMarking()`
6946 // to decide whether to mark the loaded GC root or not. Instead, we
6947 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6948 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6949 // vice versa.
6950 //
6951 // We use thunks for the slow path. That thunk checks the reference
6952 // and jumps to the entrypoint if needed.
6953 //
6954 // temp = Thread::Current()->pReadBarrierMarkReg00
6955 // // AKA &art_quick_read_barrier_mark_introspection.
6956 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6957 // if (temp != nullptr) {
6958 // temp = &gc_root_thunk<root_reg>
6959 // root = temp(root)
6960 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006961
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006962 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6963 const int32_t entry_point_offset =
6964 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6965 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6966 int16_t offset_low = Low16Bits(offset);
6967 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6968 // extension in lw.
6969 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6970 Register base = short_offset ? obj : TMP;
6971 // Loading the entrypoint does not require a load acquire since it is only changed when
6972 // threads are suspended or running a checkpoint.
6973 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6974 reordering = __ SetReorder(false);
6975 if (!short_offset) {
6976 DCHECK(!label_low);
6977 __ AddUpper(base, obj, offset_high);
6978 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006979 MipsLabel skip_call;
6980 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006981 if (label_low != nullptr) {
6982 DCHECK(short_offset);
6983 __ Bind(label_low);
6984 }
6985 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6986 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6987 // in delay slot.
6988 if (isR6) {
6989 __ Jialc(T9, thunk_disp);
6990 } else {
6991 __ Addiu(T9, T9, thunk_disp);
6992 __ Jalr(T9);
6993 __ Nop();
6994 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006995 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006996 __ SetReorder(reordering);
6997 } else {
6998 // Note that we do not actually check the value of `GetIsGcMarking()`
6999 // to decide whether to mark the loaded GC root or not. Instead, we
7000 // load into `temp` (T9) the read barrier mark entry point corresponding
7001 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7002 // is false, and vice versa.
7003 //
7004 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7005 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7006 // if (temp != null) {
7007 // root = temp(root)
7008 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007009
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007010 if (label_low != nullptr) {
7011 reordering = __ SetReorder(false);
7012 __ Bind(label_low);
7013 }
7014 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7015 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7016 if (label_low != nullptr) {
7017 __ SetReorder(reordering);
7018 }
7019 static_assert(
7020 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7021 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7022 "have different sizes.");
7023 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7024 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7025 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007026
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007027 // Slow path marking the GC root `root`.
7028 Location temp = Location::RegisterLocation(T9);
7029 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007030 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007031 instruction,
7032 root,
7033 /*entrypoint*/ temp);
7034 codegen_->AddSlowPath(slow_path);
7035
7036 const int32_t entry_point_offset =
7037 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7038 // Loading the entrypoint does not require a load acquire since it is only changed when
7039 // threads are suspended or running a checkpoint.
7040 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7041 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7042 __ Bind(slow_path->GetExitLabel());
7043 }
Alexey Frunze15958152017-02-09 19:08:30 -08007044 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007045 if (label_low != nullptr) {
7046 reordering = __ SetReorder(false);
7047 __ Bind(label_low);
7048 }
Alexey Frunze15958152017-02-09 19:08:30 -08007049 // GC root loaded through a slow path for read barriers other
7050 // than Baker's.
7051 // /* GcRoot<mirror::Object>* */ root = obj + offset
7052 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007053 if (label_low != nullptr) {
7054 __ SetReorder(reordering);
7055 }
Alexey Frunze15958152017-02-09 19:08:30 -08007056 // /* mirror::Object* */ root = root->Read()
7057 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7058 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007059 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007060 if (label_low != nullptr) {
7061 reordering = __ SetReorder(false);
7062 __ Bind(label_low);
7063 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007064 // Plain GC root load with no read barrier.
7065 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7066 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7067 // Note that GC roots are not affected by heap poisoning, thus we
7068 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007069 if (label_low != nullptr) {
7070 __ SetReorder(reordering);
7071 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007072 }
7073}
7074
Alexey Frunze15958152017-02-09 19:08:30 -08007075void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7076 Location ref,
7077 Register obj,
7078 uint32_t offset,
7079 Location temp,
7080 bool needs_null_check) {
7081 DCHECK(kEmitCompilerReadBarrier);
7082 DCHECK(kUseBakerReadBarrier);
7083
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007084 if (kBakerReadBarrierThunksEnableForFields) {
7085 // Note that we do not actually check the value of `GetIsGcMarking()`
7086 // to decide whether to mark the loaded reference or not. Instead, we
7087 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7088 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7089 // vice versa.
7090 //
7091 // We use thunks for the slow path. That thunk checks the reference
7092 // and jumps to the entrypoint if needed. If the holder is not gray,
7093 // it issues a load-load memory barrier and returns to the original
7094 // reference load.
7095 //
7096 // temp = Thread::Current()->pReadBarrierMarkReg00
7097 // // AKA &art_quick_read_barrier_mark_introspection.
7098 // if (temp != nullptr) {
7099 // temp = &field_array_thunk<holder_reg>
7100 // temp()
7101 // }
7102 // not_gray_return_address:
7103 // // If the offset is too large to fit into the lw instruction, we
7104 // // use an adjusted base register (TMP) here. This register
7105 // // receives bits 16 ... 31 of the offset before the thunk invocation
7106 // // and the thunk benefits from it.
7107 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7108 // gray_return_address:
7109
7110 DCHECK(temp.IsInvalid());
7111 bool isR6 = GetInstructionSetFeatures().IsR6();
7112 int16_t offset_low = Low16Bits(offset);
7113 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7114 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7115 bool reordering = __ SetReorder(false);
7116 const int32_t entry_point_offset =
7117 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7118 // There may have or may have not been a null check if the field offset is smaller than
7119 // the page size.
7120 // There must've been a null check in case it's actually a load from an array.
7121 // We will, however, perform an explicit null check in the thunk as it's easier to
7122 // do it than not.
7123 if (instruction->IsArrayGet()) {
7124 DCHECK(!needs_null_check);
7125 }
7126 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7127 // Loading the entrypoint does not require a load acquire since it is only changed when
7128 // threads are suspended or running a checkpoint.
7129 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7130 Register ref_reg = ref.AsRegister<Register>();
7131 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007132 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007133 if (short_offset) {
7134 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007135 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007136 __ Nop(); // In forbidden slot.
7137 __ Jialc(T9, thunk_disp);
7138 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007139 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007140 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7141 __ Jalr(T9);
7142 __ Nop(); // In delay slot.
7143 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007144 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007145 } else {
7146 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007147 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007148 __ Aui(base, obj, offset_high); // In delay slot.
7149 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007150 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007151 } else {
7152 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007153 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007154 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7155 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007156 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007157 __ Addu(base, base, obj); // In delay slot.
7158 }
7159 }
7160 // /* HeapReference<Object> */ ref = *(obj + offset)
7161 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7162 if (needs_null_check) {
7163 MaybeRecordImplicitNullCheck(instruction);
7164 }
7165 __ MaybeUnpoisonHeapReference(ref_reg);
7166 __ SetReorder(reordering);
7167 return;
7168 }
7169
Alexey Frunze15958152017-02-09 19:08:30 -08007170 // /* HeapReference<Object> */ ref = *(obj + offset)
7171 Location no_index = Location::NoLocation();
7172 ScaleFactor no_scale_factor = TIMES_1;
7173 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7174 ref,
7175 obj,
7176 offset,
7177 no_index,
7178 no_scale_factor,
7179 temp,
7180 needs_null_check);
7181}
7182
7183void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7184 Location ref,
7185 Register obj,
7186 uint32_t data_offset,
7187 Location index,
7188 Location temp,
7189 bool needs_null_check) {
7190 DCHECK(kEmitCompilerReadBarrier);
7191 DCHECK(kUseBakerReadBarrier);
7192
7193 static_assert(
7194 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7195 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007196 ScaleFactor scale_factor = TIMES_4;
7197
7198 if (kBakerReadBarrierThunksEnableForArrays) {
7199 // Note that we do not actually check the value of `GetIsGcMarking()`
7200 // to decide whether to mark the loaded reference or not. Instead, we
7201 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7202 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7203 // vice versa.
7204 //
7205 // We use thunks for the slow path. That thunk checks the reference
7206 // and jumps to the entrypoint if needed. If the holder is not gray,
7207 // it issues a load-load memory barrier and returns to the original
7208 // reference load.
7209 //
7210 // temp = Thread::Current()->pReadBarrierMarkReg00
7211 // // AKA &art_quick_read_barrier_mark_introspection.
7212 // if (temp != nullptr) {
7213 // temp = &field_array_thunk<holder_reg>
7214 // temp()
7215 // }
7216 // not_gray_return_address:
7217 // // The element address is pre-calculated in the TMP register before the
7218 // // thunk invocation and the thunk benefits from it.
7219 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7220 // gray_return_address:
7221
7222 DCHECK(temp.IsInvalid());
7223 DCHECK(index.IsValid());
7224 bool reordering = __ SetReorder(false);
7225 const int32_t entry_point_offset =
7226 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7227 // We will not do the explicit null check in the thunk as some form of a null check
7228 // must've been done earlier.
7229 DCHECK(!needs_null_check);
7230 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7231 // Loading the entrypoint does not require a load acquire since it is only changed when
7232 // threads are suspended or running a checkpoint.
7233 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7234 Register ref_reg = ref.AsRegister<Register>();
7235 Register index_reg = index.IsRegisterPair()
7236 ? index.AsRegisterPairLow<Register>()
7237 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007238 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007239 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007240 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007241 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7242 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007243 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007244 } else {
7245 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007246 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007247 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7248 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007249 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007250 __ Addu(TMP, TMP, obj); // In delay slot.
7251 }
7252 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7253 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7254 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7255 __ MaybeUnpoisonHeapReference(ref_reg);
7256 __ SetReorder(reordering);
7257 return;
7258 }
7259
Alexey Frunze15958152017-02-09 19:08:30 -08007260 // /* HeapReference<Object> */ ref =
7261 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007262 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7263 ref,
7264 obj,
7265 data_offset,
7266 index,
7267 scale_factor,
7268 temp,
7269 needs_null_check);
7270}
7271
7272void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7273 Location ref,
7274 Register obj,
7275 uint32_t offset,
7276 Location index,
7277 ScaleFactor scale_factor,
7278 Location temp,
7279 bool needs_null_check,
7280 bool always_update_field) {
7281 DCHECK(kEmitCompilerReadBarrier);
7282 DCHECK(kUseBakerReadBarrier);
7283
7284 // In slow path based read barriers, the read barrier call is
7285 // inserted after the original load. However, in fast path based
7286 // Baker's read barriers, we need to perform the load of
7287 // mirror::Object::monitor_ *before* the original reference load.
7288 // This load-load ordering is required by the read barrier.
7289 // The fast path/slow path (for Baker's algorithm) should look like:
7290 //
7291 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7292 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7293 // HeapReference<Object> ref = *src; // Original reference load.
7294 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7295 // if (is_gray) {
7296 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7297 // }
7298 //
7299 // Note: the original implementation in ReadBarrier::Barrier is
7300 // slightly more complex as it performs additional checks that we do
7301 // not do here for performance reasons.
7302
7303 Register ref_reg = ref.AsRegister<Register>();
7304 Register temp_reg = temp.AsRegister<Register>();
7305 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7306
7307 // /* int32_t */ monitor = obj->monitor_
7308 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7309 if (needs_null_check) {
7310 MaybeRecordImplicitNullCheck(instruction);
7311 }
7312 // /* LockWord */ lock_word = LockWord(monitor)
7313 static_assert(sizeof(LockWord) == sizeof(int32_t),
7314 "art::LockWord and int32_t have different sizes.");
7315
7316 __ Sync(0); // Barrier to prevent load-load reordering.
7317
7318 // The actual reference load.
7319 if (index.IsValid()) {
7320 // Load types involving an "index": ArrayGet,
7321 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7322 // intrinsics.
7323 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7324 if (index.IsConstant()) {
7325 size_t computed_offset =
7326 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7327 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7328 } else {
7329 // Handle the special case of the
7330 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7331 // intrinsics, which use a register pair as index ("long
7332 // offset"), of which only the low part contains data.
7333 Register index_reg = index.IsRegisterPair()
7334 ? index.AsRegisterPairLow<Register>()
7335 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007336 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007337 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7338 }
7339 } else {
7340 // /* HeapReference<Object> */ ref = *(obj + offset)
7341 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7342 }
7343
7344 // Object* ref = ref_addr->AsMirrorPtr()
7345 __ MaybeUnpoisonHeapReference(ref_reg);
7346
7347 // Slow path marking the object `ref` when it is gray.
7348 SlowPathCodeMIPS* slow_path;
7349 if (always_update_field) {
7350 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7351 // of the form `obj + field_offset`, where `obj` is a register and
7352 // `field_offset` is a register pair (of which only the lower half
7353 // is used). Thus `offset` and `scale_factor` above are expected
7354 // to be null in this code path.
7355 DCHECK_EQ(offset, 0u);
7356 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007357 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007358 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7359 ref,
7360 obj,
7361 /* field_offset */ index,
7362 temp_reg);
7363 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007364 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007365 }
7366 AddSlowPath(slow_path);
7367
7368 // if (rb_state == ReadBarrier::GrayState())
7369 // ref = ReadBarrier::Mark(ref);
7370 // Given the numeric representation, it's enough to check the low bit of the
7371 // rb_state. We do that by shifting the bit into the sign bit (31) and
7372 // performing a branch on less than zero.
7373 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7374 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7375 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7376 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7377 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7378 __ Bind(slow_path->GetExitLabel());
7379}
7380
7381void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7382 Location out,
7383 Location ref,
7384 Location obj,
7385 uint32_t offset,
7386 Location index) {
7387 DCHECK(kEmitCompilerReadBarrier);
7388
7389 // Insert a slow path based read barrier *after* the reference load.
7390 //
7391 // If heap poisoning is enabled, the unpoisoning of the loaded
7392 // reference will be carried out by the runtime within the slow
7393 // path.
7394 //
7395 // Note that `ref` currently does not get unpoisoned (when heap
7396 // poisoning is enabled), which is alright as the `ref` argument is
7397 // not used by the artReadBarrierSlow entry point.
7398 //
7399 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007400 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007401 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7402 AddSlowPath(slow_path);
7403
7404 __ B(slow_path->GetEntryLabel());
7405 __ Bind(slow_path->GetExitLabel());
7406}
7407
7408void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7409 Location out,
7410 Location ref,
7411 Location obj,
7412 uint32_t offset,
7413 Location index) {
7414 if (kEmitCompilerReadBarrier) {
7415 // Baker's read barriers shall be handled by the fast path
7416 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7417 DCHECK(!kUseBakerReadBarrier);
7418 // If heap poisoning is enabled, unpoisoning will be taken care of
7419 // by the runtime within the slow path.
7420 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7421 } else if (kPoisonHeapReferences) {
7422 __ UnpoisonHeapReference(out.AsRegister<Register>());
7423 }
7424}
7425
7426void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7427 Location out,
7428 Location root) {
7429 DCHECK(kEmitCompilerReadBarrier);
7430
7431 // Insert a slow path based read barrier *after* the GC root load.
7432 //
7433 // Note that GC roots are not affected by heap poisoning, so we do
7434 // not need to do anything special for this here.
7435 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007436 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007437 AddSlowPath(slow_path);
7438
7439 __ B(slow_path->GetEntryLabel());
7440 __ Bind(slow_path->GetExitLabel());
7441}
7442
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007443void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007444 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7445 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007446 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007447 switch (type_check_kind) {
7448 case TypeCheckKind::kExactCheck:
7449 case TypeCheckKind::kAbstractClassCheck:
7450 case TypeCheckKind::kClassHierarchyCheck:
7451 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007452 call_kind =
7453 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007454 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007455 break;
7456 case TypeCheckKind::kArrayCheck:
7457 case TypeCheckKind::kUnresolvedCheck:
7458 case TypeCheckKind::kInterfaceCheck:
7459 call_kind = LocationSummary::kCallOnSlowPath;
7460 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007461 case TypeCheckKind::kBitstringCheck:
7462 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007463 }
7464
Vladimir Markoca6fff82017-10-03 14:49:14 +01007465 LocationSummary* locations =
7466 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007467 if (baker_read_barrier_slow_path) {
7468 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7469 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007470 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007471 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7472 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7473 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7474 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7475 } else {
7476 locations->SetInAt(1, Location::RequiresRegister());
7477 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007478 // The output does overlap inputs.
7479 // Note that TypeCheckSlowPathMIPS uses this register too.
7480 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007481 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007482}
7483
7484void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007485 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007486 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007487 Location obj_loc = locations->InAt(0);
7488 Register obj = obj_loc.AsRegister<Register>();
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007489 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007490 Location out_loc = locations->Out();
7491 Register out = out_loc.AsRegister<Register>();
7492 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7493 DCHECK_LE(num_temps, 1u);
7494 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007495 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7496 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7497 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7498 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007499 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007500 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007501
7502 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007503 // Avoid this check if we know `obj` is not null.
7504 if (instruction->MustDoNullCheck()) {
7505 __ Move(out, ZERO);
7506 __ Beqz(obj, &done);
7507 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007508
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007509 switch (type_check_kind) {
7510 case TypeCheckKind::kExactCheck: {
7511 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007512 GenerateReferenceLoadTwoRegisters(instruction,
7513 out_loc,
7514 obj_loc,
7515 class_offset,
7516 maybe_temp_loc,
7517 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007518 // Classes must be equal for the instanceof to succeed.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007519 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007520 __ Sltiu(out, out, 1);
7521 break;
7522 }
7523
7524 case TypeCheckKind::kAbstractClassCheck: {
7525 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007526 GenerateReferenceLoadTwoRegisters(instruction,
7527 out_loc,
7528 obj_loc,
7529 class_offset,
7530 maybe_temp_loc,
7531 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007532 // If the class is abstract, we eagerly fetch the super class of the
7533 // object to avoid doing a comparison we know will fail.
7534 MipsLabel loop;
7535 __ Bind(&loop);
7536 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007537 GenerateReferenceLoadOneRegister(instruction,
7538 out_loc,
7539 super_offset,
7540 maybe_temp_loc,
7541 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007542 // If `out` is null, we use it for the result, and jump to `done`.
7543 __ Beqz(out, &done);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007544 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007545 __ LoadConst32(out, 1);
7546 break;
7547 }
7548
7549 case TypeCheckKind::kClassHierarchyCheck: {
7550 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007551 GenerateReferenceLoadTwoRegisters(instruction,
7552 out_loc,
7553 obj_loc,
7554 class_offset,
7555 maybe_temp_loc,
7556 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007557 // Walk over the class hierarchy to find a match.
7558 MipsLabel loop, success;
7559 __ Bind(&loop);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007560 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007561 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007562 GenerateReferenceLoadOneRegister(instruction,
7563 out_loc,
7564 super_offset,
7565 maybe_temp_loc,
7566 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007567 __ Bnez(out, &loop);
7568 // If `out` is null, we use it for the result, and jump to `done`.
7569 __ B(&done);
7570 __ Bind(&success);
7571 __ LoadConst32(out, 1);
7572 break;
7573 }
7574
7575 case TypeCheckKind::kArrayObjectCheck: {
7576 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007577 GenerateReferenceLoadTwoRegisters(instruction,
7578 out_loc,
7579 obj_loc,
7580 class_offset,
7581 maybe_temp_loc,
7582 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007583 // Do an exact check.
7584 MipsLabel success;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007585 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007586 // Otherwise, we need to check that the object's class is a non-primitive array.
7587 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007588 GenerateReferenceLoadOneRegister(instruction,
7589 out_loc,
7590 component_offset,
7591 maybe_temp_loc,
7592 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007593 // If `out` is null, we use it for the result, and jump to `done`.
7594 __ Beqz(out, &done);
7595 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7596 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7597 __ Sltiu(out, out, 1);
7598 __ B(&done);
7599 __ Bind(&success);
7600 __ LoadConst32(out, 1);
7601 break;
7602 }
7603
7604 case TypeCheckKind::kArrayCheck: {
7605 // No read barrier since the slow path will retry upon failure.
7606 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007607 GenerateReferenceLoadTwoRegisters(instruction,
7608 out_loc,
7609 obj_loc,
7610 class_offset,
7611 maybe_temp_loc,
7612 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007613 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007614 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7615 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007616 codegen_->AddSlowPath(slow_path);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007617 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007618 __ LoadConst32(out, 1);
7619 break;
7620 }
7621
7622 case TypeCheckKind::kUnresolvedCheck:
7623 case TypeCheckKind::kInterfaceCheck: {
7624 // Note that we indeed only call on slow path, but we always go
7625 // into the slow path for the unresolved and interface check
7626 // cases.
7627 //
7628 // We cannot directly call the InstanceofNonTrivial runtime
7629 // entry point without resorting to a type checking slow path
7630 // here (i.e. by calling InvokeRuntime directly), as it would
7631 // require to assign fixed registers for the inputs of this
7632 // HInstanceOf instruction (following the runtime calling
7633 // convention), which might be cluttered by the potential first
7634 // read barrier emission at the beginning of this method.
7635 //
7636 // TODO: Introduce a new runtime entry point taking the object
7637 // to test (instead of its class) as argument, and let it deal
7638 // with the read barrier issues. This will let us refactor this
7639 // case of the `switch` code as it was previously (with a direct
7640 // call to the runtime not using a type checking slow path).
7641 // This should also be beneficial for the other cases above.
7642 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007643 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7644 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007645 codegen_->AddSlowPath(slow_path);
7646 __ B(slow_path->GetEntryLabel());
7647 break;
7648 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007649
7650 case TypeCheckKind::kBitstringCheck: {
7651 // /* HeapReference<Class> */ temp = obj->klass_
7652 GenerateReferenceLoadTwoRegisters(instruction,
7653 out_loc,
7654 obj_loc,
7655 class_offset,
7656 maybe_temp_loc,
7657 kWithoutReadBarrier);
7658
7659 GenerateBitstringTypeCheckCompare(instruction, out);
7660 __ Sltiu(out, out, 1);
7661 break;
7662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007663 }
7664
7665 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007666
7667 if (slow_path != nullptr) {
7668 __ Bind(slow_path->GetExitLabel());
7669 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007670}
7671
7672void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007673 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007674 locations->SetOut(Location::ConstantLocation(constant));
7675}
7676
7677void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7678 // Will be generated at use site.
7679}
7680
7681void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007682 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007683 locations->SetOut(Location::ConstantLocation(constant));
7684}
7685
7686void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7687 // Will be generated at use site.
7688}
7689
7690void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7691 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7692 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7693}
7694
7695void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7696 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007697 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007698 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007699 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007700}
7701
7702void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7703 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7704 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007705 Location receiver = invoke->GetLocations()->InAt(0);
7706 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007707 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007708
7709 // Set the hidden argument.
7710 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7711 invoke->GetDexMethodIndex());
7712
7713 // temp = object->GetClass();
7714 if (receiver.IsStackSlot()) {
7715 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7716 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7717 } else {
7718 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7719 }
7720 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007721 // Instead of simply (possibly) unpoisoning `temp` here, we should
7722 // emit a read barrier for the previous class reference load.
7723 // However this is not required in practice, as this is an
7724 // intermediate/temporary reference and because the current
7725 // concurrent copying collector keeps the from-space memory
7726 // intact/accessible until the end of the marking phase (the
7727 // concurrent copying collector may not in the future).
7728 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007729 __ LoadFromOffset(kLoadWord, temp, temp,
7730 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7731 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007732 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007733 // temp = temp->GetImtEntryAt(method_offset);
7734 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7735 // T9 = temp->GetEntryPoint();
7736 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7737 // T9();
7738 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007739 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007740 DCHECK(!codegen_->IsLeafMethod());
7741 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7742}
7743
7744void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007745 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7746 if (intrinsic.TryDispatch(invoke)) {
7747 return;
7748 }
7749
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007750 HandleInvoke(invoke);
7751}
7752
7753void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007754 // Explicit clinit checks triggered by static invokes must have been pruned by
7755 // art::PrepareForRegisterAllocation.
7756 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007757
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007758 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007759 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7760 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007761
Chris Larsen701566a2015-10-27 15:29:13 -07007762 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7763 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007764 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7765 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7766 }
Chris Larsen701566a2015-10-27 15:29:13 -07007767 return;
7768 }
7769
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007770 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007771
7772 // Add the extra input register if either the dex cache array base register
7773 // or the PC-relative base register for accessing literals is needed.
7774 if (has_extra_input) {
7775 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7776 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007777}
7778
Orion Hodsonac141392017-01-13 11:53:47 +00007779void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7780 HandleInvoke(invoke);
7781}
7782
7783void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7784 codegen_->GenerateInvokePolymorphicCall(invoke);
7785}
7786
Chris Larsen701566a2015-10-27 15:29:13 -07007787static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007788 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007789 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7790 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007791 return true;
7792 }
7793 return false;
7794}
7795
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007796HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007797 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007798 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007799 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007800 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007801 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007802 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007803 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007804 case HLoadString::LoadKind::kJitTableAddress:
7805 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007806 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007807 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007808 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007809 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007810 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007811 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007812}
7813
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007814HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7815 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007816 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007817 case HLoadClass::LoadKind::kInvalid:
7818 LOG(FATAL) << "UNREACHABLE";
7819 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007820 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007821 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007822 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007823 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007824 case HLoadClass::LoadKind::kBssEntry:
7825 DCHECK(!Runtime::Current()->UseJitCompilation());
7826 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007827 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007828 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007829 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007830 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007831 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007832 break;
7833 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007834 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007835}
7836
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007837Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7838 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007839 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007840 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007841 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7842 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7843 if (!invoke->GetLocations()->Intrinsified()) {
7844 return location.AsRegister<Register>();
7845 }
7846 // For intrinsics we allow any location, so it may be on the stack.
7847 if (!location.IsRegister()) {
7848 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7849 return temp;
7850 }
7851 // For register locations, check if the register was saved. If so, get it from the stack.
7852 // Note: There is a chance that the register was saved but not overwritten, so we could
7853 // save one load. However, since this is just an intrinsic slow path we prefer this
7854 // simple and more robust approach rather that trying to determine if that's the case.
7855 SlowPathCode* slow_path = GetCurrentSlowPath();
7856 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7857 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7858 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7859 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7860 return temp;
7861 }
7862 return location.AsRegister<Register>();
7863}
7864
Vladimir Markodc151b22015-10-15 18:02:30 +01007865HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7866 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007867 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007868 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007869}
7870
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007871void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7872 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007873 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007874 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007875 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7876 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007877 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007878 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7879 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007880 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7881 : ZERO;
7882
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007883 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007884 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007885 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007886 uint32_t offset =
7887 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007888 __ LoadFromOffset(kLoadWord,
7889 temp.AsRegister<Register>(),
7890 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007891 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007892 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007893 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007894 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007895 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007896 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007897 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7898 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007899 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7900 PcRelativePatchInfo* info_low =
7901 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007902 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007903 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7904 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007905 break;
7906 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007907 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7908 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7909 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007910 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007911 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007912 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007913 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7914 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007915 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007916 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7917 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007918 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007919 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007920 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7921 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7922 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007923 }
7924 }
7925
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007926 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007927 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007928 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007929 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007930 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7931 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007932 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007933 T9,
7934 callee_method.AsRegister<Register>(),
7935 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007936 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007937 // T9()
7938 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007939 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007940 break;
7941 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007942 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7943
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007944 DCHECK(!IsLeafMethod());
7945}
7946
7947void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007948 // Explicit clinit checks triggered by static invokes must have been pruned by
7949 // art::PrepareForRegisterAllocation.
7950 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007951
7952 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7953 return;
7954 }
7955
7956 LocationSummary* locations = invoke->GetLocations();
7957 codegen_->GenerateStaticOrDirectCall(invoke,
7958 locations->HasTemps()
7959 ? locations->GetTemp(0)
7960 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007961}
7962
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007963void CodeGeneratorMIPS::GenerateVirtualCall(
7964 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007965 // Use the calling convention instead of the location of the receiver, as
7966 // intrinsics may have put the receiver in a different register. In the intrinsics
7967 // slow path, the arguments have been moved to the right place, so here we are
7968 // guaranteed that the receiver is the first register of the calling convention.
7969 InvokeDexCallingConvention calling_convention;
7970 Register receiver = calling_convention.GetRegisterAt(0);
7971
Chris Larsen3acee732015-11-18 13:31:08 -08007972 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007973 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7974 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7975 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007976 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007977
7978 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007979 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007980 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007981 // Instead of simply (possibly) unpoisoning `temp` here, we should
7982 // emit a read barrier for the previous class reference load.
7983 // However this is not required in practice, as this is an
7984 // intermediate/temporary reference and because the current
7985 // concurrent copying collector keeps the from-space memory
7986 // intact/accessible until the end of the marking phase (the
7987 // concurrent copying collector may not in the future).
7988 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007989 // temp = temp->GetMethodAt(method_offset);
7990 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7991 // T9 = temp->GetEntryPoint();
7992 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7993 // T9();
7994 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007995 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007996 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007997}
7998
7999void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8000 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8001 return;
8002 }
8003
8004 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008005 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008006}
8007
8008void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008009 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008010 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008011 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008012 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8013 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008014 return;
8015 }
Vladimir Marko41559982017-01-06 14:04:23 +00008016 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008017 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008018 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008019 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8020 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008021 ? LocationSummary::kCallOnSlowPath
8022 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008023 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008024 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8025 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8026 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008027 switch (load_kind) {
8028 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008029 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008030 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008031 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008032 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008033 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008034 break;
8035 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008036 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008037 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
8038 codegen_->ClobberRA();
8039 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008040 break;
8041 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008042 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008043 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008044 locations->SetInAt(0, Location::RequiresRegister());
8045 break;
8046 default:
8047 break;
8048 }
8049 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008050 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8051 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8052 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008053 RegisterSet caller_saves = RegisterSet::Empty();
8054 InvokeRuntimeCallingConvention calling_convention;
8055 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8056 locations->SetCustomSlowPathCallerSaves(caller_saves);
8057 } else {
8058 // For non-Baker read barriers we have a temp-clobbering call.
8059 }
8060 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008061}
8062
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008063// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8064// move.
8065void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008066 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008067 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008068 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008069 return;
8070 }
Vladimir Marko41559982017-01-06 14:04:23 +00008071 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008072
Vladimir Marko41559982017-01-06 14:04:23 +00008073 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008074 Location out_loc = locations->Out();
8075 Register out = out_loc.AsRegister<Register>();
8076 Register base_or_current_method_reg;
8077 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008078 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008079 switch (load_kind) {
8080 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008081 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008082 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008083 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008084 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008085 base_or_current_method_reg =
8086 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008087 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008088 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008089 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008090 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8091 break;
8092 default:
8093 base_or_current_method_reg = ZERO;
8094 break;
8095 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008096
Alexey Frunze15958152017-02-09 19:08:30 -08008097 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8098 ? kWithoutReadBarrier
8099 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008100 bool generate_null_check = false;
8101 switch (load_kind) {
8102 case HLoadClass::LoadKind::kReferrersClass: {
8103 DCHECK(!cls->CanCallRuntime());
8104 DCHECK(!cls->MustGenerateClinitCheck());
8105 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8106 GenerateGcRootFieldLoad(cls,
8107 out_loc,
8108 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008109 ArtMethod::DeclaringClassOffset().Int32Value(),
8110 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008111 break;
8112 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008113 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008114 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008115 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008116 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07008117 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008118 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8119 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008120 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8121 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008122 base_or_current_method_reg);
8123 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008124 break;
8125 }
8126 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008127 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008128 uint32_t address = dchecked_integral_cast<uint32_t>(
8129 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8130 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008131 if (isR6 || !has_irreducible_loops) {
8132 __ LoadLiteral(out,
8133 base_or_current_method_reg,
8134 codegen_->DeduplicateBootImageAddressLiteral(address));
8135 } else {
8136 __ LoadConst32(out, address);
8137 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008138 break;
8139 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008140 case HLoadClass::LoadKind::kBootImageClassTable: {
8141 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8142 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8143 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
8144 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8145 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
8146 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8147 out,
8148 base_or_current_method_reg);
8149 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8150 // Extract the reference from the slot data, i.e. clear the hash bits.
8151 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
8152 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
8153 if (masked_hash != 0) {
8154 __ Addiu(out, out, -masked_hash);
8155 }
8156 break;
8157 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008158 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008159 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8160 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008161 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8162 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008163 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008164 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008165 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008166 GenerateGcRootFieldLoad(cls,
8167 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008168 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008169 /* placeholder */ 0x5678,
8170 read_barrier_option,
8171 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008172 generate_null_check = true;
8173 break;
8174 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008175 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008176 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8177 cls->GetTypeIndex(),
8178 cls->GetClass());
8179 bool reordering = __ SetReorder(false);
8180 __ Bind(&info->high_label);
8181 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008182 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008183 GenerateGcRootFieldLoad(cls,
8184 out_loc,
8185 out,
8186 /* placeholder */ 0x5678,
8187 read_barrier_option,
8188 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008189 break;
8190 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008191 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008192 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008193 LOG(FATAL) << "UNREACHABLE";
8194 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008195 }
8196
8197 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8198 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008199 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008200 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008201 codegen_->AddSlowPath(slow_path);
8202 if (generate_null_check) {
8203 __ Beqz(out, slow_path->GetEntryLabel());
8204 }
8205 if (cls->MustGenerateClinitCheck()) {
8206 GenerateClassInitializationCheck(slow_path, out);
8207 } else {
8208 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008209 }
8210 }
8211}
8212
8213static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008214 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008215}
8216
8217void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8218 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008219 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008220 locations->SetOut(Location::RequiresRegister());
8221}
8222
8223void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8224 Register out = load->GetLocations()->Out().AsRegister<Register>();
8225 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8226}
8227
8228void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008229 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008230}
8231
8232void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8233 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8234}
8235
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008236void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008237 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008238 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008239 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008240 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008241 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008242 switch (load_kind) {
8243 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008244 case HLoadString::LoadKind::kBootImageAddress:
8245 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008246 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008247 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008248 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008249 break;
8250 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008251 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008252 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8253 codegen_->ClobberRA();
8254 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008255 break;
8256 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008257 FALLTHROUGH_INTENDED;
8258 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008259 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008260 locations->SetInAt(0, Location::RequiresRegister());
8261 break;
8262 default:
8263 break;
8264 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008265 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008266 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008267 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008268 } else {
8269 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008270 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8271 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8272 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008273 RegisterSet caller_saves = RegisterSet::Empty();
8274 InvokeRuntimeCallingConvention calling_convention;
8275 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8276 locations->SetCustomSlowPathCallerSaves(caller_saves);
8277 } else {
8278 // For non-Baker read barriers we have a temp-clobbering call.
8279 }
8280 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008282}
8283
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008284// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8285// move.
8286void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008287 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008288 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008289 Location out_loc = locations->Out();
8290 Register out = out_loc.AsRegister<Register>();
8291 Register base_or_current_method_reg;
8292 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008293 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008294 switch (load_kind) {
8295 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008296 case HLoadString::LoadKind::kBootImageAddress:
8297 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008298 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008299 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008300 base_or_current_method_reg =
8301 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008302 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008303 default:
8304 base_or_current_method_reg = ZERO;
8305 break;
8306 }
8307
8308 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008309 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008310 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008311 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008312 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008313 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8314 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008315 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8316 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008317 base_or_current_method_reg);
8318 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008319 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008320 }
8321 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008322 uint32_t address = dchecked_integral_cast<uint32_t>(
8323 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8324 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008325 if (isR6 || !has_irreducible_loops) {
8326 __ LoadLiteral(out,
8327 base_or_current_method_reg,
8328 codegen_->DeduplicateBootImageAddressLiteral(address));
8329 } else {
8330 __ LoadConst32(out, address);
8331 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008332 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008333 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008334 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008335 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008336 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008337 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008338 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8339 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008340 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8341 out,
8342 base_or_current_method_reg);
8343 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8344 return;
8345 }
8346 case HLoadString::LoadKind::kBssEntry: {
8347 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8348 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8349 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8350 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8351 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008352 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008353 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008354 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008355 GenerateGcRootFieldLoad(load,
8356 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008357 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008358 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008359 kCompilerReadBarrierOption,
8360 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008361 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008362 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008363 codegen_->AddSlowPath(slow_path);
8364 __ Beqz(out, slow_path->GetEntryLabel());
8365 __ Bind(slow_path->GetExitLabel());
8366 return;
8367 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008368 case HLoadString::LoadKind::kJitTableAddress: {
8369 CodeGeneratorMIPS::JitPatchInfo* info =
8370 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8371 load->GetStringIndex(),
8372 load->GetString());
8373 bool reordering = __ SetReorder(false);
8374 __ Bind(&info->high_label);
8375 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008376 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008377 GenerateGcRootFieldLoad(load,
8378 out_loc,
8379 out,
8380 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008381 kCompilerReadBarrierOption,
8382 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008383 return;
8384 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008385 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008386 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008387 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008388
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008389 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008390 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008391 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008392 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008393 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008394 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8395 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008396}
8397
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008398void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008399 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008400 locations->SetOut(Location::ConstantLocation(constant));
8401}
8402
8403void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8404 // Will be generated at use site.
8405}
8406
8407void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008408 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8409 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008410 InvokeRuntimeCallingConvention calling_convention;
8411 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8412}
8413
8414void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8415 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008416 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008417 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8418 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008419 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008420 }
8421 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8422}
8423
8424void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8425 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008426 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008427 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008428 case DataType::Type::kInt32:
8429 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008430 locations->SetInAt(0, Location::RequiresRegister());
8431 locations->SetInAt(1, Location::RequiresRegister());
8432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8433 break;
8434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008435 case DataType::Type::kFloat32:
8436 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008437 locations->SetInAt(0, Location::RequiresFpuRegister());
8438 locations->SetInAt(1, Location::RequiresFpuRegister());
8439 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8440 break;
8441
8442 default:
8443 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8444 }
8445}
8446
8447void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008448 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008449 LocationSummary* locations = instruction->GetLocations();
8450 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8451
8452 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008453 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008454 Register dst = locations->Out().AsRegister<Register>();
8455 Register lhs = locations->InAt(0).AsRegister<Register>();
8456 Register rhs = locations->InAt(1).AsRegister<Register>();
8457
8458 if (isR6) {
8459 __ MulR6(dst, lhs, rhs);
8460 } else {
8461 __ MulR2(dst, lhs, rhs);
8462 }
8463 break;
8464 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008465 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008466 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8467 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8468 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8469 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8470 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8471 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8472
8473 // Extra checks to protect caused by the existance of A1_A2.
8474 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8475 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8476 DCHECK_NE(dst_high, lhs_low);
8477 DCHECK_NE(dst_high, rhs_low);
8478
8479 // A_B * C_D
8480 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8481 // dst_lo: [ low(B*D) ]
8482 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8483
8484 if (isR6) {
8485 __ MulR6(TMP, lhs_high, rhs_low);
8486 __ MulR6(dst_high, lhs_low, rhs_high);
8487 __ Addu(dst_high, dst_high, TMP);
8488 __ MuhuR6(TMP, lhs_low, rhs_low);
8489 __ Addu(dst_high, dst_high, TMP);
8490 __ MulR6(dst_low, lhs_low, rhs_low);
8491 } else {
8492 __ MulR2(TMP, lhs_high, rhs_low);
8493 __ MulR2(dst_high, lhs_low, rhs_high);
8494 __ Addu(dst_high, dst_high, TMP);
8495 __ MultuR2(lhs_low, rhs_low);
8496 __ Mfhi(TMP);
8497 __ Addu(dst_high, dst_high, TMP);
8498 __ Mflo(dst_low);
8499 }
8500 break;
8501 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008502 case DataType::Type::kFloat32:
8503 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008504 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8505 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8506 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008507 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508 __ MulS(dst, lhs, rhs);
8509 } else {
8510 __ MulD(dst, lhs, rhs);
8511 }
8512 break;
8513 }
8514 default:
8515 LOG(FATAL) << "Unexpected mul type " << type;
8516 }
8517}
8518
8519void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8520 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008521 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008522 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008523 case DataType::Type::kInt32:
8524 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525 locations->SetInAt(0, Location::RequiresRegister());
8526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8527 break;
8528
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008529 case DataType::Type::kFloat32:
8530 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008531 locations->SetInAt(0, Location::RequiresFpuRegister());
8532 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8533 break;
8534
8535 default:
8536 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8537 }
8538}
8539
8540void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008541 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542 LocationSummary* locations = instruction->GetLocations();
8543
8544 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008545 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008546 Register dst = locations->Out().AsRegister<Register>();
8547 Register src = locations->InAt(0).AsRegister<Register>();
8548 __ Subu(dst, ZERO, src);
8549 break;
8550 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008551 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008552 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8553 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8554 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8555 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8556 __ Subu(dst_low, ZERO, src_low);
8557 __ Sltu(TMP, ZERO, dst_low);
8558 __ Subu(dst_high, ZERO, src_high);
8559 __ Subu(dst_high, dst_high, TMP);
8560 break;
8561 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008562 case DataType::Type::kFloat32:
8563 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008564 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8565 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008566 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008567 __ NegS(dst, src);
8568 } else {
8569 __ NegD(dst, src);
8570 }
8571 break;
8572 }
8573 default:
8574 LOG(FATAL) << "Unexpected neg type " << type;
8575 }
8576}
8577
8578void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008579 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8580 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008581 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008582 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008583 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8584 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008585}
8586
8587void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008588 // Note: if heap poisoning is enabled, the entry point takes care
8589 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008590 QuickEntrypointEnum entrypoint =
8591 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8592 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008593 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008594 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008595}
8596
8597void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008598 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8599 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008600 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008601 if (instruction->IsStringAlloc()) {
8602 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8603 } else {
8604 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008605 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008606 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008607}
8608
8609void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008610 // Note: if heap poisoning is enabled, the entry point takes care
8611 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008612 if (instruction->IsStringAlloc()) {
8613 // String is allocated through StringFactory. Call NewEmptyString entry point.
8614 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008615 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008616 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8617 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8618 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008619 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008620 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8621 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008622 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008623 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008624 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008625}
8626
8627void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008628 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629 locations->SetInAt(0, Location::RequiresRegister());
8630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8631}
8632
8633void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008634 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008635 LocationSummary* locations = instruction->GetLocations();
8636
8637 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008638 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008639 Register dst = locations->Out().AsRegister<Register>();
8640 Register src = locations->InAt(0).AsRegister<Register>();
8641 __ Nor(dst, src, ZERO);
8642 break;
8643 }
8644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008645 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008646 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8647 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8648 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8649 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8650 __ Nor(dst_high, src_high, ZERO);
8651 __ Nor(dst_low, src_low, ZERO);
8652 break;
8653 }
8654
8655 default:
8656 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8657 }
8658}
8659
8660void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008661 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008662 locations->SetInAt(0, Location::RequiresRegister());
8663 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8664}
8665
8666void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8667 LocationSummary* locations = instruction->GetLocations();
8668 __ Xori(locations->Out().AsRegister<Register>(),
8669 locations->InAt(0).AsRegister<Register>(),
8670 1);
8671}
8672
8673void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008674 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8675 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008676}
8677
Calin Juravle2ae48182016-03-16 14:05:09 +00008678void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8679 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008680 return;
8681 }
8682 Location obj = instruction->GetLocations()->InAt(0);
8683
8684 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008685 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008686}
8687
Calin Juravle2ae48182016-03-16 14:05:09 +00008688void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008689 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008690 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008691
8692 Location obj = instruction->GetLocations()->InAt(0);
8693
8694 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8695}
8696
8697void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008698 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008699}
8700
8701void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8702 HandleBinaryOp(instruction);
8703}
8704
8705void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8706 HandleBinaryOp(instruction);
8707}
8708
8709void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8710 LOG(FATAL) << "Unreachable";
8711}
8712
8713void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008714 if (instruction->GetNext()->IsSuspendCheck() &&
8715 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8716 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8717 // The back edge will generate the suspend check.
8718 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8719 }
8720
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008721 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8722}
8723
8724void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008725 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8727 if (location.IsStackSlot()) {
8728 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8729 } else if (location.IsDoubleStackSlot()) {
8730 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8731 }
8732 locations->SetOut(location);
8733}
8734
8735void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8736 ATTRIBUTE_UNUSED) {
8737 // Nothing to do, the parameter is already at its location.
8738}
8739
8740void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008742 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008743 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8744}
8745
8746void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8747 ATTRIBUTE_UNUSED) {
8748 // Nothing to do, the method is already at its location.
8749}
8750
8751void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008752 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008753 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008754 locations->SetInAt(i, Location::Any());
8755 }
8756 locations->SetOut(Location::Any());
8757}
8758
8759void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8760 LOG(FATAL) << "Unreachable";
8761}
8762
8763void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008764 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008765 bool call_rem;
8766 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8767 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8768 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8769 } else {
8770 call_rem = (type != DataType::Type::kInt32);
8771 }
8772 LocationSummary::CallKind call_kind = call_rem
8773 ? LocationSummary::kCallOnMainOnly
8774 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008775 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008776
8777 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008778 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008779 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008780 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008781 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8782 break;
8783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008784 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008785 if (call_rem) {
8786 InvokeRuntimeCallingConvention calling_convention;
8787 locations->SetInAt(0, Location::RegisterPairLocation(
8788 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8789 locations->SetInAt(1, Location::RegisterPairLocation(
8790 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8791 locations->SetOut(calling_convention.GetReturnLocation(type));
8792 } else {
8793 locations->SetInAt(0, Location::RequiresRegister());
8794 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8795 locations->SetOut(Location::RequiresRegister());
8796 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008797 break;
8798 }
8799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008800 case DataType::Type::kFloat32:
8801 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008802 InvokeRuntimeCallingConvention calling_convention;
8803 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8804 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8805 locations->SetOut(calling_convention.GetReturnLocation(type));
8806 break;
8807 }
8808
8809 default:
8810 LOG(FATAL) << "Unexpected rem type " << type;
8811 }
8812}
8813
8814void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008815 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008816 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008817
8818 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008819 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008820 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008821 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008822 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008823 if (locations->InAt(1).IsConstant()) {
8824 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8825 if (imm == 0) {
8826 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8827 } else if (imm == 1 || imm == -1) {
8828 DivRemOneOrMinusOne(instruction);
8829 } else {
8830 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8831 DivRemByPowerOfTwo(instruction);
8832 }
8833 } else {
8834 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8835 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8836 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008837 break;
8838 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008839 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008840 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008841 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008842 break;
8843 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008844 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008845 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008846 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008847 break;
8848 }
8849 default:
8850 LOG(FATAL) << "Unexpected rem type " << type;
8851 }
8852}
8853
Igor Murashkind01745e2017-04-05 16:40:31 -07008854void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8855 constructor_fence->SetLocations(nullptr);
8856}
8857
8858void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8859 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8860 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8861}
8862
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008863void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8864 memory_barrier->SetLocations(nullptr);
8865}
8866
8867void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8868 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8869}
8870
8871void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008872 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008873 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008874 locations->SetInAt(0, MipsReturnLocation(return_type));
8875}
8876
8877void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8878 codegen_->GenerateFrameExit();
8879}
8880
8881void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8882 ret->SetLocations(nullptr);
8883}
8884
8885void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8886 codegen_->GenerateFrameExit();
8887}
8888
Alexey Frunze92d90602015-12-18 18:16:36 -08008889void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8890 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008891}
8892
Alexey Frunze92d90602015-12-18 18:16:36 -08008893void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8894 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008895}
8896
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008897void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8898 HandleShift(shl);
8899}
8900
8901void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8902 HandleShift(shl);
8903}
8904
8905void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8906 HandleShift(shr);
8907}
8908
8909void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8910 HandleShift(shr);
8911}
8912
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008913void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8914 HandleBinaryOp(instruction);
8915}
8916
8917void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8918 HandleBinaryOp(instruction);
8919}
8920
8921void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8922 HandleFieldGet(instruction, instruction->GetFieldInfo());
8923}
8924
8925void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8926 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8927}
8928
8929void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8930 HandleFieldSet(instruction, instruction->GetFieldInfo());
8931}
8932
8933void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008934 HandleFieldSet(instruction,
8935 instruction->GetFieldInfo(),
8936 instruction->GetDexPc(),
8937 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008938}
8939
8940void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8941 HUnresolvedInstanceFieldGet* instruction) {
8942 FieldAccessCallingConventionMIPS calling_convention;
8943 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8944 instruction->GetFieldType(),
8945 calling_convention);
8946}
8947
8948void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8949 HUnresolvedInstanceFieldGet* instruction) {
8950 FieldAccessCallingConventionMIPS calling_convention;
8951 codegen_->GenerateUnresolvedFieldAccess(instruction,
8952 instruction->GetFieldType(),
8953 instruction->GetFieldIndex(),
8954 instruction->GetDexPc(),
8955 calling_convention);
8956}
8957
8958void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8959 HUnresolvedInstanceFieldSet* instruction) {
8960 FieldAccessCallingConventionMIPS calling_convention;
8961 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8962 instruction->GetFieldType(),
8963 calling_convention);
8964}
8965
8966void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8967 HUnresolvedInstanceFieldSet* instruction) {
8968 FieldAccessCallingConventionMIPS calling_convention;
8969 codegen_->GenerateUnresolvedFieldAccess(instruction,
8970 instruction->GetFieldType(),
8971 instruction->GetFieldIndex(),
8972 instruction->GetDexPc(),
8973 calling_convention);
8974}
8975
8976void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8977 HUnresolvedStaticFieldGet* instruction) {
8978 FieldAccessCallingConventionMIPS calling_convention;
8979 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8980 instruction->GetFieldType(),
8981 calling_convention);
8982}
8983
8984void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8985 HUnresolvedStaticFieldGet* instruction) {
8986 FieldAccessCallingConventionMIPS calling_convention;
8987 codegen_->GenerateUnresolvedFieldAccess(instruction,
8988 instruction->GetFieldType(),
8989 instruction->GetFieldIndex(),
8990 instruction->GetDexPc(),
8991 calling_convention);
8992}
8993
8994void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8995 HUnresolvedStaticFieldSet* instruction) {
8996 FieldAccessCallingConventionMIPS calling_convention;
8997 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8998 instruction->GetFieldType(),
8999 calling_convention);
9000}
9001
9002void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9003 HUnresolvedStaticFieldSet* instruction) {
9004 FieldAccessCallingConventionMIPS calling_convention;
9005 codegen_->GenerateUnresolvedFieldAccess(instruction,
9006 instruction->GetFieldType(),
9007 instruction->GetFieldIndex(),
9008 instruction->GetDexPc(),
9009 calling_convention);
9010}
9011
9012void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009013 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9014 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009015 // In suspend check slow path, usually there are no caller-save registers at all.
9016 // If SIMD instructions are present, however, we force spilling all live SIMD
9017 // registers in full width (since the runtime only saves/restores lower part).
9018 locations->SetCustomSlowPathCallerSaves(
9019 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009020}
9021
9022void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9023 HBasicBlock* block = instruction->GetBlock();
9024 if (block->GetLoopInformation() != nullptr) {
9025 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9026 // The back edge will generate the suspend check.
9027 return;
9028 }
9029 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9030 // The goto will generate the suspend check.
9031 return;
9032 }
9033 GenerateSuspendCheck(instruction, nullptr);
9034}
9035
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009036void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009037 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9038 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009039 InvokeRuntimeCallingConvention calling_convention;
9040 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9041}
9042
9043void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009044 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009045 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9046}
9047
9048void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009049 DataType::Type input_type = conversion->GetInputType();
9050 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009051 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9052 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009053 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009055 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9056 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009057 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9058 }
9059
9060 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009061 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009062 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9063 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009064 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009065 }
9066
Vladimir Markoca6fff82017-10-03 14:49:14 +01009067 LocationSummary* locations =
9068 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009069
9070 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009071 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009072 locations->SetInAt(0, Location::RequiresFpuRegister());
9073 } else {
9074 locations->SetInAt(0, Location::RequiresRegister());
9075 }
9076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009077 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009078 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9079 } else {
9080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9081 }
9082 } else {
9083 InvokeRuntimeCallingConvention calling_convention;
9084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009085 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009086 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9087 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009088 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009089 locations->SetInAt(0, Location::RegisterPairLocation(
9090 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9091 }
9092
9093 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9094 }
9095}
9096
9097void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9098 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009099 DataType::Type result_type = conversion->GetResultType();
9100 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009101 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009102 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009103
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009104 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9105 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009107 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009108 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9109 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9110 Register src = locations->InAt(0).AsRegister<Register>();
9111
Alexey Frunzea871ef12016-06-27 15:20:11 -07009112 if (dst_low != src) {
9113 __ Move(dst_low, src);
9114 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009115 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009116 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009117 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009118 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009119 ? locations->InAt(0).AsRegisterPairLow<Register>()
9120 : locations->InAt(0).AsRegister<Register>();
9121
9122 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009123 case DataType::Type::kUint8:
9124 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009125 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009126 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009127 if (has_sign_extension) {
9128 __ Seb(dst, src);
9129 } else {
9130 __ Sll(dst, src, 24);
9131 __ Sra(dst, dst, 24);
9132 }
9133 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009134 case DataType::Type::kUint16:
9135 __ Andi(dst, src, 0xFFFF);
9136 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009137 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009138 if (has_sign_extension) {
9139 __ Seh(dst, src);
9140 } else {
9141 __ Sll(dst, src, 16);
9142 __ Sra(dst, dst, 16);
9143 }
9144 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009145 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009146 if (dst != src) {
9147 __ Move(dst, src);
9148 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009149 break;
9150
9151 default:
9152 LOG(FATAL) << "Unexpected type conversion from " << input_type
9153 << " to " << result_type;
9154 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009155 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9156 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009157 if (isR6) {
9158 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9159 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9160 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9161 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9162 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9163 __ Mtc1(src_low, FTMP);
9164 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009165 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009166 __ Cvtsl(dst, FTMP);
9167 } else {
9168 __ Cvtdl(dst, FTMP);
9169 }
9170 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009171 QuickEntrypointEnum entrypoint =
9172 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009173 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009174 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009175 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9176 } else {
9177 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9178 }
9179 }
9180 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009181 Register src = locations->InAt(0).AsRegister<Register>();
9182 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9183 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009184 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009185 __ Cvtsw(dst, FTMP);
9186 } else {
9187 __ Cvtdw(dst, FTMP);
9188 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009189 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009190 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9191 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009192
9193 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9194 // value of the output type if the input is outside of the range after the truncation or
9195 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9196 // results. This matches the desired float/double-to-int/long conversion exactly.
9197 //
9198 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9199 // value when the input is either a NaN or is outside of the range of the output type
9200 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9201 // the same result.
9202 //
9203 // The code takes care of the different behaviors by first comparing the input to the
9204 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9205 // If the input is greater than or equal to the minimum, it procedes to the truncate
9206 // instruction, which will handle such an input the same way irrespective of NAN2008.
9207 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9208 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009209 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009210 if (isR6) {
9211 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9212 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9213 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9214 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9215 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009217 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009218 __ TruncLS(FTMP, src);
9219 } else {
9220 __ TruncLD(FTMP, src);
9221 }
9222 __ Mfc1(dst_low, FTMP);
9223 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009224 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009225 QuickEntrypointEnum entrypoint =
9226 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009227 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009228 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009229 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9230 } else {
9231 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9232 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009233 }
9234 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009235 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9236 Register dst = locations->Out().AsRegister<Register>();
9237 MipsLabel truncate;
9238 MipsLabel done;
9239
Lena Djokicf4e23a82017-05-09 15:43:45 +02009240 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009241 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009242 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9243 __ LoadConst32(TMP, min_val);
9244 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009245 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009246 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9247 __ LoadConst32(TMP, High32Bits(min_val));
9248 __ Mtc1(ZERO, FTMP);
9249 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009250 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009252 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009253 __ ColeS(0, FTMP, src);
9254 } else {
9255 __ ColeD(0, FTMP, src);
9256 }
9257 __ Bc1t(0, &truncate);
9258
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009259 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009260 __ CeqS(0, src, src);
9261 } else {
9262 __ CeqD(0, src, src);
9263 }
9264 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9265 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009266
9267 __ B(&done);
9268
9269 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009270 }
9271
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009272 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009273 __ TruncWS(FTMP, src);
9274 } else {
9275 __ TruncWD(FTMP, src);
9276 }
9277 __ Mfc1(dst, FTMP);
9278
Lena Djokicf4e23a82017-05-09 15:43:45 +02009279 if (!isR6) {
9280 __ Bind(&done);
9281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009282 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009283 } else if (DataType::IsFloatingPointType(result_type) &&
9284 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009285 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9286 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009287 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009288 __ Cvtsd(dst, src);
9289 } else {
9290 __ Cvtds(dst, src);
9291 }
9292 } else {
9293 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9294 << " to " << result_type;
9295 }
9296}
9297
9298void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9299 HandleShift(ushr);
9300}
9301
9302void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9303 HandleShift(ushr);
9304}
9305
9306void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9307 HandleBinaryOp(instruction);
9308}
9309
9310void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9311 HandleBinaryOp(instruction);
9312}
9313
9314void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9315 // Nothing to do, this should be removed during prepare for register allocator.
9316 LOG(FATAL) << "Unreachable";
9317}
9318
9319void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9320 // Nothing to do, this should be removed during prepare for register allocator.
9321 LOG(FATAL) << "Unreachable";
9322}
9323
9324void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009325 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009326}
9327
9328void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009329 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009330}
9331
9332void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009333 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009334}
9335
9336void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009337 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009338}
9339
9340void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009341 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009342}
9343
9344void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009345 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009346}
9347
9348void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009349 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009350}
9351
9352void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009353 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009354}
9355
9356void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009357 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009358}
9359
9360void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009361 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009362}
9363
9364void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009365 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009366}
9367
9368void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009369 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009370}
9371
9372void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009373 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009374}
9375
9376void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009377 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009378}
9379
9380void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009381 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009382}
9383
9384void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009385 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009386}
9387
9388void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009389 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009390}
9391
9392void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009393 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009394}
9395
9396void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009397 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009398}
9399
9400void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009401 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009402}
9403
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009404void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9405 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009406 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009407 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009408 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9409 uint32_t num_entries = switch_instr->GetNumEntries();
9410 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9411 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9412 // instruction to simulate PC-relative addressing when accessing the jump table.
9413 // NAL clobbers RA. Make sure RA is preserved.
9414 codegen_->ClobberRA();
9415 }
9416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009417}
9418
Alexey Frunze96b66822016-09-10 02:32:44 -07009419void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9420 int32_t lower_bound,
9421 uint32_t num_entries,
9422 HBasicBlock* switch_block,
9423 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009424 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009425 Register temp_reg = TMP;
9426 __ Addiu32(temp_reg, value_reg, -lower_bound);
9427 // Jump to default if index is negative
9428 // Note: We don't check the case that index is positive while value < lower_bound, because in
9429 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9430 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9431
Alexey Frunze96b66822016-09-10 02:32:44 -07009432 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009433 // Jump to successors[0] if value == lower_bound.
9434 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9435 int32_t last_index = 0;
9436 for (; num_entries - last_index > 2; last_index += 2) {
9437 __ Addiu(temp_reg, temp_reg, -2);
9438 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9439 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9440 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9441 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9442 }
9443 if (num_entries - last_index == 2) {
9444 // The last missing case_value.
9445 __ Addiu(temp_reg, temp_reg, -1);
9446 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009447 }
9448
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009449 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009450 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009451 __ B(codegen_->GetLabelOf(default_block));
9452 }
9453}
9454
Alexey Frunze96b66822016-09-10 02:32:44 -07009455void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9456 Register constant_area,
9457 int32_t lower_bound,
9458 uint32_t num_entries,
9459 HBasicBlock* switch_block,
9460 HBasicBlock* default_block) {
9461 // Create a jump table.
9462 std::vector<MipsLabel*> labels(num_entries);
9463 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9464 for (uint32_t i = 0; i < num_entries; i++) {
9465 labels[i] = codegen_->GetLabelOf(successors[i]);
9466 }
9467 JumpTable* table = __ CreateJumpTable(std::move(labels));
9468
9469 // Is the value in range?
9470 __ Addiu32(TMP, value_reg, -lower_bound);
9471 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9472 __ Sltiu(AT, TMP, num_entries);
9473 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9474 } else {
9475 __ LoadConst32(AT, num_entries);
9476 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9477 }
9478
9479 // We are in the range of the table.
9480 // Load the target address from the jump table, indexing by the value.
9481 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009482 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009483 __ Lw(TMP, TMP, 0);
9484 // Compute the absolute target address by adding the table start address
9485 // (the table contains offsets to targets relative to its start).
9486 __ Addu(TMP, TMP, AT);
9487 // And jump.
9488 __ Jr(TMP);
9489 __ NopIfNoReordering();
9490}
9491
9492void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9493 int32_t lower_bound = switch_instr->GetStartValue();
9494 uint32_t num_entries = switch_instr->GetNumEntries();
9495 LocationSummary* locations = switch_instr->GetLocations();
9496 Register value_reg = locations->InAt(0).AsRegister<Register>();
9497 HBasicBlock* switch_block = switch_instr->GetBlock();
9498 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9499
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009500 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009501 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009502 //
9503 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9504 // to access the jump table and it is implemented by changing HPackedSwitch to
9505 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9506 // VisitMipsPackedSwitch()).
9507 //
9508 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9509 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9510 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009511 GenTableBasedPackedSwitch(value_reg,
9512 ZERO,
9513 lower_bound,
9514 num_entries,
9515 switch_block,
9516 default_block);
9517 } else {
9518 GenPackedSwitchWithCompares(value_reg,
9519 lower_bound,
9520 num_entries,
9521 switch_block,
9522 default_block);
9523 }
9524}
9525
9526void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9527 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009528 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009529 locations->SetInAt(0, Location::RequiresRegister());
9530 // Constant area pointer (HMipsComputeBaseMethodAddress).
9531 locations->SetInAt(1, Location::RequiresRegister());
9532}
9533
9534void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9535 int32_t lower_bound = switch_instr->GetStartValue();
9536 uint32_t num_entries = switch_instr->GetNumEntries();
9537 LocationSummary* locations = switch_instr->GetLocations();
9538 Register value_reg = locations->InAt(0).AsRegister<Register>();
9539 Register constant_area = locations->InAt(1).AsRegister<Register>();
9540 HBasicBlock* switch_block = switch_instr->GetBlock();
9541 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9542
9543 // This is an R2-only path. HPackedSwitch has been changed to
9544 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9545 // required to address the jump table relative to PC.
9546 GenTableBasedPackedSwitch(value_reg,
9547 constant_area,
9548 lower_bound,
9549 num_entries,
9550 switch_block,
9551 default_block);
9552}
9553
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009554void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9555 HMipsComputeBaseMethodAddress* insn) {
9556 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009557 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009558 locations->SetOut(Location::RequiresRegister());
9559}
9560
9561void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9562 HMipsComputeBaseMethodAddress* insn) {
9563 LocationSummary* locations = insn->GetLocations();
9564 Register reg = locations->Out().AsRegister<Register>();
9565
9566 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9567
9568 // Generate a dummy PC-relative call to obtain PC.
9569 __ Nal();
9570 // Grab the return address off RA.
9571 __ Move(reg, RA);
9572
9573 // Remember this offset (the obtained PC value) for later use with constant area.
9574 __ BindPcRelBaseLabel();
9575}
9576
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009577void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9578 // The trampoline uses the same calling convention as dex calling conventions,
9579 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9580 // the method_idx.
9581 HandleInvoke(invoke);
9582}
9583
9584void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9585 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9586}
9587
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009588void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9589 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009590 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009591 locations->SetInAt(0, Location::RequiresRegister());
9592 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009593}
9594
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009595void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9596 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009597 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009598 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009599 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009600 __ LoadFromOffset(kLoadWord,
9601 locations->Out().AsRegister<Register>(),
9602 locations->InAt(0).AsRegister<Register>(),
9603 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009604 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009605 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009606 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009607 __ LoadFromOffset(kLoadWord,
9608 locations->Out().AsRegister<Register>(),
9609 locations->InAt(0).AsRegister<Register>(),
9610 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009611 __ LoadFromOffset(kLoadWord,
9612 locations->Out().AsRegister<Register>(),
9613 locations->Out().AsRegister<Register>(),
9614 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009615 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009616}
9617
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009618void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9619 ATTRIBUTE_UNUSED) {
9620 LOG(FATAL) << "Unreachable";
9621}
9622
9623void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9624 ATTRIBUTE_UNUSED) {
9625 LOG(FATAL) << "Unreachable";
9626}
9627
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009628#undef __
9629#undef QUICK_ENTRY_POINT
9630
9631} // namespace mips
9632} // namespace art