blob: 2ed0ab79a149553e92868db545f6765121837909 [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());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003771
3772 LocationSummary* locations = instruction->GetLocations();
3773 Location second = locations->InAt(1);
3774 DCHECK(second.IsConstant());
3775
3776 Register out = locations->Out().AsRegister<Register>();
3777 Register dividend = locations->InAt(0).AsRegister<Register>();
3778 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3779 DCHECK(imm == 1 || imm == -1);
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 }
3790}
3791
3792void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3793 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003795
3796 LocationSummary* locations = instruction->GetLocations();
3797 Location second = locations->InAt(1);
3798 DCHECK(second.IsConstant());
3799
3800 Register out = locations->Out().AsRegister<Register>();
3801 Register dividend = locations->InAt(0).AsRegister<Register>();
3802 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003803 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003804 int ctz_imm = CTZ(abs_imm);
3805
3806 if (instruction->IsDiv()) {
3807 if (ctz_imm == 1) {
3808 // Fast path for division by +/-2, which is very common.
3809 __ Srl(TMP, dividend, 31);
3810 } else {
3811 __ Sra(TMP, dividend, 31);
3812 __ Srl(TMP, TMP, 32 - ctz_imm);
3813 }
3814 __ Addu(out, dividend, TMP);
3815 __ Sra(out, out, ctz_imm);
3816 if (imm < 0) {
3817 __ Subu(out, ZERO, out);
3818 }
3819 } else {
3820 if (ctz_imm == 1) {
3821 // Fast path for modulo +/-2, which is very common.
3822 __ Sra(TMP, dividend, 31);
3823 __ Subu(out, dividend, TMP);
3824 __ Andi(out, out, 1);
3825 __ Addu(out, out, TMP);
3826 } else {
3827 __ Sra(TMP, dividend, 31);
3828 __ Srl(TMP, TMP, 32 - ctz_imm);
3829 __ Addu(out, dividend, TMP);
3830 if (IsUint<16>(abs_imm - 1)) {
3831 __ Andi(out, out, abs_imm - 1);
3832 } else {
Lena Djokica556e6b2017-12-13 12:09:42 +01003833 if (codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2()) {
3834 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3835 } else {
3836 __ Sll(out, out, 32 - ctz_imm);
3837 __ Srl(out, out, 32 - ctz_imm);
3838 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003839 }
3840 __ Subu(out, out, TMP);
3841 }
3842 }
3843}
3844
3845void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3846 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003847 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003848
3849 LocationSummary* locations = instruction->GetLocations();
3850 Location second = locations->InAt(1);
3851 DCHECK(second.IsConstant());
3852
3853 Register out = locations->Out().AsRegister<Register>();
3854 Register dividend = locations->InAt(0).AsRegister<Register>();
3855 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3856
3857 int64_t magic;
3858 int shift;
3859 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3860
3861 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3862
3863 __ LoadConst32(TMP, magic);
3864 if (isR6) {
3865 __ MuhR6(TMP, dividend, TMP);
3866 } else {
3867 __ MultR2(dividend, TMP);
3868 __ Mfhi(TMP);
3869 }
3870 if (imm > 0 && magic < 0) {
3871 __ Addu(TMP, TMP, dividend);
3872 } else if (imm < 0 && magic > 0) {
3873 __ Subu(TMP, TMP, dividend);
3874 }
3875
3876 if (shift != 0) {
3877 __ Sra(TMP, TMP, shift);
3878 }
3879
3880 if (instruction->IsDiv()) {
3881 __ Sra(out, TMP, 31);
3882 __ Subu(out, TMP, out);
3883 } else {
3884 __ Sra(AT, TMP, 31);
3885 __ Subu(AT, TMP, AT);
3886 __ LoadConst32(TMP, imm);
3887 if (isR6) {
3888 __ MulR6(TMP, AT, TMP);
3889 } else {
3890 __ MulR2(TMP, AT, TMP);
3891 }
3892 __ Subu(out, dividend, TMP);
3893 }
3894}
3895
3896void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3897 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003898 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003899
3900 LocationSummary* locations = instruction->GetLocations();
3901 Register out = locations->Out().AsRegister<Register>();
3902 Location second = locations->InAt(1);
3903
3904 if (second.IsConstant()) {
3905 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3906 if (imm == 0) {
3907 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3908 } else if (imm == 1 || imm == -1) {
3909 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003910 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003911 DivRemByPowerOfTwo(instruction);
3912 } else {
3913 DCHECK(imm <= -2 || imm >= 2);
3914 GenerateDivRemWithAnyConstant(instruction);
3915 }
3916 } else {
3917 Register dividend = locations->InAt(0).AsRegister<Register>();
3918 Register divisor = second.AsRegister<Register>();
3919 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3920 if (instruction->IsDiv()) {
3921 if (isR6) {
3922 __ DivR6(out, dividend, divisor);
3923 } else {
3924 __ DivR2(out, dividend, divisor);
3925 }
3926 } else {
3927 if (isR6) {
3928 __ ModR6(out, dividend, divisor);
3929 } else {
3930 __ ModR2(out, dividend, divisor);
3931 }
3932 }
3933 }
3934}
3935
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003936void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 DataType::Type type = div->GetResultType();
3938 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003939 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003940 : LocationSummary::kNoCall;
3941
Vladimir Markoca6fff82017-10-03 14:49:14 +01003942 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003943
3944 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003945 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003947 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3949 break;
3950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003951 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003952 InvokeRuntimeCallingConvention calling_convention;
3953 locations->SetInAt(0, Location::RegisterPairLocation(
3954 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3955 locations->SetInAt(1, Location::RegisterPairLocation(
3956 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3957 locations->SetOut(calling_convention.GetReturnLocation(type));
3958 break;
3959 }
3960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003961 case DataType::Type::kFloat32:
3962 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003963 locations->SetInAt(0, Location::RequiresFpuRegister());
3964 locations->SetInAt(1, Location::RequiresFpuRegister());
3965 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3966 break;
3967
3968 default:
3969 LOG(FATAL) << "Unexpected div type " << type;
3970 }
3971}
3972
3973void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003974 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003975 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003976
3977 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003979 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003980 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003981 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003982 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003983 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3984 break;
3985 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003986 case DataType::Type::kFloat32:
3987 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003988 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3989 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3990 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003991 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003992 __ DivS(dst, lhs, rhs);
3993 } else {
3994 __ DivD(dst, lhs, rhs);
3995 }
3996 break;
3997 }
3998 default:
3999 LOG(FATAL) << "Unexpected div type " << type;
4000 }
4001}
4002
4003void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004004 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004005 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004006}
4007
4008void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004009 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004010 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004011 codegen_->AddSlowPath(slow_path);
4012 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004013 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004014
4015 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004016 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004017 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004018 case DataType::Type::kInt8:
4019 case DataType::Type::kUint16:
4020 case DataType::Type::kInt16:
4021 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004022 if (value.IsConstant()) {
4023 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4024 __ B(slow_path->GetEntryLabel());
4025 } else {
4026 // A division by a non-null constant is valid. We don't need to perform
4027 // any check, so simply fall through.
4028 }
4029 } else {
4030 DCHECK(value.IsRegister()) << value;
4031 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4032 }
4033 break;
4034 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004036 if (value.IsConstant()) {
4037 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4038 __ B(slow_path->GetEntryLabel());
4039 } else {
4040 // A division by a non-null constant is valid. We don't need to perform
4041 // any check, so simply fall through.
4042 }
4043 } else {
4044 DCHECK(value.IsRegisterPair()) << value;
4045 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4046 __ Beqz(TMP, slow_path->GetEntryLabel());
4047 }
4048 break;
4049 }
4050 default:
4051 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4052 }
4053}
4054
4055void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4056 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004057 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004058 locations->SetOut(Location::ConstantLocation(constant));
4059}
4060
4061void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4062 // Will be generated at use site.
4063}
4064
4065void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4066 exit->SetLocations(nullptr);
4067}
4068
4069void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4070}
4071
4072void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4073 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004074 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004075 locations->SetOut(Location::ConstantLocation(constant));
4076}
4077
4078void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4079 // Will be generated at use site.
4080}
4081
4082void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4083 got->SetLocations(nullptr);
4084}
4085
4086void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004087 if (successor->IsExitBlock()) {
4088 DCHECK(got->GetPrevious()->AlwaysThrows());
4089 return; // no code needed
4090 }
4091
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004092 HBasicBlock* block = got->GetBlock();
4093 HInstruction* previous = got->GetPrevious();
4094 HLoopInformation* info = block->GetLoopInformation();
4095
4096 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004097 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4098 return;
4099 }
4100 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4101 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4102 }
4103 if (!codegen_->GoesToNextBlock(block, successor)) {
4104 __ B(codegen_->GetLabelOf(successor));
4105 }
4106}
4107
4108void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4109 HandleGoto(got, got->GetSuccessor());
4110}
4111
4112void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4113 try_boundary->SetLocations(nullptr);
4114}
4115
4116void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4117 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4118 if (!successor->IsExitBlock()) {
4119 HandleGoto(try_boundary, successor);
4120 }
4121}
4122
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004123void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4124 LocationSummary* locations) {
4125 Register dst = locations->Out().AsRegister<Register>();
4126 Register lhs = locations->InAt(0).AsRegister<Register>();
4127 Location rhs_location = locations->InAt(1);
4128 Register rhs_reg = ZERO;
4129 int64_t rhs_imm = 0;
4130 bool use_imm = rhs_location.IsConstant();
4131 if (use_imm) {
4132 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4133 } else {
4134 rhs_reg = rhs_location.AsRegister<Register>();
4135 }
4136
4137 switch (cond) {
4138 case kCondEQ:
4139 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004140 if (use_imm && IsInt<16>(-rhs_imm)) {
4141 if (rhs_imm == 0) {
4142 if (cond == kCondEQ) {
4143 __ Sltiu(dst, lhs, 1);
4144 } else {
4145 __ Sltu(dst, ZERO, lhs);
4146 }
4147 } else {
4148 __ Addiu(dst, lhs, -rhs_imm);
4149 if (cond == kCondEQ) {
4150 __ Sltiu(dst, dst, 1);
4151 } else {
4152 __ Sltu(dst, ZERO, dst);
4153 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004154 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004155 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004156 if (use_imm && IsUint<16>(rhs_imm)) {
4157 __ Xori(dst, lhs, rhs_imm);
4158 } else {
4159 if (use_imm) {
4160 rhs_reg = TMP;
4161 __ LoadConst32(rhs_reg, rhs_imm);
4162 }
4163 __ Xor(dst, lhs, rhs_reg);
4164 }
4165 if (cond == kCondEQ) {
4166 __ Sltiu(dst, dst, 1);
4167 } else {
4168 __ Sltu(dst, ZERO, dst);
4169 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004170 }
4171 break;
4172
4173 case kCondLT:
4174 case kCondGE:
4175 if (use_imm && IsInt<16>(rhs_imm)) {
4176 __ Slti(dst, lhs, rhs_imm);
4177 } else {
4178 if (use_imm) {
4179 rhs_reg = TMP;
4180 __ LoadConst32(rhs_reg, rhs_imm);
4181 }
4182 __ Slt(dst, lhs, rhs_reg);
4183 }
4184 if (cond == kCondGE) {
4185 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4186 // only the slt instruction but no sge.
4187 __ Xori(dst, dst, 1);
4188 }
4189 break;
4190
4191 case kCondLE:
4192 case kCondGT:
4193 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4194 // Simulate lhs <= rhs via lhs < rhs + 1.
4195 __ Slti(dst, lhs, rhs_imm + 1);
4196 if (cond == kCondGT) {
4197 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4198 // only the slti instruction but no sgti.
4199 __ Xori(dst, dst, 1);
4200 }
4201 } else {
4202 if (use_imm) {
4203 rhs_reg = TMP;
4204 __ LoadConst32(rhs_reg, rhs_imm);
4205 }
4206 __ Slt(dst, rhs_reg, lhs);
4207 if (cond == kCondLE) {
4208 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4209 // only the slt instruction but no sle.
4210 __ Xori(dst, dst, 1);
4211 }
4212 }
4213 break;
4214
4215 case kCondB:
4216 case kCondAE:
4217 if (use_imm && IsInt<16>(rhs_imm)) {
4218 // Sltiu sign-extends its 16-bit immediate operand before
4219 // the comparison and thus lets us compare directly with
4220 // unsigned values in the ranges [0, 0x7fff] and
4221 // [0xffff8000, 0xffffffff].
4222 __ Sltiu(dst, lhs, rhs_imm);
4223 } else {
4224 if (use_imm) {
4225 rhs_reg = TMP;
4226 __ LoadConst32(rhs_reg, rhs_imm);
4227 }
4228 __ Sltu(dst, lhs, rhs_reg);
4229 }
4230 if (cond == kCondAE) {
4231 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4232 // only the sltu instruction but no sgeu.
4233 __ Xori(dst, dst, 1);
4234 }
4235 break;
4236
4237 case kCondBE:
4238 case kCondA:
4239 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4240 // Simulate lhs <= rhs via lhs < rhs + 1.
4241 // Note that this only works if rhs + 1 does not overflow
4242 // to 0, hence the check above.
4243 // Sltiu sign-extends its 16-bit immediate operand before
4244 // the comparison and thus lets us compare directly with
4245 // unsigned values in the ranges [0, 0x7fff] and
4246 // [0xffff8000, 0xffffffff].
4247 __ Sltiu(dst, lhs, rhs_imm + 1);
4248 if (cond == kCondA) {
4249 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4250 // only the sltiu instruction but no sgtiu.
4251 __ Xori(dst, dst, 1);
4252 }
4253 } else {
4254 if (use_imm) {
4255 rhs_reg = TMP;
4256 __ LoadConst32(rhs_reg, rhs_imm);
4257 }
4258 __ Sltu(dst, rhs_reg, lhs);
4259 if (cond == kCondBE) {
4260 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4261 // only the sltu instruction but no sleu.
4262 __ Xori(dst, dst, 1);
4263 }
4264 }
4265 break;
4266 }
4267}
4268
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004269bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4270 LocationSummary* input_locations,
4271 Register dst) {
4272 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4273 Location rhs_location = input_locations->InAt(1);
4274 Register rhs_reg = ZERO;
4275 int64_t rhs_imm = 0;
4276 bool use_imm = rhs_location.IsConstant();
4277 if (use_imm) {
4278 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4279 } else {
4280 rhs_reg = rhs_location.AsRegister<Register>();
4281 }
4282
4283 switch (cond) {
4284 case kCondEQ:
4285 case kCondNE:
4286 if (use_imm && IsInt<16>(-rhs_imm)) {
4287 __ Addiu(dst, lhs, -rhs_imm);
4288 } else if (use_imm && IsUint<16>(rhs_imm)) {
4289 __ Xori(dst, lhs, rhs_imm);
4290 } else {
4291 if (use_imm) {
4292 rhs_reg = TMP;
4293 __ LoadConst32(rhs_reg, rhs_imm);
4294 }
4295 __ Xor(dst, lhs, rhs_reg);
4296 }
4297 return (cond == kCondEQ);
4298
4299 case kCondLT:
4300 case kCondGE:
4301 if (use_imm && IsInt<16>(rhs_imm)) {
4302 __ Slti(dst, lhs, rhs_imm);
4303 } else {
4304 if (use_imm) {
4305 rhs_reg = TMP;
4306 __ LoadConst32(rhs_reg, rhs_imm);
4307 }
4308 __ Slt(dst, lhs, rhs_reg);
4309 }
4310 return (cond == kCondGE);
4311
4312 case kCondLE:
4313 case kCondGT:
4314 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4315 // Simulate lhs <= rhs via lhs < rhs + 1.
4316 __ Slti(dst, lhs, rhs_imm + 1);
4317 return (cond == kCondGT);
4318 } else {
4319 if (use_imm) {
4320 rhs_reg = TMP;
4321 __ LoadConst32(rhs_reg, rhs_imm);
4322 }
4323 __ Slt(dst, rhs_reg, lhs);
4324 return (cond == kCondLE);
4325 }
4326
4327 case kCondB:
4328 case kCondAE:
4329 if (use_imm && IsInt<16>(rhs_imm)) {
4330 // Sltiu sign-extends its 16-bit immediate operand before
4331 // the comparison and thus lets us compare directly with
4332 // unsigned values in the ranges [0, 0x7fff] and
4333 // [0xffff8000, 0xffffffff].
4334 __ Sltiu(dst, lhs, rhs_imm);
4335 } else {
4336 if (use_imm) {
4337 rhs_reg = TMP;
4338 __ LoadConst32(rhs_reg, rhs_imm);
4339 }
4340 __ Sltu(dst, lhs, rhs_reg);
4341 }
4342 return (cond == kCondAE);
4343
4344 case kCondBE:
4345 case kCondA:
4346 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4347 // Simulate lhs <= rhs via lhs < rhs + 1.
4348 // Note that this only works if rhs + 1 does not overflow
4349 // to 0, hence the check above.
4350 // Sltiu sign-extends its 16-bit immediate operand before
4351 // the comparison and thus lets us compare directly with
4352 // unsigned values in the ranges [0, 0x7fff] and
4353 // [0xffff8000, 0xffffffff].
4354 __ Sltiu(dst, lhs, rhs_imm + 1);
4355 return (cond == kCondA);
4356 } else {
4357 if (use_imm) {
4358 rhs_reg = TMP;
4359 __ LoadConst32(rhs_reg, rhs_imm);
4360 }
4361 __ Sltu(dst, rhs_reg, lhs);
4362 return (cond == kCondBE);
4363 }
4364 }
4365}
4366
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004367void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4368 LocationSummary* locations,
4369 MipsLabel* label) {
4370 Register lhs = locations->InAt(0).AsRegister<Register>();
4371 Location rhs_location = locations->InAt(1);
4372 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004373 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004374 bool use_imm = rhs_location.IsConstant();
4375 if (use_imm) {
4376 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4377 } else {
4378 rhs_reg = rhs_location.AsRegister<Register>();
4379 }
4380
4381 if (use_imm && rhs_imm == 0) {
4382 switch (cond) {
4383 case kCondEQ:
4384 case kCondBE: // <= 0 if zero
4385 __ Beqz(lhs, label);
4386 break;
4387 case kCondNE:
4388 case kCondA: // > 0 if non-zero
4389 __ Bnez(lhs, label);
4390 break;
4391 case kCondLT:
4392 __ Bltz(lhs, label);
4393 break;
4394 case kCondGE:
4395 __ Bgez(lhs, label);
4396 break;
4397 case kCondLE:
4398 __ Blez(lhs, label);
4399 break;
4400 case kCondGT:
4401 __ Bgtz(lhs, label);
4402 break;
4403 case kCondB: // always false
4404 break;
4405 case kCondAE: // always true
4406 __ B(label);
4407 break;
4408 }
4409 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004410 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4411 if (isR6 || !use_imm) {
4412 if (use_imm) {
4413 rhs_reg = TMP;
4414 __ LoadConst32(rhs_reg, rhs_imm);
4415 }
4416 switch (cond) {
4417 case kCondEQ:
4418 __ Beq(lhs, rhs_reg, label);
4419 break;
4420 case kCondNE:
4421 __ Bne(lhs, rhs_reg, label);
4422 break;
4423 case kCondLT:
4424 __ Blt(lhs, rhs_reg, label);
4425 break;
4426 case kCondGE:
4427 __ Bge(lhs, rhs_reg, label);
4428 break;
4429 case kCondLE:
4430 __ Bge(rhs_reg, lhs, label);
4431 break;
4432 case kCondGT:
4433 __ Blt(rhs_reg, lhs, label);
4434 break;
4435 case kCondB:
4436 __ Bltu(lhs, rhs_reg, label);
4437 break;
4438 case kCondAE:
4439 __ Bgeu(lhs, rhs_reg, label);
4440 break;
4441 case kCondBE:
4442 __ Bgeu(rhs_reg, lhs, label);
4443 break;
4444 case kCondA:
4445 __ Bltu(rhs_reg, lhs, label);
4446 break;
4447 }
4448 } else {
4449 // Special cases for more efficient comparison with constants on R2.
4450 switch (cond) {
4451 case kCondEQ:
4452 __ LoadConst32(TMP, rhs_imm);
4453 __ Beq(lhs, TMP, label);
4454 break;
4455 case kCondNE:
4456 __ LoadConst32(TMP, rhs_imm);
4457 __ Bne(lhs, TMP, label);
4458 break;
4459 case kCondLT:
4460 if (IsInt<16>(rhs_imm)) {
4461 __ Slti(TMP, lhs, rhs_imm);
4462 __ Bnez(TMP, label);
4463 } else {
4464 __ LoadConst32(TMP, rhs_imm);
4465 __ Blt(lhs, TMP, label);
4466 }
4467 break;
4468 case kCondGE:
4469 if (IsInt<16>(rhs_imm)) {
4470 __ Slti(TMP, lhs, rhs_imm);
4471 __ Beqz(TMP, label);
4472 } else {
4473 __ LoadConst32(TMP, rhs_imm);
4474 __ Bge(lhs, TMP, label);
4475 }
4476 break;
4477 case kCondLE:
4478 if (IsInt<16>(rhs_imm + 1)) {
4479 // Simulate lhs <= rhs via lhs < rhs + 1.
4480 __ Slti(TMP, lhs, rhs_imm + 1);
4481 __ Bnez(TMP, label);
4482 } else {
4483 __ LoadConst32(TMP, rhs_imm);
4484 __ Bge(TMP, lhs, label);
4485 }
4486 break;
4487 case kCondGT:
4488 if (IsInt<16>(rhs_imm + 1)) {
4489 // Simulate lhs > rhs via !(lhs < rhs + 1).
4490 __ Slti(TMP, lhs, rhs_imm + 1);
4491 __ Beqz(TMP, label);
4492 } else {
4493 __ LoadConst32(TMP, rhs_imm);
4494 __ Blt(TMP, lhs, label);
4495 }
4496 break;
4497 case kCondB:
4498 if (IsInt<16>(rhs_imm)) {
4499 __ Sltiu(TMP, lhs, rhs_imm);
4500 __ Bnez(TMP, label);
4501 } else {
4502 __ LoadConst32(TMP, rhs_imm);
4503 __ Bltu(lhs, TMP, label);
4504 }
4505 break;
4506 case kCondAE:
4507 if (IsInt<16>(rhs_imm)) {
4508 __ Sltiu(TMP, lhs, rhs_imm);
4509 __ Beqz(TMP, label);
4510 } else {
4511 __ LoadConst32(TMP, rhs_imm);
4512 __ Bgeu(lhs, TMP, label);
4513 }
4514 break;
4515 case kCondBE:
4516 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4517 // Simulate lhs <= rhs via lhs < rhs + 1.
4518 // Note that this only works if rhs + 1 does not overflow
4519 // to 0, hence the check above.
4520 __ Sltiu(TMP, lhs, rhs_imm + 1);
4521 __ Bnez(TMP, label);
4522 } else {
4523 __ LoadConst32(TMP, rhs_imm);
4524 __ Bgeu(TMP, lhs, label);
4525 }
4526 break;
4527 case kCondA:
4528 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4529 // Simulate lhs > rhs via !(lhs < rhs + 1).
4530 // Note that this only works if rhs + 1 does not overflow
4531 // to 0, hence the check above.
4532 __ Sltiu(TMP, lhs, rhs_imm + 1);
4533 __ Beqz(TMP, label);
4534 } else {
4535 __ LoadConst32(TMP, rhs_imm);
4536 __ Bltu(TMP, lhs, label);
4537 }
4538 break;
4539 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004540 }
4541 }
4542}
4543
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004544void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4545 LocationSummary* locations) {
4546 Register dst = locations->Out().AsRegister<Register>();
4547 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4548 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4549 Location rhs_location = locations->InAt(1);
4550 Register rhs_high = ZERO;
4551 Register rhs_low = ZERO;
4552 int64_t imm = 0;
4553 uint32_t imm_high = 0;
4554 uint32_t imm_low = 0;
4555 bool use_imm = rhs_location.IsConstant();
4556 if (use_imm) {
4557 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4558 imm_high = High32Bits(imm);
4559 imm_low = Low32Bits(imm);
4560 } else {
4561 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4562 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4563 }
4564 if (use_imm && imm == 0) {
4565 switch (cond) {
4566 case kCondEQ:
4567 case kCondBE: // <= 0 if zero
4568 __ Or(dst, lhs_high, lhs_low);
4569 __ Sltiu(dst, dst, 1);
4570 break;
4571 case kCondNE:
4572 case kCondA: // > 0 if non-zero
4573 __ Or(dst, lhs_high, lhs_low);
4574 __ Sltu(dst, ZERO, dst);
4575 break;
4576 case kCondLT:
4577 __ Slt(dst, lhs_high, ZERO);
4578 break;
4579 case kCondGE:
4580 __ Slt(dst, lhs_high, ZERO);
4581 __ Xori(dst, dst, 1);
4582 break;
4583 case kCondLE:
4584 __ Or(TMP, lhs_high, lhs_low);
4585 __ Sra(AT, lhs_high, 31);
4586 __ Sltu(dst, AT, TMP);
4587 __ Xori(dst, dst, 1);
4588 break;
4589 case kCondGT:
4590 __ Or(TMP, lhs_high, lhs_low);
4591 __ Sra(AT, lhs_high, 31);
4592 __ Sltu(dst, AT, TMP);
4593 break;
4594 case kCondB: // always false
4595 __ Andi(dst, dst, 0);
4596 break;
4597 case kCondAE: // always true
4598 __ Ori(dst, ZERO, 1);
4599 break;
4600 }
4601 } else if (use_imm) {
4602 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4603 switch (cond) {
4604 case kCondEQ:
4605 __ LoadConst32(TMP, imm_high);
4606 __ Xor(TMP, TMP, lhs_high);
4607 __ LoadConst32(AT, imm_low);
4608 __ Xor(AT, AT, lhs_low);
4609 __ Or(dst, TMP, AT);
4610 __ Sltiu(dst, dst, 1);
4611 break;
4612 case kCondNE:
4613 __ LoadConst32(TMP, imm_high);
4614 __ Xor(TMP, TMP, lhs_high);
4615 __ LoadConst32(AT, imm_low);
4616 __ Xor(AT, AT, lhs_low);
4617 __ Or(dst, TMP, AT);
4618 __ Sltu(dst, ZERO, dst);
4619 break;
4620 case kCondLT:
4621 case kCondGE:
4622 if (dst == lhs_low) {
4623 __ LoadConst32(TMP, imm_low);
4624 __ Sltu(dst, lhs_low, TMP);
4625 }
4626 __ LoadConst32(TMP, imm_high);
4627 __ Slt(AT, lhs_high, TMP);
4628 __ Slt(TMP, TMP, lhs_high);
4629 if (dst != lhs_low) {
4630 __ LoadConst32(dst, imm_low);
4631 __ Sltu(dst, lhs_low, dst);
4632 }
4633 __ Slt(dst, TMP, dst);
4634 __ Or(dst, dst, AT);
4635 if (cond == kCondGE) {
4636 __ Xori(dst, dst, 1);
4637 }
4638 break;
4639 case kCondGT:
4640 case kCondLE:
4641 if (dst == lhs_low) {
4642 __ LoadConst32(TMP, imm_low);
4643 __ Sltu(dst, TMP, lhs_low);
4644 }
4645 __ LoadConst32(TMP, imm_high);
4646 __ Slt(AT, TMP, lhs_high);
4647 __ Slt(TMP, lhs_high, TMP);
4648 if (dst != lhs_low) {
4649 __ LoadConst32(dst, imm_low);
4650 __ Sltu(dst, dst, lhs_low);
4651 }
4652 __ Slt(dst, TMP, dst);
4653 __ Or(dst, dst, AT);
4654 if (cond == kCondLE) {
4655 __ Xori(dst, dst, 1);
4656 }
4657 break;
4658 case kCondB:
4659 case kCondAE:
4660 if (dst == lhs_low) {
4661 __ LoadConst32(TMP, imm_low);
4662 __ Sltu(dst, lhs_low, TMP);
4663 }
4664 __ LoadConst32(TMP, imm_high);
4665 __ Sltu(AT, lhs_high, TMP);
4666 __ Sltu(TMP, TMP, lhs_high);
4667 if (dst != lhs_low) {
4668 __ LoadConst32(dst, imm_low);
4669 __ Sltu(dst, lhs_low, dst);
4670 }
4671 __ Slt(dst, TMP, dst);
4672 __ Or(dst, dst, AT);
4673 if (cond == kCondAE) {
4674 __ Xori(dst, dst, 1);
4675 }
4676 break;
4677 case kCondA:
4678 case kCondBE:
4679 if (dst == lhs_low) {
4680 __ LoadConst32(TMP, imm_low);
4681 __ Sltu(dst, TMP, lhs_low);
4682 }
4683 __ LoadConst32(TMP, imm_high);
4684 __ Sltu(AT, TMP, lhs_high);
4685 __ Sltu(TMP, lhs_high, TMP);
4686 if (dst != lhs_low) {
4687 __ LoadConst32(dst, imm_low);
4688 __ Sltu(dst, dst, lhs_low);
4689 }
4690 __ Slt(dst, TMP, dst);
4691 __ Or(dst, dst, AT);
4692 if (cond == kCondBE) {
4693 __ Xori(dst, dst, 1);
4694 }
4695 break;
4696 }
4697 } else {
4698 switch (cond) {
4699 case kCondEQ:
4700 __ Xor(TMP, lhs_high, rhs_high);
4701 __ Xor(AT, lhs_low, rhs_low);
4702 __ Or(dst, TMP, AT);
4703 __ Sltiu(dst, dst, 1);
4704 break;
4705 case kCondNE:
4706 __ Xor(TMP, lhs_high, rhs_high);
4707 __ Xor(AT, lhs_low, rhs_low);
4708 __ Or(dst, TMP, AT);
4709 __ Sltu(dst, ZERO, dst);
4710 break;
4711 case kCondLT:
4712 case kCondGE:
4713 __ Slt(TMP, rhs_high, lhs_high);
4714 __ Sltu(AT, lhs_low, rhs_low);
4715 __ Slt(TMP, TMP, AT);
4716 __ Slt(AT, lhs_high, rhs_high);
4717 __ Or(dst, AT, TMP);
4718 if (cond == kCondGE) {
4719 __ Xori(dst, dst, 1);
4720 }
4721 break;
4722 case kCondGT:
4723 case kCondLE:
4724 __ Slt(TMP, lhs_high, rhs_high);
4725 __ Sltu(AT, rhs_low, lhs_low);
4726 __ Slt(TMP, TMP, AT);
4727 __ Slt(AT, rhs_high, lhs_high);
4728 __ Or(dst, AT, TMP);
4729 if (cond == kCondLE) {
4730 __ Xori(dst, dst, 1);
4731 }
4732 break;
4733 case kCondB:
4734 case kCondAE:
4735 __ Sltu(TMP, rhs_high, lhs_high);
4736 __ Sltu(AT, lhs_low, rhs_low);
4737 __ Slt(TMP, TMP, AT);
4738 __ Sltu(AT, lhs_high, rhs_high);
4739 __ Or(dst, AT, TMP);
4740 if (cond == kCondAE) {
4741 __ Xori(dst, dst, 1);
4742 }
4743 break;
4744 case kCondA:
4745 case kCondBE:
4746 __ Sltu(TMP, lhs_high, rhs_high);
4747 __ Sltu(AT, rhs_low, lhs_low);
4748 __ Slt(TMP, TMP, AT);
4749 __ Sltu(AT, rhs_high, lhs_high);
4750 __ Or(dst, AT, TMP);
4751 if (cond == kCondBE) {
4752 __ Xori(dst, dst, 1);
4753 }
4754 break;
4755 }
4756 }
4757}
4758
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004759void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4760 LocationSummary* locations,
4761 MipsLabel* label) {
4762 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4763 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4764 Location rhs_location = locations->InAt(1);
4765 Register rhs_high = ZERO;
4766 Register rhs_low = ZERO;
4767 int64_t imm = 0;
4768 uint32_t imm_high = 0;
4769 uint32_t imm_low = 0;
4770 bool use_imm = rhs_location.IsConstant();
4771 if (use_imm) {
4772 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4773 imm_high = High32Bits(imm);
4774 imm_low = Low32Bits(imm);
4775 } else {
4776 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4777 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4778 }
4779
4780 if (use_imm && imm == 0) {
4781 switch (cond) {
4782 case kCondEQ:
4783 case kCondBE: // <= 0 if zero
4784 __ Or(TMP, lhs_high, lhs_low);
4785 __ Beqz(TMP, label);
4786 break;
4787 case kCondNE:
4788 case kCondA: // > 0 if non-zero
4789 __ Or(TMP, lhs_high, lhs_low);
4790 __ Bnez(TMP, label);
4791 break;
4792 case kCondLT:
4793 __ Bltz(lhs_high, label);
4794 break;
4795 case kCondGE:
4796 __ Bgez(lhs_high, label);
4797 break;
4798 case kCondLE:
4799 __ Or(TMP, lhs_high, lhs_low);
4800 __ Sra(AT, lhs_high, 31);
4801 __ Bgeu(AT, TMP, label);
4802 break;
4803 case kCondGT:
4804 __ Or(TMP, lhs_high, lhs_low);
4805 __ Sra(AT, lhs_high, 31);
4806 __ Bltu(AT, TMP, label);
4807 break;
4808 case kCondB: // always false
4809 break;
4810 case kCondAE: // always true
4811 __ B(label);
4812 break;
4813 }
4814 } else if (use_imm) {
4815 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4816 switch (cond) {
4817 case kCondEQ:
4818 __ LoadConst32(TMP, imm_high);
4819 __ Xor(TMP, TMP, lhs_high);
4820 __ LoadConst32(AT, imm_low);
4821 __ Xor(AT, AT, lhs_low);
4822 __ Or(TMP, TMP, AT);
4823 __ Beqz(TMP, label);
4824 break;
4825 case kCondNE:
4826 __ LoadConst32(TMP, imm_high);
4827 __ Xor(TMP, TMP, lhs_high);
4828 __ LoadConst32(AT, imm_low);
4829 __ Xor(AT, AT, lhs_low);
4830 __ Or(TMP, TMP, AT);
4831 __ Bnez(TMP, label);
4832 break;
4833 case kCondLT:
4834 __ LoadConst32(TMP, imm_high);
4835 __ Blt(lhs_high, TMP, label);
4836 __ Slt(TMP, TMP, lhs_high);
4837 __ LoadConst32(AT, imm_low);
4838 __ Sltu(AT, lhs_low, AT);
4839 __ Blt(TMP, AT, label);
4840 break;
4841 case kCondGE:
4842 __ LoadConst32(TMP, imm_high);
4843 __ Blt(TMP, lhs_high, label);
4844 __ Slt(TMP, lhs_high, TMP);
4845 __ LoadConst32(AT, imm_low);
4846 __ Sltu(AT, lhs_low, AT);
4847 __ Or(TMP, TMP, AT);
4848 __ Beqz(TMP, label);
4849 break;
4850 case kCondLE:
4851 __ LoadConst32(TMP, imm_high);
4852 __ Blt(lhs_high, TMP, label);
4853 __ Slt(TMP, TMP, lhs_high);
4854 __ LoadConst32(AT, imm_low);
4855 __ Sltu(AT, AT, lhs_low);
4856 __ Or(TMP, TMP, AT);
4857 __ Beqz(TMP, label);
4858 break;
4859 case kCondGT:
4860 __ LoadConst32(TMP, imm_high);
4861 __ Blt(TMP, lhs_high, label);
4862 __ Slt(TMP, lhs_high, TMP);
4863 __ LoadConst32(AT, imm_low);
4864 __ Sltu(AT, AT, lhs_low);
4865 __ Blt(TMP, AT, label);
4866 break;
4867 case kCondB:
4868 __ LoadConst32(TMP, imm_high);
4869 __ Bltu(lhs_high, TMP, label);
4870 __ Sltu(TMP, TMP, lhs_high);
4871 __ LoadConst32(AT, imm_low);
4872 __ Sltu(AT, lhs_low, AT);
4873 __ Blt(TMP, AT, label);
4874 break;
4875 case kCondAE:
4876 __ LoadConst32(TMP, imm_high);
4877 __ Bltu(TMP, lhs_high, label);
4878 __ Sltu(TMP, lhs_high, TMP);
4879 __ LoadConst32(AT, imm_low);
4880 __ Sltu(AT, lhs_low, AT);
4881 __ Or(TMP, TMP, AT);
4882 __ Beqz(TMP, label);
4883 break;
4884 case kCondBE:
4885 __ LoadConst32(TMP, imm_high);
4886 __ Bltu(lhs_high, TMP, label);
4887 __ Sltu(TMP, TMP, lhs_high);
4888 __ LoadConst32(AT, imm_low);
4889 __ Sltu(AT, AT, lhs_low);
4890 __ Or(TMP, TMP, AT);
4891 __ Beqz(TMP, label);
4892 break;
4893 case kCondA:
4894 __ LoadConst32(TMP, imm_high);
4895 __ Bltu(TMP, lhs_high, label);
4896 __ Sltu(TMP, lhs_high, TMP);
4897 __ LoadConst32(AT, imm_low);
4898 __ Sltu(AT, AT, lhs_low);
4899 __ Blt(TMP, AT, label);
4900 break;
4901 }
4902 } else {
4903 switch (cond) {
4904 case kCondEQ:
4905 __ Xor(TMP, lhs_high, rhs_high);
4906 __ Xor(AT, lhs_low, rhs_low);
4907 __ Or(TMP, TMP, AT);
4908 __ Beqz(TMP, label);
4909 break;
4910 case kCondNE:
4911 __ Xor(TMP, lhs_high, rhs_high);
4912 __ Xor(AT, lhs_low, rhs_low);
4913 __ Or(TMP, TMP, AT);
4914 __ Bnez(TMP, label);
4915 break;
4916 case kCondLT:
4917 __ Blt(lhs_high, rhs_high, label);
4918 __ Slt(TMP, rhs_high, lhs_high);
4919 __ Sltu(AT, lhs_low, rhs_low);
4920 __ Blt(TMP, AT, label);
4921 break;
4922 case kCondGE:
4923 __ Blt(rhs_high, lhs_high, label);
4924 __ Slt(TMP, lhs_high, rhs_high);
4925 __ Sltu(AT, lhs_low, rhs_low);
4926 __ Or(TMP, TMP, AT);
4927 __ Beqz(TMP, label);
4928 break;
4929 case kCondLE:
4930 __ Blt(lhs_high, rhs_high, label);
4931 __ Slt(TMP, rhs_high, lhs_high);
4932 __ Sltu(AT, rhs_low, lhs_low);
4933 __ Or(TMP, TMP, AT);
4934 __ Beqz(TMP, label);
4935 break;
4936 case kCondGT:
4937 __ Blt(rhs_high, lhs_high, label);
4938 __ Slt(TMP, lhs_high, rhs_high);
4939 __ Sltu(AT, rhs_low, lhs_low);
4940 __ Blt(TMP, AT, label);
4941 break;
4942 case kCondB:
4943 __ Bltu(lhs_high, rhs_high, label);
4944 __ Sltu(TMP, rhs_high, lhs_high);
4945 __ Sltu(AT, lhs_low, rhs_low);
4946 __ Blt(TMP, AT, label);
4947 break;
4948 case kCondAE:
4949 __ Bltu(rhs_high, lhs_high, label);
4950 __ Sltu(TMP, lhs_high, rhs_high);
4951 __ Sltu(AT, lhs_low, rhs_low);
4952 __ Or(TMP, TMP, AT);
4953 __ Beqz(TMP, label);
4954 break;
4955 case kCondBE:
4956 __ Bltu(lhs_high, rhs_high, label);
4957 __ Sltu(TMP, rhs_high, lhs_high);
4958 __ Sltu(AT, rhs_low, lhs_low);
4959 __ Or(TMP, TMP, AT);
4960 __ Beqz(TMP, label);
4961 break;
4962 case kCondA:
4963 __ Bltu(rhs_high, lhs_high, label);
4964 __ Sltu(TMP, lhs_high, rhs_high);
4965 __ Sltu(AT, rhs_low, lhs_low);
4966 __ Blt(TMP, AT, label);
4967 break;
4968 }
4969 }
4970}
4971
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004972void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4973 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004974 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004975 LocationSummary* locations) {
4976 Register dst = locations->Out().AsRegister<Register>();
4977 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4978 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4979 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004980 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004981 if (isR6) {
4982 switch (cond) {
4983 case kCondEQ:
4984 __ CmpEqS(FTMP, lhs, rhs);
4985 __ Mfc1(dst, FTMP);
4986 __ Andi(dst, dst, 1);
4987 break;
4988 case kCondNE:
4989 __ CmpEqS(FTMP, lhs, rhs);
4990 __ Mfc1(dst, FTMP);
4991 __ Addiu(dst, dst, 1);
4992 break;
4993 case kCondLT:
4994 if (gt_bias) {
4995 __ CmpLtS(FTMP, lhs, rhs);
4996 } else {
4997 __ CmpUltS(FTMP, lhs, rhs);
4998 }
4999 __ Mfc1(dst, FTMP);
5000 __ Andi(dst, dst, 1);
5001 break;
5002 case kCondLE:
5003 if (gt_bias) {
5004 __ CmpLeS(FTMP, lhs, rhs);
5005 } else {
5006 __ CmpUleS(FTMP, lhs, rhs);
5007 }
5008 __ Mfc1(dst, FTMP);
5009 __ Andi(dst, dst, 1);
5010 break;
5011 case kCondGT:
5012 if (gt_bias) {
5013 __ CmpUltS(FTMP, rhs, lhs);
5014 } else {
5015 __ CmpLtS(FTMP, rhs, lhs);
5016 }
5017 __ Mfc1(dst, FTMP);
5018 __ Andi(dst, dst, 1);
5019 break;
5020 case kCondGE:
5021 if (gt_bias) {
5022 __ CmpUleS(FTMP, rhs, lhs);
5023 } else {
5024 __ CmpLeS(FTMP, rhs, lhs);
5025 }
5026 __ Mfc1(dst, FTMP);
5027 __ Andi(dst, dst, 1);
5028 break;
5029 default:
5030 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5031 UNREACHABLE();
5032 }
5033 } else {
5034 switch (cond) {
5035 case kCondEQ:
5036 __ CeqS(0, lhs, rhs);
5037 __ LoadConst32(dst, 1);
5038 __ Movf(dst, ZERO, 0);
5039 break;
5040 case kCondNE:
5041 __ CeqS(0, lhs, rhs);
5042 __ LoadConst32(dst, 1);
5043 __ Movt(dst, ZERO, 0);
5044 break;
5045 case kCondLT:
5046 if (gt_bias) {
5047 __ ColtS(0, lhs, rhs);
5048 } else {
5049 __ CultS(0, lhs, rhs);
5050 }
5051 __ LoadConst32(dst, 1);
5052 __ Movf(dst, ZERO, 0);
5053 break;
5054 case kCondLE:
5055 if (gt_bias) {
5056 __ ColeS(0, lhs, rhs);
5057 } else {
5058 __ CuleS(0, lhs, rhs);
5059 }
5060 __ LoadConst32(dst, 1);
5061 __ Movf(dst, ZERO, 0);
5062 break;
5063 case kCondGT:
5064 if (gt_bias) {
5065 __ CultS(0, rhs, lhs);
5066 } else {
5067 __ ColtS(0, rhs, lhs);
5068 }
5069 __ LoadConst32(dst, 1);
5070 __ Movf(dst, ZERO, 0);
5071 break;
5072 case kCondGE:
5073 if (gt_bias) {
5074 __ CuleS(0, rhs, lhs);
5075 } else {
5076 __ ColeS(0, rhs, lhs);
5077 }
5078 __ LoadConst32(dst, 1);
5079 __ Movf(dst, ZERO, 0);
5080 break;
5081 default:
5082 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5083 UNREACHABLE();
5084 }
5085 }
5086 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005087 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005088 if (isR6) {
5089 switch (cond) {
5090 case kCondEQ:
5091 __ CmpEqD(FTMP, lhs, rhs);
5092 __ Mfc1(dst, FTMP);
5093 __ Andi(dst, dst, 1);
5094 break;
5095 case kCondNE:
5096 __ CmpEqD(FTMP, lhs, rhs);
5097 __ Mfc1(dst, FTMP);
5098 __ Addiu(dst, dst, 1);
5099 break;
5100 case kCondLT:
5101 if (gt_bias) {
5102 __ CmpLtD(FTMP, lhs, rhs);
5103 } else {
5104 __ CmpUltD(FTMP, lhs, rhs);
5105 }
5106 __ Mfc1(dst, FTMP);
5107 __ Andi(dst, dst, 1);
5108 break;
5109 case kCondLE:
5110 if (gt_bias) {
5111 __ CmpLeD(FTMP, lhs, rhs);
5112 } else {
5113 __ CmpUleD(FTMP, lhs, rhs);
5114 }
5115 __ Mfc1(dst, FTMP);
5116 __ Andi(dst, dst, 1);
5117 break;
5118 case kCondGT:
5119 if (gt_bias) {
5120 __ CmpUltD(FTMP, rhs, lhs);
5121 } else {
5122 __ CmpLtD(FTMP, rhs, lhs);
5123 }
5124 __ Mfc1(dst, FTMP);
5125 __ Andi(dst, dst, 1);
5126 break;
5127 case kCondGE:
5128 if (gt_bias) {
5129 __ CmpUleD(FTMP, rhs, lhs);
5130 } else {
5131 __ CmpLeD(FTMP, rhs, lhs);
5132 }
5133 __ Mfc1(dst, FTMP);
5134 __ Andi(dst, dst, 1);
5135 break;
5136 default:
5137 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5138 UNREACHABLE();
5139 }
5140 } else {
5141 switch (cond) {
5142 case kCondEQ:
5143 __ CeqD(0, lhs, rhs);
5144 __ LoadConst32(dst, 1);
5145 __ Movf(dst, ZERO, 0);
5146 break;
5147 case kCondNE:
5148 __ CeqD(0, lhs, rhs);
5149 __ LoadConst32(dst, 1);
5150 __ Movt(dst, ZERO, 0);
5151 break;
5152 case kCondLT:
5153 if (gt_bias) {
5154 __ ColtD(0, lhs, rhs);
5155 } else {
5156 __ CultD(0, lhs, rhs);
5157 }
5158 __ LoadConst32(dst, 1);
5159 __ Movf(dst, ZERO, 0);
5160 break;
5161 case kCondLE:
5162 if (gt_bias) {
5163 __ ColeD(0, lhs, rhs);
5164 } else {
5165 __ CuleD(0, lhs, rhs);
5166 }
5167 __ LoadConst32(dst, 1);
5168 __ Movf(dst, ZERO, 0);
5169 break;
5170 case kCondGT:
5171 if (gt_bias) {
5172 __ CultD(0, rhs, lhs);
5173 } else {
5174 __ ColtD(0, rhs, lhs);
5175 }
5176 __ LoadConst32(dst, 1);
5177 __ Movf(dst, ZERO, 0);
5178 break;
5179 case kCondGE:
5180 if (gt_bias) {
5181 __ CuleD(0, rhs, lhs);
5182 } else {
5183 __ ColeD(0, rhs, lhs);
5184 }
5185 __ LoadConst32(dst, 1);
5186 __ Movf(dst, ZERO, 0);
5187 break;
5188 default:
5189 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5190 UNREACHABLE();
5191 }
5192 }
5193 }
5194}
5195
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005196bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5197 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005198 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005199 LocationSummary* input_locations,
5200 int cc) {
5201 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5202 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5203 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005204 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005205 switch (cond) {
5206 case kCondEQ:
5207 __ CeqS(cc, lhs, rhs);
5208 return false;
5209 case kCondNE:
5210 __ CeqS(cc, lhs, rhs);
5211 return true;
5212 case kCondLT:
5213 if (gt_bias) {
5214 __ ColtS(cc, lhs, rhs);
5215 } else {
5216 __ CultS(cc, lhs, rhs);
5217 }
5218 return false;
5219 case kCondLE:
5220 if (gt_bias) {
5221 __ ColeS(cc, lhs, rhs);
5222 } else {
5223 __ CuleS(cc, lhs, rhs);
5224 }
5225 return false;
5226 case kCondGT:
5227 if (gt_bias) {
5228 __ CultS(cc, rhs, lhs);
5229 } else {
5230 __ ColtS(cc, rhs, lhs);
5231 }
5232 return false;
5233 case kCondGE:
5234 if (gt_bias) {
5235 __ CuleS(cc, rhs, lhs);
5236 } else {
5237 __ ColeS(cc, rhs, lhs);
5238 }
5239 return false;
5240 default:
5241 LOG(FATAL) << "Unexpected non-floating-point condition";
5242 UNREACHABLE();
5243 }
5244 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005245 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005246 switch (cond) {
5247 case kCondEQ:
5248 __ CeqD(cc, lhs, rhs);
5249 return false;
5250 case kCondNE:
5251 __ CeqD(cc, lhs, rhs);
5252 return true;
5253 case kCondLT:
5254 if (gt_bias) {
5255 __ ColtD(cc, lhs, rhs);
5256 } else {
5257 __ CultD(cc, lhs, rhs);
5258 }
5259 return false;
5260 case kCondLE:
5261 if (gt_bias) {
5262 __ ColeD(cc, lhs, rhs);
5263 } else {
5264 __ CuleD(cc, lhs, rhs);
5265 }
5266 return false;
5267 case kCondGT:
5268 if (gt_bias) {
5269 __ CultD(cc, rhs, lhs);
5270 } else {
5271 __ ColtD(cc, rhs, lhs);
5272 }
5273 return false;
5274 case kCondGE:
5275 if (gt_bias) {
5276 __ CuleD(cc, rhs, lhs);
5277 } else {
5278 __ ColeD(cc, rhs, lhs);
5279 }
5280 return false;
5281 default:
5282 LOG(FATAL) << "Unexpected non-floating-point condition";
5283 UNREACHABLE();
5284 }
5285 }
5286}
5287
5288bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5289 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005290 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005291 LocationSummary* input_locations,
5292 FRegister dst) {
5293 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5294 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5295 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005296 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005297 switch (cond) {
5298 case kCondEQ:
5299 __ CmpEqS(dst, lhs, rhs);
5300 return false;
5301 case kCondNE:
5302 __ CmpEqS(dst, lhs, rhs);
5303 return true;
5304 case kCondLT:
5305 if (gt_bias) {
5306 __ CmpLtS(dst, lhs, rhs);
5307 } else {
5308 __ CmpUltS(dst, lhs, rhs);
5309 }
5310 return false;
5311 case kCondLE:
5312 if (gt_bias) {
5313 __ CmpLeS(dst, lhs, rhs);
5314 } else {
5315 __ CmpUleS(dst, lhs, rhs);
5316 }
5317 return false;
5318 case kCondGT:
5319 if (gt_bias) {
5320 __ CmpUltS(dst, rhs, lhs);
5321 } else {
5322 __ CmpLtS(dst, rhs, lhs);
5323 }
5324 return false;
5325 case kCondGE:
5326 if (gt_bias) {
5327 __ CmpUleS(dst, rhs, lhs);
5328 } else {
5329 __ CmpLeS(dst, rhs, lhs);
5330 }
5331 return false;
5332 default:
5333 LOG(FATAL) << "Unexpected non-floating-point condition";
5334 UNREACHABLE();
5335 }
5336 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005338 switch (cond) {
5339 case kCondEQ:
5340 __ CmpEqD(dst, lhs, rhs);
5341 return false;
5342 case kCondNE:
5343 __ CmpEqD(dst, lhs, rhs);
5344 return true;
5345 case kCondLT:
5346 if (gt_bias) {
5347 __ CmpLtD(dst, lhs, rhs);
5348 } else {
5349 __ CmpUltD(dst, lhs, rhs);
5350 }
5351 return false;
5352 case kCondLE:
5353 if (gt_bias) {
5354 __ CmpLeD(dst, lhs, rhs);
5355 } else {
5356 __ CmpUleD(dst, lhs, rhs);
5357 }
5358 return false;
5359 case kCondGT:
5360 if (gt_bias) {
5361 __ CmpUltD(dst, rhs, lhs);
5362 } else {
5363 __ CmpLtD(dst, rhs, lhs);
5364 }
5365 return false;
5366 case kCondGE:
5367 if (gt_bias) {
5368 __ CmpUleD(dst, rhs, lhs);
5369 } else {
5370 __ CmpLeD(dst, rhs, lhs);
5371 }
5372 return false;
5373 default:
5374 LOG(FATAL) << "Unexpected non-floating-point condition";
5375 UNREACHABLE();
5376 }
5377 }
5378}
5379
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005380void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5381 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005383 LocationSummary* locations,
5384 MipsLabel* label) {
5385 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5386 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5387 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005388 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005389 if (isR6) {
5390 switch (cond) {
5391 case kCondEQ:
5392 __ CmpEqS(FTMP, lhs, rhs);
5393 __ Bc1nez(FTMP, label);
5394 break;
5395 case kCondNE:
5396 __ CmpEqS(FTMP, lhs, rhs);
5397 __ Bc1eqz(FTMP, label);
5398 break;
5399 case kCondLT:
5400 if (gt_bias) {
5401 __ CmpLtS(FTMP, lhs, rhs);
5402 } else {
5403 __ CmpUltS(FTMP, lhs, rhs);
5404 }
5405 __ Bc1nez(FTMP, label);
5406 break;
5407 case kCondLE:
5408 if (gt_bias) {
5409 __ CmpLeS(FTMP, lhs, rhs);
5410 } else {
5411 __ CmpUleS(FTMP, lhs, rhs);
5412 }
5413 __ Bc1nez(FTMP, label);
5414 break;
5415 case kCondGT:
5416 if (gt_bias) {
5417 __ CmpUltS(FTMP, rhs, lhs);
5418 } else {
5419 __ CmpLtS(FTMP, rhs, lhs);
5420 }
5421 __ Bc1nez(FTMP, label);
5422 break;
5423 case kCondGE:
5424 if (gt_bias) {
5425 __ CmpUleS(FTMP, rhs, lhs);
5426 } else {
5427 __ CmpLeS(FTMP, rhs, lhs);
5428 }
5429 __ Bc1nez(FTMP, label);
5430 break;
5431 default:
5432 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005433 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005434 }
5435 } else {
5436 switch (cond) {
5437 case kCondEQ:
5438 __ CeqS(0, lhs, rhs);
5439 __ Bc1t(0, label);
5440 break;
5441 case kCondNE:
5442 __ CeqS(0, lhs, rhs);
5443 __ Bc1f(0, label);
5444 break;
5445 case kCondLT:
5446 if (gt_bias) {
5447 __ ColtS(0, lhs, rhs);
5448 } else {
5449 __ CultS(0, lhs, rhs);
5450 }
5451 __ Bc1t(0, label);
5452 break;
5453 case kCondLE:
5454 if (gt_bias) {
5455 __ ColeS(0, lhs, rhs);
5456 } else {
5457 __ CuleS(0, lhs, rhs);
5458 }
5459 __ Bc1t(0, label);
5460 break;
5461 case kCondGT:
5462 if (gt_bias) {
5463 __ CultS(0, rhs, lhs);
5464 } else {
5465 __ ColtS(0, rhs, lhs);
5466 }
5467 __ Bc1t(0, label);
5468 break;
5469 case kCondGE:
5470 if (gt_bias) {
5471 __ CuleS(0, rhs, lhs);
5472 } else {
5473 __ ColeS(0, rhs, lhs);
5474 }
5475 __ Bc1t(0, label);
5476 break;
5477 default:
5478 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005479 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005480 }
5481 }
5482 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005483 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005484 if (isR6) {
5485 switch (cond) {
5486 case kCondEQ:
5487 __ CmpEqD(FTMP, lhs, rhs);
5488 __ Bc1nez(FTMP, label);
5489 break;
5490 case kCondNE:
5491 __ CmpEqD(FTMP, lhs, rhs);
5492 __ Bc1eqz(FTMP, label);
5493 break;
5494 case kCondLT:
5495 if (gt_bias) {
5496 __ CmpLtD(FTMP, lhs, rhs);
5497 } else {
5498 __ CmpUltD(FTMP, lhs, rhs);
5499 }
5500 __ Bc1nez(FTMP, label);
5501 break;
5502 case kCondLE:
5503 if (gt_bias) {
5504 __ CmpLeD(FTMP, lhs, rhs);
5505 } else {
5506 __ CmpUleD(FTMP, lhs, rhs);
5507 }
5508 __ Bc1nez(FTMP, label);
5509 break;
5510 case kCondGT:
5511 if (gt_bias) {
5512 __ CmpUltD(FTMP, rhs, lhs);
5513 } else {
5514 __ CmpLtD(FTMP, rhs, lhs);
5515 }
5516 __ Bc1nez(FTMP, label);
5517 break;
5518 case kCondGE:
5519 if (gt_bias) {
5520 __ CmpUleD(FTMP, rhs, lhs);
5521 } else {
5522 __ CmpLeD(FTMP, rhs, lhs);
5523 }
5524 __ Bc1nez(FTMP, label);
5525 break;
5526 default:
5527 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005528 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005529 }
5530 } else {
5531 switch (cond) {
5532 case kCondEQ:
5533 __ CeqD(0, lhs, rhs);
5534 __ Bc1t(0, label);
5535 break;
5536 case kCondNE:
5537 __ CeqD(0, lhs, rhs);
5538 __ Bc1f(0, label);
5539 break;
5540 case kCondLT:
5541 if (gt_bias) {
5542 __ ColtD(0, lhs, rhs);
5543 } else {
5544 __ CultD(0, lhs, rhs);
5545 }
5546 __ Bc1t(0, label);
5547 break;
5548 case kCondLE:
5549 if (gt_bias) {
5550 __ ColeD(0, lhs, rhs);
5551 } else {
5552 __ CuleD(0, lhs, rhs);
5553 }
5554 __ Bc1t(0, label);
5555 break;
5556 case kCondGT:
5557 if (gt_bias) {
5558 __ CultD(0, rhs, lhs);
5559 } else {
5560 __ ColtD(0, rhs, lhs);
5561 }
5562 __ Bc1t(0, label);
5563 break;
5564 case kCondGE:
5565 if (gt_bias) {
5566 __ CuleD(0, rhs, lhs);
5567 } else {
5568 __ ColeD(0, rhs, lhs);
5569 }
5570 __ Bc1t(0, label);
5571 break;
5572 default:
5573 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005574 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005575 }
5576 }
5577 }
5578}
5579
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005581 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005582 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005583 MipsLabel* false_target) {
5584 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005585
David Brazdil0debae72015-11-12 18:37:00 +00005586 if (true_target == nullptr && false_target == nullptr) {
5587 // Nothing to do. The code always falls through.
5588 return;
5589 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005590 // Constant condition, statically compared against "true" (integer value 1).
5591 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005592 if (true_target != nullptr) {
5593 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005594 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005595 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005596 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005597 if (false_target != nullptr) {
5598 __ B(false_target);
5599 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005600 }
David Brazdil0debae72015-11-12 18:37:00 +00005601 return;
5602 }
5603
5604 // The following code generates these patterns:
5605 // (1) true_target == nullptr && false_target != nullptr
5606 // - opposite condition true => branch to false_target
5607 // (2) true_target != nullptr && false_target == nullptr
5608 // - condition true => branch to true_target
5609 // (3) true_target != nullptr && false_target != nullptr
5610 // - condition true => branch to true_target
5611 // - branch to false_target
5612 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005613 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005614 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005615 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005616 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005617 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5618 } else {
5619 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5620 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005621 } else {
5622 // The condition instruction has not been materialized, use its inputs as
5623 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005624 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005626 LocationSummary* locations = cond->GetLocations();
5627 IfCondition if_cond = condition->GetCondition();
5628 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005629
David Brazdil0debae72015-11-12 18:37:00 +00005630 if (true_target == nullptr) {
5631 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005632 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005633 }
5634
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005635 switch (type) {
5636 default:
5637 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5638 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005639 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005640 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5641 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005642 case DataType::Type::kFloat32:
5643 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005644 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5645 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005646 }
5647 }
David Brazdil0debae72015-11-12 18:37:00 +00005648
5649 // If neither branch falls through (case 3), the conditional branch to `true_target`
5650 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5651 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005652 __ B(false_target);
5653 }
5654}
5655
5656void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005657 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005658 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005659 locations->SetInAt(0, Location::RequiresRegister());
5660 }
5661}
5662
5663void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005664 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5665 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5666 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5667 nullptr : codegen_->GetLabelOf(true_successor);
5668 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5669 nullptr : codegen_->GetLabelOf(false_successor);
5670 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005671}
5672
5673void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005674 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005675 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005676 InvokeRuntimeCallingConvention calling_convention;
5677 RegisterSet caller_saves = RegisterSet::Empty();
5678 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5679 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005680 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005681 locations->SetInAt(0, Location::RequiresRegister());
5682 }
5683}
5684
5685void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005686 SlowPathCodeMIPS* slow_path =
5687 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005688 GenerateTestAndBranch(deoptimize,
5689 /* condition_input_index */ 0,
5690 slow_path->GetEntryLabel(),
5691 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005692}
5693
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005694// This function returns true if a conditional move can be generated for HSelect.
5695// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5696// branches and regular moves.
5697//
5698// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5699//
5700// While determining feasibility of a conditional move and setting inputs/outputs
5701// are two distinct tasks, this function does both because they share quite a bit
5702// of common logic.
5703static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5704 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5705 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5706 HCondition* condition = cond->AsCondition();
5707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 DataType::Type cond_type =
5709 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5710 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005711
5712 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5713 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5714 bool is_true_value_zero_constant =
5715 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5716 bool is_false_value_zero_constant =
5717 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5718
5719 bool can_move_conditionally = false;
5720 bool use_const_for_false_in = false;
5721 bool use_const_for_true_in = false;
5722
5723 if (!cond->IsConstant()) {
5724 switch (cond_type) {
5725 default:
5726 switch (dst_type) {
5727 default:
5728 // Moving int on int condition.
5729 if (is_r6) {
5730 if (is_true_value_zero_constant) {
5731 // seleqz out_reg, false_reg, cond_reg
5732 can_move_conditionally = true;
5733 use_const_for_true_in = true;
5734 } else if (is_false_value_zero_constant) {
5735 // selnez out_reg, true_reg, cond_reg
5736 can_move_conditionally = true;
5737 use_const_for_false_in = true;
5738 } else if (materialized) {
5739 // Not materializing unmaterialized int conditions
5740 // to keep the instruction count low.
5741 // selnez AT, true_reg, cond_reg
5742 // seleqz TMP, false_reg, cond_reg
5743 // or out_reg, AT, TMP
5744 can_move_conditionally = true;
5745 }
5746 } else {
5747 // movn out_reg, true_reg/ZERO, cond_reg
5748 can_move_conditionally = true;
5749 use_const_for_true_in = is_true_value_zero_constant;
5750 }
5751 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005753 // Moving long on int condition.
5754 if (is_r6) {
5755 if (is_true_value_zero_constant) {
5756 // seleqz out_reg_lo, false_reg_lo, cond_reg
5757 // seleqz out_reg_hi, false_reg_hi, cond_reg
5758 can_move_conditionally = true;
5759 use_const_for_true_in = true;
5760 } else if (is_false_value_zero_constant) {
5761 // selnez out_reg_lo, true_reg_lo, cond_reg
5762 // selnez out_reg_hi, true_reg_hi, cond_reg
5763 can_move_conditionally = true;
5764 use_const_for_false_in = true;
5765 }
5766 // Other long conditional moves would generate 6+ instructions,
5767 // which is too many.
5768 } else {
5769 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5770 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5771 can_move_conditionally = true;
5772 use_const_for_true_in = is_true_value_zero_constant;
5773 }
5774 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 case DataType::Type::kFloat32:
5776 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005777 // Moving float/double on int condition.
5778 if (is_r6) {
5779 if (materialized) {
5780 // Not materializing unmaterialized int conditions
5781 // to keep the instruction count low.
5782 can_move_conditionally = true;
5783 if (is_true_value_zero_constant) {
5784 // sltu TMP, ZERO, cond_reg
5785 // mtc1 TMP, temp_cond_reg
5786 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5787 use_const_for_true_in = true;
5788 } else if (is_false_value_zero_constant) {
5789 // sltu TMP, ZERO, cond_reg
5790 // mtc1 TMP, temp_cond_reg
5791 // selnez.fmt out_reg, true_reg, temp_cond_reg
5792 use_const_for_false_in = true;
5793 } else {
5794 // sltu TMP, ZERO, cond_reg
5795 // mtc1 TMP, temp_cond_reg
5796 // sel.fmt temp_cond_reg, false_reg, true_reg
5797 // mov.fmt out_reg, temp_cond_reg
5798 }
5799 }
5800 } else {
5801 // movn.fmt out_reg, true_reg, cond_reg
5802 can_move_conditionally = true;
5803 }
5804 break;
5805 }
5806 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005808 // We don't materialize long comparison now
5809 // and use conditional branches instead.
5810 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005811 case DataType::Type::kFloat32:
5812 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005813 switch (dst_type) {
5814 default:
5815 // Moving int on float/double condition.
5816 if (is_r6) {
5817 if (is_true_value_zero_constant) {
5818 // mfc1 TMP, temp_cond_reg
5819 // seleqz out_reg, false_reg, TMP
5820 can_move_conditionally = true;
5821 use_const_for_true_in = true;
5822 } else if (is_false_value_zero_constant) {
5823 // mfc1 TMP, temp_cond_reg
5824 // selnez out_reg, true_reg, TMP
5825 can_move_conditionally = true;
5826 use_const_for_false_in = true;
5827 } else {
5828 // mfc1 TMP, temp_cond_reg
5829 // selnez AT, true_reg, TMP
5830 // seleqz TMP, false_reg, TMP
5831 // or out_reg, AT, TMP
5832 can_move_conditionally = true;
5833 }
5834 } else {
5835 // movt out_reg, true_reg/ZERO, cc
5836 can_move_conditionally = true;
5837 use_const_for_true_in = is_true_value_zero_constant;
5838 }
5839 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005841 // Moving long on float/double condition.
5842 if (is_r6) {
5843 if (is_true_value_zero_constant) {
5844 // mfc1 TMP, temp_cond_reg
5845 // seleqz out_reg_lo, false_reg_lo, TMP
5846 // seleqz out_reg_hi, false_reg_hi, TMP
5847 can_move_conditionally = true;
5848 use_const_for_true_in = true;
5849 } else if (is_false_value_zero_constant) {
5850 // mfc1 TMP, temp_cond_reg
5851 // selnez out_reg_lo, true_reg_lo, TMP
5852 // selnez out_reg_hi, true_reg_hi, TMP
5853 can_move_conditionally = true;
5854 use_const_for_false_in = true;
5855 }
5856 // Other long conditional moves would generate 6+ instructions,
5857 // which is too many.
5858 } else {
5859 // movt out_reg_lo, true_reg_lo/ZERO, cc
5860 // movt out_reg_hi, true_reg_hi/ZERO, cc
5861 can_move_conditionally = true;
5862 use_const_for_true_in = is_true_value_zero_constant;
5863 }
5864 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005865 case DataType::Type::kFloat32:
5866 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005867 // Moving float/double on float/double condition.
5868 if (is_r6) {
5869 can_move_conditionally = true;
5870 if (is_true_value_zero_constant) {
5871 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5872 use_const_for_true_in = true;
5873 } else if (is_false_value_zero_constant) {
5874 // selnez.fmt out_reg, true_reg, temp_cond_reg
5875 use_const_for_false_in = true;
5876 } else {
5877 // sel.fmt temp_cond_reg, false_reg, true_reg
5878 // mov.fmt out_reg, temp_cond_reg
5879 }
5880 } else {
5881 // movt.fmt out_reg, true_reg, cc
5882 can_move_conditionally = true;
5883 }
5884 break;
5885 }
5886 break;
5887 }
5888 }
5889
5890 if (can_move_conditionally) {
5891 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5892 } else {
5893 DCHECK(!use_const_for_false_in);
5894 DCHECK(!use_const_for_true_in);
5895 }
5896
5897 if (locations_to_set != nullptr) {
5898 if (use_const_for_false_in) {
5899 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5900 } else {
5901 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005902 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005903 ? Location::RequiresFpuRegister()
5904 : Location::RequiresRegister());
5905 }
5906 if (use_const_for_true_in) {
5907 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5908 } else {
5909 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005911 ? Location::RequiresFpuRegister()
5912 : Location::RequiresRegister());
5913 }
5914 if (materialized) {
5915 locations_to_set->SetInAt(2, Location::RequiresRegister());
5916 }
5917 // On R6 we don't require the output to be the same as the
5918 // first input for conditional moves unlike on R2.
5919 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5920 if (is_out_same_as_first_in) {
5921 locations_to_set->SetOut(Location::SameAsFirstInput());
5922 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005923 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005924 ? Location::RequiresFpuRegister()
5925 : Location::RequiresRegister());
5926 }
5927 }
5928
5929 return can_move_conditionally;
5930}
5931
5932void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5933 LocationSummary* locations = select->GetLocations();
5934 Location dst = locations->Out();
5935 Location src = locations->InAt(1);
5936 Register src_reg = ZERO;
5937 Register src_reg_high = ZERO;
5938 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5939 Register cond_reg = TMP;
5940 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005941 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005942 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005944
5945 if (IsBooleanValueOrMaterializedCondition(cond)) {
5946 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5947 } else {
5948 HCondition* condition = cond->AsCondition();
5949 LocationSummary* cond_locations = cond->GetLocations();
5950 IfCondition if_cond = condition->GetCondition();
5951 cond_type = condition->InputAt(0)->GetType();
5952 switch (cond_type) {
5953 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005955 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5956 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 case DataType::Type::kFloat32:
5958 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005959 cond_inverted = MaterializeFpCompareR2(if_cond,
5960 condition->IsGtBias(),
5961 cond_type,
5962 cond_locations,
5963 cond_cc);
5964 break;
5965 }
5966 }
5967
5968 DCHECK(dst.Equals(locations->InAt(0)));
5969 if (src.IsRegister()) {
5970 src_reg = src.AsRegister<Register>();
5971 } else if (src.IsRegisterPair()) {
5972 src_reg = src.AsRegisterPairLow<Register>();
5973 src_reg_high = src.AsRegisterPairHigh<Register>();
5974 } else if (src.IsConstant()) {
5975 DCHECK(src.GetConstant()->IsZeroBitPattern());
5976 }
5977
5978 switch (cond_type) {
5979 default:
5980 switch (dst_type) {
5981 default:
5982 if (cond_inverted) {
5983 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5984 } else {
5985 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5986 }
5987 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005988 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005989 if (cond_inverted) {
5990 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5991 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5992 } else {
5993 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5994 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5995 }
5996 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005997 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005998 if (cond_inverted) {
5999 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6000 } else {
6001 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6002 }
6003 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006005 if (cond_inverted) {
6006 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6007 } else {
6008 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6009 }
6010 break;
6011 }
6012 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006014 LOG(FATAL) << "Unreachable";
6015 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006016 case DataType::Type::kFloat32:
6017 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006018 switch (dst_type) {
6019 default:
6020 if (cond_inverted) {
6021 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6022 } else {
6023 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6024 }
6025 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006027 if (cond_inverted) {
6028 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6029 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6030 } else {
6031 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6032 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6033 }
6034 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006036 if (cond_inverted) {
6037 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6038 } else {
6039 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6040 }
6041 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006043 if (cond_inverted) {
6044 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6045 } else {
6046 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6047 }
6048 break;
6049 }
6050 break;
6051 }
6052}
6053
6054void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6055 LocationSummary* locations = select->GetLocations();
6056 Location dst = locations->Out();
6057 Location false_src = locations->InAt(0);
6058 Location true_src = locations->InAt(1);
6059 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6060 Register cond_reg = TMP;
6061 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006063 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006064 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006065
6066 if (IsBooleanValueOrMaterializedCondition(cond)) {
6067 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6068 } else {
6069 HCondition* condition = cond->AsCondition();
6070 LocationSummary* cond_locations = cond->GetLocations();
6071 IfCondition if_cond = condition->GetCondition();
6072 cond_type = condition->InputAt(0)->GetType();
6073 switch (cond_type) {
6074 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006075 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006076 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6077 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006078 case DataType::Type::kFloat32:
6079 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006080 cond_inverted = MaterializeFpCompareR6(if_cond,
6081 condition->IsGtBias(),
6082 cond_type,
6083 cond_locations,
6084 fcond_reg);
6085 break;
6086 }
6087 }
6088
6089 if (true_src.IsConstant()) {
6090 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6091 }
6092 if (false_src.IsConstant()) {
6093 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6094 }
6095
6096 switch (dst_type) {
6097 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006099 __ Mfc1(cond_reg, fcond_reg);
6100 }
6101 if (true_src.IsConstant()) {
6102 if (cond_inverted) {
6103 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6104 } else {
6105 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6106 }
6107 } else if (false_src.IsConstant()) {
6108 if (cond_inverted) {
6109 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6110 } else {
6111 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6112 }
6113 } else {
6114 DCHECK_NE(cond_reg, AT);
6115 if (cond_inverted) {
6116 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6117 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6118 } else {
6119 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6120 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6121 }
6122 __ Or(dst.AsRegister<Register>(), AT, TMP);
6123 }
6124 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006125 case DataType::Type::kInt64: {
6126 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006127 __ Mfc1(cond_reg, fcond_reg);
6128 }
6129 Register dst_lo = dst.AsRegisterPairLow<Register>();
6130 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6131 if (true_src.IsConstant()) {
6132 Register src_lo = false_src.AsRegisterPairLow<Register>();
6133 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6134 if (cond_inverted) {
6135 __ Selnez(dst_lo, src_lo, cond_reg);
6136 __ Selnez(dst_hi, src_hi, cond_reg);
6137 } else {
6138 __ Seleqz(dst_lo, src_lo, cond_reg);
6139 __ Seleqz(dst_hi, src_hi, cond_reg);
6140 }
6141 } else {
6142 DCHECK(false_src.IsConstant());
6143 Register src_lo = true_src.AsRegisterPairLow<Register>();
6144 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6145 if (cond_inverted) {
6146 __ Seleqz(dst_lo, src_lo, cond_reg);
6147 __ Seleqz(dst_hi, src_hi, cond_reg);
6148 } else {
6149 __ Selnez(dst_lo, src_lo, cond_reg);
6150 __ Selnez(dst_hi, src_hi, cond_reg);
6151 }
6152 }
6153 break;
6154 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 case DataType::Type::kFloat32: {
6156 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006157 // sel*.fmt tests bit 0 of the condition register, account for that.
6158 __ Sltu(TMP, ZERO, cond_reg);
6159 __ Mtc1(TMP, fcond_reg);
6160 }
6161 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6162 if (true_src.IsConstant()) {
6163 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6164 if (cond_inverted) {
6165 __ SelnezS(dst_reg, src_reg, fcond_reg);
6166 } else {
6167 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6168 }
6169 } else if (false_src.IsConstant()) {
6170 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6171 if (cond_inverted) {
6172 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6173 } else {
6174 __ SelnezS(dst_reg, src_reg, fcond_reg);
6175 }
6176 } else {
6177 if (cond_inverted) {
6178 __ SelS(fcond_reg,
6179 true_src.AsFpuRegister<FRegister>(),
6180 false_src.AsFpuRegister<FRegister>());
6181 } else {
6182 __ SelS(fcond_reg,
6183 false_src.AsFpuRegister<FRegister>(),
6184 true_src.AsFpuRegister<FRegister>());
6185 }
6186 __ MovS(dst_reg, fcond_reg);
6187 }
6188 break;
6189 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 case DataType::Type::kFloat64: {
6191 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006192 // sel*.fmt tests bit 0 of the condition register, account for that.
6193 __ Sltu(TMP, ZERO, cond_reg);
6194 __ Mtc1(TMP, fcond_reg);
6195 }
6196 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6197 if (true_src.IsConstant()) {
6198 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6199 if (cond_inverted) {
6200 __ SelnezD(dst_reg, src_reg, fcond_reg);
6201 } else {
6202 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6203 }
6204 } else if (false_src.IsConstant()) {
6205 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6206 if (cond_inverted) {
6207 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6208 } else {
6209 __ SelnezD(dst_reg, src_reg, fcond_reg);
6210 }
6211 } else {
6212 if (cond_inverted) {
6213 __ SelD(fcond_reg,
6214 true_src.AsFpuRegister<FRegister>(),
6215 false_src.AsFpuRegister<FRegister>());
6216 } else {
6217 __ SelD(fcond_reg,
6218 false_src.AsFpuRegister<FRegister>(),
6219 true_src.AsFpuRegister<FRegister>());
6220 }
6221 __ MovD(dst_reg, fcond_reg);
6222 }
6223 break;
6224 }
6225 }
6226}
6227
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006228void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006229 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006230 LocationSummary(flag, LocationSummary::kNoCall);
6231 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006232}
6233
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006234void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6235 __ LoadFromOffset(kLoadWord,
6236 flag->GetLocations()->Out().AsRegister<Register>(),
6237 SP,
6238 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006239}
6240
David Brazdil74eb1b22015-12-14 11:44:01 +00006241void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006242 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006243 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006244}
6245
6246void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006247 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6248 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6249 if (is_r6) {
6250 GenConditionalMoveR6(select);
6251 } else {
6252 GenConditionalMoveR2(select);
6253 }
6254 } else {
6255 LocationSummary* locations = select->GetLocations();
6256 MipsLabel false_target;
6257 GenerateTestAndBranch(select,
6258 /* condition_input_index */ 2,
6259 /* true_target */ nullptr,
6260 &false_target);
6261 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6262 __ Bind(&false_target);
6263 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006264}
6265
David Srbecky0cf44932015-12-09 14:09:59 +00006266void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006267 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006268}
6269
David Srbeckyd28f4a02016-03-14 17:14:24 +00006270void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6271 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006272}
6273
6274void CodeGeneratorMIPS::GenerateNop() {
6275 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006276}
6277
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006278void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 DataType::Type field_type = field_info.GetFieldType();
6280 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006281 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006282 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006283 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006284 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006285 instruction,
6286 generate_volatile
6287 ? LocationSummary::kCallOnMainOnly
6288 : (object_field_get_with_read_barrier
6289 ? LocationSummary::kCallOnSlowPath
6290 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006291
Alexey Frunzec61c0762017-04-10 13:54:23 -07006292 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6293 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6294 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 locations->SetInAt(0, Location::RequiresRegister());
6296 if (generate_volatile) {
6297 InvokeRuntimeCallingConvention calling_convention;
6298 // need A0 to hold base + offset
6299 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006300 if (field_type == DataType::Type::kInt64) {
6301 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006302 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006303 // Use Location::Any() to prevent situations when running out of available fp registers.
6304 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006305 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006307 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6308 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6309 }
6310 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006311 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006312 locations->SetOut(Location::RequiresFpuRegister());
6313 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006314 // The output overlaps in the case of an object field get with
6315 // read barriers enabled: we do not want the move to overwrite the
6316 // object's location, as we need it to emit the read barrier.
6317 locations->SetOut(Location::RequiresRegister(),
6318 object_field_get_with_read_barrier
6319 ? Location::kOutputOverlap
6320 : Location::kNoOutputOverlap);
6321 }
6322 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6323 // We need a temporary register for the read barrier marking slow
6324 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006325 if (!kBakerReadBarrierThunksEnableForFields) {
6326 locations->AddTemp(Location::RequiresRegister());
6327 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006328 }
6329 }
6330}
6331
6332void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6333 const FieldInfo& field_info,
6334 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006335 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6336 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006337 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006338 Location obj_loc = locations->InAt(0);
6339 Register obj = obj_loc.AsRegister<Register>();
6340 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006341 LoadOperandType load_type = kLoadUnsignedByte;
6342 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006343 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006344 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006345
6346 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006348 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006349 load_type = kLoadUnsignedByte;
6350 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006351 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006352 load_type = kLoadSignedByte;
6353 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006354 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006355 load_type = kLoadUnsignedHalfword;
6356 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006357 case DataType::Type::kInt16:
6358 load_type = kLoadSignedHalfword;
6359 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006360 case DataType::Type::kInt32:
6361 case DataType::Type::kFloat32:
6362 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006363 load_type = kLoadWord;
6364 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 case DataType::Type::kInt64:
6366 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006367 load_type = kLoadDoubleword;
6368 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 LOG(FATAL) << "Unreachable type " << type;
6371 UNREACHABLE();
6372 }
6373
6374 if (is_volatile && load_type == kLoadDoubleword) {
6375 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006376 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006377 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006378 __ LoadFromOffset(kLoadWord,
6379 ZERO,
6380 locations->GetTemp(0).AsRegister<Register>(),
6381 0,
6382 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006383 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006384 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006385 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006386 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006387 if (dst_loc.IsFpuRegister()) {
6388 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006389 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006390 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006391 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006392 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006393 __ StoreToOffset(kStoreWord,
6394 locations->GetTemp(1).AsRegister<Register>(),
6395 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006396 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006397 __ StoreToOffset(kStoreWord,
6398 locations->GetTemp(2).AsRegister<Register>(),
6399 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006400 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006401 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006402 }
6403 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006404 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006405 // /* HeapReference<Object> */ dst = *(obj + offset)
6406 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006407 Location temp_loc =
6408 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006409 // Note that a potential implicit null check is handled in this
6410 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6411 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6412 dst_loc,
6413 obj,
6414 offset,
6415 temp_loc,
6416 /* needs_null_check */ true);
6417 if (is_volatile) {
6418 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6419 }
6420 } else {
6421 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6422 if (is_volatile) {
6423 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6424 }
6425 // If read barriers are enabled, emit read barriers other than
6426 // Baker's using a slow path (and also unpoison the loaded
6427 // reference, if heap poisoning is enabled).
6428 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6429 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006430 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006431 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006432 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006433 DCHECK(dst_loc.IsRegisterPair());
6434 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006435 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006436 DCHECK(dst_loc.IsRegister());
6437 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006438 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006439 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006441 DCHECK(dst_loc.IsFpuRegister());
6442 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006443 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006444 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006445 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006446 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006447 }
6448 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006449 }
6450
Alexey Frunze15958152017-02-09 19:08:30 -08006451 // Memory barriers, in the case of references, are handled in the
6452 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006453 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006454 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6455 }
6456}
6457
6458void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 DataType::Type field_type = field_info.GetFieldType();
6460 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006461 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006462 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006463 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006464
6465 locations->SetInAt(0, Location::RequiresRegister());
6466 if (generate_volatile) {
6467 InvokeRuntimeCallingConvention calling_convention;
6468 // need A0 to hold base + offset
6469 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006470 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006471 locations->SetInAt(1, Location::RegisterPairLocation(
6472 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6473 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006474 // Use Location::Any() to prevent situations when running out of available fp registers.
6475 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006476 // Pass FP parameters in core registers.
6477 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6478 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6479 }
6480 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006481 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006482 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006483 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006484 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006485 }
6486 }
6487}
6488
6489void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6490 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006491 uint32_t dex_pc,
6492 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006493 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494 LocationSummary* locations = instruction->GetLocations();
6495 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006496 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006497 StoreOperandType store_type = kStoreByte;
6498 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006499 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006500 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006501 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006502
6503 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006504 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006505 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006506 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006507 store_type = kStoreByte;
6508 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006510 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 store_type = kStoreHalfword;
6512 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 case DataType::Type::kInt32:
6514 case DataType::Type::kFloat32:
6515 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006516 store_type = kStoreWord;
6517 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006518 case DataType::Type::kInt64:
6519 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 store_type = kStoreDoubleword;
6521 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006522 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006523 LOG(FATAL) << "Unreachable type " << type;
6524 UNREACHABLE();
6525 }
6526
6527 if (is_volatile) {
6528 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6529 }
6530
6531 if (is_volatile && store_type == kStoreDoubleword) {
6532 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006533 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006535 __ LoadFromOffset(kLoadWord,
6536 ZERO,
6537 locations->GetTemp(0).AsRegister<Register>(),
6538 0,
6539 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006541 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006542 if (value_location.IsFpuRegister()) {
6543 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6544 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006545 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006546 value_location.AsFpuRegister<FRegister>());
6547 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006548 __ LoadFromOffset(kLoadWord,
6549 locations->GetTemp(1).AsRegister<Register>(),
6550 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006551 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006552 __ LoadFromOffset(kLoadWord,
6553 locations->GetTemp(2).AsRegister<Register>(),
6554 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006555 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006556 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006557 DCHECK(value_location.IsConstant());
6558 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6559 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006560 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6561 locations->GetTemp(1).AsRegister<Register>(),
6562 value);
6563 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006564 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006565 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006566 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6567 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006568 if (value_location.IsConstant()) {
6569 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6570 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006572 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006574 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006575 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006576 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006577 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006578 if (kPoisonHeapReferences && needs_write_barrier) {
6579 // Note that in the case where `value` is a null reference,
6580 // we do not enter this block, as a null reference does not
6581 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006582 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006583 __ PoisonHeapReference(TMP, src);
6584 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6585 } else {
6586 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6587 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006588 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006589 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006590 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006591 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006592 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006593 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006594 }
6595 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006596 }
6597
Alexey Frunzec061de12017-02-14 13:27:23 -08006598 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006599 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006600 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006601 }
6602
6603 if (is_volatile) {
6604 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6605 }
6606}
6607
6608void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6609 HandleFieldGet(instruction, instruction->GetFieldInfo());
6610}
6611
6612void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6613 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6614}
6615
6616void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6617 HandleFieldSet(instruction, instruction->GetFieldInfo());
6618}
6619
6620void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006621 HandleFieldSet(instruction,
6622 instruction->GetFieldInfo(),
6623 instruction->GetDexPc(),
6624 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006625}
6626
Alexey Frunze15958152017-02-09 19:08:30 -08006627void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6628 HInstruction* instruction,
6629 Location out,
6630 uint32_t offset,
6631 Location maybe_temp,
6632 ReadBarrierOption read_barrier_option) {
6633 Register out_reg = out.AsRegister<Register>();
6634 if (read_barrier_option == kWithReadBarrier) {
6635 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006636 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6637 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6638 }
Alexey Frunze15958152017-02-09 19:08:30 -08006639 if (kUseBakerReadBarrier) {
6640 // Load with fast path based Baker's read barrier.
6641 // /* HeapReference<Object> */ out = *(out + offset)
6642 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6643 out,
6644 out_reg,
6645 offset,
6646 maybe_temp,
6647 /* needs_null_check */ false);
6648 } else {
6649 // Load with slow path based read barrier.
6650 // Save the value of `out` into `maybe_temp` before overwriting it
6651 // in the following move operation, as we will need it for the
6652 // read barrier below.
6653 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6654 // /* HeapReference<Object> */ out = *(out + offset)
6655 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6656 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6657 }
6658 } else {
6659 // Plain load with no read barrier.
6660 // /* HeapReference<Object> */ out = *(out + offset)
6661 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6662 __ MaybeUnpoisonHeapReference(out_reg);
6663 }
6664}
6665
6666void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6667 HInstruction* instruction,
6668 Location out,
6669 Location obj,
6670 uint32_t offset,
6671 Location maybe_temp,
6672 ReadBarrierOption read_barrier_option) {
6673 Register out_reg = out.AsRegister<Register>();
6674 Register obj_reg = obj.AsRegister<Register>();
6675 if (read_barrier_option == kWithReadBarrier) {
6676 CHECK(kEmitCompilerReadBarrier);
6677 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006678 if (!kBakerReadBarrierThunksEnableForFields) {
6679 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6680 }
Alexey Frunze15958152017-02-09 19:08:30 -08006681 // Load with fast path based Baker's read barrier.
6682 // /* HeapReference<Object> */ out = *(obj + offset)
6683 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6684 out,
6685 obj_reg,
6686 offset,
6687 maybe_temp,
6688 /* needs_null_check */ false);
6689 } else {
6690 // Load with slow path based read barrier.
6691 // /* HeapReference<Object> */ out = *(obj + offset)
6692 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6693 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6694 }
6695 } else {
6696 // Plain load with no read barrier.
6697 // /* HeapReference<Object> */ out = *(obj + offset)
6698 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6699 __ MaybeUnpoisonHeapReference(out_reg);
6700 }
6701}
6702
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006703static inline int GetBakerMarkThunkNumber(Register reg) {
6704 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6705 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6706 return reg - V0;
6707 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6708 return 14 + (reg - S2);
6709 } else if (reg == FP) { // One more.
6710 return 20;
6711 }
6712 LOG(FATAL) << "Unexpected register " << reg;
6713 UNREACHABLE();
6714}
6715
6716static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6717 int num = GetBakerMarkThunkNumber(reg) +
6718 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6719 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6720}
6721
6722static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6723 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6724 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6725}
6726
Alexey Frunze15958152017-02-09 19:08:30 -08006727void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6728 Location root,
6729 Register obj,
6730 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006731 ReadBarrierOption read_barrier_option,
6732 MipsLabel* label_low) {
6733 bool reordering;
6734 if (label_low != nullptr) {
6735 DCHECK_EQ(offset, 0x5678u);
6736 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006737 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006738 if (read_barrier_option == kWithReadBarrier) {
6739 DCHECK(kEmitCompilerReadBarrier);
6740 if (kUseBakerReadBarrier) {
6741 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6742 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006743 if (kBakerReadBarrierThunksEnableForGcRoots) {
6744 // Note that we do not actually check the value of `GetIsGcMarking()`
6745 // to decide whether to mark the loaded GC root or not. Instead, we
6746 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6747 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6748 // vice versa.
6749 //
6750 // We use thunks for the slow path. That thunk checks the reference
6751 // and jumps to the entrypoint if needed.
6752 //
6753 // temp = Thread::Current()->pReadBarrierMarkReg00
6754 // // AKA &art_quick_read_barrier_mark_introspection.
6755 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6756 // if (temp != nullptr) {
6757 // temp = &gc_root_thunk<root_reg>
6758 // root = temp(root)
6759 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006760
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006761 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6762 const int32_t entry_point_offset =
6763 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6764 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6765 int16_t offset_low = Low16Bits(offset);
6766 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6767 // extension in lw.
6768 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6769 Register base = short_offset ? obj : TMP;
6770 // Loading the entrypoint does not require a load acquire since it is only changed when
6771 // threads are suspended or running a checkpoint.
6772 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6773 reordering = __ SetReorder(false);
6774 if (!short_offset) {
6775 DCHECK(!label_low);
6776 __ AddUpper(base, obj, offset_high);
6777 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006778 MipsLabel skip_call;
6779 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006780 if (label_low != nullptr) {
6781 DCHECK(short_offset);
6782 __ Bind(label_low);
6783 }
6784 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6785 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6786 // in delay slot.
6787 if (isR6) {
6788 __ Jialc(T9, thunk_disp);
6789 } else {
6790 __ Addiu(T9, T9, thunk_disp);
6791 __ Jalr(T9);
6792 __ Nop();
6793 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006794 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006795 __ SetReorder(reordering);
6796 } else {
6797 // Note that we do not actually check the value of `GetIsGcMarking()`
6798 // to decide whether to mark the loaded GC root or not. Instead, we
6799 // load into `temp` (T9) the read barrier mark entry point corresponding
6800 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6801 // is false, and vice versa.
6802 //
6803 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6804 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6805 // if (temp != null) {
6806 // root = temp(root)
6807 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006808
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006809 if (label_low != nullptr) {
6810 reordering = __ SetReorder(false);
6811 __ Bind(label_low);
6812 }
6813 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6814 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6815 if (label_low != nullptr) {
6816 __ SetReorder(reordering);
6817 }
6818 static_assert(
6819 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6820 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6821 "have different sizes.");
6822 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6823 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6824 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006825
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006826 // Slow path marking the GC root `root`.
6827 Location temp = Location::RegisterLocation(T9);
6828 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006829 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006830 instruction,
6831 root,
6832 /*entrypoint*/ temp);
6833 codegen_->AddSlowPath(slow_path);
6834
6835 const int32_t entry_point_offset =
6836 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6837 // Loading the entrypoint does not require a load acquire since it is only changed when
6838 // threads are suspended or running a checkpoint.
6839 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6840 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6841 __ Bind(slow_path->GetExitLabel());
6842 }
Alexey Frunze15958152017-02-09 19:08:30 -08006843 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006844 if (label_low != nullptr) {
6845 reordering = __ SetReorder(false);
6846 __ Bind(label_low);
6847 }
Alexey Frunze15958152017-02-09 19:08:30 -08006848 // GC root loaded through a slow path for read barriers other
6849 // than Baker's.
6850 // /* GcRoot<mirror::Object>* */ root = obj + offset
6851 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006852 if (label_low != nullptr) {
6853 __ SetReorder(reordering);
6854 }
Alexey Frunze15958152017-02-09 19:08:30 -08006855 // /* mirror::Object* */ root = root->Read()
6856 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6857 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006858 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006859 if (label_low != nullptr) {
6860 reordering = __ SetReorder(false);
6861 __ Bind(label_low);
6862 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006863 // Plain GC root load with no read barrier.
6864 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6865 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6866 // Note that GC roots are not affected by heap poisoning, thus we
6867 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006868 if (label_low != nullptr) {
6869 __ SetReorder(reordering);
6870 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006871 }
6872}
6873
Alexey Frunze15958152017-02-09 19:08:30 -08006874void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6875 Location ref,
6876 Register obj,
6877 uint32_t offset,
6878 Location temp,
6879 bool needs_null_check) {
6880 DCHECK(kEmitCompilerReadBarrier);
6881 DCHECK(kUseBakerReadBarrier);
6882
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006883 if (kBakerReadBarrierThunksEnableForFields) {
6884 // Note that we do not actually check the value of `GetIsGcMarking()`
6885 // to decide whether to mark the loaded reference or not. Instead, we
6886 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6887 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6888 // vice versa.
6889 //
6890 // We use thunks for the slow path. That thunk checks the reference
6891 // and jumps to the entrypoint if needed. If the holder is not gray,
6892 // it issues a load-load memory barrier and returns to the original
6893 // reference load.
6894 //
6895 // temp = Thread::Current()->pReadBarrierMarkReg00
6896 // // AKA &art_quick_read_barrier_mark_introspection.
6897 // if (temp != nullptr) {
6898 // temp = &field_array_thunk<holder_reg>
6899 // temp()
6900 // }
6901 // not_gray_return_address:
6902 // // If the offset is too large to fit into the lw instruction, we
6903 // // use an adjusted base register (TMP) here. This register
6904 // // receives bits 16 ... 31 of the offset before the thunk invocation
6905 // // and the thunk benefits from it.
6906 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6907 // gray_return_address:
6908
6909 DCHECK(temp.IsInvalid());
6910 bool isR6 = GetInstructionSetFeatures().IsR6();
6911 int16_t offset_low = Low16Bits(offset);
6912 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6913 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6914 bool reordering = __ SetReorder(false);
6915 const int32_t entry_point_offset =
6916 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6917 // There may have or may have not been a null check if the field offset is smaller than
6918 // the page size.
6919 // There must've been a null check in case it's actually a load from an array.
6920 // We will, however, perform an explicit null check in the thunk as it's easier to
6921 // do it than not.
6922 if (instruction->IsArrayGet()) {
6923 DCHECK(!needs_null_check);
6924 }
6925 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6926 // Loading the entrypoint does not require a load acquire since it is only changed when
6927 // threads are suspended or running a checkpoint.
6928 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6929 Register ref_reg = ref.AsRegister<Register>();
6930 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006931 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006932 if (short_offset) {
6933 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006934 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006935 __ Nop(); // In forbidden slot.
6936 __ Jialc(T9, thunk_disp);
6937 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006938 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006939 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6940 __ Jalr(T9);
6941 __ Nop(); // In delay slot.
6942 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006943 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006944 } else {
6945 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006946 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006947 __ Aui(base, obj, offset_high); // In delay slot.
6948 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006949 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006950 } else {
6951 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006952 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006953 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6954 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006955 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006956 __ Addu(base, base, obj); // In delay slot.
6957 }
6958 }
6959 // /* HeapReference<Object> */ ref = *(obj + offset)
6960 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6961 if (needs_null_check) {
6962 MaybeRecordImplicitNullCheck(instruction);
6963 }
6964 __ MaybeUnpoisonHeapReference(ref_reg);
6965 __ SetReorder(reordering);
6966 return;
6967 }
6968
Alexey Frunze15958152017-02-09 19:08:30 -08006969 // /* HeapReference<Object> */ ref = *(obj + offset)
6970 Location no_index = Location::NoLocation();
6971 ScaleFactor no_scale_factor = TIMES_1;
6972 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6973 ref,
6974 obj,
6975 offset,
6976 no_index,
6977 no_scale_factor,
6978 temp,
6979 needs_null_check);
6980}
6981
6982void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6983 Location ref,
6984 Register obj,
6985 uint32_t data_offset,
6986 Location index,
6987 Location temp,
6988 bool needs_null_check) {
6989 DCHECK(kEmitCompilerReadBarrier);
6990 DCHECK(kUseBakerReadBarrier);
6991
6992 static_assert(
6993 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6994 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006995 ScaleFactor scale_factor = TIMES_4;
6996
6997 if (kBakerReadBarrierThunksEnableForArrays) {
6998 // Note that we do not actually check the value of `GetIsGcMarking()`
6999 // to decide whether to mark the loaded reference or not. Instead, we
7000 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7001 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7002 // vice versa.
7003 //
7004 // We use thunks for the slow path. That thunk checks the reference
7005 // and jumps to the entrypoint if needed. If the holder is not gray,
7006 // it issues a load-load memory barrier and returns to the original
7007 // reference load.
7008 //
7009 // temp = Thread::Current()->pReadBarrierMarkReg00
7010 // // AKA &art_quick_read_barrier_mark_introspection.
7011 // if (temp != nullptr) {
7012 // temp = &field_array_thunk<holder_reg>
7013 // temp()
7014 // }
7015 // not_gray_return_address:
7016 // // The element address is pre-calculated in the TMP register before the
7017 // // thunk invocation and the thunk benefits from it.
7018 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7019 // gray_return_address:
7020
7021 DCHECK(temp.IsInvalid());
7022 DCHECK(index.IsValid());
7023 bool reordering = __ SetReorder(false);
7024 const int32_t entry_point_offset =
7025 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7026 // We will not do the explicit null check in the thunk as some form of a null check
7027 // must've been done earlier.
7028 DCHECK(!needs_null_check);
7029 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7030 // Loading the entrypoint does not require a load acquire since it is only changed when
7031 // threads are suspended or running a checkpoint.
7032 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7033 Register ref_reg = ref.AsRegister<Register>();
7034 Register index_reg = index.IsRegisterPair()
7035 ? index.AsRegisterPairLow<Register>()
7036 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007037 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007038 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007039 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007040 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7041 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007042 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007043 } else {
7044 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007045 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007046 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7047 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007048 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007049 __ Addu(TMP, TMP, obj); // In delay slot.
7050 }
7051 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7052 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7053 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7054 __ MaybeUnpoisonHeapReference(ref_reg);
7055 __ SetReorder(reordering);
7056 return;
7057 }
7058
Alexey Frunze15958152017-02-09 19:08:30 -08007059 // /* HeapReference<Object> */ ref =
7060 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007061 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7062 ref,
7063 obj,
7064 data_offset,
7065 index,
7066 scale_factor,
7067 temp,
7068 needs_null_check);
7069}
7070
7071void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7072 Location ref,
7073 Register obj,
7074 uint32_t offset,
7075 Location index,
7076 ScaleFactor scale_factor,
7077 Location temp,
7078 bool needs_null_check,
7079 bool always_update_field) {
7080 DCHECK(kEmitCompilerReadBarrier);
7081 DCHECK(kUseBakerReadBarrier);
7082
7083 // In slow path based read barriers, the read barrier call is
7084 // inserted after the original load. However, in fast path based
7085 // Baker's read barriers, we need to perform the load of
7086 // mirror::Object::monitor_ *before* the original reference load.
7087 // This load-load ordering is required by the read barrier.
7088 // The fast path/slow path (for Baker's algorithm) should look like:
7089 //
7090 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7091 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7092 // HeapReference<Object> ref = *src; // Original reference load.
7093 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7094 // if (is_gray) {
7095 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7096 // }
7097 //
7098 // Note: the original implementation in ReadBarrier::Barrier is
7099 // slightly more complex as it performs additional checks that we do
7100 // not do here for performance reasons.
7101
7102 Register ref_reg = ref.AsRegister<Register>();
7103 Register temp_reg = temp.AsRegister<Register>();
7104 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7105
7106 // /* int32_t */ monitor = obj->monitor_
7107 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7108 if (needs_null_check) {
7109 MaybeRecordImplicitNullCheck(instruction);
7110 }
7111 // /* LockWord */ lock_word = LockWord(monitor)
7112 static_assert(sizeof(LockWord) == sizeof(int32_t),
7113 "art::LockWord and int32_t have different sizes.");
7114
7115 __ Sync(0); // Barrier to prevent load-load reordering.
7116
7117 // The actual reference load.
7118 if (index.IsValid()) {
7119 // Load types involving an "index": ArrayGet,
7120 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7121 // intrinsics.
7122 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7123 if (index.IsConstant()) {
7124 size_t computed_offset =
7125 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7126 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7127 } else {
7128 // Handle the special case of the
7129 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7130 // intrinsics, which use a register pair as index ("long
7131 // offset"), of which only the low part contains data.
7132 Register index_reg = index.IsRegisterPair()
7133 ? index.AsRegisterPairLow<Register>()
7134 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007135 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007136 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7137 }
7138 } else {
7139 // /* HeapReference<Object> */ ref = *(obj + offset)
7140 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7141 }
7142
7143 // Object* ref = ref_addr->AsMirrorPtr()
7144 __ MaybeUnpoisonHeapReference(ref_reg);
7145
7146 // Slow path marking the object `ref` when it is gray.
7147 SlowPathCodeMIPS* slow_path;
7148 if (always_update_field) {
7149 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7150 // of the form `obj + field_offset`, where `obj` is a register and
7151 // `field_offset` is a register pair (of which only the lower half
7152 // is used). Thus `offset` and `scale_factor` above are expected
7153 // to be null in this code path.
7154 DCHECK_EQ(offset, 0u);
7155 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007156 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007157 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7158 ref,
7159 obj,
7160 /* field_offset */ index,
7161 temp_reg);
7162 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007163 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007164 }
7165 AddSlowPath(slow_path);
7166
7167 // if (rb_state == ReadBarrier::GrayState())
7168 // ref = ReadBarrier::Mark(ref);
7169 // Given the numeric representation, it's enough to check the low bit of the
7170 // rb_state. We do that by shifting the bit into the sign bit (31) and
7171 // performing a branch on less than zero.
7172 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7173 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7174 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7175 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7176 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7177 __ Bind(slow_path->GetExitLabel());
7178}
7179
7180void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7181 Location out,
7182 Location ref,
7183 Location obj,
7184 uint32_t offset,
7185 Location index) {
7186 DCHECK(kEmitCompilerReadBarrier);
7187
7188 // Insert a slow path based read barrier *after* the reference load.
7189 //
7190 // If heap poisoning is enabled, the unpoisoning of the loaded
7191 // reference will be carried out by the runtime within the slow
7192 // path.
7193 //
7194 // Note that `ref` currently does not get unpoisoned (when heap
7195 // poisoning is enabled), which is alright as the `ref` argument is
7196 // not used by the artReadBarrierSlow entry point.
7197 //
7198 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007199 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007200 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7201 AddSlowPath(slow_path);
7202
7203 __ B(slow_path->GetEntryLabel());
7204 __ Bind(slow_path->GetExitLabel());
7205}
7206
7207void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7208 Location out,
7209 Location ref,
7210 Location obj,
7211 uint32_t offset,
7212 Location index) {
7213 if (kEmitCompilerReadBarrier) {
7214 // Baker's read barriers shall be handled by the fast path
7215 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7216 DCHECK(!kUseBakerReadBarrier);
7217 // If heap poisoning is enabled, unpoisoning will be taken care of
7218 // by the runtime within the slow path.
7219 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7220 } else if (kPoisonHeapReferences) {
7221 __ UnpoisonHeapReference(out.AsRegister<Register>());
7222 }
7223}
7224
7225void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7226 Location out,
7227 Location root) {
7228 DCHECK(kEmitCompilerReadBarrier);
7229
7230 // Insert a slow path based read barrier *after* the GC root load.
7231 //
7232 // Note that GC roots are not affected by heap poisoning, so we do
7233 // not need to do anything special for this here.
7234 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007235 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007236 AddSlowPath(slow_path);
7237
7238 __ B(slow_path->GetEntryLabel());
7239 __ Bind(slow_path->GetExitLabel());
7240}
7241
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007242void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007243 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7244 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007245 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007246 switch (type_check_kind) {
7247 case TypeCheckKind::kExactCheck:
7248 case TypeCheckKind::kAbstractClassCheck:
7249 case TypeCheckKind::kClassHierarchyCheck:
7250 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007251 call_kind =
7252 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007253 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007254 break;
7255 case TypeCheckKind::kArrayCheck:
7256 case TypeCheckKind::kUnresolvedCheck:
7257 case TypeCheckKind::kInterfaceCheck:
7258 call_kind = LocationSummary::kCallOnSlowPath;
7259 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007260 case TypeCheckKind::kBitstringCheck:
7261 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007262 }
7263
Vladimir Markoca6fff82017-10-03 14:49:14 +01007264 LocationSummary* locations =
7265 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007266 if (baker_read_barrier_slow_path) {
7267 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7268 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007269 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007270 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7271 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7272 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7273 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7274 } else {
7275 locations->SetInAt(1, Location::RequiresRegister());
7276 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007277 // The output does overlap inputs.
7278 // Note that TypeCheckSlowPathMIPS uses this register too.
7279 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007280 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007281}
7282
7283void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007284 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007285 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007286 Location obj_loc = locations->InAt(0);
7287 Register obj = obj_loc.AsRegister<Register>();
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007288 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007289 Location out_loc = locations->Out();
7290 Register out = out_loc.AsRegister<Register>();
7291 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7292 DCHECK_LE(num_temps, 1u);
7293 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007294 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7295 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7296 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7297 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007298 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007299 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007300
7301 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007302 // Avoid this check if we know `obj` is not null.
7303 if (instruction->MustDoNullCheck()) {
7304 __ Move(out, ZERO);
7305 __ Beqz(obj, &done);
7306 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007307
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007308 switch (type_check_kind) {
7309 case TypeCheckKind::kExactCheck: {
7310 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007311 GenerateReferenceLoadTwoRegisters(instruction,
7312 out_loc,
7313 obj_loc,
7314 class_offset,
7315 maybe_temp_loc,
7316 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007317 // Classes must be equal for the instanceof to succeed.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007318 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007319 __ Sltiu(out, out, 1);
7320 break;
7321 }
7322
7323 case TypeCheckKind::kAbstractClassCheck: {
7324 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007325 GenerateReferenceLoadTwoRegisters(instruction,
7326 out_loc,
7327 obj_loc,
7328 class_offset,
7329 maybe_temp_loc,
7330 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007331 // If the class is abstract, we eagerly fetch the super class of the
7332 // object to avoid doing a comparison we know will fail.
7333 MipsLabel loop;
7334 __ Bind(&loop);
7335 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007336 GenerateReferenceLoadOneRegister(instruction,
7337 out_loc,
7338 super_offset,
7339 maybe_temp_loc,
7340 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007341 // If `out` is null, we use it for the result, and jump to `done`.
7342 __ Beqz(out, &done);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007343 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007344 __ LoadConst32(out, 1);
7345 break;
7346 }
7347
7348 case TypeCheckKind::kClassHierarchyCheck: {
7349 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007350 GenerateReferenceLoadTwoRegisters(instruction,
7351 out_loc,
7352 obj_loc,
7353 class_offset,
7354 maybe_temp_loc,
7355 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007356 // Walk over the class hierarchy to find a match.
7357 MipsLabel loop, success;
7358 __ Bind(&loop);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007359 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007360 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007361 GenerateReferenceLoadOneRegister(instruction,
7362 out_loc,
7363 super_offset,
7364 maybe_temp_loc,
7365 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007366 __ Bnez(out, &loop);
7367 // If `out` is null, we use it for the result, and jump to `done`.
7368 __ B(&done);
7369 __ Bind(&success);
7370 __ LoadConst32(out, 1);
7371 break;
7372 }
7373
7374 case TypeCheckKind::kArrayObjectCheck: {
7375 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007376 GenerateReferenceLoadTwoRegisters(instruction,
7377 out_loc,
7378 obj_loc,
7379 class_offset,
7380 maybe_temp_loc,
7381 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007382 // Do an exact check.
7383 MipsLabel success;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007384 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007385 // Otherwise, we need to check that the object's class is a non-primitive array.
7386 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007387 GenerateReferenceLoadOneRegister(instruction,
7388 out_loc,
7389 component_offset,
7390 maybe_temp_loc,
7391 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007392 // If `out` is null, we use it for the result, and jump to `done`.
7393 __ Beqz(out, &done);
7394 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7395 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7396 __ Sltiu(out, out, 1);
7397 __ B(&done);
7398 __ Bind(&success);
7399 __ LoadConst32(out, 1);
7400 break;
7401 }
7402
7403 case TypeCheckKind::kArrayCheck: {
7404 // No read barrier since the slow path will retry upon failure.
7405 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007406 GenerateReferenceLoadTwoRegisters(instruction,
7407 out_loc,
7408 obj_loc,
7409 class_offset,
7410 maybe_temp_loc,
7411 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007412 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007413 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7414 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007415 codegen_->AddSlowPath(slow_path);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007416 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007417 __ LoadConst32(out, 1);
7418 break;
7419 }
7420
7421 case TypeCheckKind::kUnresolvedCheck:
7422 case TypeCheckKind::kInterfaceCheck: {
7423 // Note that we indeed only call on slow path, but we always go
7424 // into the slow path for the unresolved and interface check
7425 // cases.
7426 //
7427 // We cannot directly call the InstanceofNonTrivial runtime
7428 // entry point without resorting to a type checking slow path
7429 // here (i.e. by calling InvokeRuntime directly), as it would
7430 // require to assign fixed registers for the inputs of this
7431 // HInstanceOf instruction (following the runtime calling
7432 // convention), which might be cluttered by the potential first
7433 // read barrier emission at the beginning of this method.
7434 //
7435 // TODO: Introduce a new runtime entry point taking the object
7436 // to test (instead of its class) as argument, and let it deal
7437 // with the read barrier issues. This will let us refactor this
7438 // case of the `switch` code as it was previously (with a direct
7439 // call to the runtime not using a type checking slow path).
7440 // This should also be beneficial for the other cases above.
7441 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007442 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7443 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007444 codegen_->AddSlowPath(slow_path);
7445 __ B(slow_path->GetEntryLabel());
7446 break;
7447 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00007448
7449 case TypeCheckKind::kBitstringCheck: {
7450 // /* HeapReference<Class> */ temp = obj->klass_
7451 GenerateReferenceLoadTwoRegisters(instruction,
7452 out_loc,
7453 obj_loc,
7454 class_offset,
7455 maybe_temp_loc,
7456 kWithoutReadBarrier);
7457
7458 GenerateBitstringTypeCheckCompare(instruction, out);
7459 __ Sltiu(out, out, 1);
7460 break;
7461 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007462 }
7463
7464 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007465
7466 if (slow_path != nullptr) {
7467 __ Bind(slow_path->GetExitLabel());
7468 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007469}
7470
7471void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007472 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007473 locations->SetOut(Location::ConstantLocation(constant));
7474}
7475
7476void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7477 // Will be generated at use site.
7478}
7479
7480void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007481 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007482 locations->SetOut(Location::ConstantLocation(constant));
7483}
7484
7485void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7486 // Will be generated at use site.
7487}
7488
7489void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7490 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7491 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7492}
7493
7494void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7495 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007496 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007497 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007498 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007499}
7500
7501void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7502 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7503 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007504 Location receiver = invoke->GetLocations()->InAt(0);
7505 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007506 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007507
7508 // Set the hidden argument.
7509 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7510 invoke->GetDexMethodIndex());
7511
7512 // temp = object->GetClass();
7513 if (receiver.IsStackSlot()) {
7514 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7515 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7516 } else {
7517 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7518 }
7519 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007520 // Instead of simply (possibly) unpoisoning `temp` here, we should
7521 // emit a read barrier for the previous class reference load.
7522 // However this is not required in practice, as this is an
7523 // intermediate/temporary reference and because the current
7524 // concurrent copying collector keeps the from-space memory
7525 // intact/accessible until the end of the marking phase (the
7526 // concurrent copying collector may not in the future).
7527 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007528 __ LoadFromOffset(kLoadWord, temp, temp,
7529 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7530 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007531 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007532 // temp = temp->GetImtEntryAt(method_offset);
7533 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7534 // T9 = temp->GetEntryPoint();
7535 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7536 // T9();
7537 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007538 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007539 DCHECK(!codegen_->IsLeafMethod());
7540 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7541}
7542
7543void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007544 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7545 if (intrinsic.TryDispatch(invoke)) {
7546 return;
7547 }
7548
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007549 HandleInvoke(invoke);
7550}
7551
7552void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007553 // Explicit clinit checks triggered by static invokes must have been pruned by
7554 // art::PrepareForRegisterAllocation.
7555 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007556
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007557 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007558 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7559 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007560
Chris Larsen701566a2015-10-27 15:29:13 -07007561 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7562 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007563 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7564 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7565 }
Chris Larsen701566a2015-10-27 15:29:13 -07007566 return;
7567 }
7568
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007569 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007570
7571 // Add the extra input register if either the dex cache array base register
7572 // or the PC-relative base register for accessing literals is needed.
7573 if (has_extra_input) {
7574 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7575 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007576}
7577
Orion Hodsonac141392017-01-13 11:53:47 +00007578void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7579 HandleInvoke(invoke);
7580}
7581
7582void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7583 codegen_->GenerateInvokePolymorphicCall(invoke);
7584}
7585
Chris Larsen701566a2015-10-27 15:29:13 -07007586static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007587 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007588 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7589 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007590 return true;
7591 }
7592 return false;
7593}
7594
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007595HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007596 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007597 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007598 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007599 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007600 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007601 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007602 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007603 case HLoadString::LoadKind::kJitTableAddress:
7604 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007605 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007606 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007607 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007608 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007609 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007610 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007611}
7612
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007613HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7614 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007615 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007616 case HLoadClass::LoadKind::kInvalid:
7617 LOG(FATAL) << "UNREACHABLE";
7618 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007619 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007620 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007621 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007622 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007623 case HLoadClass::LoadKind::kBssEntry:
7624 DCHECK(!Runtime::Current()->UseJitCompilation());
7625 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007626 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007627 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007628 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007629 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007630 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007631 break;
7632 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007633 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007634}
7635
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007636Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7637 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007638 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007639 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007640 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7641 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7642 if (!invoke->GetLocations()->Intrinsified()) {
7643 return location.AsRegister<Register>();
7644 }
7645 // For intrinsics we allow any location, so it may be on the stack.
7646 if (!location.IsRegister()) {
7647 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7648 return temp;
7649 }
7650 // For register locations, check if the register was saved. If so, get it from the stack.
7651 // Note: There is a chance that the register was saved but not overwritten, so we could
7652 // save one load. However, since this is just an intrinsic slow path we prefer this
7653 // simple and more robust approach rather that trying to determine if that's the case.
7654 SlowPathCode* slow_path = GetCurrentSlowPath();
7655 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7656 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7657 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7658 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7659 return temp;
7660 }
7661 return location.AsRegister<Register>();
7662}
7663
Vladimir Markodc151b22015-10-15 18:02:30 +01007664HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7665 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007666 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007667 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007668}
7669
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007670void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7671 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007672 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007673 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007674 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7675 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007676 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007677 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7678 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007679 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7680 : ZERO;
7681
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007682 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007683 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007684 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007685 uint32_t offset =
7686 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007687 __ LoadFromOffset(kLoadWord,
7688 temp.AsRegister<Register>(),
7689 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007690 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007691 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007692 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007693 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007694 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007695 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007696 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7697 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007698 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7699 PcRelativePatchInfo* info_low =
7700 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007701 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007702 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7703 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007704 break;
7705 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007706 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7707 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7708 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007709 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007710 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007711 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007712 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7713 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007714 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007715 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7716 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007717 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007718 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007719 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7720 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7721 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007722 }
7723 }
7724
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007725 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007726 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007727 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007728 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007729 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7730 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007731 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007732 T9,
7733 callee_method.AsRegister<Register>(),
7734 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007735 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007736 // T9()
7737 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007738 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007739 break;
7740 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007741 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7742
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007743 DCHECK(!IsLeafMethod());
7744}
7745
7746void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007747 // Explicit clinit checks triggered by static invokes must have been pruned by
7748 // art::PrepareForRegisterAllocation.
7749 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007750
7751 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7752 return;
7753 }
7754
7755 LocationSummary* locations = invoke->GetLocations();
7756 codegen_->GenerateStaticOrDirectCall(invoke,
7757 locations->HasTemps()
7758 ? locations->GetTemp(0)
7759 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007760}
7761
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007762void CodeGeneratorMIPS::GenerateVirtualCall(
7763 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007764 // Use the calling convention instead of the location of the receiver, as
7765 // intrinsics may have put the receiver in a different register. In the intrinsics
7766 // slow path, the arguments have been moved to the right place, so here we are
7767 // guaranteed that the receiver is the first register of the calling convention.
7768 InvokeDexCallingConvention calling_convention;
7769 Register receiver = calling_convention.GetRegisterAt(0);
7770
Chris Larsen3acee732015-11-18 13:31:08 -08007771 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007772 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7773 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7774 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007775 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007776
7777 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007778 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007779 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007780 // Instead of simply (possibly) unpoisoning `temp` here, we should
7781 // emit a read barrier for the previous class reference load.
7782 // However this is not required in practice, as this is an
7783 // intermediate/temporary reference and because the current
7784 // concurrent copying collector keeps the from-space memory
7785 // intact/accessible until the end of the marking phase (the
7786 // concurrent copying collector may not in the future).
7787 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007788 // temp = temp->GetMethodAt(method_offset);
7789 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7790 // T9 = temp->GetEntryPoint();
7791 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7792 // T9();
7793 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007794 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007795 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007796}
7797
7798void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7799 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7800 return;
7801 }
7802
7803 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007804 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007805}
7806
7807void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007808 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007809 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007810 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007811 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7812 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007813 return;
7814 }
Vladimir Marko41559982017-01-06 14:04:23 +00007815 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007816 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007817 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007818 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7819 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007820 ? LocationSummary::kCallOnSlowPath
7821 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007822 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007823 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7824 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7825 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007826 switch (load_kind) {
7827 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007828 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007829 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007830 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007831 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007832 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007833 break;
7834 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007835 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007836 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7837 codegen_->ClobberRA();
7838 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007839 break;
7840 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007841 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007843 locations->SetInAt(0, Location::RequiresRegister());
7844 break;
7845 default:
7846 break;
7847 }
7848 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007849 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7850 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7851 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007852 RegisterSet caller_saves = RegisterSet::Empty();
7853 InvokeRuntimeCallingConvention calling_convention;
7854 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7855 locations->SetCustomSlowPathCallerSaves(caller_saves);
7856 } else {
7857 // For non-Baker read barriers we have a temp-clobbering call.
7858 }
7859 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007860}
7861
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007862// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7863// move.
7864void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007865 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007866 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007867 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007868 return;
7869 }
Vladimir Marko41559982017-01-06 14:04:23 +00007870 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007871
Vladimir Marko41559982017-01-06 14:04:23 +00007872 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007873 Location out_loc = locations->Out();
7874 Register out = out_loc.AsRegister<Register>();
7875 Register base_or_current_method_reg;
7876 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007877 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007878 switch (load_kind) {
7879 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007880 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007881 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007882 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007883 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007884 base_or_current_method_reg =
7885 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007886 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007887 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007888 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007889 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7890 break;
7891 default:
7892 base_or_current_method_reg = ZERO;
7893 break;
7894 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007895
Alexey Frunze15958152017-02-09 19:08:30 -08007896 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7897 ? kWithoutReadBarrier
7898 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007899 bool generate_null_check = false;
7900 switch (load_kind) {
7901 case HLoadClass::LoadKind::kReferrersClass: {
7902 DCHECK(!cls->CanCallRuntime());
7903 DCHECK(!cls->MustGenerateClinitCheck());
7904 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7905 GenerateGcRootFieldLoad(cls,
7906 out_loc,
7907 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007908 ArtMethod::DeclaringClassOffset().Int32Value(),
7909 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007910 break;
7911 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007912 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007913 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007914 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007915 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007916 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007917 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7918 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007919 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7920 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007921 base_or_current_method_reg);
7922 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007923 break;
7924 }
7925 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007926 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007927 uint32_t address = dchecked_integral_cast<uint32_t>(
7928 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7929 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007930 if (isR6 || !has_irreducible_loops) {
7931 __ LoadLiteral(out,
7932 base_or_current_method_reg,
7933 codegen_->DeduplicateBootImageAddressLiteral(address));
7934 } else {
7935 __ LoadConst32(out, address);
7936 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007937 break;
7938 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007939 case HLoadClass::LoadKind::kBootImageClassTable: {
7940 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7941 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7942 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7943 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7944 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7945 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7946 out,
7947 base_or_current_method_reg);
7948 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7949 // Extract the reference from the slot data, i.e. clear the hash bits.
7950 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7951 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7952 if (masked_hash != 0) {
7953 __ Addiu(out, out, -masked_hash);
7954 }
7955 break;
7956 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007957 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007958 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7959 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007960 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7961 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007962 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007963 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007964 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007965 GenerateGcRootFieldLoad(cls,
7966 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007967 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007968 /* placeholder */ 0x5678,
7969 read_barrier_option,
7970 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007971 generate_null_check = true;
7972 break;
7973 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007974 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007975 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7976 cls->GetTypeIndex(),
7977 cls->GetClass());
7978 bool reordering = __ SetReorder(false);
7979 __ Bind(&info->high_label);
7980 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007981 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007982 GenerateGcRootFieldLoad(cls,
7983 out_loc,
7984 out,
7985 /* placeholder */ 0x5678,
7986 read_barrier_option,
7987 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007988 break;
7989 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007990 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007991 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007992 LOG(FATAL) << "UNREACHABLE";
7993 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007994 }
7995
7996 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7997 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007998 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007999 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008000 codegen_->AddSlowPath(slow_path);
8001 if (generate_null_check) {
8002 __ Beqz(out, slow_path->GetEntryLabel());
8003 }
8004 if (cls->MustGenerateClinitCheck()) {
8005 GenerateClassInitializationCheck(slow_path, out);
8006 } else {
8007 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008008 }
8009 }
8010}
8011
8012static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008013 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008014}
8015
8016void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8017 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008018 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008019 locations->SetOut(Location::RequiresRegister());
8020}
8021
8022void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8023 Register out = load->GetLocations()->Out().AsRegister<Register>();
8024 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8025}
8026
8027void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008028 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008029}
8030
8031void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8032 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8033}
8034
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008035void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008036 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008037 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008038 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008039 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008040 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008041 switch (load_kind) {
8042 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008043 case HLoadString::LoadKind::kBootImageAddress:
8044 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008045 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008046 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008047 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008048 break;
8049 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008050 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008051 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8052 codegen_->ClobberRA();
8053 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008054 break;
8055 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008056 FALLTHROUGH_INTENDED;
8057 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008058 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008059 locations->SetInAt(0, Location::RequiresRegister());
8060 break;
8061 default:
8062 break;
8063 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008064 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008065 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008066 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008067 } else {
8068 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008069 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8070 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8071 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008072 RegisterSet caller_saves = RegisterSet::Empty();
8073 InvokeRuntimeCallingConvention calling_convention;
8074 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8075 locations->SetCustomSlowPathCallerSaves(caller_saves);
8076 } else {
8077 // For non-Baker read barriers we have a temp-clobbering call.
8078 }
8079 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008080 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008081}
8082
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008083// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8084// move.
8085void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008086 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008087 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008088 Location out_loc = locations->Out();
8089 Register out = out_loc.AsRegister<Register>();
8090 Register base_or_current_method_reg;
8091 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008092 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008093 switch (load_kind) {
8094 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008095 case HLoadString::LoadKind::kBootImageAddress:
8096 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008097 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008098 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008099 base_or_current_method_reg =
8100 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008101 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008102 default:
8103 base_or_current_method_reg = ZERO;
8104 break;
8105 }
8106
8107 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008108 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008109 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008110 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008111 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008112 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8113 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008114 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8115 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008116 base_or_current_method_reg);
8117 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008118 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008119 }
8120 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008121 uint32_t address = dchecked_integral_cast<uint32_t>(
8122 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8123 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008124 if (isR6 || !has_irreducible_loops) {
8125 __ LoadLiteral(out,
8126 base_or_current_method_reg,
8127 codegen_->DeduplicateBootImageAddressLiteral(address));
8128 } else {
8129 __ LoadConst32(out, address);
8130 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008131 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008132 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008133 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008134 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008135 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008136 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008137 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8138 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008139 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8140 out,
8141 base_or_current_method_reg);
8142 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8143 return;
8144 }
8145 case HLoadString::LoadKind::kBssEntry: {
8146 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8147 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8148 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8149 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8150 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008151 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008152 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008153 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008154 GenerateGcRootFieldLoad(load,
8155 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008156 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008157 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008158 kCompilerReadBarrierOption,
8159 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008160 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008161 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008162 codegen_->AddSlowPath(slow_path);
8163 __ Beqz(out, slow_path->GetEntryLabel());
8164 __ Bind(slow_path->GetExitLabel());
8165 return;
8166 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008167 case HLoadString::LoadKind::kJitTableAddress: {
8168 CodeGeneratorMIPS::JitPatchInfo* info =
8169 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8170 load->GetStringIndex(),
8171 load->GetString());
8172 bool reordering = __ SetReorder(false);
8173 __ Bind(&info->high_label);
8174 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008175 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008176 GenerateGcRootFieldLoad(load,
8177 out_loc,
8178 out,
8179 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008180 kCompilerReadBarrierOption,
8181 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008182 return;
8183 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008184 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008185 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008186 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008187
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008188 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008189 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008190 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008191 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008192 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008193 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8194 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008195}
8196
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008197void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008198 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008199 locations->SetOut(Location::ConstantLocation(constant));
8200}
8201
8202void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8203 // Will be generated at use site.
8204}
8205
8206void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008207 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8208 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008209 InvokeRuntimeCallingConvention calling_convention;
8210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8211}
8212
8213void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8214 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008215 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008216 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8217 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008218 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008219 }
8220 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8221}
8222
8223void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008225 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008226 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008227 case DataType::Type::kInt32:
8228 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008229 locations->SetInAt(0, Location::RequiresRegister());
8230 locations->SetInAt(1, Location::RequiresRegister());
8231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8232 break;
8233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008234 case DataType::Type::kFloat32:
8235 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008236 locations->SetInAt(0, Location::RequiresFpuRegister());
8237 locations->SetInAt(1, Location::RequiresFpuRegister());
8238 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8239 break;
8240
8241 default:
8242 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8243 }
8244}
8245
8246void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008247 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008248 LocationSummary* locations = instruction->GetLocations();
8249 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8250
8251 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008252 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008253 Register dst = locations->Out().AsRegister<Register>();
8254 Register lhs = locations->InAt(0).AsRegister<Register>();
8255 Register rhs = locations->InAt(1).AsRegister<Register>();
8256
8257 if (isR6) {
8258 __ MulR6(dst, lhs, rhs);
8259 } else {
8260 __ MulR2(dst, lhs, rhs);
8261 }
8262 break;
8263 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008264 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008265 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8266 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8267 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8268 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8269 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8270 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8271
8272 // Extra checks to protect caused by the existance of A1_A2.
8273 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8274 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8275 DCHECK_NE(dst_high, lhs_low);
8276 DCHECK_NE(dst_high, rhs_low);
8277
8278 // A_B * C_D
8279 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8280 // dst_lo: [ low(B*D) ]
8281 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8282
8283 if (isR6) {
8284 __ MulR6(TMP, lhs_high, rhs_low);
8285 __ MulR6(dst_high, lhs_low, rhs_high);
8286 __ Addu(dst_high, dst_high, TMP);
8287 __ MuhuR6(TMP, lhs_low, rhs_low);
8288 __ Addu(dst_high, dst_high, TMP);
8289 __ MulR6(dst_low, lhs_low, rhs_low);
8290 } else {
8291 __ MulR2(TMP, lhs_high, rhs_low);
8292 __ MulR2(dst_high, lhs_low, rhs_high);
8293 __ Addu(dst_high, dst_high, TMP);
8294 __ MultuR2(lhs_low, rhs_low);
8295 __ Mfhi(TMP);
8296 __ Addu(dst_high, dst_high, TMP);
8297 __ Mflo(dst_low);
8298 }
8299 break;
8300 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008301 case DataType::Type::kFloat32:
8302 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008303 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8304 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8305 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008306 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008307 __ MulS(dst, lhs, rhs);
8308 } else {
8309 __ MulD(dst, lhs, rhs);
8310 }
8311 break;
8312 }
8313 default:
8314 LOG(FATAL) << "Unexpected mul type " << type;
8315 }
8316}
8317
8318void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8319 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008320 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008321 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008322 case DataType::Type::kInt32:
8323 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008324 locations->SetInAt(0, Location::RequiresRegister());
8325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8326 break;
8327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008328 case DataType::Type::kFloat32:
8329 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008330 locations->SetInAt(0, Location::RequiresFpuRegister());
8331 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8332 break;
8333
8334 default:
8335 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8336 }
8337}
8338
8339void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008340 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008341 LocationSummary* locations = instruction->GetLocations();
8342
8343 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008344 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345 Register dst = locations->Out().AsRegister<Register>();
8346 Register src = locations->InAt(0).AsRegister<Register>();
8347 __ Subu(dst, ZERO, src);
8348 break;
8349 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008350 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008351 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8352 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8353 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8354 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8355 __ Subu(dst_low, ZERO, src_low);
8356 __ Sltu(TMP, ZERO, dst_low);
8357 __ Subu(dst_high, ZERO, src_high);
8358 __ Subu(dst_high, dst_high, TMP);
8359 break;
8360 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008361 case DataType::Type::kFloat32:
8362 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008363 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8364 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008365 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 __ NegS(dst, src);
8367 } else {
8368 __ NegD(dst, src);
8369 }
8370 break;
8371 }
8372 default:
8373 LOG(FATAL) << "Unexpected neg type " << type;
8374 }
8375}
8376
8377void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008378 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8379 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008380 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008381 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008382 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8383 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008384}
8385
8386void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008387 // Note: if heap poisoning is enabled, the entry point takes care
8388 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008389 QuickEntrypointEnum entrypoint =
8390 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8391 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008392 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008393 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008394}
8395
8396void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008397 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8398 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008399 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008400 if (instruction->IsStringAlloc()) {
8401 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8402 } else {
8403 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008404 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008405 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008406}
8407
8408void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008409 // Note: if heap poisoning is enabled, the entry point takes care
8410 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008411 if (instruction->IsStringAlloc()) {
8412 // String is allocated through StringFactory. Call NewEmptyString entry point.
8413 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008414 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008415 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8416 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8417 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008418 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008419 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8420 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008421 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008422 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008424}
8425
8426void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008427 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008428 locations->SetInAt(0, Location::RequiresRegister());
8429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8430}
8431
8432void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008433 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008434 LocationSummary* locations = instruction->GetLocations();
8435
8436 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008437 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008438 Register dst = locations->Out().AsRegister<Register>();
8439 Register src = locations->InAt(0).AsRegister<Register>();
8440 __ Nor(dst, src, ZERO);
8441 break;
8442 }
8443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008444 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008445 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8446 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8447 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8448 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8449 __ Nor(dst_high, src_high, ZERO);
8450 __ Nor(dst_low, src_low, ZERO);
8451 break;
8452 }
8453
8454 default:
8455 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8456 }
8457}
8458
8459void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008460 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008461 locations->SetInAt(0, Location::RequiresRegister());
8462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8463}
8464
8465void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8466 LocationSummary* locations = instruction->GetLocations();
8467 __ Xori(locations->Out().AsRegister<Register>(),
8468 locations->InAt(0).AsRegister<Register>(),
8469 1);
8470}
8471
8472void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008473 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8474 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475}
8476
Calin Juravle2ae48182016-03-16 14:05:09 +00008477void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8478 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008479 return;
8480 }
8481 Location obj = instruction->GetLocations()->InAt(0);
8482
8483 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008484 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485}
8486
Calin Juravle2ae48182016-03-16 14:05:09 +00008487void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008488 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008489 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008490
8491 Location obj = instruction->GetLocations()->InAt(0);
8492
8493 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8494}
8495
8496void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008497 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008498}
8499
8500void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8501 HandleBinaryOp(instruction);
8502}
8503
8504void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8505 HandleBinaryOp(instruction);
8506}
8507
8508void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8509 LOG(FATAL) << "Unreachable";
8510}
8511
8512void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008513 if (instruction->GetNext()->IsSuspendCheck() &&
8514 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8515 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8516 // The back edge will generate the suspend check.
8517 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8518 }
8519
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8521}
8522
8523void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008524 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8526 if (location.IsStackSlot()) {
8527 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8528 } else if (location.IsDoubleStackSlot()) {
8529 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8530 }
8531 locations->SetOut(location);
8532}
8533
8534void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8535 ATTRIBUTE_UNUSED) {
8536 // Nothing to do, the parameter is already at its location.
8537}
8538
8539void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8540 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008541 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8546 ATTRIBUTE_UNUSED) {
8547 // Nothing to do, the method is already at its location.
8548}
8549
8550void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008551 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008552 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008553 locations->SetInAt(i, Location::Any());
8554 }
8555 locations->SetOut(Location::Any());
8556}
8557
8558void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8559 LOG(FATAL) << "Unreachable";
8560}
8561
8562void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008563 DataType::Type type = rem->GetResultType();
8564 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8565 ? LocationSummary::kNoCall
8566 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008567 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568
8569 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008570 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008571 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008572 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8574 break;
8575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008576 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008577 InvokeRuntimeCallingConvention calling_convention;
8578 locations->SetInAt(0, Location::RegisterPairLocation(
8579 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8580 locations->SetInAt(1, Location::RegisterPairLocation(
8581 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8582 locations->SetOut(calling_convention.GetReturnLocation(type));
8583 break;
8584 }
8585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008586 case DataType::Type::kFloat32:
8587 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008588 InvokeRuntimeCallingConvention calling_convention;
8589 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8590 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8591 locations->SetOut(calling_convention.GetReturnLocation(type));
8592 break;
8593 }
8594
8595 default:
8596 LOG(FATAL) << "Unexpected rem type " << type;
8597 }
8598}
8599
8600void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008601 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008602
8603 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008604 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008605 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008607 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008608 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008609 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8610 break;
8611 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008612 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008613 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008614 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008615 break;
8616 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008617 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008618 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008619 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008620 break;
8621 }
8622 default:
8623 LOG(FATAL) << "Unexpected rem type " << type;
8624 }
8625}
8626
Igor Murashkind01745e2017-04-05 16:40:31 -07008627void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8628 constructor_fence->SetLocations(nullptr);
8629}
8630
8631void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8632 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8633 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8634}
8635
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008636void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8637 memory_barrier->SetLocations(nullptr);
8638}
8639
8640void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8641 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8642}
8643
8644void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008645 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008646 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008647 locations->SetInAt(0, MipsReturnLocation(return_type));
8648}
8649
8650void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8651 codegen_->GenerateFrameExit();
8652}
8653
8654void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8655 ret->SetLocations(nullptr);
8656}
8657
8658void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8659 codegen_->GenerateFrameExit();
8660}
8661
Alexey Frunze92d90602015-12-18 18:16:36 -08008662void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8663 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008664}
8665
Alexey Frunze92d90602015-12-18 18:16:36 -08008666void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8667 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008668}
8669
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008670void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8671 HandleShift(shl);
8672}
8673
8674void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8675 HandleShift(shl);
8676}
8677
8678void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8679 HandleShift(shr);
8680}
8681
8682void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8683 HandleShift(shr);
8684}
8685
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008686void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8687 HandleBinaryOp(instruction);
8688}
8689
8690void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8691 HandleBinaryOp(instruction);
8692}
8693
8694void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8695 HandleFieldGet(instruction, instruction->GetFieldInfo());
8696}
8697
8698void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8699 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8700}
8701
8702void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8703 HandleFieldSet(instruction, instruction->GetFieldInfo());
8704}
8705
8706void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008707 HandleFieldSet(instruction,
8708 instruction->GetFieldInfo(),
8709 instruction->GetDexPc(),
8710 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008711}
8712
8713void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8714 HUnresolvedInstanceFieldGet* instruction) {
8715 FieldAccessCallingConventionMIPS calling_convention;
8716 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8717 instruction->GetFieldType(),
8718 calling_convention);
8719}
8720
8721void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8722 HUnresolvedInstanceFieldGet* instruction) {
8723 FieldAccessCallingConventionMIPS calling_convention;
8724 codegen_->GenerateUnresolvedFieldAccess(instruction,
8725 instruction->GetFieldType(),
8726 instruction->GetFieldIndex(),
8727 instruction->GetDexPc(),
8728 calling_convention);
8729}
8730
8731void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8732 HUnresolvedInstanceFieldSet* instruction) {
8733 FieldAccessCallingConventionMIPS calling_convention;
8734 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8735 instruction->GetFieldType(),
8736 calling_convention);
8737}
8738
8739void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8740 HUnresolvedInstanceFieldSet* instruction) {
8741 FieldAccessCallingConventionMIPS calling_convention;
8742 codegen_->GenerateUnresolvedFieldAccess(instruction,
8743 instruction->GetFieldType(),
8744 instruction->GetFieldIndex(),
8745 instruction->GetDexPc(),
8746 calling_convention);
8747}
8748
8749void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8750 HUnresolvedStaticFieldGet* instruction) {
8751 FieldAccessCallingConventionMIPS calling_convention;
8752 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8753 instruction->GetFieldType(),
8754 calling_convention);
8755}
8756
8757void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8758 HUnresolvedStaticFieldGet* instruction) {
8759 FieldAccessCallingConventionMIPS calling_convention;
8760 codegen_->GenerateUnresolvedFieldAccess(instruction,
8761 instruction->GetFieldType(),
8762 instruction->GetFieldIndex(),
8763 instruction->GetDexPc(),
8764 calling_convention);
8765}
8766
8767void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8768 HUnresolvedStaticFieldSet* instruction) {
8769 FieldAccessCallingConventionMIPS calling_convention;
8770 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8771 instruction->GetFieldType(),
8772 calling_convention);
8773}
8774
8775void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8776 HUnresolvedStaticFieldSet* instruction) {
8777 FieldAccessCallingConventionMIPS calling_convention;
8778 codegen_->GenerateUnresolvedFieldAccess(instruction,
8779 instruction->GetFieldType(),
8780 instruction->GetFieldIndex(),
8781 instruction->GetDexPc(),
8782 calling_convention);
8783}
8784
8785void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008786 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8787 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008788 // In suspend check slow path, usually there are no caller-save registers at all.
8789 // If SIMD instructions are present, however, we force spilling all live SIMD
8790 // registers in full width (since the runtime only saves/restores lower part).
8791 locations->SetCustomSlowPathCallerSaves(
8792 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008793}
8794
8795void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8796 HBasicBlock* block = instruction->GetBlock();
8797 if (block->GetLoopInformation() != nullptr) {
8798 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8799 // The back edge will generate the suspend check.
8800 return;
8801 }
8802 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8803 // The goto will generate the suspend check.
8804 return;
8805 }
8806 GenerateSuspendCheck(instruction, nullptr);
8807}
8808
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008809void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008810 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8811 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008812 InvokeRuntimeCallingConvention calling_convention;
8813 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8814}
8815
8816void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008817 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008818 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8819}
8820
8821void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008822 DataType::Type input_type = conversion->GetInputType();
8823 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008824 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8825 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008826 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008828 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8829 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008830 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8831 }
8832
8833 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008834 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008835 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8836 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008837 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008838 }
8839
Vladimir Markoca6fff82017-10-03 14:49:14 +01008840 LocationSummary* locations =
8841 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008842
8843 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008844 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008845 locations->SetInAt(0, Location::RequiresFpuRegister());
8846 } else {
8847 locations->SetInAt(0, Location::RequiresRegister());
8848 }
8849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008850 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008851 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8852 } else {
8853 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8854 }
8855 } else {
8856 InvokeRuntimeCallingConvention calling_convention;
8857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008858 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008859 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8860 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008861 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008862 locations->SetInAt(0, Location::RegisterPairLocation(
8863 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8864 }
8865
8866 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8867 }
8868}
8869
8870void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8871 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008872 DataType::Type result_type = conversion->GetResultType();
8873 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008874 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008875 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008876
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008877 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8878 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008880 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008881 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8882 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8883 Register src = locations->InAt(0).AsRegister<Register>();
8884
Alexey Frunzea871ef12016-06-27 15:20:11 -07008885 if (dst_low != src) {
8886 __ Move(dst_low, src);
8887 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008888 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008889 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008890 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008891 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008892 ? locations->InAt(0).AsRegisterPairLow<Register>()
8893 : locations->InAt(0).AsRegister<Register>();
8894
8895 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008896 case DataType::Type::kUint8:
8897 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008898 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008899 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008900 if (has_sign_extension) {
8901 __ Seb(dst, src);
8902 } else {
8903 __ Sll(dst, src, 24);
8904 __ Sra(dst, dst, 24);
8905 }
8906 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008907 case DataType::Type::kUint16:
8908 __ Andi(dst, src, 0xFFFF);
8909 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008910 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008911 if (has_sign_extension) {
8912 __ Seh(dst, src);
8913 } else {
8914 __ Sll(dst, src, 16);
8915 __ Sra(dst, dst, 16);
8916 }
8917 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008918 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008919 if (dst != src) {
8920 __ Move(dst, src);
8921 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008922 break;
8923
8924 default:
8925 LOG(FATAL) << "Unexpected type conversion from " << input_type
8926 << " to " << result_type;
8927 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008928 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8929 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008930 if (isR6) {
8931 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8932 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8933 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8934 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8935 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8936 __ Mtc1(src_low, FTMP);
8937 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008938 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008939 __ Cvtsl(dst, FTMP);
8940 } else {
8941 __ Cvtdl(dst, FTMP);
8942 }
8943 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008944 QuickEntrypointEnum entrypoint =
8945 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008946 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008947 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008948 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8949 } else {
8950 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8951 }
8952 }
8953 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008954 Register src = locations->InAt(0).AsRegister<Register>();
8955 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8956 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008957 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008958 __ Cvtsw(dst, FTMP);
8959 } else {
8960 __ Cvtdw(dst, FTMP);
8961 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008962 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008963 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8964 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008965
8966 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8967 // value of the output type if the input is outside of the range after the truncation or
8968 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8969 // results. This matches the desired float/double-to-int/long conversion exactly.
8970 //
8971 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8972 // value when the input is either a NaN or is outside of the range of the output type
8973 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8974 // the same result.
8975 //
8976 // The code takes care of the different behaviors by first comparing the input to the
8977 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8978 // If the input is greater than or equal to the minimum, it procedes to the truncate
8979 // instruction, which will handle such an input the same way irrespective of NAN2008.
8980 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8981 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008982 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008983 if (isR6) {
8984 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8985 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8986 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8987 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8988 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008990 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008991 __ TruncLS(FTMP, src);
8992 } else {
8993 __ TruncLD(FTMP, src);
8994 }
8995 __ Mfc1(dst_low, FTMP);
8996 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008997 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008998 QuickEntrypointEnum entrypoint =
8999 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009000 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009001 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009002 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9003 } else {
9004 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9005 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009006 }
9007 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009008 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9009 Register dst = locations->Out().AsRegister<Register>();
9010 MipsLabel truncate;
9011 MipsLabel done;
9012
Lena Djokicf4e23a82017-05-09 15:43:45 +02009013 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009014 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009015 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9016 __ LoadConst32(TMP, min_val);
9017 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009018 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009019 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9020 __ LoadConst32(TMP, High32Bits(min_val));
9021 __ Mtc1(ZERO, FTMP);
9022 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009023 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009024
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009025 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009026 __ ColeS(0, FTMP, src);
9027 } else {
9028 __ ColeD(0, FTMP, src);
9029 }
9030 __ Bc1t(0, &truncate);
9031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009032 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009033 __ CeqS(0, src, src);
9034 } else {
9035 __ CeqD(0, src, src);
9036 }
9037 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9038 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009039
9040 __ B(&done);
9041
9042 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009043 }
9044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009045 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009046 __ TruncWS(FTMP, src);
9047 } else {
9048 __ TruncWD(FTMP, src);
9049 }
9050 __ Mfc1(dst, FTMP);
9051
Lena Djokicf4e23a82017-05-09 15:43:45 +02009052 if (!isR6) {
9053 __ Bind(&done);
9054 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009055 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009056 } else if (DataType::IsFloatingPointType(result_type) &&
9057 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009058 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9059 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009060 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009061 __ Cvtsd(dst, src);
9062 } else {
9063 __ Cvtds(dst, src);
9064 }
9065 } else {
9066 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9067 << " to " << result_type;
9068 }
9069}
9070
9071void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9072 HandleShift(ushr);
9073}
9074
9075void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9076 HandleShift(ushr);
9077}
9078
9079void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9080 HandleBinaryOp(instruction);
9081}
9082
9083void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9084 HandleBinaryOp(instruction);
9085}
9086
9087void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9088 // Nothing to do, this should be removed during prepare for register allocator.
9089 LOG(FATAL) << "Unreachable";
9090}
9091
9092void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9093 // Nothing to do, this should be removed during prepare for register allocator.
9094 LOG(FATAL) << "Unreachable";
9095}
9096
9097void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009098 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009099}
9100
9101void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009102 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009103}
9104
9105void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009106 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009107}
9108
9109void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009110 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009111}
9112
9113void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009114 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009115}
9116
9117void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009118 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009119}
9120
9121void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009122 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009123}
9124
9125void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009126 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009127}
9128
9129void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009130 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009131}
9132
9133void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009134 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009135}
9136
9137void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009138 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009139}
9140
9141void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009142 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009143}
9144
9145void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009146 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009147}
9148
9149void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009150 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009151}
9152
9153void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009154 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009155}
9156
9157void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009158 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009159}
9160
9161void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009162 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009163}
9164
9165void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009166 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009167}
9168
9169void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009170 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009171}
9172
9173void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009174 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009175}
9176
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009177void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9178 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009179 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009180 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009181 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9182 uint32_t num_entries = switch_instr->GetNumEntries();
9183 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9184 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9185 // instruction to simulate PC-relative addressing when accessing the jump table.
9186 // NAL clobbers RA. Make sure RA is preserved.
9187 codegen_->ClobberRA();
9188 }
9189 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009190}
9191
Alexey Frunze96b66822016-09-10 02:32:44 -07009192void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9193 int32_t lower_bound,
9194 uint32_t num_entries,
9195 HBasicBlock* switch_block,
9196 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009197 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009198 Register temp_reg = TMP;
9199 __ Addiu32(temp_reg, value_reg, -lower_bound);
9200 // Jump to default if index is negative
9201 // Note: We don't check the case that index is positive while value < lower_bound, because in
9202 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9203 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9204
Alexey Frunze96b66822016-09-10 02:32:44 -07009205 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009206 // Jump to successors[0] if value == lower_bound.
9207 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9208 int32_t last_index = 0;
9209 for (; num_entries - last_index > 2; last_index += 2) {
9210 __ Addiu(temp_reg, temp_reg, -2);
9211 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9212 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9213 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9214 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9215 }
9216 if (num_entries - last_index == 2) {
9217 // The last missing case_value.
9218 __ Addiu(temp_reg, temp_reg, -1);
9219 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009220 }
9221
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009222 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009223 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009224 __ B(codegen_->GetLabelOf(default_block));
9225 }
9226}
9227
Alexey Frunze96b66822016-09-10 02:32:44 -07009228void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9229 Register constant_area,
9230 int32_t lower_bound,
9231 uint32_t num_entries,
9232 HBasicBlock* switch_block,
9233 HBasicBlock* default_block) {
9234 // Create a jump table.
9235 std::vector<MipsLabel*> labels(num_entries);
9236 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9237 for (uint32_t i = 0; i < num_entries; i++) {
9238 labels[i] = codegen_->GetLabelOf(successors[i]);
9239 }
9240 JumpTable* table = __ CreateJumpTable(std::move(labels));
9241
9242 // Is the value in range?
9243 __ Addiu32(TMP, value_reg, -lower_bound);
9244 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9245 __ Sltiu(AT, TMP, num_entries);
9246 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9247 } else {
9248 __ LoadConst32(AT, num_entries);
9249 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9250 }
9251
9252 // We are in the range of the table.
9253 // Load the target address from the jump table, indexing by the value.
9254 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009255 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009256 __ Lw(TMP, TMP, 0);
9257 // Compute the absolute target address by adding the table start address
9258 // (the table contains offsets to targets relative to its start).
9259 __ Addu(TMP, TMP, AT);
9260 // And jump.
9261 __ Jr(TMP);
9262 __ NopIfNoReordering();
9263}
9264
9265void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9266 int32_t lower_bound = switch_instr->GetStartValue();
9267 uint32_t num_entries = switch_instr->GetNumEntries();
9268 LocationSummary* locations = switch_instr->GetLocations();
9269 Register value_reg = locations->InAt(0).AsRegister<Register>();
9270 HBasicBlock* switch_block = switch_instr->GetBlock();
9271 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9272
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009273 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009274 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009275 //
9276 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9277 // to access the jump table and it is implemented by changing HPackedSwitch to
9278 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9279 // VisitMipsPackedSwitch()).
9280 //
9281 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9282 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9283 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009284 GenTableBasedPackedSwitch(value_reg,
9285 ZERO,
9286 lower_bound,
9287 num_entries,
9288 switch_block,
9289 default_block);
9290 } else {
9291 GenPackedSwitchWithCompares(value_reg,
9292 lower_bound,
9293 num_entries,
9294 switch_block,
9295 default_block);
9296 }
9297}
9298
9299void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9300 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009301 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009302 locations->SetInAt(0, Location::RequiresRegister());
9303 // Constant area pointer (HMipsComputeBaseMethodAddress).
9304 locations->SetInAt(1, Location::RequiresRegister());
9305}
9306
9307void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9308 int32_t lower_bound = switch_instr->GetStartValue();
9309 uint32_t num_entries = switch_instr->GetNumEntries();
9310 LocationSummary* locations = switch_instr->GetLocations();
9311 Register value_reg = locations->InAt(0).AsRegister<Register>();
9312 Register constant_area = locations->InAt(1).AsRegister<Register>();
9313 HBasicBlock* switch_block = switch_instr->GetBlock();
9314 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9315
9316 // This is an R2-only path. HPackedSwitch has been changed to
9317 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9318 // required to address the jump table relative to PC.
9319 GenTableBasedPackedSwitch(value_reg,
9320 constant_area,
9321 lower_bound,
9322 num_entries,
9323 switch_block,
9324 default_block);
9325}
9326
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009327void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9328 HMipsComputeBaseMethodAddress* insn) {
9329 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009330 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009331 locations->SetOut(Location::RequiresRegister());
9332}
9333
9334void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9335 HMipsComputeBaseMethodAddress* insn) {
9336 LocationSummary* locations = insn->GetLocations();
9337 Register reg = locations->Out().AsRegister<Register>();
9338
9339 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9340
9341 // Generate a dummy PC-relative call to obtain PC.
9342 __ Nal();
9343 // Grab the return address off RA.
9344 __ Move(reg, RA);
9345
9346 // Remember this offset (the obtained PC value) for later use with constant area.
9347 __ BindPcRelBaseLabel();
9348}
9349
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009350void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9351 // The trampoline uses the same calling convention as dex calling conventions,
9352 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9353 // the method_idx.
9354 HandleInvoke(invoke);
9355}
9356
9357void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9358 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9359}
9360
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009361void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9362 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009363 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009364 locations->SetInAt(0, Location::RequiresRegister());
9365 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009366}
9367
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009368void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9369 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009370 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009371 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009372 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009373 __ LoadFromOffset(kLoadWord,
9374 locations->Out().AsRegister<Register>(),
9375 locations->InAt(0).AsRegister<Register>(),
9376 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009377 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009378 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009379 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009380 __ LoadFromOffset(kLoadWord,
9381 locations->Out().AsRegister<Register>(),
9382 locations->InAt(0).AsRegister<Register>(),
9383 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009384 __ LoadFromOffset(kLoadWord,
9385 locations->Out().AsRegister<Register>(),
9386 locations->Out().AsRegister<Register>(),
9387 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009388 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009389}
9390
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009391void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9392 ATTRIBUTE_UNUSED) {
9393 LOG(FATAL) << "Unreachable";
9394}
9395
9396void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9397 ATTRIBUTE_UNUSED) {
9398 LOG(FATAL) << "Unreachable";
9399}
9400
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009401#undef __
9402#undef QUICK_ENTRY_POINT
9403
9404} // namespace mips
9405} // namespace art