blob: d6922d2f3fdffc41ab8b239f531c709a111af1da [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()) {
1098 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1099 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001100 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001101 __ MovS(FTMP, f2);
1102 __ MovS(f2, f1);
1103 __ MovS(f1, FTMP);
1104 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001105 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106 __ MovD(FTMP, f2);
1107 __ MovD(f2, f1);
1108 __ MovD(f1, FTMP);
1109 }
1110 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1111 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1112 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001113 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001114 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1115 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001116 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001117 __ Move(TMP, r2);
1118 __ Mfc1(r2, f1);
1119 __ Mtc1(TMP, f1);
1120 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1121 // Swap 2 GPR register pairs.
1122 Register r1 = loc1.AsRegisterPairLow<Register>();
1123 Register r2 = loc2.AsRegisterPairLow<Register>();
1124 __ Move(TMP, r2);
1125 __ Move(r2, r1);
1126 __ Move(r1, TMP);
1127 r1 = loc1.AsRegisterPairHigh<Register>();
1128 r2 = loc2.AsRegisterPairHigh<Register>();
1129 __ Move(TMP, r2);
1130 __ Move(r2, r1);
1131 __ Move(r1, TMP);
1132 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1133 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1134 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001135 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001136 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1137 : loc2.AsFpuRegister<FRegister>();
1138 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1139 : loc2.AsRegisterPairLow<Register>();
1140 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1141 : loc2.AsRegisterPairHigh<Register>();
1142 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1143 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1144 // unpredictable and the following mfch1 will fail.
1145 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001146 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001147 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001148 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001149 __ Move(r2_l, TMP);
1150 __ Move(r2_h, AT);
1151 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1152 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1153 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1154 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001155 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1156 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001157 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1158 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001159 __ Move(TMP, reg);
1160 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1161 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1162 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1163 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1164 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1165 : loc2.AsRegisterPairLow<Register>();
1166 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1167 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001168 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001169 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1170 : loc2.GetHighStackIndex(kMipsWordSize);
1171 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001172 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001173 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001174 __ Move(TMP, reg_h);
1175 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1176 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001177 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1178 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1179 : loc2.AsFpuRegister<FRegister>();
1180 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001182 __ MovS(FTMP, reg);
1183 __ LoadSFromOffset(reg, SP, offset);
1184 __ StoreSToOffset(FTMP, SP, offset);
1185 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001186 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001187 __ MovD(FTMP, reg);
1188 __ LoadDFromOffset(reg, SP, offset);
1189 __ StoreDToOffset(FTMP, SP, offset);
1190 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001191 } else {
1192 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1193 }
1194}
1195
1196void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1197 __ Pop(static_cast<Register>(reg));
1198}
1199
1200void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1201 __ Push(static_cast<Register>(reg));
1202}
1203
1204void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1205 // Allocate a scratch register other than TMP, if available.
1206 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1207 // automatically unspilled when the scratch scope object is destroyed).
1208 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1209 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001210 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001211 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1212 __ LoadFromOffset(kLoadWord,
1213 Register(ensure_scratch.GetRegister()),
1214 SP,
1215 index1 + stack_offset);
1216 __ LoadFromOffset(kLoadWord,
1217 TMP,
1218 SP,
1219 index2 + stack_offset);
1220 __ StoreToOffset(kStoreWord,
1221 Register(ensure_scratch.GetRegister()),
1222 SP,
1223 index2 + stack_offset);
1224 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1225 }
1226}
1227
Alexey Frunze73296a72016-06-03 22:51:46 -07001228void CodeGeneratorMIPS::ComputeSpillMask() {
1229 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1230 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1231 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1232 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1233 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1234 // within the stack frame.
1235 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1236 core_spill_mask_ |= (1 << ZERO);
1237 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001238}
1239
1240bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001241 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001242 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1243 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1244 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001245 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001246}
1247
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001248static dwarf::Reg DWARFReg(Register reg) {
1249 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1250}
1251
1252// TODO: mapping of floating-point registers to DWARF.
1253
1254void CodeGeneratorMIPS::GenerateFrameEntry() {
1255 __ Bind(&frame_entry_label_);
1256
Vladimir Marko33bff252017-11-01 14:35:42 +00001257 bool do_overflow_check =
1258 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259
1260 if (do_overflow_check) {
1261 __ LoadFromOffset(kLoadWord,
1262 ZERO,
1263 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001264 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001265 RecordPcInfo(nullptr, 0);
1266 }
1267
1268 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001269 CHECK_EQ(fpu_spill_mask_, 0u);
1270 CHECK_EQ(core_spill_mask_, 1u << RA);
1271 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001272 return;
1273 }
1274
1275 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001276 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1277 LOG(FATAL) << "Stack frame larger than "
1278 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001279 }
1280
1281 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282
Alexey Frunze73296a72016-06-03 22:51:46 -07001283 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284 __ IncreaseFrameSize(ofs);
1285
Alexey Frunze73296a72016-06-03 22:51:46 -07001286 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1287 Register reg = static_cast<Register>(MostSignificantBit(mask));
1288 mask ^= 1u << reg;
1289 ofs -= kMipsWordSize;
1290 // The ZERO register is only included for alignment.
1291 if (reg != ZERO) {
1292 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001293 __ cfi().RelOffset(DWARFReg(reg), ofs);
1294 }
1295 }
1296
Alexey Frunze73296a72016-06-03 22:51:46 -07001297 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1298 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1299 mask ^= 1u << reg;
1300 ofs -= kMipsDoublewordSize;
1301 __ StoreDToOffset(reg, SP, ofs);
1302 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001303 }
1304
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001305 // Save the current method if we need it. Note that we do not
1306 // do this in HCurrentMethod, as the instruction might have been removed
1307 // in the SSA graph.
1308 if (RequiresCurrentMethod()) {
1309 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1310 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001311
1312 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1313 // Initialize should deoptimize flag to 0.
1314 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1315 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316}
1317
1318void CodeGeneratorMIPS::GenerateFrameExit() {
1319 __ cfi().RememberState();
1320
1321 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001322 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001323
Alexey Frunze73296a72016-06-03 22:51:46 -07001324 // For better instruction scheduling restore RA before other registers.
1325 uint32_t ofs = GetFrameSize();
1326 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1327 Register reg = static_cast<Register>(MostSignificantBit(mask));
1328 mask ^= 1u << reg;
1329 ofs -= kMipsWordSize;
1330 // The ZERO register is only included for alignment.
1331 if (reg != ZERO) {
1332 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001333 __ cfi().Restore(DWARFReg(reg));
1334 }
1335 }
1336
Alexey Frunze73296a72016-06-03 22:51:46 -07001337 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1338 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1339 mask ^= 1u << reg;
1340 ofs -= kMipsDoublewordSize;
1341 __ LoadDFromOffset(reg, SP, ofs);
1342 // TODO: __ cfi().Restore(DWARFReg(reg));
1343 }
1344
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001345 size_t frame_size = GetFrameSize();
1346 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1347 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1348 bool reordering = __ SetReorder(false);
1349 if (exchange) {
1350 __ Jr(RA);
1351 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1352 } else {
1353 __ DecreaseFrameSize(frame_size);
1354 __ Jr(RA);
1355 __ Nop(); // In delay slot.
1356 }
1357 __ SetReorder(reordering);
1358 } else {
1359 __ Jr(RA);
1360 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001361 }
1362
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001363 __ cfi().RestoreState();
1364 __ cfi().DefCFAOffset(GetFrameSize());
1365}
1366
1367void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1368 __ Bind(GetLabelOf(block));
1369}
1370
Lena Djokicca8c2952017-05-29 11:31:46 +02001371VectorRegister VectorRegisterFrom(Location location) {
1372 DCHECK(location.IsFpuRegister());
1373 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1374}
1375
Lena Djokic8098da92017-06-28 12:07:50 +02001376void CodeGeneratorMIPS::MoveLocation(Location destination,
1377 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001378 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 if (source.Equals(destination)) {
1380 return;
1381 }
1382
Lena Djokic8098da92017-06-28 12:07:50 +02001383 if (source.IsConstant()) {
1384 MoveConstant(destination, source.GetConstant());
1385 } else {
1386 if (destination.IsRegister()) {
1387 if (source.IsRegister()) {
1388 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1389 } else if (source.IsFpuRegister()) {
1390 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1391 } else {
1392 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001393 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001394 }
1395 } else if (destination.IsRegisterPair()) {
1396 if (source.IsRegisterPair()) {
1397 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1398 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1399 } else if (source.IsFpuRegister()) {
1400 Register dst_high = destination.AsRegisterPairHigh<Register>();
1401 Register dst_low = destination.AsRegisterPairLow<Register>();
1402 FRegister src = source.AsFpuRegister<FRegister>();
1403 __ Mfc1(dst_low, src);
1404 __ MoveFromFpuHigh(dst_high, src);
1405 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001406 DCHECK(source.IsDoubleStackSlot())
1407 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001408 int32_t off = source.GetStackIndex();
1409 Register r = destination.AsRegisterPairLow<Register>();
1410 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1411 }
1412 } else if (destination.IsFpuRegister()) {
1413 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001414 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001415 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1416 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001417 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001418 FRegister dst = destination.AsFpuRegister<FRegister>();
1419 Register src_high = source.AsRegisterPairHigh<Register>();
1420 Register src_low = source.AsRegisterPairLow<Register>();
1421 __ Mtc1(src_low, dst);
1422 __ MoveToFpuHigh(src_high, dst);
1423 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001424 if (GetGraph()->HasSIMD()) {
1425 __ MoveV(VectorRegisterFrom(destination),
1426 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001427 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001428 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001429 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1430 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001431 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001432 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1433 }
Lena Djokic8098da92017-06-28 12:07:50 +02001434 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001435 } else if (source.IsSIMDStackSlot()) {
1436 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001437 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001438 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001439 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1440 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001441 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001442 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1443 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1444 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001445 } else if (destination.IsSIMDStackSlot()) {
1446 if (source.IsFpuRegister()) {
1447 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1448 } else {
1449 DCHECK(source.IsSIMDStackSlot());
1450 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1451 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1452 }
Lena Djokic8098da92017-06-28 12:07:50 +02001453 } else if (destination.IsDoubleStackSlot()) {
1454 int32_t dst_offset = destination.GetStackIndex();
1455 if (source.IsRegisterPair()) {
1456 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1457 } else if (source.IsFpuRegister()) {
1458 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1459 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001460 DCHECK(source.IsDoubleStackSlot())
1461 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001462 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1463 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1464 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1465 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1466 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001467 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001468 DCHECK(destination.IsStackSlot()) << destination;
1469 int32_t dst_offset = destination.GetStackIndex();
1470 if (source.IsRegister()) {
1471 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1472 } else if (source.IsFpuRegister()) {
1473 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1474 } else {
1475 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1476 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1477 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1478 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479 }
1480 }
1481}
1482
1483void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1484 if (c->IsIntConstant() || c->IsNullConstant()) {
1485 // Move 32 bit constant.
1486 int32_t value = GetInt32ValueOf(c);
1487 if (destination.IsRegister()) {
1488 Register dst = destination.AsRegister<Register>();
1489 __ LoadConst32(dst, value);
1490 } else {
1491 DCHECK(destination.IsStackSlot())
1492 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001493 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001494 }
1495 } else if (c->IsLongConstant()) {
1496 // Move 64 bit constant.
1497 int64_t value = GetInt64ValueOf(c);
1498 if (destination.IsRegisterPair()) {
1499 Register r_h = destination.AsRegisterPairHigh<Register>();
1500 Register r_l = destination.AsRegisterPairLow<Register>();
1501 __ LoadConst64(r_h, r_l, value);
1502 } else {
1503 DCHECK(destination.IsDoubleStackSlot())
1504 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001505 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001506 }
1507 } else if (c->IsFloatConstant()) {
1508 // Move 32 bit float constant.
1509 int32_t value = GetInt32ValueOf(c);
1510 if (destination.IsFpuRegister()) {
1511 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
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 {
1518 // Move 64 bit double constant.
1519 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1520 int64_t value = GetInt64ValueOf(c);
1521 if (destination.IsFpuRegister()) {
1522 FRegister fd = destination.AsFpuRegister<FRegister>();
1523 __ LoadDConst64(fd, value, TMP);
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 }
1530}
1531
1532void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1533 DCHECK(destination.IsRegister());
1534 Register dst = destination.AsRegister<Register>();
1535 __ LoadConst32(dst, value);
1536}
1537
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1539 if (location.IsRegister()) {
1540 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001541 } else if (location.IsRegisterPair()) {
1542 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1543 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001544 } else {
1545 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1546 }
1547}
1548
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001549template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001550inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1551 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001552 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001553 for (const PcRelativePatchInfo& info : infos) {
1554 const DexFile& dex_file = info.target_dex_file;
1555 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001556 DCHECK(info.label.IsBound());
1557 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001558 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1559 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001560 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1561 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1562 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001563 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001564 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001565 }
1566}
1567
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001568void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001569 DCHECK(linker_patches->empty());
1570 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001571 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001572 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001573 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001574 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001575 pc_relative_string_patches_.size() +
1576 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001577 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001578 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001579 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1580 pc_relative_method_patches_, linker_patches);
1581 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1582 pc_relative_type_patches_, linker_patches);
1583 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1584 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001585 } else {
1586 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001587 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1588 pc_relative_type_patches_, linker_patches);
1589 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1590 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001591 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001592 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1593 method_bss_entry_patches_, linker_patches);
1594 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1595 type_bss_entry_patches_, linker_patches);
1596 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1597 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001598 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001599}
1600
Vladimir Marko65979462017-05-19 17:25:12 +01001601CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001602 MethodReference target_method,
1603 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001604 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001605 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001606 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001607 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001608}
1609
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001610CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001611 MethodReference target_method,
1612 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001613 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001614 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001615 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001616 &method_bss_entry_patches_);
1617}
1618
Alexey Frunze06a46c42016-07-19 15:00:40 -07001619CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001620 const DexFile& dex_file,
1621 dex::TypeIndex type_index,
1622 const PcRelativePatchInfo* info_high) {
1623 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001624}
1625
Vladimir Marko1998cd02017-01-13 13:02:58 +00001626CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001627 const DexFile& dex_file,
1628 dex::TypeIndex type_index,
1629 const PcRelativePatchInfo* info_high) {
1630 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001631}
1632
Vladimir Marko65979462017-05-19 17:25:12 +01001633CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001634 const DexFile& dex_file,
1635 dex::StringIndex string_index,
1636 const PcRelativePatchInfo* info_high) {
1637 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001638}
1639
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001640CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1641 const DexFile& dex_file,
1642 dex::StringIndex string_index,
1643 const PcRelativePatchInfo* info_high) {
1644 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1645}
1646
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001647CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001648 const DexFile& dex_file,
1649 uint32_t offset_or_index,
1650 const PcRelativePatchInfo* info_high,
1651 ArenaDeque<PcRelativePatchInfo>* patches) {
1652 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001653 return &patches->back();
1654}
1655
Alexey Frunze06a46c42016-07-19 15:00:40 -07001656Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1657 return map->GetOrCreate(
1658 value,
1659 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1660}
1661
Alexey Frunze06a46c42016-07-19 15:00:40 -07001662Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001663 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001664}
1665
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001666void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001667 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001668 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001669 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001670 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001671 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001672 if (GetInstructionSetFeatures().IsR6()) {
1673 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001674 __ Bind(&info_high->label);
1675 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001676 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001677 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001678 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001679 } else {
1680 // If base is ZERO, emit NAL to obtain the actual base.
1681 if (base == ZERO) {
1682 // Generate a dummy PC-relative call to obtain PC.
1683 __ Nal();
1684 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001685 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001686 __ Lui(out, /* placeholder */ 0x1234);
1687 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1688 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1689 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001690 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001691 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001692 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001693 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001694 __ Addu(out, out, (base == ZERO) ? RA : base);
1695 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001696 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001697 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001698}
1699
Alexey Frunze627c1a02017-01-30 19:28:14 -08001700CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1701 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001702 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001703 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001704 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1705 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001706 return &jit_string_patches_.back();
1707}
1708
1709CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1710 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001711 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001712 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001713 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1714 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001715 return &jit_class_patches_.back();
1716}
1717
1718void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1719 const uint8_t* roots_data,
1720 const CodeGeneratorMIPS::JitPatchInfo& info,
1721 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001722 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1723 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001724 uintptr_t address =
1725 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1726 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1727 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001728 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1729 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1730 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1731 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001732 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001733 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1734 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001735 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001736 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001737 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1738 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001739 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001740 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1741 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001742}
1743
1744void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1745 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001746 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1747 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001748 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001749 }
1750 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001751 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1752 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001753 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001754 }
1755}
1756
Goran Jakovljevice114da22016-12-26 14:21:43 +01001757void CodeGeneratorMIPS::MarkGCCard(Register object,
1758 Register value,
1759 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001760 MipsLabel done;
1761 Register card = AT;
1762 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001763 if (value_can_be_null) {
1764 __ Beqz(value, &done);
1765 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001766 __ LoadFromOffset(kLoadWord,
1767 card,
1768 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001769 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001770 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1771 __ Addu(temp, card, temp);
1772 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001773 if (value_can_be_null) {
1774 __ Bind(&done);
1775 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001776}
1777
David Brazdil58282f42016-01-14 12:45:10 +00001778void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001779 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1780 blocked_core_registers_[ZERO] = true;
1781 blocked_core_registers_[K0] = true;
1782 blocked_core_registers_[K1] = true;
1783 blocked_core_registers_[GP] = true;
1784 blocked_core_registers_[SP] = true;
1785 blocked_core_registers_[RA] = true;
1786
1787 // AT and TMP(T8) are used as temporary/scratch registers
1788 // (similar to how AT is used by MIPS assemblers).
1789 blocked_core_registers_[AT] = true;
1790 blocked_core_registers_[TMP] = true;
1791 blocked_fpu_registers_[FTMP] = true;
1792
1793 // Reserve suspend and thread registers.
1794 blocked_core_registers_[S0] = true;
1795 blocked_core_registers_[TR] = true;
1796
1797 // Reserve T9 for function calls
1798 blocked_core_registers_[T9] = true;
1799
1800 // Reserve odd-numbered FPU registers.
1801 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1802 blocked_fpu_registers_[i] = true;
1803 }
1804
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001805 if (GetGraph()->IsDebuggable()) {
1806 // Stubs do not save callee-save floating point registers. If the graph
1807 // is debuggable, we need to deal with these registers differently. For
1808 // now, just block them.
1809 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1810 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1811 }
1812 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001813}
1814
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001815size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1816 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1817 return kMipsWordSize;
1818}
1819
1820size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1821 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1822 return kMipsWordSize;
1823}
1824
1825size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001826 if (GetGraph()->HasSIMD()) {
1827 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1828 } else {
1829 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1830 }
1831 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001832}
1833
1834size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001835 if (GetGraph()->HasSIMD()) {
1836 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1837 } else {
1838 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1839 }
1840 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001841}
1842
1843void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001844 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845}
1846
1847void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001848 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849}
1850
Serban Constantinescufca16662016-07-14 09:21:59 +01001851constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1852
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1854 HInstruction* instruction,
1855 uint32_t dex_pc,
1856 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001857 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001858 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1859 IsDirectEntrypoint(entrypoint));
1860 if (EntrypointRequiresStackMap(entrypoint)) {
1861 RecordPcInfo(instruction, dex_pc, slow_path);
1862 }
1863}
1864
1865void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1866 HInstruction* instruction,
1867 SlowPathCode* slow_path,
1868 bool direct) {
1869 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1870 GenerateInvokeRuntime(entry_point_offset, direct);
1871}
1872
1873void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001874 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001875 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001876 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001877 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878 // Reserve argument space on stack (for $a0-$a3) for
1879 // entrypoints that directly reference native implementations.
1880 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001881 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001883 } else {
1884 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001885 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001886 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887}
1888
1889void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1890 Register class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07001891 __ LoadFromOffset(kLoadSignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001892 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1893 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1894 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1895 __ Sync(0);
1896 __ Bind(slow_path->GetExitLabel());
1897}
1898
1899void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1900 __ Sync(0); // Only stype 0 is supported.
1901}
1902
1903void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1904 HBasicBlock* successor) {
1905 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001906 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1907
1908 if (slow_path == nullptr) {
1909 slow_path =
1910 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1911 instruction->SetSlowPath(slow_path);
1912 codegen_->AddSlowPath(slow_path);
1913 if (successor != nullptr) {
1914 DCHECK(successor->IsLoopHeader());
1915 }
1916 } else {
1917 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1918 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919
1920 __ LoadFromOffset(kLoadUnsignedHalfword,
1921 TMP,
1922 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001923 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 if (successor == nullptr) {
1925 __ Bnez(TMP, slow_path->GetEntryLabel());
1926 __ Bind(slow_path->GetReturnLabel());
1927 } else {
1928 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1929 __ B(slow_path->GetEntryLabel());
1930 // slow_path will return to GetLabelOf(successor).
1931 }
1932}
1933
1934InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1935 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001936 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 assembler_(codegen->GetAssembler()),
1938 codegen_(codegen) {}
1939
1940void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1941 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001942 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001943 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001945 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946 locations->SetInAt(0, Location::RequiresRegister());
1947 HInstruction* right = instruction->InputAt(1);
1948 bool can_use_imm = false;
1949 if (right->IsConstant()) {
1950 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1951 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1952 can_use_imm = IsUint<16>(imm);
1953 } else if (instruction->IsAdd()) {
1954 can_use_imm = IsInt<16>(imm);
1955 } else {
1956 DCHECK(instruction->IsSub());
1957 can_use_imm = IsInt<16>(-imm);
1958 }
1959 }
1960 if (can_use_imm)
1961 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1962 else
1963 locations->SetInAt(1, Location::RequiresRegister());
1964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1965 break;
1966 }
1967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001968 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001970 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1971 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001972 break;
1973 }
1974
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 case DataType::Type::kFloat32:
1976 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 DCHECK(instruction->IsAdd() || instruction->IsSub());
1978 locations->SetInAt(0, Location::RequiresFpuRegister());
1979 locations->SetInAt(1, Location::RequiresFpuRegister());
1980 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1981 break;
1982
1983 default:
1984 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1985 }
1986}
1987
1988void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001990 LocationSummary* locations = instruction->GetLocations();
1991
1992 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001993 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001994 Register dst = locations->Out().AsRegister<Register>();
1995 Register lhs = locations->InAt(0).AsRegister<Register>();
1996 Location rhs_location = locations->InAt(1);
1997
1998 Register rhs_reg = ZERO;
1999 int32_t rhs_imm = 0;
2000 bool use_imm = rhs_location.IsConstant();
2001 if (use_imm) {
2002 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2003 } else {
2004 rhs_reg = rhs_location.AsRegister<Register>();
2005 }
2006
2007 if (instruction->IsAnd()) {
2008 if (use_imm)
2009 __ Andi(dst, lhs, rhs_imm);
2010 else
2011 __ And(dst, lhs, rhs_reg);
2012 } else if (instruction->IsOr()) {
2013 if (use_imm)
2014 __ Ori(dst, lhs, rhs_imm);
2015 else
2016 __ Or(dst, lhs, rhs_reg);
2017 } else if (instruction->IsXor()) {
2018 if (use_imm)
2019 __ Xori(dst, lhs, rhs_imm);
2020 else
2021 __ Xor(dst, lhs, rhs_reg);
2022 } else if (instruction->IsAdd()) {
2023 if (use_imm)
2024 __ Addiu(dst, lhs, rhs_imm);
2025 else
2026 __ Addu(dst, lhs, rhs_reg);
2027 } else {
2028 DCHECK(instruction->IsSub());
2029 if (use_imm)
2030 __ Addiu(dst, lhs, -rhs_imm);
2031 else
2032 __ Subu(dst, lhs, rhs_reg);
2033 }
2034 break;
2035 }
2036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002038 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2039 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2040 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2041 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002042 Location rhs_location = locations->InAt(1);
2043 bool use_imm = rhs_location.IsConstant();
2044 if (!use_imm) {
2045 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2046 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2047 if (instruction->IsAnd()) {
2048 __ And(dst_low, lhs_low, rhs_low);
2049 __ And(dst_high, lhs_high, rhs_high);
2050 } else if (instruction->IsOr()) {
2051 __ Or(dst_low, lhs_low, rhs_low);
2052 __ Or(dst_high, lhs_high, rhs_high);
2053 } else if (instruction->IsXor()) {
2054 __ Xor(dst_low, lhs_low, rhs_low);
2055 __ Xor(dst_high, lhs_high, rhs_high);
2056 } else if (instruction->IsAdd()) {
2057 if (lhs_low == rhs_low) {
2058 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2059 __ Slt(TMP, lhs_low, ZERO);
2060 __ Addu(dst_low, lhs_low, rhs_low);
2061 } else {
2062 __ Addu(dst_low, lhs_low, rhs_low);
2063 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2064 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2065 }
2066 __ Addu(dst_high, lhs_high, rhs_high);
2067 __ Addu(dst_high, dst_high, TMP);
2068 } else {
2069 DCHECK(instruction->IsSub());
2070 __ Sltu(TMP, lhs_low, rhs_low);
2071 __ Subu(dst_low, lhs_low, rhs_low);
2072 __ Subu(dst_high, lhs_high, rhs_high);
2073 __ Subu(dst_high, dst_high, TMP);
2074 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002075 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002076 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2077 if (instruction->IsOr()) {
2078 uint32_t low = Low32Bits(value);
2079 uint32_t high = High32Bits(value);
2080 if (IsUint<16>(low)) {
2081 if (dst_low != lhs_low || low != 0) {
2082 __ Ori(dst_low, lhs_low, low);
2083 }
2084 } else {
2085 __ LoadConst32(TMP, low);
2086 __ Or(dst_low, lhs_low, TMP);
2087 }
2088 if (IsUint<16>(high)) {
2089 if (dst_high != lhs_high || high != 0) {
2090 __ Ori(dst_high, lhs_high, high);
2091 }
2092 } else {
2093 if (high != low) {
2094 __ LoadConst32(TMP, high);
2095 }
2096 __ Or(dst_high, lhs_high, TMP);
2097 }
2098 } else if (instruction->IsXor()) {
2099 uint32_t low = Low32Bits(value);
2100 uint32_t high = High32Bits(value);
2101 if (IsUint<16>(low)) {
2102 if (dst_low != lhs_low || low != 0) {
2103 __ Xori(dst_low, lhs_low, low);
2104 }
2105 } else {
2106 __ LoadConst32(TMP, low);
2107 __ Xor(dst_low, lhs_low, TMP);
2108 }
2109 if (IsUint<16>(high)) {
2110 if (dst_high != lhs_high || high != 0) {
2111 __ Xori(dst_high, lhs_high, high);
2112 }
2113 } else {
2114 if (high != low) {
2115 __ LoadConst32(TMP, high);
2116 }
2117 __ Xor(dst_high, lhs_high, TMP);
2118 }
2119 } else if (instruction->IsAnd()) {
2120 uint32_t low = Low32Bits(value);
2121 uint32_t high = High32Bits(value);
2122 if (IsUint<16>(low)) {
2123 __ Andi(dst_low, lhs_low, low);
2124 } else if (low != 0xFFFFFFFF) {
2125 __ LoadConst32(TMP, low);
2126 __ And(dst_low, lhs_low, TMP);
2127 } else if (dst_low != lhs_low) {
2128 __ Move(dst_low, lhs_low);
2129 }
2130 if (IsUint<16>(high)) {
2131 __ Andi(dst_high, lhs_high, high);
2132 } else if (high != 0xFFFFFFFF) {
2133 if (high != low) {
2134 __ LoadConst32(TMP, high);
2135 }
2136 __ And(dst_high, lhs_high, TMP);
2137 } else if (dst_high != lhs_high) {
2138 __ Move(dst_high, lhs_high);
2139 }
2140 } else {
2141 if (instruction->IsSub()) {
2142 value = -value;
2143 } else {
2144 DCHECK(instruction->IsAdd());
2145 }
2146 int32_t low = Low32Bits(value);
2147 int32_t high = High32Bits(value);
2148 if (IsInt<16>(low)) {
2149 if (dst_low != lhs_low || low != 0) {
2150 __ Addiu(dst_low, lhs_low, low);
2151 }
2152 if (low != 0) {
2153 __ Sltiu(AT, dst_low, low);
2154 }
2155 } else {
2156 __ LoadConst32(TMP, low);
2157 __ Addu(dst_low, lhs_low, TMP);
2158 __ Sltu(AT, dst_low, TMP);
2159 }
2160 if (IsInt<16>(high)) {
2161 if (dst_high != lhs_high || high != 0) {
2162 __ Addiu(dst_high, lhs_high, high);
2163 }
2164 } else {
2165 if (high != low) {
2166 __ LoadConst32(TMP, high);
2167 }
2168 __ Addu(dst_high, lhs_high, TMP);
2169 }
2170 if (low != 0) {
2171 __ Addu(dst_high, dst_high, AT);
2172 }
2173 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002174 }
2175 break;
2176 }
2177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 case DataType::Type::kFloat32:
2179 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002180 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2181 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2182 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2183 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002185 __ AddS(dst, lhs, rhs);
2186 } else {
2187 __ AddD(dst, lhs, rhs);
2188 }
2189 } else {
2190 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002191 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002192 __ SubS(dst, lhs, rhs);
2193 } else {
2194 __ SubD(dst, lhs, rhs);
2195 }
2196 }
2197 break;
2198 }
2199
2200 default:
2201 LOG(FATAL) << "Unexpected binary operation type " << type;
2202 }
2203}
2204
2205void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002206 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002207
Vladimir Markoca6fff82017-10-03 14:49:14 +01002208 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002210 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002211 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002212 locations->SetInAt(0, Location::RequiresRegister());
2213 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2215 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002216 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002217 locations->SetInAt(0, Location::RequiresRegister());
2218 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2219 locations->SetOut(Location::RequiresRegister());
2220 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002221 default:
2222 LOG(FATAL) << "Unexpected shift type " << type;
2223 }
2224}
2225
2226static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2227
2228void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002229 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002230 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232
2233 Location rhs_location = locations->InAt(1);
2234 bool use_imm = rhs_location.IsConstant();
2235 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2236 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002237 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002239 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002240 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2241 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002242
2243 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002245 Register dst = locations->Out().AsRegister<Register>();
2246 Register lhs = locations->InAt(0).AsRegister<Register>();
2247 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002248 if (shift_value == 0) {
2249 if (dst != lhs) {
2250 __ Move(dst, lhs);
2251 }
2252 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002253 __ Sll(dst, lhs, shift_value);
2254 } else if (instr->IsShr()) {
2255 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002256 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002257 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002258 } else {
2259 if (has_ins_rotr) {
2260 __ Rotr(dst, lhs, shift_value);
2261 } else {
2262 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2263 __ Srl(dst, lhs, shift_value);
2264 __ Or(dst, dst, TMP);
2265 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002266 }
2267 } else {
2268 if (instr->IsShl()) {
2269 __ Sllv(dst, lhs, rhs_reg);
2270 } else if (instr->IsShr()) {
2271 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002272 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002274 } else {
2275 if (has_ins_rotr) {
2276 __ Rotrv(dst, lhs, rhs_reg);
2277 } else {
2278 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002279 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2280 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2281 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2282 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2283 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002284 __ Sllv(TMP, lhs, TMP);
2285 __ Srlv(dst, lhs, rhs_reg);
2286 __ Or(dst, dst, TMP);
2287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 }
2289 }
2290 break;
2291 }
2292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002294 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2295 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2296 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2297 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2298 if (use_imm) {
2299 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002300 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002302 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002303 if (instr->IsShl()) {
2304 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2305 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2306 __ Sll(dst_low, lhs_low, shift_value);
2307 } else if (instr->IsShr()) {
2308 __ Srl(dst_low, lhs_low, shift_value);
2309 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2310 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 } else if (instr->IsUShr()) {
2312 __ Srl(dst_low, lhs_low, shift_value);
2313 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2314 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002315 } else {
2316 __ Srl(dst_low, lhs_low, shift_value);
2317 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2318 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002319 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002320 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002322 if (instr->IsShl()) {
2323 __ Sll(dst_low, lhs_low, shift_value);
2324 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2325 __ Sll(dst_high, lhs_high, shift_value);
2326 __ Or(dst_high, dst_high, TMP);
2327 } else if (instr->IsShr()) {
2328 __ Sra(dst_high, lhs_high, shift_value);
2329 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2330 __ Srl(dst_low, lhs_low, shift_value);
2331 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002332 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002333 __ Srl(dst_high, lhs_high, shift_value);
2334 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2335 __ Srl(dst_low, lhs_low, shift_value);
2336 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 } else {
2338 __ Srl(TMP, lhs_low, shift_value);
2339 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2340 __ Or(dst_low, dst_low, TMP);
2341 __ Srl(TMP, lhs_high, shift_value);
2342 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2343 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002344 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002345 }
2346 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002347 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002349 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 __ Move(dst_low, ZERO);
2351 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002352 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002355 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002357 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002358 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002359 // 64-bit rotation by 32 is just a swap.
2360 __ Move(dst_low, lhs_high);
2361 __ Move(dst_high, lhs_low);
2362 } else {
2363 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002364 __ Srl(dst_low, lhs_high, shift_value_high);
2365 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2366 __ Srl(dst_high, lhs_low, shift_value_high);
2367 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002368 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002369 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2370 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002371 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002372 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2373 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 __ Or(dst_high, dst_high, TMP);
2375 }
2376 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002377 }
2378 }
2379 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002380 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 MipsLabel done;
2382 if (instr->IsShl()) {
2383 __ Sllv(dst_low, lhs_low, rhs_reg);
2384 __ Nor(AT, ZERO, rhs_reg);
2385 __ Srl(TMP, lhs_low, 1);
2386 __ Srlv(TMP, TMP, AT);
2387 __ Sllv(dst_high, lhs_high, rhs_reg);
2388 __ Or(dst_high, dst_high, TMP);
2389 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002390 if (isR6) {
2391 __ Beqzc(TMP, &done, /* is_bare */ true);
2392 __ Move(dst_high, dst_low);
2393 __ Move(dst_low, ZERO);
2394 } else {
2395 __ Movn(dst_high, dst_low, TMP);
2396 __ Movn(dst_low, ZERO, TMP);
2397 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 } else if (instr->IsShr()) {
2399 __ Srav(dst_high, lhs_high, rhs_reg);
2400 __ Nor(AT, ZERO, rhs_reg);
2401 __ Sll(TMP, lhs_high, 1);
2402 __ Sllv(TMP, TMP, AT);
2403 __ Srlv(dst_low, lhs_low, rhs_reg);
2404 __ Or(dst_low, dst_low, TMP);
2405 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002406 if (isR6) {
2407 __ Beqzc(TMP, &done, /* is_bare */ true);
2408 __ Move(dst_low, dst_high);
2409 __ Sra(dst_high, dst_high, 31);
2410 } else {
2411 __ Sra(AT, dst_high, 31);
2412 __ Movn(dst_low, dst_high, TMP);
2413 __ Movn(dst_high, AT, TMP);
2414 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002415 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 __ Srlv(dst_high, lhs_high, rhs_reg);
2417 __ Nor(AT, ZERO, rhs_reg);
2418 __ Sll(TMP, lhs_high, 1);
2419 __ Sllv(TMP, TMP, AT);
2420 __ Srlv(dst_low, lhs_low, rhs_reg);
2421 __ Or(dst_low, dst_low, TMP);
2422 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002423 if (isR6) {
2424 __ Beqzc(TMP, &done, /* is_bare */ true);
2425 __ Move(dst_low, dst_high);
2426 __ Move(dst_high, ZERO);
2427 } else {
2428 __ Movn(dst_low, dst_high, TMP);
2429 __ Movn(dst_high, ZERO, TMP);
2430 }
2431 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002432 __ Nor(AT, ZERO, rhs_reg);
2433 __ Srlv(TMP, lhs_low, rhs_reg);
2434 __ Sll(dst_low, lhs_high, 1);
2435 __ Sllv(dst_low, dst_low, AT);
2436 __ Or(dst_low, dst_low, TMP);
2437 __ Srlv(TMP, lhs_high, rhs_reg);
2438 __ Sll(dst_high, lhs_low, 1);
2439 __ Sllv(dst_high, dst_high, AT);
2440 __ Or(dst_high, dst_high, TMP);
2441 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002442 if (isR6) {
2443 __ Beqzc(TMP, &done, /* is_bare */ true);
2444 __ Move(TMP, dst_high);
2445 __ Move(dst_high, dst_low);
2446 __ Move(dst_low, TMP);
2447 } else {
2448 __ Movn(AT, dst_high, TMP);
2449 __ Movn(dst_high, dst_low, TMP);
2450 __ Movn(dst_low, AT, TMP);
2451 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002452 }
2453 __ Bind(&done);
2454 }
2455 break;
2456 }
2457
2458 default:
2459 LOG(FATAL) << "Unexpected shift operation type " << type;
2460 }
2461}
2462
2463void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2464 HandleBinaryOp(instruction);
2465}
2466
2467void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2468 HandleBinaryOp(instruction);
2469}
2470
2471void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2472 HandleBinaryOp(instruction);
2473}
2474
2475void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2476 HandleBinaryOp(instruction);
2477}
2478
2479void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002481 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002483 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002484 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2485 object_array_get_with_read_barrier
2486 ? LocationSummary::kCallOnSlowPath
2487 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002488 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2489 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2490 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002491 locations->SetInAt(0, Location::RequiresRegister());
2492 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2495 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002496 // The output overlaps in the case of an object array get with
2497 // read barriers enabled: we do not want the move to overwrite the
2498 // array's location, as we need it to emit the read barrier.
2499 locations->SetOut(Location::RequiresRegister(),
2500 object_array_get_with_read_barrier
2501 ? Location::kOutputOverlap
2502 : Location::kNoOutputOverlap);
2503 }
2504 // We need a temporary register for the read barrier marking slow
2505 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2506 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002507 bool temp_needed = instruction->GetIndex()->IsConstant()
2508 ? !kBakerReadBarrierThunksEnableForFields
2509 : !kBakerReadBarrierThunksEnableForArrays;
2510 if (temp_needed) {
2511 locations->AddTemp(Location::RequiresRegister());
2512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 }
2514}
2515
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002516static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2517 auto null_checker = [codegen, instruction]() {
2518 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002519 };
2520 return null_checker;
2521}
2522
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002523void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2524 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002525 Location obj_loc = locations->InAt(0);
2526 Register obj = obj_loc.AsRegister<Register>();
2527 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002528 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002529 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002530 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002533 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2534 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002535 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002536 case DataType::Type::kBool:
2537 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002538 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002539 if (index.IsConstant()) {
2540 size_t offset =
2541 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002542 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002543 } else {
2544 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002545 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002546 }
2547 break;
2548 }
2549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002551 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002552 if (index.IsConstant()) {
2553 size_t offset =
2554 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002555 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 } else {
2557 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002558 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002559 }
2560 break;
2561 }
2562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002564 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002565 if (maybe_compressed_char_at) {
2566 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2567 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2568 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2569 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2570 "Expecting 0=compressed, 1=uncompressed");
2571 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002572 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002573 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2574 if (maybe_compressed_char_at) {
2575 MipsLabel uncompressed_load, done;
2576 __ Bnez(TMP, &uncompressed_load);
2577 __ LoadFromOffset(kLoadUnsignedByte,
2578 out,
2579 obj,
2580 data_offset + (const_index << TIMES_1));
2581 __ B(&done);
2582 __ Bind(&uncompressed_load);
2583 __ LoadFromOffset(kLoadUnsignedHalfword,
2584 out,
2585 obj,
2586 data_offset + (const_index << TIMES_2));
2587 __ Bind(&done);
2588 } else {
2589 __ LoadFromOffset(kLoadUnsignedHalfword,
2590 out,
2591 obj,
2592 data_offset + (const_index << TIMES_2),
2593 null_checker);
2594 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002595 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002596 Register index_reg = index.AsRegister<Register>();
2597 if (maybe_compressed_char_at) {
2598 MipsLabel uncompressed_load, done;
2599 __ Bnez(TMP, &uncompressed_load);
2600 __ Addu(TMP, obj, index_reg);
2601 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2602 __ B(&done);
2603 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002604 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002605 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2606 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002607 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2608 __ Addu(TMP, index_reg, obj);
2609 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002610 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002611 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002612 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2613 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002614 }
2615 break;
2616 }
2617
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002618 case DataType::Type::kInt16: {
2619 Register out = out_loc.AsRegister<Register>();
2620 if (index.IsConstant()) {
2621 size_t offset =
2622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2623 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002624 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2625 __ Addu(TMP, index.AsRegister<Register>(), obj);
2626 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002627 } else {
2628 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2629 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2630 }
2631 break;
2632 }
2633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002635 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002636 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002637 if (index.IsConstant()) {
2638 size_t offset =
2639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002640 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002641 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2642 __ Addu(TMP, index.AsRegister<Register>(), obj);
2643 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002644 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002645 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002646 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002647 }
2648 break;
2649 }
2650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002651 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002652 static_assert(
2653 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2654 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2655 // /* HeapReference<Object> */ out =
2656 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2657 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002658 bool temp_needed = index.IsConstant()
2659 ? !kBakerReadBarrierThunksEnableForFields
2660 : !kBakerReadBarrierThunksEnableForArrays;
2661 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002662 // Note that a potential implicit null check is handled in this
2663 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002664 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2665 if (index.IsConstant()) {
2666 // Array load with a constant index can be treated as a field load.
2667 size_t offset =
2668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2669 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2670 out_loc,
2671 obj,
2672 offset,
2673 temp,
2674 /* needs_null_check */ false);
2675 } else {
2676 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2677 out_loc,
2678 obj,
2679 data_offset,
2680 index,
2681 temp,
2682 /* needs_null_check */ false);
2683 }
Alexey Frunze15958152017-02-09 19:08:30 -08002684 } else {
2685 Register out = out_loc.AsRegister<Register>();
2686 if (index.IsConstant()) {
2687 size_t offset =
2688 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2689 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2690 // If read barriers are enabled, emit read barriers other than
2691 // Baker's using a slow path (and also unpoison the loaded
2692 // reference, if heap poisoning is enabled).
2693 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2694 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002695 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002696 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2697 // If read barriers are enabled, emit read barriers other than
2698 // Baker's using a slow path (and also unpoison the loaded
2699 // reference, if heap poisoning is enabled).
2700 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2701 out_loc,
2702 out_loc,
2703 obj_loc,
2704 data_offset,
2705 index);
2706 }
2707 }
2708 break;
2709 }
2710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002712 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002713 if (index.IsConstant()) {
2714 size_t offset =
2715 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002716 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002717 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2718 __ Addu(TMP, index.AsRegister<Register>(), obj);
2719 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002720 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002721 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002722 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002723 }
2724 break;
2725 }
2726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002728 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002729 if (index.IsConstant()) {
2730 size_t offset =
2731 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002732 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002733 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2734 __ Addu(TMP, index.AsRegister<Register>(), obj);
2735 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002736 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002737 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002738 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002739 }
2740 break;
2741 }
2742
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002744 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002745 if (index.IsConstant()) {
2746 size_t offset =
2747 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002748 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002749 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2750 __ Addu(TMP, index.AsRegister<Register>(), obj);
2751 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002752 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002753 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002754 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002755 }
2756 break;
2757 }
2758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002760 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2761 UNREACHABLE();
2762 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002763}
2764
2765void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002766 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002767 locations->SetInAt(0, Location::RequiresRegister());
2768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2769}
2770
2771void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2772 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002773 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002774 Register obj = locations->InAt(0).AsRegister<Register>();
2775 Register out = locations->Out().AsRegister<Register>();
2776 __ LoadFromOffset(kLoadWord, out, obj, offset);
2777 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002778 // Mask out compression flag from String's array length.
2779 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2780 __ Srl(out, out, 1u);
2781 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782}
2783
Alexey Frunzef58b2482016-09-02 22:14:06 -07002784Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2785 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2786 ? Location::ConstantLocation(instruction->AsConstant())
2787 : Location::RequiresRegister();
2788}
2789
2790Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2791 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2792 // We can store a non-zero float or double constant without first loading it into the FPU,
2793 // but we should only prefer this if the constant has a single use.
2794 if (instruction->IsConstant() &&
2795 (instruction->AsConstant()->IsZeroBitPattern() ||
2796 instruction->GetUses().HasExactlyOneElement())) {
2797 return Location::ConstantLocation(instruction->AsConstant());
2798 // Otherwise fall through and require an FPU register for the constant.
2799 }
2800 return Location::RequiresFpuRegister();
2801}
2802
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002805
2806 bool needs_write_barrier =
2807 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2808 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2809
Vladimir Markoca6fff82017-10-03 14:49:14 +01002810 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002811 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002812 may_need_runtime_call_for_type_check ?
2813 LocationSummary::kCallOnSlowPath :
2814 LocationSummary::kNoCall);
2815
2816 locations->SetInAt(0, Location::RequiresRegister());
2817 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002819 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002821 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2822 }
2823 if (needs_write_barrier) {
2824 // Temporary register for the write barrier.
2825 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 }
2827}
2828
2829void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2830 LocationSummary* locations = instruction->GetLocations();
2831 Register obj = locations->InAt(0).AsRegister<Register>();
2832 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002833 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002835 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002836 bool needs_write_barrier =
2837 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002838 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002839 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002840
2841 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002843 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002845 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002846 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002847 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002848 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002849 __ Addu(base_reg, obj, index.AsRegister<Register>());
2850 }
2851 if (value_location.IsConstant()) {
2852 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2853 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2854 } else {
2855 Register value = value_location.AsRegister<Register>();
2856 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002857 }
2858 break;
2859 }
2860
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002861 case DataType::Type::kUint16:
2862 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002865 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002866 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2867 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002869 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002870 }
2871 if (value_location.IsConstant()) {
2872 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2873 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2874 } else {
2875 Register value = value_location.AsRegister<Register>();
2876 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002877 }
2878 break;
2879 }
2880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002881 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002882 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2883 if (index.IsConstant()) {
2884 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002885 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2886 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002887 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002888 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002889 }
2890 if (value_location.IsConstant()) {
2891 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2892 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2893 } else {
2894 Register value = value_location.AsRegister<Register>();
2895 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2896 }
2897 break;
2898 }
2899
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002900 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002901 if (value_location.IsConstant()) {
2902 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002903 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002904 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002905 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002907 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002908 }
Alexey Frunze15958152017-02-09 19:08:30 -08002909 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2910 DCHECK_EQ(value, 0);
2911 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2912 DCHECK(!needs_write_barrier);
2913 DCHECK(!may_need_runtime_call_for_type_check);
2914 break;
2915 }
2916
2917 DCHECK(needs_write_barrier);
2918 Register value = value_location.AsRegister<Register>();
2919 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2920 Register temp2 = TMP; // Doesn't need to survive slow path.
2921 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2922 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2923 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2924 MipsLabel done;
2925 SlowPathCodeMIPS* slow_path = nullptr;
2926
2927 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002928 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002929 codegen_->AddSlowPath(slow_path);
2930 if (instruction->GetValueCanBeNull()) {
2931 MipsLabel non_zero;
2932 __ Bnez(value, &non_zero);
2933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2934 if (index.IsConstant()) {
2935 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002936 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2937 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002938 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002939 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002940 }
Alexey Frunze15958152017-02-09 19:08:30 -08002941 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2942 __ B(&done);
2943 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002944 }
Alexey Frunze15958152017-02-09 19:08:30 -08002945
2946 // Note that when read barriers are enabled, the type checks
2947 // are performed without read barriers. This is fine, even in
2948 // the case where a class object is in the from-space after
2949 // the flip, as a comparison involving such a type would not
2950 // produce a false positive; it may of course produce a false
2951 // negative, in which case we would take the ArraySet slow
2952 // path.
2953
2954 // /* HeapReference<Class> */ temp1 = obj->klass_
2955 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2956 __ MaybeUnpoisonHeapReference(temp1);
2957
2958 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2959 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2960 // /* HeapReference<Class> */ temp2 = value->klass_
2961 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2962 // If heap poisoning is enabled, no need to unpoison `temp1`
2963 // nor `temp2`, as we are comparing two poisoned references.
2964
2965 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2966 MipsLabel do_put;
2967 __ Beq(temp1, temp2, &do_put);
2968 // If heap poisoning is enabled, the `temp1` reference has
2969 // not been unpoisoned yet; unpoison it now.
2970 __ MaybeUnpoisonHeapReference(temp1);
2971
2972 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2973 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2974 // If heap poisoning is enabled, no need to unpoison
2975 // `temp1`, as we are comparing against null below.
2976 __ Bnez(temp1, slow_path->GetEntryLabel());
2977 __ Bind(&do_put);
2978 } else {
2979 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2980 }
2981 }
2982
2983 Register source = value;
2984 if (kPoisonHeapReferences) {
2985 // Note that in the case where `value` is a null reference,
2986 // we do not enter this block, as a null reference does not
2987 // need poisoning.
2988 __ Move(temp1, value);
2989 __ PoisonHeapReference(temp1);
2990 source = temp1;
2991 }
2992
2993 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2994 if (index.IsConstant()) {
2995 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002996 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002997 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002998 }
2999 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3000
3001 if (!may_need_runtime_call_for_type_check) {
3002 codegen_->MaybeRecordImplicitNullCheck(instruction);
3003 }
3004
3005 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3006
3007 if (done.IsLinked()) {
3008 __ Bind(&done);
3009 }
3010
3011 if (slow_path != nullptr) {
3012 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003013 }
3014 break;
3015 }
3016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003019 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003020 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003021 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3022 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003023 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003024 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003025 }
3026 if (value_location.IsConstant()) {
3027 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3028 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3029 } else {
3030 Register value = value_location.AsRegisterPairLow<Register>();
3031 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003032 }
3033 break;
3034 }
3035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003037 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003038 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003039 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003040 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3041 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003042 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003043 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003044 }
3045 if (value_location.IsConstant()) {
3046 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3047 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3048 } else {
3049 FRegister value = value_location.AsFpuRegister<FRegister>();
3050 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003051 }
3052 break;
3053 }
3054
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003057 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003058 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003059 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3060 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003061 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003062 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003063 }
3064 if (value_location.IsConstant()) {
3065 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3066 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3067 } else {
3068 FRegister value = value_location.AsFpuRegister<FRegister>();
3069 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003070 }
3071 break;
3072 }
3073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003075 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3076 UNREACHABLE();
3077 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078}
3079
Lena Djokica2901602017-09-21 13:50:52 +02003080void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3081 HIntermediateArrayAddressIndex* instruction) {
3082 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003083 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003084
3085 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3086
3087 locations->SetInAt(0, Location::RequiresRegister());
3088 locations->SetInAt(1, Location::ConstantLocation(shift));
3089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3090}
3091
3092void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3093 HIntermediateArrayAddressIndex* instruction) {
3094 LocationSummary* locations = instruction->GetLocations();
3095 Register index_reg = locations->InAt(0).AsRegister<Register>();
3096 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3097 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3098}
3099
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003100void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003101 RegisterSet caller_saves = RegisterSet::Empty();
3102 InvokeRuntimeCallingConvention calling_convention;
3103 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3104 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3105 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003106 locations->SetInAt(0, Location::RequiresRegister());
3107 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108}
3109
3110void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3111 LocationSummary* locations = instruction->GetLocations();
3112 BoundsCheckSlowPathMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003113 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114 codegen_->AddSlowPath(slow_path);
3115
3116 Register index = locations->InAt(0).AsRegister<Register>();
3117 Register length = locations->InAt(1).AsRegister<Register>();
3118
3119 // length is limited by the maximum positive signed 32-bit integer.
3120 // Unsigned comparison of length and index checks for index < 0
3121 // and for length <= index simultaneously.
3122 __ Bgeu(index, length, slow_path->GetEntryLabel());
3123}
3124
Alexey Frunze15958152017-02-09 19:08:30 -08003125// Temp is used for read barrier.
3126static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3127 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003128 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003129 (kUseBakerReadBarrier ||
3130 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3131 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3132 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3133 return 1;
3134 }
3135 return 0;
3136}
3137
3138// Extra temp is used for read barrier.
3139static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3140 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3141}
3142
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003143void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003144 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3145 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3146
3147 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3148 switch (type_check_kind) {
3149 case TypeCheckKind::kExactCheck:
3150 case TypeCheckKind::kAbstractClassCheck:
3151 case TypeCheckKind::kClassHierarchyCheck:
3152 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003153 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003154 ? LocationSummary::kCallOnSlowPath
3155 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3156 break;
3157 case TypeCheckKind::kArrayCheck:
3158 case TypeCheckKind::kUnresolvedCheck:
3159 case TypeCheckKind::kInterfaceCheck:
3160 call_kind = LocationSummary::kCallOnSlowPath;
3161 break;
3162 }
3163
Vladimir Markoca6fff82017-10-03 14:49:14 +01003164 LocationSummary* locations =
3165 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003166 locations->SetInAt(0, Location::RequiresRegister());
3167 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003168 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003169}
3170
3171void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003172 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003173 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003174 Location obj_loc = locations->InAt(0);
3175 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003176 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003177 Location temp_loc = locations->GetTemp(0);
3178 Register temp = temp_loc.AsRegister<Register>();
3179 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3180 DCHECK_LE(num_temps, 2u);
3181 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003182 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3183 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3184 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3185 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3186 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3187 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3188 const uint32_t object_array_data_offset =
3189 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3190 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003191
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003192 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3193 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3194 // read barriers is done for performance and code size reasons.
3195 bool is_type_check_slow_path_fatal = false;
3196 if (!kEmitCompilerReadBarrier) {
3197 is_type_check_slow_path_fatal =
3198 (type_check_kind == TypeCheckKind::kExactCheck ||
3199 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3200 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3201 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3202 !instruction->CanThrowIntoCatchBlock();
3203 }
3204 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003205 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3206 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003207 codegen_->AddSlowPath(slow_path);
3208
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003209 // Avoid this check if we know `obj` is not null.
3210 if (instruction->MustDoNullCheck()) {
3211 __ Beqz(obj, &done);
3212 }
3213
3214 switch (type_check_kind) {
3215 case TypeCheckKind::kExactCheck:
3216 case TypeCheckKind::kArrayCheck: {
3217 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003218 GenerateReferenceLoadTwoRegisters(instruction,
3219 temp_loc,
3220 obj_loc,
3221 class_offset,
3222 maybe_temp2_loc,
3223 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003224 // Jump to slow path for throwing the exception or doing a
3225 // more involved array check.
3226 __ Bne(temp, cls, slow_path->GetEntryLabel());
3227 break;
3228 }
3229
3230 case TypeCheckKind::kAbstractClassCheck: {
3231 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003232 GenerateReferenceLoadTwoRegisters(instruction,
3233 temp_loc,
3234 obj_loc,
3235 class_offset,
3236 maybe_temp2_loc,
3237 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003238 // If the class is abstract, we eagerly fetch the super class of the
3239 // object to avoid doing a comparison we know will fail.
3240 MipsLabel loop;
3241 __ Bind(&loop);
3242 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003243 GenerateReferenceLoadOneRegister(instruction,
3244 temp_loc,
3245 super_offset,
3246 maybe_temp2_loc,
3247 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003248 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3249 // exception.
3250 __ Beqz(temp, slow_path->GetEntryLabel());
3251 // Otherwise, compare the classes.
3252 __ Bne(temp, cls, &loop);
3253 break;
3254 }
3255
3256 case TypeCheckKind::kClassHierarchyCheck: {
3257 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003258 GenerateReferenceLoadTwoRegisters(instruction,
3259 temp_loc,
3260 obj_loc,
3261 class_offset,
3262 maybe_temp2_loc,
3263 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003264 // Walk over the class hierarchy to find a match.
3265 MipsLabel loop;
3266 __ Bind(&loop);
3267 __ Beq(temp, cls, &done);
3268 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003269 GenerateReferenceLoadOneRegister(instruction,
3270 temp_loc,
3271 super_offset,
3272 maybe_temp2_loc,
3273 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003274 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3275 // exception. Otherwise, jump to the beginning of the loop.
3276 __ Bnez(temp, &loop);
3277 __ B(slow_path->GetEntryLabel());
3278 break;
3279 }
3280
3281 case TypeCheckKind::kArrayObjectCheck: {
3282 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003283 GenerateReferenceLoadTwoRegisters(instruction,
3284 temp_loc,
3285 obj_loc,
3286 class_offset,
3287 maybe_temp2_loc,
3288 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003289 // Do an exact check.
3290 __ Beq(temp, cls, &done);
3291 // Otherwise, we need to check that the object's class is a non-primitive array.
3292 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003293 GenerateReferenceLoadOneRegister(instruction,
3294 temp_loc,
3295 component_offset,
3296 maybe_temp2_loc,
3297 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003298 // If the component type is null, jump to the slow path to throw the exception.
3299 __ Beqz(temp, slow_path->GetEntryLabel());
3300 // Otherwise, the object is indeed an array, further check that this component
3301 // type is not a primitive type.
3302 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3303 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3304 __ Bnez(temp, slow_path->GetEntryLabel());
3305 break;
3306 }
3307
3308 case TypeCheckKind::kUnresolvedCheck:
3309 // We always go into the type check slow path for the unresolved check case.
3310 // We cannot directly call the CheckCast runtime entry point
3311 // without resorting to a type checking slow path here (i.e. by
3312 // calling InvokeRuntime directly), as it would require to
3313 // assign fixed registers for the inputs of this HInstanceOf
3314 // instruction (following the runtime calling convention), which
3315 // might be cluttered by the potential first read barrier
3316 // emission at the beginning of this method.
3317 __ B(slow_path->GetEntryLabel());
3318 break;
3319
3320 case TypeCheckKind::kInterfaceCheck: {
3321 // Avoid read barriers to improve performance of the fast path. We can not get false
3322 // positives by doing this.
3323 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003324 GenerateReferenceLoadTwoRegisters(instruction,
3325 temp_loc,
3326 obj_loc,
3327 class_offset,
3328 maybe_temp2_loc,
3329 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003330 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003331 GenerateReferenceLoadTwoRegisters(instruction,
3332 temp_loc,
3333 temp_loc,
3334 iftable_offset,
3335 maybe_temp2_loc,
3336 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003337 // Iftable is never null.
3338 __ Lw(TMP, temp, array_length_offset);
3339 // Loop through the iftable and check if any class matches.
3340 MipsLabel loop;
3341 __ Bind(&loop);
3342 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3343 __ Beqz(TMP, slow_path->GetEntryLabel());
3344 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3345 __ MaybeUnpoisonHeapReference(AT);
3346 // Go to next interface.
3347 __ Addiu(TMP, TMP, -2);
3348 // Compare the classes and continue the loop if they do not match.
3349 __ Bne(AT, cls, &loop);
3350 break;
3351 }
3352 }
3353
3354 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003355 __ Bind(slow_path->GetExitLabel());
3356}
3357
3358void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3359 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003360 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003361 locations->SetInAt(0, Location::RequiresRegister());
3362 if (check->HasUses()) {
3363 locations->SetOut(Location::SameAsFirstInput());
3364 }
3365}
3366
3367void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3368 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003369 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003370 check->GetLoadClass(),
3371 check,
3372 check->GetDexPc(),
3373 true);
3374 codegen_->AddSlowPath(slow_path);
3375 GenerateClassInitializationCheck(slow_path,
3376 check->GetLocations()->InAt(0).AsRegister<Register>());
3377}
3378
3379void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003380 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003381
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003383 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003384
3385 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003387 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003389 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003390 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003391 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003392 locations->SetInAt(0, Location::RequiresRegister());
3393 locations->SetInAt(1, Location::RequiresRegister());
3394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3395 break;
3396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetInAt(1, Location::RequiresRegister());
3400 // Output overlaps because it is written before doing the low comparison.
3401 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3402 break;
3403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003404 case DataType::Type::kFloat32:
3405 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003406 locations->SetInAt(0, Location::RequiresFpuRegister());
3407 locations->SetInAt(1, Location::RequiresFpuRegister());
3408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003409 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003410
3411 default:
3412 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3413 }
3414}
3415
3416void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3417 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003418 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003419 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003420 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003421
3422 // 0 if: left == right
3423 // 1 if: left > right
3424 // -1 if: left < right
3425 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003426 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003427 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003428 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003429 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003430 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003431 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003432 Register lhs = locations->InAt(0).AsRegister<Register>();
3433 Register rhs = locations->InAt(1).AsRegister<Register>();
3434 __ Slt(TMP, lhs, rhs);
3435 __ Slt(res, rhs, lhs);
3436 __ Subu(res, res, TMP);
3437 break;
3438 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003440 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003441 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3442 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3443 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3444 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3445 // TODO: more efficient (direct) comparison with a constant.
3446 __ Slt(TMP, lhs_high, rhs_high);
3447 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3448 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3449 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3450 __ Sltu(TMP, lhs_low, rhs_low);
3451 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3452 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3453 __ Bind(&done);
3454 break;
3455 }
3456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003457 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003458 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003459 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3460 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3461 MipsLabel done;
3462 if (isR6) {
3463 __ CmpEqS(FTMP, lhs, rhs);
3464 __ LoadConst32(res, 0);
3465 __ Bc1nez(FTMP, &done);
3466 if (gt_bias) {
3467 __ CmpLtS(FTMP, lhs, rhs);
3468 __ LoadConst32(res, -1);
3469 __ Bc1nez(FTMP, &done);
3470 __ LoadConst32(res, 1);
3471 } else {
3472 __ CmpLtS(FTMP, rhs, lhs);
3473 __ LoadConst32(res, 1);
3474 __ Bc1nez(FTMP, &done);
3475 __ LoadConst32(res, -1);
3476 }
3477 } else {
3478 if (gt_bias) {
3479 __ ColtS(0, lhs, rhs);
3480 __ LoadConst32(res, -1);
3481 __ Bc1t(0, &done);
3482 __ CeqS(0, lhs, rhs);
3483 __ LoadConst32(res, 1);
3484 __ Movt(res, ZERO, 0);
3485 } else {
3486 __ ColtS(0, rhs, lhs);
3487 __ LoadConst32(res, 1);
3488 __ Bc1t(0, &done);
3489 __ CeqS(0, lhs, rhs);
3490 __ LoadConst32(res, -1);
3491 __ Movt(res, ZERO, 0);
3492 }
3493 }
3494 __ Bind(&done);
3495 break;
3496 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003497 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003498 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003499 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3500 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3501 MipsLabel done;
3502 if (isR6) {
3503 __ CmpEqD(FTMP, lhs, rhs);
3504 __ LoadConst32(res, 0);
3505 __ Bc1nez(FTMP, &done);
3506 if (gt_bias) {
3507 __ CmpLtD(FTMP, lhs, rhs);
3508 __ LoadConst32(res, -1);
3509 __ Bc1nez(FTMP, &done);
3510 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003511 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003512 __ CmpLtD(FTMP, rhs, lhs);
3513 __ LoadConst32(res, 1);
3514 __ Bc1nez(FTMP, &done);
3515 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003516 }
3517 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003518 if (gt_bias) {
3519 __ ColtD(0, lhs, rhs);
3520 __ LoadConst32(res, -1);
3521 __ Bc1t(0, &done);
3522 __ CeqD(0, lhs, rhs);
3523 __ LoadConst32(res, 1);
3524 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003525 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003526 __ ColtD(0, rhs, lhs);
3527 __ LoadConst32(res, 1);
3528 __ Bc1t(0, &done);
3529 __ CeqD(0, lhs, rhs);
3530 __ LoadConst32(res, -1);
3531 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003532 }
3533 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003534 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 break;
3536 }
3537
3538 default:
3539 LOG(FATAL) << "Unimplemented compare type " << in_type;
3540 }
3541}
3542
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003543void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003544 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003545 switch (instruction->InputAt(0)->GetType()) {
3546 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003547 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003548 locations->SetInAt(0, Location::RequiresRegister());
3549 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3550 break;
3551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 case DataType::Type::kFloat32:
3553 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003554 locations->SetInAt(0, Location::RequiresFpuRegister());
3555 locations->SetInAt(1, Location::RequiresFpuRegister());
3556 break;
3557 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003558 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3560 }
3561}
3562
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003563void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003564 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003565 return;
3566 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003569 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003570
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571 switch (type) {
3572 default:
3573 // Integer case.
3574 GenerateIntCompare(instruction->GetCondition(), locations);
3575 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003577 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003578 GenerateLongCompare(instruction->GetCondition(), locations);
3579 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 case DataType::Type::kFloat32:
3582 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003583 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3584 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003585 }
3586}
3587
Alexey Frunze7e99e052015-11-24 19:28:01 -08003588void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3589 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003590 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003591
3592 LocationSummary* locations = instruction->GetLocations();
3593 Location second = locations->InAt(1);
3594 DCHECK(second.IsConstant());
3595
3596 Register out = locations->Out().AsRegister<Register>();
3597 Register dividend = locations->InAt(0).AsRegister<Register>();
3598 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3599 DCHECK(imm == 1 || imm == -1);
3600
3601 if (instruction->IsRem()) {
3602 __ Move(out, ZERO);
3603 } else {
3604 if (imm == -1) {
3605 __ Subu(out, ZERO, dividend);
3606 } else if (out != dividend) {
3607 __ Move(out, dividend);
3608 }
3609 }
3610}
3611
3612void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3613 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003614 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003615
3616 LocationSummary* locations = instruction->GetLocations();
3617 Location second = locations->InAt(1);
3618 DCHECK(second.IsConstant());
3619
3620 Register out = locations->Out().AsRegister<Register>();
3621 Register dividend = locations->InAt(0).AsRegister<Register>();
3622 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003623 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003624 int ctz_imm = CTZ(abs_imm);
3625
3626 if (instruction->IsDiv()) {
3627 if (ctz_imm == 1) {
3628 // Fast path for division by +/-2, which is very common.
3629 __ Srl(TMP, dividend, 31);
3630 } else {
3631 __ Sra(TMP, dividend, 31);
3632 __ Srl(TMP, TMP, 32 - ctz_imm);
3633 }
3634 __ Addu(out, dividend, TMP);
3635 __ Sra(out, out, ctz_imm);
3636 if (imm < 0) {
3637 __ Subu(out, ZERO, out);
3638 }
3639 } else {
3640 if (ctz_imm == 1) {
3641 // Fast path for modulo +/-2, which is very common.
3642 __ Sra(TMP, dividend, 31);
3643 __ Subu(out, dividend, TMP);
3644 __ Andi(out, out, 1);
3645 __ Addu(out, out, TMP);
3646 } else {
3647 __ Sra(TMP, dividend, 31);
3648 __ Srl(TMP, TMP, 32 - ctz_imm);
3649 __ Addu(out, dividend, TMP);
3650 if (IsUint<16>(abs_imm - 1)) {
3651 __ Andi(out, out, abs_imm - 1);
3652 } else {
3653 __ Sll(out, out, 32 - ctz_imm);
3654 __ Srl(out, out, 32 - ctz_imm);
3655 }
3656 __ Subu(out, out, TMP);
3657 }
3658 }
3659}
3660
3661void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3662 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003664
3665 LocationSummary* locations = instruction->GetLocations();
3666 Location second = locations->InAt(1);
3667 DCHECK(second.IsConstant());
3668
3669 Register out = locations->Out().AsRegister<Register>();
3670 Register dividend = locations->InAt(0).AsRegister<Register>();
3671 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3672
3673 int64_t magic;
3674 int shift;
3675 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3676
3677 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3678
3679 __ LoadConst32(TMP, magic);
3680 if (isR6) {
3681 __ MuhR6(TMP, dividend, TMP);
3682 } else {
3683 __ MultR2(dividend, TMP);
3684 __ Mfhi(TMP);
3685 }
3686 if (imm > 0 && magic < 0) {
3687 __ Addu(TMP, TMP, dividend);
3688 } else if (imm < 0 && magic > 0) {
3689 __ Subu(TMP, TMP, dividend);
3690 }
3691
3692 if (shift != 0) {
3693 __ Sra(TMP, TMP, shift);
3694 }
3695
3696 if (instruction->IsDiv()) {
3697 __ Sra(out, TMP, 31);
3698 __ Subu(out, TMP, out);
3699 } else {
3700 __ Sra(AT, TMP, 31);
3701 __ Subu(AT, TMP, AT);
3702 __ LoadConst32(TMP, imm);
3703 if (isR6) {
3704 __ MulR6(TMP, AT, TMP);
3705 } else {
3706 __ MulR2(TMP, AT, TMP);
3707 }
3708 __ Subu(out, dividend, TMP);
3709 }
3710}
3711
3712void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3713 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003715
3716 LocationSummary* locations = instruction->GetLocations();
3717 Register out = locations->Out().AsRegister<Register>();
3718 Location second = locations->InAt(1);
3719
3720 if (second.IsConstant()) {
3721 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3722 if (imm == 0) {
3723 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3724 } else if (imm == 1 || imm == -1) {
3725 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003726 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003727 DivRemByPowerOfTwo(instruction);
3728 } else {
3729 DCHECK(imm <= -2 || imm >= 2);
3730 GenerateDivRemWithAnyConstant(instruction);
3731 }
3732 } else {
3733 Register dividend = locations->InAt(0).AsRegister<Register>();
3734 Register divisor = second.AsRegister<Register>();
3735 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3736 if (instruction->IsDiv()) {
3737 if (isR6) {
3738 __ DivR6(out, dividend, divisor);
3739 } else {
3740 __ DivR2(out, dividend, divisor);
3741 }
3742 } else {
3743 if (isR6) {
3744 __ ModR6(out, dividend, divisor);
3745 } else {
3746 __ ModR2(out, dividend, divisor);
3747 }
3748 }
3749 }
3750}
3751
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003752void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003753 DataType::Type type = div->GetResultType();
3754 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003755 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003756 : LocationSummary::kNoCall;
3757
Vladimir Markoca6fff82017-10-03 14:49:14 +01003758 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003759
3760 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003762 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003763 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3765 break;
3766
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003767 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003768 InvokeRuntimeCallingConvention calling_convention;
3769 locations->SetInAt(0, Location::RegisterPairLocation(
3770 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3771 locations->SetInAt(1, Location::RegisterPairLocation(
3772 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3773 locations->SetOut(calling_convention.GetReturnLocation(type));
3774 break;
3775 }
3776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kFloat32:
3778 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003779 locations->SetInAt(0, Location::RequiresFpuRegister());
3780 locations->SetInAt(1, Location::RequiresFpuRegister());
3781 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3782 break;
3783
3784 default:
3785 LOG(FATAL) << "Unexpected div type " << type;
3786 }
3787}
3788
3789void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003791 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003792
3793 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003795 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003796 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003797 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003798 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003799 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3800 break;
3801 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kFloat32:
3803 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003804 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3805 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3806 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003808 __ DivS(dst, lhs, rhs);
3809 } else {
3810 __ DivD(dst, lhs, rhs);
3811 }
3812 break;
3813 }
3814 default:
3815 LOG(FATAL) << "Unexpected div type " << type;
3816 }
3817}
3818
3819void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003820 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003822}
3823
3824void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003825 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003826 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003827 codegen_->AddSlowPath(slow_path);
3828 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003830
3831 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003832 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003833 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kInt8:
3835 case DataType::Type::kUint16:
3836 case DataType::Type::kInt16:
3837 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003838 if (value.IsConstant()) {
3839 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3840 __ B(slow_path->GetEntryLabel());
3841 } else {
3842 // A division by a non-null constant is valid. We don't need to perform
3843 // any check, so simply fall through.
3844 }
3845 } else {
3846 DCHECK(value.IsRegister()) << value;
3847 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3848 }
3849 break;
3850 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003851 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003852 if (value.IsConstant()) {
3853 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3854 __ B(slow_path->GetEntryLabel());
3855 } else {
3856 // A division by a non-null constant is valid. We don't need to perform
3857 // any check, so simply fall through.
3858 }
3859 } else {
3860 DCHECK(value.IsRegisterPair()) << value;
3861 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3862 __ Beqz(TMP, slow_path->GetEntryLabel());
3863 }
3864 break;
3865 }
3866 default:
3867 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3868 }
3869}
3870
3871void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003873 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003874 locations->SetOut(Location::ConstantLocation(constant));
3875}
3876
3877void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3878 // Will be generated at use site.
3879}
3880
3881void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3882 exit->SetLocations(nullptr);
3883}
3884
3885void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3886}
3887
3888void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3889 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003890 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003891 locations->SetOut(Location::ConstantLocation(constant));
3892}
3893
3894void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3895 // Will be generated at use site.
3896}
3897
3898void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3899 got->SetLocations(nullptr);
3900}
3901
3902void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3903 DCHECK(!successor->IsExitBlock());
3904 HBasicBlock* block = got->GetBlock();
3905 HInstruction* previous = got->GetPrevious();
3906 HLoopInformation* info = block->GetLoopInformation();
3907
3908 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3910 return;
3911 }
3912 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3913 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3914 }
3915 if (!codegen_->GoesToNextBlock(block, successor)) {
3916 __ B(codegen_->GetLabelOf(successor));
3917 }
3918}
3919
3920void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3921 HandleGoto(got, got->GetSuccessor());
3922}
3923
3924void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3925 try_boundary->SetLocations(nullptr);
3926}
3927
3928void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3929 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3930 if (!successor->IsExitBlock()) {
3931 HandleGoto(try_boundary, successor);
3932 }
3933}
3934
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003935void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3936 LocationSummary* locations) {
3937 Register dst = locations->Out().AsRegister<Register>();
3938 Register lhs = locations->InAt(0).AsRegister<Register>();
3939 Location rhs_location = locations->InAt(1);
3940 Register rhs_reg = ZERO;
3941 int64_t rhs_imm = 0;
3942 bool use_imm = rhs_location.IsConstant();
3943 if (use_imm) {
3944 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3945 } else {
3946 rhs_reg = rhs_location.AsRegister<Register>();
3947 }
3948
3949 switch (cond) {
3950 case kCondEQ:
3951 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003952 if (use_imm && IsInt<16>(-rhs_imm)) {
3953 if (rhs_imm == 0) {
3954 if (cond == kCondEQ) {
3955 __ Sltiu(dst, lhs, 1);
3956 } else {
3957 __ Sltu(dst, ZERO, lhs);
3958 }
3959 } else {
3960 __ Addiu(dst, lhs, -rhs_imm);
3961 if (cond == kCondEQ) {
3962 __ Sltiu(dst, dst, 1);
3963 } else {
3964 __ Sltu(dst, ZERO, dst);
3965 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003966 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003967 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003968 if (use_imm && IsUint<16>(rhs_imm)) {
3969 __ Xori(dst, lhs, rhs_imm);
3970 } else {
3971 if (use_imm) {
3972 rhs_reg = TMP;
3973 __ LoadConst32(rhs_reg, rhs_imm);
3974 }
3975 __ Xor(dst, lhs, rhs_reg);
3976 }
3977 if (cond == kCondEQ) {
3978 __ Sltiu(dst, dst, 1);
3979 } else {
3980 __ Sltu(dst, ZERO, dst);
3981 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003982 }
3983 break;
3984
3985 case kCondLT:
3986 case kCondGE:
3987 if (use_imm && IsInt<16>(rhs_imm)) {
3988 __ Slti(dst, lhs, rhs_imm);
3989 } else {
3990 if (use_imm) {
3991 rhs_reg = TMP;
3992 __ LoadConst32(rhs_reg, rhs_imm);
3993 }
3994 __ Slt(dst, lhs, rhs_reg);
3995 }
3996 if (cond == kCondGE) {
3997 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3998 // only the slt instruction but no sge.
3999 __ Xori(dst, dst, 1);
4000 }
4001 break;
4002
4003 case kCondLE:
4004 case kCondGT:
4005 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4006 // Simulate lhs <= rhs via lhs < rhs + 1.
4007 __ Slti(dst, lhs, rhs_imm + 1);
4008 if (cond == kCondGT) {
4009 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4010 // only the slti instruction but no sgti.
4011 __ Xori(dst, dst, 1);
4012 }
4013 } else {
4014 if (use_imm) {
4015 rhs_reg = TMP;
4016 __ LoadConst32(rhs_reg, rhs_imm);
4017 }
4018 __ Slt(dst, rhs_reg, lhs);
4019 if (cond == kCondLE) {
4020 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4021 // only the slt instruction but no sle.
4022 __ Xori(dst, dst, 1);
4023 }
4024 }
4025 break;
4026
4027 case kCondB:
4028 case kCondAE:
4029 if (use_imm && IsInt<16>(rhs_imm)) {
4030 // Sltiu sign-extends its 16-bit immediate operand before
4031 // the comparison and thus lets us compare directly with
4032 // unsigned values in the ranges [0, 0x7fff] and
4033 // [0xffff8000, 0xffffffff].
4034 __ Sltiu(dst, lhs, rhs_imm);
4035 } else {
4036 if (use_imm) {
4037 rhs_reg = TMP;
4038 __ LoadConst32(rhs_reg, rhs_imm);
4039 }
4040 __ Sltu(dst, lhs, rhs_reg);
4041 }
4042 if (cond == kCondAE) {
4043 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4044 // only the sltu instruction but no sgeu.
4045 __ Xori(dst, dst, 1);
4046 }
4047 break;
4048
4049 case kCondBE:
4050 case kCondA:
4051 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4052 // Simulate lhs <= rhs via lhs < rhs + 1.
4053 // Note that this only works if rhs + 1 does not overflow
4054 // to 0, hence the check above.
4055 // Sltiu sign-extends its 16-bit immediate operand before
4056 // the comparison and thus lets us compare directly with
4057 // unsigned values in the ranges [0, 0x7fff] and
4058 // [0xffff8000, 0xffffffff].
4059 __ Sltiu(dst, lhs, rhs_imm + 1);
4060 if (cond == kCondA) {
4061 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4062 // only the sltiu instruction but no sgtiu.
4063 __ Xori(dst, dst, 1);
4064 }
4065 } else {
4066 if (use_imm) {
4067 rhs_reg = TMP;
4068 __ LoadConst32(rhs_reg, rhs_imm);
4069 }
4070 __ Sltu(dst, rhs_reg, lhs);
4071 if (cond == kCondBE) {
4072 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4073 // only the sltu instruction but no sleu.
4074 __ Xori(dst, dst, 1);
4075 }
4076 }
4077 break;
4078 }
4079}
4080
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004081bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4082 LocationSummary* input_locations,
4083 Register dst) {
4084 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4085 Location rhs_location = input_locations->InAt(1);
4086 Register rhs_reg = ZERO;
4087 int64_t rhs_imm = 0;
4088 bool use_imm = rhs_location.IsConstant();
4089 if (use_imm) {
4090 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4091 } else {
4092 rhs_reg = rhs_location.AsRegister<Register>();
4093 }
4094
4095 switch (cond) {
4096 case kCondEQ:
4097 case kCondNE:
4098 if (use_imm && IsInt<16>(-rhs_imm)) {
4099 __ Addiu(dst, lhs, -rhs_imm);
4100 } else if (use_imm && IsUint<16>(rhs_imm)) {
4101 __ Xori(dst, lhs, rhs_imm);
4102 } else {
4103 if (use_imm) {
4104 rhs_reg = TMP;
4105 __ LoadConst32(rhs_reg, rhs_imm);
4106 }
4107 __ Xor(dst, lhs, rhs_reg);
4108 }
4109 return (cond == kCondEQ);
4110
4111 case kCondLT:
4112 case kCondGE:
4113 if (use_imm && IsInt<16>(rhs_imm)) {
4114 __ Slti(dst, lhs, rhs_imm);
4115 } else {
4116 if (use_imm) {
4117 rhs_reg = TMP;
4118 __ LoadConst32(rhs_reg, rhs_imm);
4119 }
4120 __ Slt(dst, lhs, rhs_reg);
4121 }
4122 return (cond == kCondGE);
4123
4124 case kCondLE:
4125 case kCondGT:
4126 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4127 // Simulate lhs <= rhs via lhs < rhs + 1.
4128 __ Slti(dst, lhs, rhs_imm + 1);
4129 return (cond == kCondGT);
4130 } else {
4131 if (use_imm) {
4132 rhs_reg = TMP;
4133 __ LoadConst32(rhs_reg, rhs_imm);
4134 }
4135 __ Slt(dst, rhs_reg, lhs);
4136 return (cond == kCondLE);
4137 }
4138
4139 case kCondB:
4140 case kCondAE:
4141 if (use_imm && IsInt<16>(rhs_imm)) {
4142 // Sltiu sign-extends its 16-bit immediate operand before
4143 // the comparison and thus lets us compare directly with
4144 // unsigned values in the ranges [0, 0x7fff] and
4145 // [0xffff8000, 0xffffffff].
4146 __ Sltiu(dst, lhs, rhs_imm);
4147 } else {
4148 if (use_imm) {
4149 rhs_reg = TMP;
4150 __ LoadConst32(rhs_reg, rhs_imm);
4151 }
4152 __ Sltu(dst, lhs, rhs_reg);
4153 }
4154 return (cond == kCondAE);
4155
4156 case kCondBE:
4157 case kCondA:
4158 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4159 // Simulate lhs <= rhs via lhs < rhs + 1.
4160 // Note that this only works if rhs + 1 does not overflow
4161 // to 0, hence the check above.
4162 // Sltiu sign-extends its 16-bit immediate operand before
4163 // the comparison and thus lets us compare directly with
4164 // unsigned values in the ranges [0, 0x7fff] and
4165 // [0xffff8000, 0xffffffff].
4166 __ Sltiu(dst, lhs, rhs_imm + 1);
4167 return (cond == kCondA);
4168 } else {
4169 if (use_imm) {
4170 rhs_reg = TMP;
4171 __ LoadConst32(rhs_reg, rhs_imm);
4172 }
4173 __ Sltu(dst, rhs_reg, lhs);
4174 return (cond == kCondBE);
4175 }
4176 }
4177}
4178
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004179void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4180 LocationSummary* locations,
4181 MipsLabel* label) {
4182 Register lhs = locations->InAt(0).AsRegister<Register>();
4183 Location rhs_location = locations->InAt(1);
4184 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004185 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004186 bool use_imm = rhs_location.IsConstant();
4187 if (use_imm) {
4188 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4189 } else {
4190 rhs_reg = rhs_location.AsRegister<Register>();
4191 }
4192
4193 if (use_imm && rhs_imm == 0) {
4194 switch (cond) {
4195 case kCondEQ:
4196 case kCondBE: // <= 0 if zero
4197 __ Beqz(lhs, label);
4198 break;
4199 case kCondNE:
4200 case kCondA: // > 0 if non-zero
4201 __ Bnez(lhs, label);
4202 break;
4203 case kCondLT:
4204 __ Bltz(lhs, label);
4205 break;
4206 case kCondGE:
4207 __ Bgez(lhs, label);
4208 break;
4209 case kCondLE:
4210 __ Blez(lhs, label);
4211 break;
4212 case kCondGT:
4213 __ Bgtz(lhs, label);
4214 break;
4215 case kCondB: // always false
4216 break;
4217 case kCondAE: // always true
4218 __ B(label);
4219 break;
4220 }
4221 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004222 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4223 if (isR6 || !use_imm) {
4224 if (use_imm) {
4225 rhs_reg = TMP;
4226 __ LoadConst32(rhs_reg, rhs_imm);
4227 }
4228 switch (cond) {
4229 case kCondEQ:
4230 __ Beq(lhs, rhs_reg, label);
4231 break;
4232 case kCondNE:
4233 __ Bne(lhs, rhs_reg, label);
4234 break;
4235 case kCondLT:
4236 __ Blt(lhs, rhs_reg, label);
4237 break;
4238 case kCondGE:
4239 __ Bge(lhs, rhs_reg, label);
4240 break;
4241 case kCondLE:
4242 __ Bge(rhs_reg, lhs, label);
4243 break;
4244 case kCondGT:
4245 __ Blt(rhs_reg, lhs, label);
4246 break;
4247 case kCondB:
4248 __ Bltu(lhs, rhs_reg, label);
4249 break;
4250 case kCondAE:
4251 __ Bgeu(lhs, rhs_reg, label);
4252 break;
4253 case kCondBE:
4254 __ Bgeu(rhs_reg, lhs, label);
4255 break;
4256 case kCondA:
4257 __ Bltu(rhs_reg, lhs, label);
4258 break;
4259 }
4260 } else {
4261 // Special cases for more efficient comparison with constants on R2.
4262 switch (cond) {
4263 case kCondEQ:
4264 __ LoadConst32(TMP, rhs_imm);
4265 __ Beq(lhs, TMP, label);
4266 break;
4267 case kCondNE:
4268 __ LoadConst32(TMP, rhs_imm);
4269 __ Bne(lhs, TMP, label);
4270 break;
4271 case kCondLT:
4272 if (IsInt<16>(rhs_imm)) {
4273 __ Slti(TMP, lhs, rhs_imm);
4274 __ Bnez(TMP, label);
4275 } else {
4276 __ LoadConst32(TMP, rhs_imm);
4277 __ Blt(lhs, TMP, label);
4278 }
4279 break;
4280 case kCondGE:
4281 if (IsInt<16>(rhs_imm)) {
4282 __ Slti(TMP, lhs, rhs_imm);
4283 __ Beqz(TMP, label);
4284 } else {
4285 __ LoadConst32(TMP, rhs_imm);
4286 __ Bge(lhs, TMP, label);
4287 }
4288 break;
4289 case kCondLE:
4290 if (IsInt<16>(rhs_imm + 1)) {
4291 // Simulate lhs <= rhs via lhs < rhs + 1.
4292 __ Slti(TMP, lhs, rhs_imm + 1);
4293 __ Bnez(TMP, label);
4294 } else {
4295 __ LoadConst32(TMP, rhs_imm);
4296 __ Bge(TMP, lhs, label);
4297 }
4298 break;
4299 case kCondGT:
4300 if (IsInt<16>(rhs_imm + 1)) {
4301 // Simulate lhs > rhs via !(lhs < rhs + 1).
4302 __ Slti(TMP, lhs, rhs_imm + 1);
4303 __ Beqz(TMP, label);
4304 } else {
4305 __ LoadConst32(TMP, rhs_imm);
4306 __ Blt(TMP, lhs, label);
4307 }
4308 break;
4309 case kCondB:
4310 if (IsInt<16>(rhs_imm)) {
4311 __ Sltiu(TMP, lhs, rhs_imm);
4312 __ Bnez(TMP, label);
4313 } else {
4314 __ LoadConst32(TMP, rhs_imm);
4315 __ Bltu(lhs, TMP, label);
4316 }
4317 break;
4318 case kCondAE:
4319 if (IsInt<16>(rhs_imm)) {
4320 __ Sltiu(TMP, lhs, rhs_imm);
4321 __ Beqz(TMP, label);
4322 } else {
4323 __ LoadConst32(TMP, rhs_imm);
4324 __ Bgeu(lhs, TMP, label);
4325 }
4326 break;
4327 case kCondBE:
4328 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4329 // Simulate lhs <= rhs via lhs < rhs + 1.
4330 // Note that this only works if rhs + 1 does not overflow
4331 // to 0, hence the check above.
4332 __ Sltiu(TMP, lhs, rhs_imm + 1);
4333 __ Bnez(TMP, label);
4334 } else {
4335 __ LoadConst32(TMP, rhs_imm);
4336 __ Bgeu(TMP, lhs, label);
4337 }
4338 break;
4339 case kCondA:
4340 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4341 // Simulate lhs > rhs via !(lhs < rhs + 1).
4342 // Note that this only works if rhs + 1 does not overflow
4343 // to 0, hence the check above.
4344 __ Sltiu(TMP, lhs, rhs_imm + 1);
4345 __ Beqz(TMP, label);
4346 } else {
4347 __ LoadConst32(TMP, rhs_imm);
4348 __ Bltu(TMP, lhs, label);
4349 }
4350 break;
4351 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004352 }
4353 }
4354}
4355
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004356void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4357 LocationSummary* locations) {
4358 Register dst = locations->Out().AsRegister<Register>();
4359 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4360 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4361 Location rhs_location = locations->InAt(1);
4362 Register rhs_high = ZERO;
4363 Register rhs_low = ZERO;
4364 int64_t imm = 0;
4365 uint32_t imm_high = 0;
4366 uint32_t imm_low = 0;
4367 bool use_imm = rhs_location.IsConstant();
4368 if (use_imm) {
4369 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4370 imm_high = High32Bits(imm);
4371 imm_low = Low32Bits(imm);
4372 } else {
4373 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4374 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4375 }
4376 if (use_imm && imm == 0) {
4377 switch (cond) {
4378 case kCondEQ:
4379 case kCondBE: // <= 0 if zero
4380 __ Or(dst, lhs_high, lhs_low);
4381 __ Sltiu(dst, dst, 1);
4382 break;
4383 case kCondNE:
4384 case kCondA: // > 0 if non-zero
4385 __ Or(dst, lhs_high, lhs_low);
4386 __ Sltu(dst, ZERO, dst);
4387 break;
4388 case kCondLT:
4389 __ Slt(dst, lhs_high, ZERO);
4390 break;
4391 case kCondGE:
4392 __ Slt(dst, lhs_high, ZERO);
4393 __ Xori(dst, dst, 1);
4394 break;
4395 case kCondLE:
4396 __ Or(TMP, lhs_high, lhs_low);
4397 __ Sra(AT, lhs_high, 31);
4398 __ Sltu(dst, AT, TMP);
4399 __ Xori(dst, dst, 1);
4400 break;
4401 case kCondGT:
4402 __ Or(TMP, lhs_high, lhs_low);
4403 __ Sra(AT, lhs_high, 31);
4404 __ Sltu(dst, AT, TMP);
4405 break;
4406 case kCondB: // always false
4407 __ Andi(dst, dst, 0);
4408 break;
4409 case kCondAE: // always true
4410 __ Ori(dst, ZERO, 1);
4411 break;
4412 }
4413 } else if (use_imm) {
4414 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4415 switch (cond) {
4416 case kCondEQ:
4417 __ LoadConst32(TMP, imm_high);
4418 __ Xor(TMP, TMP, lhs_high);
4419 __ LoadConst32(AT, imm_low);
4420 __ Xor(AT, AT, lhs_low);
4421 __ Or(dst, TMP, AT);
4422 __ Sltiu(dst, dst, 1);
4423 break;
4424 case kCondNE:
4425 __ LoadConst32(TMP, imm_high);
4426 __ Xor(TMP, TMP, lhs_high);
4427 __ LoadConst32(AT, imm_low);
4428 __ Xor(AT, AT, lhs_low);
4429 __ Or(dst, TMP, AT);
4430 __ Sltu(dst, ZERO, dst);
4431 break;
4432 case kCondLT:
4433 case kCondGE:
4434 if (dst == lhs_low) {
4435 __ LoadConst32(TMP, imm_low);
4436 __ Sltu(dst, lhs_low, TMP);
4437 }
4438 __ LoadConst32(TMP, imm_high);
4439 __ Slt(AT, lhs_high, TMP);
4440 __ Slt(TMP, TMP, lhs_high);
4441 if (dst != lhs_low) {
4442 __ LoadConst32(dst, imm_low);
4443 __ Sltu(dst, lhs_low, dst);
4444 }
4445 __ Slt(dst, TMP, dst);
4446 __ Or(dst, dst, AT);
4447 if (cond == kCondGE) {
4448 __ Xori(dst, dst, 1);
4449 }
4450 break;
4451 case kCondGT:
4452 case kCondLE:
4453 if (dst == lhs_low) {
4454 __ LoadConst32(TMP, imm_low);
4455 __ Sltu(dst, TMP, lhs_low);
4456 }
4457 __ LoadConst32(TMP, imm_high);
4458 __ Slt(AT, TMP, lhs_high);
4459 __ Slt(TMP, lhs_high, TMP);
4460 if (dst != lhs_low) {
4461 __ LoadConst32(dst, imm_low);
4462 __ Sltu(dst, dst, lhs_low);
4463 }
4464 __ Slt(dst, TMP, dst);
4465 __ Or(dst, dst, AT);
4466 if (cond == kCondLE) {
4467 __ Xori(dst, dst, 1);
4468 }
4469 break;
4470 case kCondB:
4471 case kCondAE:
4472 if (dst == lhs_low) {
4473 __ LoadConst32(TMP, imm_low);
4474 __ Sltu(dst, lhs_low, TMP);
4475 }
4476 __ LoadConst32(TMP, imm_high);
4477 __ Sltu(AT, lhs_high, TMP);
4478 __ Sltu(TMP, TMP, lhs_high);
4479 if (dst != lhs_low) {
4480 __ LoadConst32(dst, imm_low);
4481 __ Sltu(dst, lhs_low, dst);
4482 }
4483 __ Slt(dst, TMP, dst);
4484 __ Or(dst, dst, AT);
4485 if (cond == kCondAE) {
4486 __ Xori(dst, dst, 1);
4487 }
4488 break;
4489 case kCondA:
4490 case kCondBE:
4491 if (dst == lhs_low) {
4492 __ LoadConst32(TMP, imm_low);
4493 __ Sltu(dst, TMP, lhs_low);
4494 }
4495 __ LoadConst32(TMP, imm_high);
4496 __ Sltu(AT, TMP, lhs_high);
4497 __ Sltu(TMP, lhs_high, TMP);
4498 if (dst != lhs_low) {
4499 __ LoadConst32(dst, imm_low);
4500 __ Sltu(dst, dst, lhs_low);
4501 }
4502 __ Slt(dst, TMP, dst);
4503 __ Or(dst, dst, AT);
4504 if (cond == kCondBE) {
4505 __ Xori(dst, dst, 1);
4506 }
4507 break;
4508 }
4509 } else {
4510 switch (cond) {
4511 case kCondEQ:
4512 __ Xor(TMP, lhs_high, rhs_high);
4513 __ Xor(AT, lhs_low, rhs_low);
4514 __ Or(dst, TMP, AT);
4515 __ Sltiu(dst, dst, 1);
4516 break;
4517 case kCondNE:
4518 __ Xor(TMP, lhs_high, rhs_high);
4519 __ Xor(AT, lhs_low, rhs_low);
4520 __ Or(dst, TMP, AT);
4521 __ Sltu(dst, ZERO, dst);
4522 break;
4523 case kCondLT:
4524 case kCondGE:
4525 __ Slt(TMP, rhs_high, lhs_high);
4526 __ Sltu(AT, lhs_low, rhs_low);
4527 __ Slt(TMP, TMP, AT);
4528 __ Slt(AT, lhs_high, rhs_high);
4529 __ Or(dst, AT, TMP);
4530 if (cond == kCondGE) {
4531 __ Xori(dst, dst, 1);
4532 }
4533 break;
4534 case kCondGT:
4535 case kCondLE:
4536 __ Slt(TMP, lhs_high, rhs_high);
4537 __ Sltu(AT, rhs_low, lhs_low);
4538 __ Slt(TMP, TMP, AT);
4539 __ Slt(AT, rhs_high, lhs_high);
4540 __ Or(dst, AT, TMP);
4541 if (cond == kCondLE) {
4542 __ Xori(dst, dst, 1);
4543 }
4544 break;
4545 case kCondB:
4546 case kCondAE:
4547 __ Sltu(TMP, rhs_high, lhs_high);
4548 __ Sltu(AT, lhs_low, rhs_low);
4549 __ Slt(TMP, TMP, AT);
4550 __ Sltu(AT, lhs_high, rhs_high);
4551 __ Or(dst, AT, TMP);
4552 if (cond == kCondAE) {
4553 __ Xori(dst, dst, 1);
4554 }
4555 break;
4556 case kCondA:
4557 case kCondBE:
4558 __ Sltu(TMP, lhs_high, rhs_high);
4559 __ Sltu(AT, rhs_low, lhs_low);
4560 __ Slt(TMP, TMP, AT);
4561 __ Sltu(AT, rhs_high, lhs_high);
4562 __ Or(dst, AT, TMP);
4563 if (cond == kCondBE) {
4564 __ Xori(dst, dst, 1);
4565 }
4566 break;
4567 }
4568 }
4569}
4570
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004571void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4572 LocationSummary* locations,
4573 MipsLabel* label) {
4574 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4575 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4576 Location rhs_location = locations->InAt(1);
4577 Register rhs_high = ZERO;
4578 Register rhs_low = ZERO;
4579 int64_t imm = 0;
4580 uint32_t imm_high = 0;
4581 uint32_t imm_low = 0;
4582 bool use_imm = rhs_location.IsConstant();
4583 if (use_imm) {
4584 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4585 imm_high = High32Bits(imm);
4586 imm_low = Low32Bits(imm);
4587 } else {
4588 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4589 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4590 }
4591
4592 if (use_imm && imm == 0) {
4593 switch (cond) {
4594 case kCondEQ:
4595 case kCondBE: // <= 0 if zero
4596 __ Or(TMP, lhs_high, lhs_low);
4597 __ Beqz(TMP, label);
4598 break;
4599 case kCondNE:
4600 case kCondA: // > 0 if non-zero
4601 __ Or(TMP, lhs_high, lhs_low);
4602 __ Bnez(TMP, label);
4603 break;
4604 case kCondLT:
4605 __ Bltz(lhs_high, label);
4606 break;
4607 case kCondGE:
4608 __ Bgez(lhs_high, label);
4609 break;
4610 case kCondLE:
4611 __ Or(TMP, lhs_high, lhs_low);
4612 __ Sra(AT, lhs_high, 31);
4613 __ Bgeu(AT, TMP, label);
4614 break;
4615 case kCondGT:
4616 __ Or(TMP, lhs_high, lhs_low);
4617 __ Sra(AT, lhs_high, 31);
4618 __ Bltu(AT, TMP, label);
4619 break;
4620 case kCondB: // always false
4621 break;
4622 case kCondAE: // always true
4623 __ B(label);
4624 break;
4625 }
4626 } else if (use_imm) {
4627 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4628 switch (cond) {
4629 case kCondEQ:
4630 __ LoadConst32(TMP, imm_high);
4631 __ Xor(TMP, TMP, lhs_high);
4632 __ LoadConst32(AT, imm_low);
4633 __ Xor(AT, AT, lhs_low);
4634 __ Or(TMP, TMP, AT);
4635 __ Beqz(TMP, label);
4636 break;
4637 case kCondNE:
4638 __ LoadConst32(TMP, imm_high);
4639 __ Xor(TMP, TMP, lhs_high);
4640 __ LoadConst32(AT, imm_low);
4641 __ Xor(AT, AT, lhs_low);
4642 __ Or(TMP, TMP, AT);
4643 __ Bnez(TMP, label);
4644 break;
4645 case kCondLT:
4646 __ LoadConst32(TMP, imm_high);
4647 __ Blt(lhs_high, TMP, label);
4648 __ Slt(TMP, TMP, lhs_high);
4649 __ LoadConst32(AT, imm_low);
4650 __ Sltu(AT, lhs_low, AT);
4651 __ Blt(TMP, AT, label);
4652 break;
4653 case kCondGE:
4654 __ LoadConst32(TMP, imm_high);
4655 __ Blt(TMP, lhs_high, label);
4656 __ Slt(TMP, lhs_high, TMP);
4657 __ LoadConst32(AT, imm_low);
4658 __ Sltu(AT, lhs_low, AT);
4659 __ Or(TMP, TMP, AT);
4660 __ Beqz(TMP, label);
4661 break;
4662 case kCondLE:
4663 __ LoadConst32(TMP, imm_high);
4664 __ Blt(lhs_high, TMP, label);
4665 __ Slt(TMP, TMP, lhs_high);
4666 __ LoadConst32(AT, imm_low);
4667 __ Sltu(AT, AT, lhs_low);
4668 __ Or(TMP, TMP, AT);
4669 __ Beqz(TMP, label);
4670 break;
4671 case kCondGT:
4672 __ LoadConst32(TMP, imm_high);
4673 __ Blt(TMP, lhs_high, label);
4674 __ Slt(TMP, lhs_high, TMP);
4675 __ LoadConst32(AT, imm_low);
4676 __ Sltu(AT, AT, lhs_low);
4677 __ Blt(TMP, AT, label);
4678 break;
4679 case kCondB:
4680 __ LoadConst32(TMP, imm_high);
4681 __ Bltu(lhs_high, TMP, label);
4682 __ Sltu(TMP, TMP, lhs_high);
4683 __ LoadConst32(AT, imm_low);
4684 __ Sltu(AT, lhs_low, AT);
4685 __ Blt(TMP, AT, label);
4686 break;
4687 case kCondAE:
4688 __ LoadConst32(TMP, imm_high);
4689 __ Bltu(TMP, lhs_high, label);
4690 __ Sltu(TMP, lhs_high, TMP);
4691 __ LoadConst32(AT, imm_low);
4692 __ Sltu(AT, lhs_low, AT);
4693 __ Or(TMP, TMP, AT);
4694 __ Beqz(TMP, label);
4695 break;
4696 case kCondBE:
4697 __ LoadConst32(TMP, imm_high);
4698 __ Bltu(lhs_high, TMP, label);
4699 __ Sltu(TMP, TMP, lhs_high);
4700 __ LoadConst32(AT, imm_low);
4701 __ Sltu(AT, AT, lhs_low);
4702 __ Or(TMP, TMP, AT);
4703 __ Beqz(TMP, label);
4704 break;
4705 case kCondA:
4706 __ LoadConst32(TMP, imm_high);
4707 __ Bltu(TMP, lhs_high, label);
4708 __ Sltu(TMP, lhs_high, TMP);
4709 __ LoadConst32(AT, imm_low);
4710 __ Sltu(AT, AT, lhs_low);
4711 __ Blt(TMP, AT, label);
4712 break;
4713 }
4714 } else {
4715 switch (cond) {
4716 case kCondEQ:
4717 __ Xor(TMP, lhs_high, rhs_high);
4718 __ Xor(AT, lhs_low, rhs_low);
4719 __ Or(TMP, TMP, AT);
4720 __ Beqz(TMP, label);
4721 break;
4722 case kCondNE:
4723 __ Xor(TMP, lhs_high, rhs_high);
4724 __ Xor(AT, lhs_low, rhs_low);
4725 __ Or(TMP, TMP, AT);
4726 __ Bnez(TMP, label);
4727 break;
4728 case kCondLT:
4729 __ Blt(lhs_high, rhs_high, label);
4730 __ Slt(TMP, rhs_high, lhs_high);
4731 __ Sltu(AT, lhs_low, rhs_low);
4732 __ Blt(TMP, AT, label);
4733 break;
4734 case kCondGE:
4735 __ Blt(rhs_high, lhs_high, label);
4736 __ Slt(TMP, lhs_high, rhs_high);
4737 __ Sltu(AT, lhs_low, rhs_low);
4738 __ Or(TMP, TMP, AT);
4739 __ Beqz(TMP, label);
4740 break;
4741 case kCondLE:
4742 __ Blt(lhs_high, rhs_high, label);
4743 __ Slt(TMP, rhs_high, lhs_high);
4744 __ Sltu(AT, rhs_low, lhs_low);
4745 __ Or(TMP, TMP, AT);
4746 __ Beqz(TMP, label);
4747 break;
4748 case kCondGT:
4749 __ Blt(rhs_high, lhs_high, label);
4750 __ Slt(TMP, lhs_high, rhs_high);
4751 __ Sltu(AT, rhs_low, lhs_low);
4752 __ Blt(TMP, AT, label);
4753 break;
4754 case kCondB:
4755 __ Bltu(lhs_high, rhs_high, label);
4756 __ Sltu(TMP, rhs_high, lhs_high);
4757 __ Sltu(AT, lhs_low, rhs_low);
4758 __ Blt(TMP, AT, label);
4759 break;
4760 case kCondAE:
4761 __ Bltu(rhs_high, lhs_high, label);
4762 __ Sltu(TMP, lhs_high, rhs_high);
4763 __ Sltu(AT, lhs_low, rhs_low);
4764 __ Or(TMP, TMP, AT);
4765 __ Beqz(TMP, label);
4766 break;
4767 case kCondBE:
4768 __ Bltu(lhs_high, rhs_high, label);
4769 __ Sltu(TMP, rhs_high, lhs_high);
4770 __ Sltu(AT, rhs_low, lhs_low);
4771 __ Or(TMP, TMP, AT);
4772 __ Beqz(TMP, label);
4773 break;
4774 case kCondA:
4775 __ Bltu(rhs_high, lhs_high, label);
4776 __ Sltu(TMP, lhs_high, rhs_high);
4777 __ Sltu(AT, rhs_low, lhs_low);
4778 __ Blt(TMP, AT, label);
4779 break;
4780 }
4781 }
4782}
4783
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004784void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4785 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004786 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004787 LocationSummary* locations) {
4788 Register dst = locations->Out().AsRegister<Register>();
4789 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4790 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4791 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004793 if (isR6) {
4794 switch (cond) {
4795 case kCondEQ:
4796 __ CmpEqS(FTMP, lhs, rhs);
4797 __ Mfc1(dst, FTMP);
4798 __ Andi(dst, dst, 1);
4799 break;
4800 case kCondNE:
4801 __ CmpEqS(FTMP, lhs, rhs);
4802 __ Mfc1(dst, FTMP);
4803 __ Addiu(dst, dst, 1);
4804 break;
4805 case kCondLT:
4806 if (gt_bias) {
4807 __ CmpLtS(FTMP, lhs, rhs);
4808 } else {
4809 __ CmpUltS(FTMP, lhs, rhs);
4810 }
4811 __ Mfc1(dst, FTMP);
4812 __ Andi(dst, dst, 1);
4813 break;
4814 case kCondLE:
4815 if (gt_bias) {
4816 __ CmpLeS(FTMP, lhs, rhs);
4817 } else {
4818 __ CmpUleS(FTMP, lhs, rhs);
4819 }
4820 __ Mfc1(dst, FTMP);
4821 __ Andi(dst, dst, 1);
4822 break;
4823 case kCondGT:
4824 if (gt_bias) {
4825 __ CmpUltS(FTMP, rhs, lhs);
4826 } else {
4827 __ CmpLtS(FTMP, rhs, lhs);
4828 }
4829 __ Mfc1(dst, FTMP);
4830 __ Andi(dst, dst, 1);
4831 break;
4832 case kCondGE:
4833 if (gt_bias) {
4834 __ CmpUleS(FTMP, rhs, lhs);
4835 } else {
4836 __ CmpLeS(FTMP, rhs, lhs);
4837 }
4838 __ Mfc1(dst, FTMP);
4839 __ Andi(dst, dst, 1);
4840 break;
4841 default:
4842 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4843 UNREACHABLE();
4844 }
4845 } else {
4846 switch (cond) {
4847 case kCondEQ:
4848 __ CeqS(0, lhs, rhs);
4849 __ LoadConst32(dst, 1);
4850 __ Movf(dst, ZERO, 0);
4851 break;
4852 case kCondNE:
4853 __ CeqS(0, lhs, rhs);
4854 __ LoadConst32(dst, 1);
4855 __ Movt(dst, ZERO, 0);
4856 break;
4857 case kCondLT:
4858 if (gt_bias) {
4859 __ ColtS(0, lhs, rhs);
4860 } else {
4861 __ CultS(0, lhs, rhs);
4862 }
4863 __ LoadConst32(dst, 1);
4864 __ Movf(dst, ZERO, 0);
4865 break;
4866 case kCondLE:
4867 if (gt_bias) {
4868 __ ColeS(0, lhs, rhs);
4869 } else {
4870 __ CuleS(0, lhs, rhs);
4871 }
4872 __ LoadConst32(dst, 1);
4873 __ Movf(dst, ZERO, 0);
4874 break;
4875 case kCondGT:
4876 if (gt_bias) {
4877 __ CultS(0, rhs, lhs);
4878 } else {
4879 __ ColtS(0, rhs, lhs);
4880 }
4881 __ LoadConst32(dst, 1);
4882 __ Movf(dst, ZERO, 0);
4883 break;
4884 case kCondGE:
4885 if (gt_bias) {
4886 __ CuleS(0, rhs, lhs);
4887 } else {
4888 __ ColeS(0, rhs, lhs);
4889 }
4890 __ LoadConst32(dst, 1);
4891 __ Movf(dst, ZERO, 0);
4892 break;
4893 default:
4894 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4895 UNREACHABLE();
4896 }
4897 }
4898 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004899 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004900 if (isR6) {
4901 switch (cond) {
4902 case kCondEQ:
4903 __ CmpEqD(FTMP, lhs, rhs);
4904 __ Mfc1(dst, FTMP);
4905 __ Andi(dst, dst, 1);
4906 break;
4907 case kCondNE:
4908 __ CmpEqD(FTMP, lhs, rhs);
4909 __ Mfc1(dst, FTMP);
4910 __ Addiu(dst, dst, 1);
4911 break;
4912 case kCondLT:
4913 if (gt_bias) {
4914 __ CmpLtD(FTMP, lhs, rhs);
4915 } else {
4916 __ CmpUltD(FTMP, lhs, rhs);
4917 }
4918 __ Mfc1(dst, FTMP);
4919 __ Andi(dst, dst, 1);
4920 break;
4921 case kCondLE:
4922 if (gt_bias) {
4923 __ CmpLeD(FTMP, lhs, rhs);
4924 } else {
4925 __ CmpUleD(FTMP, lhs, rhs);
4926 }
4927 __ Mfc1(dst, FTMP);
4928 __ Andi(dst, dst, 1);
4929 break;
4930 case kCondGT:
4931 if (gt_bias) {
4932 __ CmpUltD(FTMP, rhs, lhs);
4933 } else {
4934 __ CmpLtD(FTMP, rhs, lhs);
4935 }
4936 __ Mfc1(dst, FTMP);
4937 __ Andi(dst, dst, 1);
4938 break;
4939 case kCondGE:
4940 if (gt_bias) {
4941 __ CmpUleD(FTMP, rhs, lhs);
4942 } else {
4943 __ CmpLeD(FTMP, rhs, lhs);
4944 }
4945 __ Mfc1(dst, FTMP);
4946 __ Andi(dst, dst, 1);
4947 break;
4948 default:
4949 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4950 UNREACHABLE();
4951 }
4952 } else {
4953 switch (cond) {
4954 case kCondEQ:
4955 __ CeqD(0, lhs, rhs);
4956 __ LoadConst32(dst, 1);
4957 __ Movf(dst, ZERO, 0);
4958 break;
4959 case kCondNE:
4960 __ CeqD(0, lhs, rhs);
4961 __ LoadConst32(dst, 1);
4962 __ Movt(dst, ZERO, 0);
4963 break;
4964 case kCondLT:
4965 if (gt_bias) {
4966 __ ColtD(0, lhs, rhs);
4967 } else {
4968 __ CultD(0, lhs, rhs);
4969 }
4970 __ LoadConst32(dst, 1);
4971 __ Movf(dst, ZERO, 0);
4972 break;
4973 case kCondLE:
4974 if (gt_bias) {
4975 __ ColeD(0, lhs, rhs);
4976 } else {
4977 __ CuleD(0, lhs, rhs);
4978 }
4979 __ LoadConst32(dst, 1);
4980 __ Movf(dst, ZERO, 0);
4981 break;
4982 case kCondGT:
4983 if (gt_bias) {
4984 __ CultD(0, rhs, lhs);
4985 } else {
4986 __ ColtD(0, rhs, lhs);
4987 }
4988 __ LoadConst32(dst, 1);
4989 __ Movf(dst, ZERO, 0);
4990 break;
4991 case kCondGE:
4992 if (gt_bias) {
4993 __ CuleD(0, rhs, lhs);
4994 } else {
4995 __ ColeD(0, rhs, lhs);
4996 }
4997 __ LoadConst32(dst, 1);
4998 __ Movf(dst, ZERO, 0);
4999 break;
5000 default:
5001 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5002 UNREACHABLE();
5003 }
5004 }
5005 }
5006}
5007
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005008bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5009 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005010 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005011 LocationSummary* input_locations,
5012 int cc) {
5013 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5014 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5015 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005016 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005017 switch (cond) {
5018 case kCondEQ:
5019 __ CeqS(cc, lhs, rhs);
5020 return false;
5021 case kCondNE:
5022 __ CeqS(cc, lhs, rhs);
5023 return true;
5024 case kCondLT:
5025 if (gt_bias) {
5026 __ ColtS(cc, lhs, rhs);
5027 } else {
5028 __ CultS(cc, lhs, rhs);
5029 }
5030 return false;
5031 case kCondLE:
5032 if (gt_bias) {
5033 __ ColeS(cc, lhs, rhs);
5034 } else {
5035 __ CuleS(cc, lhs, rhs);
5036 }
5037 return false;
5038 case kCondGT:
5039 if (gt_bias) {
5040 __ CultS(cc, rhs, lhs);
5041 } else {
5042 __ ColtS(cc, rhs, lhs);
5043 }
5044 return false;
5045 case kCondGE:
5046 if (gt_bias) {
5047 __ CuleS(cc, rhs, lhs);
5048 } else {
5049 __ ColeS(cc, rhs, lhs);
5050 }
5051 return false;
5052 default:
5053 LOG(FATAL) << "Unexpected non-floating-point condition";
5054 UNREACHABLE();
5055 }
5056 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005057 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005058 switch (cond) {
5059 case kCondEQ:
5060 __ CeqD(cc, lhs, rhs);
5061 return false;
5062 case kCondNE:
5063 __ CeqD(cc, lhs, rhs);
5064 return true;
5065 case kCondLT:
5066 if (gt_bias) {
5067 __ ColtD(cc, lhs, rhs);
5068 } else {
5069 __ CultD(cc, lhs, rhs);
5070 }
5071 return false;
5072 case kCondLE:
5073 if (gt_bias) {
5074 __ ColeD(cc, lhs, rhs);
5075 } else {
5076 __ CuleD(cc, lhs, rhs);
5077 }
5078 return false;
5079 case kCondGT:
5080 if (gt_bias) {
5081 __ CultD(cc, rhs, lhs);
5082 } else {
5083 __ ColtD(cc, rhs, lhs);
5084 }
5085 return false;
5086 case kCondGE:
5087 if (gt_bias) {
5088 __ CuleD(cc, rhs, lhs);
5089 } else {
5090 __ ColeD(cc, rhs, lhs);
5091 }
5092 return false;
5093 default:
5094 LOG(FATAL) << "Unexpected non-floating-point condition";
5095 UNREACHABLE();
5096 }
5097 }
5098}
5099
5100bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5101 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005103 LocationSummary* input_locations,
5104 FRegister dst) {
5105 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5106 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5107 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005108 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005109 switch (cond) {
5110 case kCondEQ:
5111 __ CmpEqS(dst, lhs, rhs);
5112 return false;
5113 case kCondNE:
5114 __ CmpEqS(dst, lhs, rhs);
5115 return true;
5116 case kCondLT:
5117 if (gt_bias) {
5118 __ CmpLtS(dst, lhs, rhs);
5119 } else {
5120 __ CmpUltS(dst, lhs, rhs);
5121 }
5122 return false;
5123 case kCondLE:
5124 if (gt_bias) {
5125 __ CmpLeS(dst, lhs, rhs);
5126 } else {
5127 __ CmpUleS(dst, lhs, rhs);
5128 }
5129 return false;
5130 case kCondGT:
5131 if (gt_bias) {
5132 __ CmpUltS(dst, rhs, lhs);
5133 } else {
5134 __ CmpLtS(dst, rhs, lhs);
5135 }
5136 return false;
5137 case kCondGE:
5138 if (gt_bias) {
5139 __ CmpUleS(dst, rhs, lhs);
5140 } else {
5141 __ CmpLeS(dst, rhs, lhs);
5142 }
5143 return false;
5144 default:
5145 LOG(FATAL) << "Unexpected non-floating-point condition";
5146 UNREACHABLE();
5147 }
5148 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005149 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005150 switch (cond) {
5151 case kCondEQ:
5152 __ CmpEqD(dst, lhs, rhs);
5153 return false;
5154 case kCondNE:
5155 __ CmpEqD(dst, lhs, rhs);
5156 return true;
5157 case kCondLT:
5158 if (gt_bias) {
5159 __ CmpLtD(dst, lhs, rhs);
5160 } else {
5161 __ CmpUltD(dst, lhs, rhs);
5162 }
5163 return false;
5164 case kCondLE:
5165 if (gt_bias) {
5166 __ CmpLeD(dst, lhs, rhs);
5167 } else {
5168 __ CmpUleD(dst, lhs, rhs);
5169 }
5170 return false;
5171 case kCondGT:
5172 if (gt_bias) {
5173 __ CmpUltD(dst, rhs, lhs);
5174 } else {
5175 __ CmpLtD(dst, rhs, lhs);
5176 }
5177 return false;
5178 case kCondGE:
5179 if (gt_bias) {
5180 __ CmpUleD(dst, rhs, lhs);
5181 } else {
5182 __ CmpLeD(dst, rhs, lhs);
5183 }
5184 return false;
5185 default:
5186 LOG(FATAL) << "Unexpected non-floating-point condition";
5187 UNREACHABLE();
5188 }
5189 }
5190}
5191
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005192void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5193 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005194 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005195 LocationSummary* locations,
5196 MipsLabel* label) {
5197 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5198 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5199 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005200 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005201 if (isR6) {
5202 switch (cond) {
5203 case kCondEQ:
5204 __ CmpEqS(FTMP, lhs, rhs);
5205 __ Bc1nez(FTMP, label);
5206 break;
5207 case kCondNE:
5208 __ CmpEqS(FTMP, lhs, rhs);
5209 __ Bc1eqz(FTMP, label);
5210 break;
5211 case kCondLT:
5212 if (gt_bias) {
5213 __ CmpLtS(FTMP, lhs, rhs);
5214 } else {
5215 __ CmpUltS(FTMP, lhs, rhs);
5216 }
5217 __ Bc1nez(FTMP, label);
5218 break;
5219 case kCondLE:
5220 if (gt_bias) {
5221 __ CmpLeS(FTMP, lhs, rhs);
5222 } else {
5223 __ CmpUleS(FTMP, lhs, rhs);
5224 }
5225 __ Bc1nez(FTMP, label);
5226 break;
5227 case kCondGT:
5228 if (gt_bias) {
5229 __ CmpUltS(FTMP, rhs, lhs);
5230 } else {
5231 __ CmpLtS(FTMP, rhs, lhs);
5232 }
5233 __ Bc1nez(FTMP, label);
5234 break;
5235 case kCondGE:
5236 if (gt_bias) {
5237 __ CmpUleS(FTMP, rhs, lhs);
5238 } else {
5239 __ CmpLeS(FTMP, rhs, lhs);
5240 }
5241 __ Bc1nez(FTMP, label);
5242 break;
5243 default:
5244 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005245 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005246 }
5247 } else {
5248 switch (cond) {
5249 case kCondEQ:
5250 __ CeqS(0, lhs, rhs);
5251 __ Bc1t(0, label);
5252 break;
5253 case kCondNE:
5254 __ CeqS(0, lhs, rhs);
5255 __ Bc1f(0, label);
5256 break;
5257 case kCondLT:
5258 if (gt_bias) {
5259 __ ColtS(0, lhs, rhs);
5260 } else {
5261 __ CultS(0, lhs, rhs);
5262 }
5263 __ Bc1t(0, label);
5264 break;
5265 case kCondLE:
5266 if (gt_bias) {
5267 __ ColeS(0, lhs, rhs);
5268 } else {
5269 __ CuleS(0, lhs, rhs);
5270 }
5271 __ Bc1t(0, label);
5272 break;
5273 case kCondGT:
5274 if (gt_bias) {
5275 __ CultS(0, rhs, lhs);
5276 } else {
5277 __ ColtS(0, rhs, lhs);
5278 }
5279 __ Bc1t(0, label);
5280 break;
5281 case kCondGE:
5282 if (gt_bias) {
5283 __ CuleS(0, rhs, lhs);
5284 } else {
5285 __ ColeS(0, rhs, lhs);
5286 }
5287 __ Bc1t(0, label);
5288 break;
5289 default:
5290 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005291 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005292 }
5293 }
5294 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005295 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005296 if (isR6) {
5297 switch (cond) {
5298 case kCondEQ:
5299 __ CmpEqD(FTMP, lhs, rhs);
5300 __ Bc1nez(FTMP, label);
5301 break;
5302 case kCondNE:
5303 __ CmpEqD(FTMP, lhs, rhs);
5304 __ Bc1eqz(FTMP, label);
5305 break;
5306 case kCondLT:
5307 if (gt_bias) {
5308 __ CmpLtD(FTMP, lhs, rhs);
5309 } else {
5310 __ CmpUltD(FTMP, lhs, rhs);
5311 }
5312 __ Bc1nez(FTMP, label);
5313 break;
5314 case kCondLE:
5315 if (gt_bias) {
5316 __ CmpLeD(FTMP, lhs, rhs);
5317 } else {
5318 __ CmpUleD(FTMP, lhs, rhs);
5319 }
5320 __ Bc1nez(FTMP, label);
5321 break;
5322 case kCondGT:
5323 if (gt_bias) {
5324 __ CmpUltD(FTMP, rhs, lhs);
5325 } else {
5326 __ CmpLtD(FTMP, rhs, lhs);
5327 }
5328 __ Bc1nez(FTMP, label);
5329 break;
5330 case kCondGE:
5331 if (gt_bias) {
5332 __ CmpUleD(FTMP, rhs, lhs);
5333 } else {
5334 __ CmpLeD(FTMP, rhs, lhs);
5335 }
5336 __ Bc1nez(FTMP, label);
5337 break;
5338 default:
5339 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005340 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005341 }
5342 } else {
5343 switch (cond) {
5344 case kCondEQ:
5345 __ CeqD(0, lhs, rhs);
5346 __ Bc1t(0, label);
5347 break;
5348 case kCondNE:
5349 __ CeqD(0, lhs, rhs);
5350 __ Bc1f(0, label);
5351 break;
5352 case kCondLT:
5353 if (gt_bias) {
5354 __ ColtD(0, lhs, rhs);
5355 } else {
5356 __ CultD(0, lhs, rhs);
5357 }
5358 __ Bc1t(0, label);
5359 break;
5360 case kCondLE:
5361 if (gt_bias) {
5362 __ ColeD(0, lhs, rhs);
5363 } else {
5364 __ CuleD(0, lhs, rhs);
5365 }
5366 __ Bc1t(0, label);
5367 break;
5368 case kCondGT:
5369 if (gt_bias) {
5370 __ CultD(0, rhs, lhs);
5371 } else {
5372 __ ColtD(0, rhs, lhs);
5373 }
5374 __ Bc1t(0, label);
5375 break;
5376 case kCondGE:
5377 if (gt_bias) {
5378 __ CuleD(0, rhs, lhs);
5379 } else {
5380 __ ColeD(0, rhs, lhs);
5381 }
5382 __ Bc1t(0, label);
5383 break;
5384 default:
5385 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005386 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005387 }
5388 }
5389 }
5390}
5391
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005392void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005393 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005394 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005395 MipsLabel* false_target) {
5396 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005397
David Brazdil0debae72015-11-12 18:37:00 +00005398 if (true_target == nullptr && false_target == nullptr) {
5399 // Nothing to do. The code always falls through.
5400 return;
5401 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005402 // Constant condition, statically compared against "true" (integer value 1).
5403 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005404 if (true_target != nullptr) {
5405 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005406 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005407 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005408 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005409 if (false_target != nullptr) {
5410 __ B(false_target);
5411 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005412 }
David Brazdil0debae72015-11-12 18:37:00 +00005413 return;
5414 }
5415
5416 // The following code generates these patterns:
5417 // (1) true_target == nullptr && false_target != nullptr
5418 // - opposite condition true => branch to false_target
5419 // (2) true_target != nullptr && false_target == nullptr
5420 // - condition true => branch to true_target
5421 // (3) true_target != nullptr && false_target != nullptr
5422 // - condition true => branch to true_target
5423 // - branch to false_target
5424 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005426 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005427 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005428 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005429 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5430 } else {
5431 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5432 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005433 } else {
5434 // The condition instruction has not been materialized, use its inputs as
5435 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005436 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005438 LocationSummary* locations = cond->GetLocations();
5439 IfCondition if_cond = condition->GetCondition();
5440 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005441
David Brazdil0debae72015-11-12 18:37:00 +00005442 if (true_target == nullptr) {
5443 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005444 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005445 }
5446
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005447 switch (type) {
5448 default:
5449 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5450 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005452 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5453 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 case DataType::Type::kFloat32:
5455 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005456 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5457 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005458 }
5459 }
David Brazdil0debae72015-11-12 18:37:00 +00005460
5461 // If neither branch falls through (case 3), the conditional branch to `true_target`
5462 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5463 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005464 __ B(false_target);
5465 }
5466}
5467
5468void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005469 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005470 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005471 locations->SetInAt(0, Location::RequiresRegister());
5472 }
5473}
5474
5475void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005476 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5477 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5478 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5479 nullptr : codegen_->GetLabelOf(true_successor);
5480 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5481 nullptr : codegen_->GetLabelOf(false_successor);
5482 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005483}
5484
5485void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005486 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005487 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005488 InvokeRuntimeCallingConvention calling_convention;
5489 RegisterSet caller_saves = RegisterSet::Empty();
5490 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5491 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005492 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005493 locations->SetInAt(0, Location::RequiresRegister());
5494 }
5495}
5496
5497void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005498 SlowPathCodeMIPS* slow_path =
5499 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005500 GenerateTestAndBranch(deoptimize,
5501 /* condition_input_index */ 0,
5502 slow_path->GetEntryLabel(),
5503 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005504}
5505
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005506// This function returns true if a conditional move can be generated for HSelect.
5507// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5508// branches and regular moves.
5509//
5510// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5511//
5512// While determining feasibility of a conditional move and setting inputs/outputs
5513// are two distinct tasks, this function does both because they share quite a bit
5514// of common logic.
5515static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5516 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5517 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5518 HCondition* condition = cond->AsCondition();
5519
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 DataType::Type cond_type =
5521 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5522 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005523
5524 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5525 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5526 bool is_true_value_zero_constant =
5527 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5528 bool is_false_value_zero_constant =
5529 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5530
5531 bool can_move_conditionally = false;
5532 bool use_const_for_false_in = false;
5533 bool use_const_for_true_in = false;
5534
5535 if (!cond->IsConstant()) {
5536 switch (cond_type) {
5537 default:
5538 switch (dst_type) {
5539 default:
5540 // Moving int on int condition.
5541 if (is_r6) {
5542 if (is_true_value_zero_constant) {
5543 // seleqz out_reg, false_reg, cond_reg
5544 can_move_conditionally = true;
5545 use_const_for_true_in = true;
5546 } else if (is_false_value_zero_constant) {
5547 // selnez out_reg, true_reg, cond_reg
5548 can_move_conditionally = true;
5549 use_const_for_false_in = true;
5550 } else if (materialized) {
5551 // Not materializing unmaterialized int conditions
5552 // to keep the instruction count low.
5553 // selnez AT, true_reg, cond_reg
5554 // seleqz TMP, false_reg, cond_reg
5555 // or out_reg, AT, TMP
5556 can_move_conditionally = true;
5557 }
5558 } else {
5559 // movn out_reg, true_reg/ZERO, cond_reg
5560 can_move_conditionally = true;
5561 use_const_for_true_in = is_true_value_zero_constant;
5562 }
5563 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005565 // Moving long on int condition.
5566 if (is_r6) {
5567 if (is_true_value_zero_constant) {
5568 // seleqz out_reg_lo, false_reg_lo, cond_reg
5569 // seleqz out_reg_hi, false_reg_hi, cond_reg
5570 can_move_conditionally = true;
5571 use_const_for_true_in = true;
5572 } else if (is_false_value_zero_constant) {
5573 // selnez out_reg_lo, true_reg_lo, cond_reg
5574 // selnez out_reg_hi, true_reg_hi, cond_reg
5575 can_move_conditionally = true;
5576 use_const_for_false_in = true;
5577 }
5578 // Other long conditional moves would generate 6+ instructions,
5579 // which is too many.
5580 } else {
5581 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5582 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5583 can_move_conditionally = true;
5584 use_const_for_true_in = is_true_value_zero_constant;
5585 }
5586 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005587 case DataType::Type::kFloat32:
5588 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005589 // Moving float/double on int condition.
5590 if (is_r6) {
5591 if (materialized) {
5592 // Not materializing unmaterialized int conditions
5593 // to keep the instruction count low.
5594 can_move_conditionally = true;
5595 if (is_true_value_zero_constant) {
5596 // sltu TMP, ZERO, cond_reg
5597 // mtc1 TMP, temp_cond_reg
5598 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5599 use_const_for_true_in = true;
5600 } else if (is_false_value_zero_constant) {
5601 // sltu TMP, ZERO, cond_reg
5602 // mtc1 TMP, temp_cond_reg
5603 // selnez.fmt out_reg, true_reg, temp_cond_reg
5604 use_const_for_false_in = true;
5605 } else {
5606 // sltu TMP, ZERO, cond_reg
5607 // mtc1 TMP, temp_cond_reg
5608 // sel.fmt temp_cond_reg, false_reg, true_reg
5609 // mov.fmt out_reg, temp_cond_reg
5610 }
5611 }
5612 } else {
5613 // movn.fmt out_reg, true_reg, cond_reg
5614 can_move_conditionally = true;
5615 }
5616 break;
5617 }
5618 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005619 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005620 // We don't materialize long comparison now
5621 // and use conditional branches instead.
5622 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005623 case DataType::Type::kFloat32:
5624 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005625 switch (dst_type) {
5626 default:
5627 // Moving int on float/double condition.
5628 if (is_r6) {
5629 if (is_true_value_zero_constant) {
5630 // mfc1 TMP, temp_cond_reg
5631 // seleqz out_reg, false_reg, TMP
5632 can_move_conditionally = true;
5633 use_const_for_true_in = true;
5634 } else if (is_false_value_zero_constant) {
5635 // mfc1 TMP, temp_cond_reg
5636 // selnez out_reg, true_reg, TMP
5637 can_move_conditionally = true;
5638 use_const_for_false_in = true;
5639 } else {
5640 // mfc1 TMP, temp_cond_reg
5641 // selnez AT, true_reg, TMP
5642 // seleqz TMP, false_reg, TMP
5643 // or out_reg, AT, TMP
5644 can_move_conditionally = true;
5645 }
5646 } else {
5647 // movt out_reg, true_reg/ZERO, cc
5648 can_move_conditionally = true;
5649 use_const_for_true_in = is_true_value_zero_constant;
5650 }
5651 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005653 // Moving long on float/double condition.
5654 if (is_r6) {
5655 if (is_true_value_zero_constant) {
5656 // mfc1 TMP, temp_cond_reg
5657 // seleqz out_reg_lo, false_reg_lo, TMP
5658 // seleqz out_reg_hi, false_reg_hi, TMP
5659 can_move_conditionally = true;
5660 use_const_for_true_in = true;
5661 } else if (is_false_value_zero_constant) {
5662 // mfc1 TMP, temp_cond_reg
5663 // selnez out_reg_lo, true_reg_lo, TMP
5664 // selnez out_reg_hi, true_reg_hi, TMP
5665 can_move_conditionally = true;
5666 use_const_for_false_in = true;
5667 }
5668 // Other long conditional moves would generate 6+ instructions,
5669 // which is too many.
5670 } else {
5671 // movt out_reg_lo, true_reg_lo/ZERO, cc
5672 // movt out_reg_hi, true_reg_hi/ZERO, cc
5673 can_move_conditionally = true;
5674 use_const_for_true_in = is_true_value_zero_constant;
5675 }
5676 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 case DataType::Type::kFloat32:
5678 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005679 // Moving float/double on float/double condition.
5680 if (is_r6) {
5681 can_move_conditionally = true;
5682 if (is_true_value_zero_constant) {
5683 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5684 use_const_for_true_in = true;
5685 } else if (is_false_value_zero_constant) {
5686 // selnez.fmt out_reg, true_reg, temp_cond_reg
5687 use_const_for_false_in = true;
5688 } else {
5689 // sel.fmt temp_cond_reg, false_reg, true_reg
5690 // mov.fmt out_reg, temp_cond_reg
5691 }
5692 } else {
5693 // movt.fmt out_reg, true_reg, cc
5694 can_move_conditionally = true;
5695 }
5696 break;
5697 }
5698 break;
5699 }
5700 }
5701
5702 if (can_move_conditionally) {
5703 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5704 } else {
5705 DCHECK(!use_const_for_false_in);
5706 DCHECK(!use_const_for_true_in);
5707 }
5708
5709 if (locations_to_set != nullptr) {
5710 if (use_const_for_false_in) {
5711 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5712 } else {
5713 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005714 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005715 ? Location::RequiresFpuRegister()
5716 : Location::RequiresRegister());
5717 }
5718 if (use_const_for_true_in) {
5719 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5720 } else {
5721 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005723 ? Location::RequiresFpuRegister()
5724 : Location::RequiresRegister());
5725 }
5726 if (materialized) {
5727 locations_to_set->SetInAt(2, Location::RequiresRegister());
5728 }
5729 // On R6 we don't require the output to be the same as the
5730 // first input for conditional moves unlike on R2.
5731 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5732 if (is_out_same_as_first_in) {
5733 locations_to_set->SetOut(Location::SameAsFirstInput());
5734 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005735 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005736 ? Location::RequiresFpuRegister()
5737 : Location::RequiresRegister());
5738 }
5739 }
5740
5741 return can_move_conditionally;
5742}
5743
5744void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5745 LocationSummary* locations = select->GetLocations();
5746 Location dst = locations->Out();
5747 Location src = locations->InAt(1);
5748 Register src_reg = ZERO;
5749 Register src_reg_high = ZERO;
5750 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5751 Register cond_reg = TMP;
5752 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005753 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005754 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005755 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005756
5757 if (IsBooleanValueOrMaterializedCondition(cond)) {
5758 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5759 } else {
5760 HCondition* condition = cond->AsCondition();
5761 LocationSummary* cond_locations = cond->GetLocations();
5762 IfCondition if_cond = condition->GetCondition();
5763 cond_type = condition->InputAt(0)->GetType();
5764 switch (cond_type) {
5765 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005767 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5768 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 case DataType::Type::kFloat32:
5770 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005771 cond_inverted = MaterializeFpCompareR2(if_cond,
5772 condition->IsGtBias(),
5773 cond_type,
5774 cond_locations,
5775 cond_cc);
5776 break;
5777 }
5778 }
5779
5780 DCHECK(dst.Equals(locations->InAt(0)));
5781 if (src.IsRegister()) {
5782 src_reg = src.AsRegister<Register>();
5783 } else if (src.IsRegisterPair()) {
5784 src_reg = src.AsRegisterPairLow<Register>();
5785 src_reg_high = src.AsRegisterPairHigh<Register>();
5786 } else if (src.IsConstant()) {
5787 DCHECK(src.GetConstant()->IsZeroBitPattern());
5788 }
5789
5790 switch (cond_type) {
5791 default:
5792 switch (dst_type) {
5793 default:
5794 if (cond_inverted) {
5795 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5796 } else {
5797 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5798 }
5799 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005801 if (cond_inverted) {
5802 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5803 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5804 } else {
5805 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5806 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5807 }
5808 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005810 if (cond_inverted) {
5811 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5812 } else {
5813 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5814 }
5815 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005816 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005817 if (cond_inverted) {
5818 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5819 } else {
5820 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5821 }
5822 break;
5823 }
5824 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005826 LOG(FATAL) << "Unreachable";
5827 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 case DataType::Type::kFloat32:
5829 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005830 switch (dst_type) {
5831 default:
5832 if (cond_inverted) {
5833 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5834 } else {
5835 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5836 }
5837 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005839 if (cond_inverted) {
5840 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5841 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5842 } else {
5843 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5844 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5845 }
5846 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005847 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005848 if (cond_inverted) {
5849 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5850 } else {
5851 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5852 }
5853 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005854 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005855 if (cond_inverted) {
5856 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5857 } else {
5858 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5859 }
5860 break;
5861 }
5862 break;
5863 }
5864}
5865
5866void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5867 LocationSummary* locations = select->GetLocations();
5868 Location dst = locations->Out();
5869 Location false_src = locations->InAt(0);
5870 Location true_src = locations->InAt(1);
5871 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5872 Register cond_reg = TMP;
5873 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005875 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005876 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005877
5878 if (IsBooleanValueOrMaterializedCondition(cond)) {
5879 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5880 } else {
5881 HCondition* condition = cond->AsCondition();
5882 LocationSummary* cond_locations = cond->GetLocations();
5883 IfCondition if_cond = condition->GetCondition();
5884 cond_type = condition->InputAt(0)->GetType();
5885 switch (cond_type) {
5886 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005887 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005888 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5889 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 case DataType::Type::kFloat32:
5891 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005892 cond_inverted = MaterializeFpCompareR6(if_cond,
5893 condition->IsGtBias(),
5894 cond_type,
5895 cond_locations,
5896 fcond_reg);
5897 break;
5898 }
5899 }
5900
5901 if (true_src.IsConstant()) {
5902 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5903 }
5904 if (false_src.IsConstant()) {
5905 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5906 }
5907
5908 switch (dst_type) {
5909 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005911 __ Mfc1(cond_reg, fcond_reg);
5912 }
5913 if (true_src.IsConstant()) {
5914 if (cond_inverted) {
5915 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5916 } else {
5917 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5918 }
5919 } else if (false_src.IsConstant()) {
5920 if (cond_inverted) {
5921 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5922 } else {
5923 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5924 }
5925 } else {
5926 DCHECK_NE(cond_reg, AT);
5927 if (cond_inverted) {
5928 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5929 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5930 } else {
5931 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5932 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5933 }
5934 __ Or(dst.AsRegister<Register>(), AT, TMP);
5935 }
5936 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 case DataType::Type::kInt64: {
5938 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005939 __ Mfc1(cond_reg, fcond_reg);
5940 }
5941 Register dst_lo = dst.AsRegisterPairLow<Register>();
5942 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5943 if (true_src.IsConstant()) {
5944 Register src_lo = false_src.AsRegisterPairLow<Register>();
5945 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5946 if (cond_inverted) {
5947 __ Selnez(dst_lo, src_lo, cond_reg);
5948 __ Selnez(dst_hi, src_hi, cond_reg);
5949 } else {
5950 __ Seleqz(dst_lo, src_lo, cond_reg);
5951 __ Seleqz(dst_hi, src_hi, cond_reg);
5952 }
5953 } else {
5954 DCHECK(false_src.IsConstant());
5955 Register src_lo = true_src.AsRegisterPairLow<Register>();
5956 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5957 if (cond_inverted) {
5958 __ Seleqz(dst_lo, src_lo, cond_reg);
5959 __ Seleqz(dst_hi, src_hi, cond_reg);
5960 } else {
5961 __ Selnez(dst_lo, src_lo, cond_reg);
5962 __ Selnez(dst_hi, src_hi, cond_reg);
5963 }
5964 }
5965 break;
5966 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kFloat32: {
5968 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005969 // sel*.fmt tests bit 0 of the condition register, account for that.
5970 __ Sltu(TMP, ZERO, cond_reg);
5971 __ Mtc1(TMP, fcond_reg);
5972 }
5973 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5974 if (true_src.IsConstant()) {
5975 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5976 if (cond_inverted) {
5977 __ SelnezS(dst_reg, src_reg, fcond_reg);
5978 } else {
5979 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5980 }
5981 } else if (false_src.IsConstant()) {
5982 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5983 if (cond_inverted) {
5984 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5985 } else {
5986 __ SelnezS(dst_reg, src_reg, fcond_reg);
5987 }
5988 } else {
5989 if (cond_inverted) {
5990 __ SelS(fcond_reg,
5991 true_src.AsFpuRegister<FRegister>(),
5992 false_src.AsFpuRegister<FRegister>());
5993 } else {
5994 __ SelS(fcond_reg,
5995 false_src.AsFpuRegister<FRegister>(),
5996 true_src.AsFpuRegister<FRegister>());
5997 }
5998 __ MovS(dst_reg, fcond_reg);
5999 }
6000 break;
6001 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006002 case DataType::Type::kFloat64: {
6003 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006004 // sel*.fmt tests bit 0 of the condition register, account for that.
6005 __ Sltu(TMP, ZERO, cond_reg);
6006 __ Mtc1(TMP, fcond_reg);
6007 }
6008 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6009 if (true_src.IsConstant()) {
6010 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6011 if (cond_inverted) {
6012 __ SelnezD(dst_reg, src_reg, fcond_reg);
6013 } else {
6014 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6015 }
6016 } else if (false_src.IsConstant()) {
6017 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6018 if (cond_inverted) {
6019 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6020 } else {
6021 __ SelnezD(dst_reg, src_reg, fcond_reg);
6022 }
6023 } else {
6024 if (cond_inverted) {
6025 __ SelD(fcond_reg,
6026 true_src.AsFpuRegister<FRegister>(),
6027 false_src.AsFpuRegister<FRegister>());
6028 } else {
6029 __ SelD(fcond_reg,
6030 false_src.AsFpuRegister<FRegister>(),
6031 true_src.AsFpuRegister<FRegister>());
6032 }
6033 __ MovD(dst_reg, fcond_reg);
6034 }
6035 break;
6036 }
6037 }
6038}
6039
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006040void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006041 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006042 LocationSummary(flag, LocationSummary::kNoCall);
6043 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006044}
6045
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006046void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6047 __ LoadFromOffset(kLoadWord,
6048 flag->GetLocations()->Out().AsRegister<Register>(),
6049 SP,
6050 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006051}
6052
David Brazdil74eb1b22015-12-14 11:44:01 +00006053void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006054 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006055 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006056}
6057
6058void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006059 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6060 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6061 if (is_r6) {
6062 GenConditionalMoveR6(select);
6063 } else {
6064 GenConditionalMoveR2(select);
6065 }
6066 } else {
6067 LocationSummary* locations = select->GetLocations();
6068 MipsLabel false_target;
6069 GenerateTestAndBranch(select,
6070 /* condition_input_index */ 2,
6071 /* true_target */ nullptr,
6072 &false_target);
6073 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6074 __ Bind(&false_target);
6075 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006076}
6077
David Srbecky0cf44932015-12-09 14:09:59 +00006078void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006079 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006080}
6081
David Srbeckyd28f4a02016-03-14 17:14:24 +00006082void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6083 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006084}
6085
6086void CodeGeneratorMIPS::GenerateNop() {
6087 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006088}
6089
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006090void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006091 DataType::Type field_type = field_info.GetFieldType();
6092 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006093 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006094 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006095 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006096 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006097 instruction,
6098 generate_volatile
6099 ? LocationSummary::kCallOnMainOnly
6100 : (object_field_get_with_read_barrier
6101 ? LocationSummary::kCallOnSlowPath
6102 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006103
Alexey Frunzec61c0762017-04-10 13:54:23 -07006104 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6105 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6106 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006107 locations->SetInAt(0, Location::RequiresRegister());
6108 if (generate_volatile) {
6109 InvokeRuntimeCallingConvention calling_convention;
6110 // need A0 to hold base + offset
6111 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 if (field_type == DataType::Type::kInt64) {
6113 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006114 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006115 // Use Location::Any() to prevent situations when running out of available fp registers.
6116 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006117 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006118 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006119 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6120 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6121 }
6122 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006123 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006124 locations->SetOut(Location::RequiresFpuRegister());
6125 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006126 // The output overlaps in the case of an object field get with
6127 // read barriers enabled: we do not want the move to overwrite the
6128 // object's location, as we need it to emit the read barrier.
6129 locations->SetOut(Location::RequiresRegister(),
6130 object_field_get_with_read_barrier
6131 ? Location::kOutputOverlap
6132 : Location::kNoOutputOverlap);
6133 }
6134 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6135 // We need a temporary register for the read barrier marking slow
6136 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006137 if (!kBakerReadBarrierThunksEnableForFields) {
6138 locations->AddTemp(Location::RequiresRegister());
6139 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006140 }
6141 }
6142}
6143
6144void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6145 const FieldInfo& field_info,
6146 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006147 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6148 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006149 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006150 Location obj_loc = locations->InAt(0);
6151 Register obj = obj_loc.AsRegister<Register>();
6152 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 LoadOperandType load_type = kLoadUnsignedByte;
6154 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006155 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006156 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006157
6158 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006159 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006160 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006161 load_type = kLoadUnsignedByte;
6162 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006163 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006164 load_type = kLoadSignedByte;
6165 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006166 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167 load_type = kLoadUnsignedHalfword;
6168 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006169 case DataType::Type::kInt16:
6170 load_type = kLoadSignedHalfword;
6171 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006172 case DataType::Type::kInt32:
6173 case DataType::Type::kFloat32:
6174 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006175 load_type = kLoadWord;
6176 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006177 case DataType::Type::kInt64:
6178 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006179 load_type = kLoadDoubleword;
6180 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006182 LOG(FATAL) << "Unreachable type " << type;
6183 UNREACHABLE();
6184 }
6185
6186 if (is_volatile && load_type == kLoadDoubleword) {
6187 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006188 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006189 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006190 __ LoadFromOffset(kLoadWord,
6191 ZERO,
6192 locations->GetTemp(0).AsRegister<Register>(),
6193 0,
6194 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006195 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006196 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006198 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006199 if (dst_loc.IsFpuRegister()) {
6200 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006201 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006202 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006203 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006204 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006205 __ StoreToOffset(kStoreWord,
6206 locations->GetTemp(1).AsRegister<Register>(),
6207 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006208 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006209 __ StoreToOffset(kStoreWord,
6210 locations->GetTemp(2).AsRegister<Register>(),
6211 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006212 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006213 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214 }
6215 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006217 // /* HeapReference<Object> */ dst = *(obj + offset)
6218 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006219 Location temp_loc =
6220 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006221 // Note that a potential implicit null check is handled in this
6222 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6223 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6224 dst_loc,
6225 obj,
6226 offset,
6227 temp_loc,
6228 /* needs_null_check */ true);
6229 if (is_volatile) {
6230 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6231 }
6232 } else {
6233 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6234 if (is_volatile) {
6235 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6236 }
6237 // If read barriers are enabled, emit read barriers other than
6238 // Baker's using a slow path (and also unpoison the loaded
6239 // reference, if heap poisoning is enabled).
6240 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6241 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006242 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006243 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006244 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006245 DCHECK(dst_loc.IsRegisterPair());
6246 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006247 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006248 DCHECK(dst_loc.IsRegister());
6249 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006251 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006252 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006253 DCHECK(dst_loc.IsFpuRegister());
6254 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006255 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006256 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006257 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006258 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006259 }
6260 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006261 }
6262
Alexey Frunze15958152017-02-09 19:08:30 -08006263 // Memory barriers, in the case of references, are handled in the
6264 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6267 }
6268}
6269
6270void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006271 DataType::Type field_type = field_info.GetFieldType();
6272 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006273 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006274 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006275 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276
6277 locations->SetInAt(0, Location::RequiresRegister());
6278 if (generate_volatile) {
6279 InvokeRuntimeCallingConvention calling_convention;
6280 // need A0 to hold base + offset
6281 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006283 locations->SetInAt(1, Location::RegisterPairLocation(
6284 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6285 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006286 // Use Location::Any() to prevent situations when running out of available fp registers.
6287 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006288 // Pass FP parameters in core registers.
6289 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6290 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6291 }
6292 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006294 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006296 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006297 }
6298 }
6299}
6300
6301void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6302 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006303 uint32_t dex_pc,
6304 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006305 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006306 LocationSummary* locations = instruction->GetLocations();
6307 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006308 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006309 StoreOperandType store_type = kStoreByte;
6310 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006311 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006312 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006313 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314
6315 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006316 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006317 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006319 store_type = kStoreByte;
6320 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006322 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006323 store_type = kStoreHalfword;
6324 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006325 case DataType::Type::kInt32:
6326 case DataType::Type::kFloat32:
6327 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006328 store_type = kStoreWord;
6329 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006330 case DataType::Type::kInt64:
6331 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006332 store_type = kStoreDoubleword;
6333 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006335 LOG(FATAL) << "Unreachable type " << type;
6336 UNREACHABLE();
6337 }
6338
6339 if (is_volatile) {
6340 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6341 }
6342
6343 if (is_volatile && store_type == kStoreDoubleword) {
6344 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006345 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006346 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006347 __ LoadFromOffset(kLoadWord,
6348 ZERO,
6349 locations->GetTemp(0).AsRegister<Register>(),
6350 0,
6351 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006353 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006354 if (value_location.IsFpuRegister()) {
6355 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6356 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006357 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006358 value_location.AsFpuRegister<FRegister>());
6359 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006360 __ LoadFromOffset(kLoadWord,
6361 locations->GetTemp(1).AsRegister<Register>(),
6362 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006363 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006364 __ LoadFromOffset(kLoadWord,
6365 locations->GetTemp(2).AsRegister<Register>(),
6366 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006367 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006368 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006369 DCHECK(value_location.IsConstant());
6370 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6371 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006372 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6373 locations->GetTemp(1).AsRegister<Register>(),
6374 value);
6375 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006377 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6379 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006380 if (value_location.IsConstant()) {
6381 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6382 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006384 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006385 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006386 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006387 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006388 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006389 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006390 if (kPoisonHeapReferences && needs_write_barrier) {
6391 // Note that in the case where `value` is a null reference,
6392 // we do not enter this block, as a null reference does not
6393 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006395 __ PoisonHeapReference(TMP, src);
6396 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6397 } else {
6398 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6399 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006400 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006401 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006403 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006404 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006405 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006406 }
6407 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006408 }
6409
Alexey Frunzec061de12017-02-14 13:27:23 -08006410 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006411 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006412 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006413 }
6414
6415 if (is_volatile) {
6416 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6417 }
6418}
6419
6420void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6421 HandleFieldGet(instruction, instruction->GetFieldInfo());
6422}
6423
6424void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6425 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6426}
6427
6428void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6429 HandleFieldSet(instruction, instruction->GetFieldInfo());
6430}
6431
6432void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006433 HandleFieldSet(instruction,
6434 instruction->GetFieldInfo(),
6435 instruction->GetDexPc(),
6436 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437}
6438
Alexey Frunze15958152017-02-09 19:08:30 -08006439void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6440 HInstruction* instruction,
6441 Location out,
6442 uint32_t offset,
6443 Location maybe_temp,
6444 ReadBarrierOption read_barrier_option) {
6445 Register out_reg = out.AsRegister<Register>();
6446 if (read_barrier_option == kWithReadBarrier) {
6447 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006448 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6449 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6450 }
Alexey Frunze15958152017-02-09 19:08:30 -08006451 if (kUseBakerReadBarrier) {
6452 // Load with fast path based Baker's read barrier.
6453 // /* HeapReference<Object> */ out = *(out + offset)
6454 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6455 out,
6456 out_reg,
6457 offset,
6458 maybe_temp,
6459 /* needs_null_check */ false);
6460 } else {
6461 // Load with slow path based read barrier.
6462 // Save the value of `out` into `maybe_temp` before overwriting it
6463 // in the following move operation, as we will need it for the
6464 // read barrier below.
6465 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6466 // /* HeapReference<Object> */ out = *(out + offset)
6467 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6468 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6469 }
6470 } else {
6471 // Plain load with no read barrier.
6472 // /* HeapReference<Object> */ out = *(out + offset)
6473 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6474 __ MaybeUnpoisonHeapReference(out_reg);
6475 }
6476}
6477
6478void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6479 HInstruction* instruction,
6480 Location out,
6481 Location obj,
6482 uint32_t offset,
6483 Location maybe_temp,
6484 ReadBarrierOption read_barrier_option) {
6485 Register out_reg = out.AsRegister<Register>();
6486 Register obj_reg = obj.AsRegister<Register>();
6487 if (read_barrier_option == kWithReadBarrier) {
6488 CHECK(kEmitCompilerReadBarrier);
6489 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006490 if (!kBakerReadBarrierThunksEnableForFields) {
6491 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6492 }
Alexey Frunze15958152017-02-09 19:08:30 -08006493 // Load with fast path based Baker's read barrier.
6494 // /* HeapReference<Object> */ out = *(obj + offset)
6495 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6496 out,
6497 obj_reg,
6498 offset,
6499 maybe_temp,
6500 /* needs_null_check */ false);
6501 } else {
6502 // Load with slow path based read barrier.
6503 // /* HeapReference<Object> */ out = *(obj + offset)
6504 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6505 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6506 }
6507 } else {
6508 // Plain load with no read barrier.
6509 // /* HeapReference<Object> */ out = *(obj + offset)
6510 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6511 __ MaybeUnpoisonHeapReference(out_reg);
6512 }
6513}
6514
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006515static inline int GetBakerMarkThunkNumber(Register reg) {
6516 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6517 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6518 return reg - V0;
6519 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6520 return 14 + (reg - S2);
6521 } else if (reg == FP) { // One more.
6522 return 20;
6523 }
6524 LOG(FATAL) << "Unexpected register " << reg;
6525 UNREACHABLE();
6526}
6527
6528static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6529 int num = GetBakerMarkThunkNumber(reg) +
6530 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6531 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6532}
6533
6534static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6535 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6536 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6537}
6538
Alexey Frunze15958152017-02-09 19:08:30 -08006539void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6540 Location root,
6541 Register obj,
6542 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006543 ReadBarrierOption read_barrier_option,
6544 MipsLabel* label_low) {
6545 bool reordering;
6546 if (label_low != nullptr) {
6547 DCHECK_EQ(offset, 0x5678u);
6548 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006549 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006550 if (read_barrier_option == kWithReadBarrier) {
6551 DCHECK(kEmitCompilerReadBarrier);
6552 if (kUseBakerReadBarrier) {
6553 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6554 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006555 if (kBakerReadBarrierThunksEnableForGcRoots) {
6556 // Note that we do not actually check the value of `GetIsGcMarking()`
6557 // to decide whether to mark the loaded GC root or not. Instead, we
6558 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6559 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6560 // vice versa.
6561 //
6562 // We use thunks for the slow path. That thunk checks the reference
6563 // and jumps to the entrypoint if needed.
6564 //
6565 // temp = Thread::Current()->pReadBarrierMarkReg00
6566 // // AKA &art_quick_read_barrier_mark_introspection.
6567 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6568 // if (temp != nullptr) {
6569 // temp = &gc_root_thunk<root_reg>
6570 // root = temp(root)
6571 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006572
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006573 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6574 const int32_t entry_point_offset =
6575 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6576 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6577 int16_t offset_low = Low16Bits(offset);
6578 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6579 // extension in lw.
6580 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6581 Register base = short_offset ? obj : TMP;
6582 // Loading the entrypoint does not require a load acquire since it is only changed when
6583 // threads are suspended or running a checkpoint.
6584 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6585 reordering = __ SetReorder(false);
6586 if (!short_offset) {
6587 DCHECK(!label_low);
6588 __ AddUpper(base, obj, offset_high);
6589 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006590 MipsLabel skip_call;
6591 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006592 if (label_low != nullptr) {
6593 DCHECK(short_offset);
6594 __ Bind(label_low);
6595 }
6596 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6597 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6598 // in delay slot.
6599 if (isR6) {
6600 __ Jialc(T9, thunk_disp);
6601 } else {
6602 __ Addiu(T9, T9, thunk_disp);
6603 __ Jalr(T9);
6604 __ Nop();
6605 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006606 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006607 __ SetReorder(reordering);
6608 } else {
6609 // Note that we do not actually check the value of `GetIsGcMarking()`
6610 // to decide whether to mark the loaded GC root or not. Instead, we
6611 // load into `temp` (T9) the read barrier mark entry point corresponding
6612 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6613 // is false, and vice versa.
6614 //
6615 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6616 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6617 // if (temp != null) {
6618 // root = temp(root)
6619 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006620
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006621 if (label_low != nullptr) {
6622 reordering = __ SetReorder(false);
6623 __ Bind(label_low);
6624 }
6625 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6626 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6627 if (label_low != nullptr) {
6628 __ SetReorder(reordering);
6629 }
6630 static_assert(
6631 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6632 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6633 "have different sizes.");
6634 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6635 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6636 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006637
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006638 // Slow path marking the GC root `root`.
6639 Location temp = Location::RegisterLocation(T9);
6640 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006641 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006642 instruction,
6643 root,
6644 /*entrypoint*/ temp);
6645 codegen_->AddSlowPath(slow_path);
6646
6647 const int32_t entry_point_offset =
6648 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6649 // Loading the entrypoint does not require a load acquire since it is only changed when
6650 // threads are suspended or running a checkpoint.
6651 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6652 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6653 __ Bind(slow_path->GetExitLabel());
6654 }
Alexey Frunze15958152017-02-09 19:08:30 -08006655 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006656 if (label_low != nullptr) {
6657 reordering = __ SetReorder(false);
6658 __ Bind(label_low);
6659 }
Alexey Frunze15958152017-02-09 19:08:30 -08006660 // GC root loaded through a slow path for read barriers other
6661 // than Baker's.
6662 // /* GcRoot<mirror::Object>* */ root = obj + offset
6663 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006664 if (label_low != nullptr) {
6665 __ SetReorder(reordering);
6666 }
Alexey Frunze15958152017-02-09 19:08:30 -08006667 // /* mirror::Object* */ root = root->Read()
6668 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6669 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006670 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006671 if (label_low != nullptr) {
6672 reordering = __ SetReorder(false);
6673 __ Bind(label_low);
6674 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006675 // Plain GC root load with no read barrier.
6676 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6677 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6678 // Note that GC roots are not affected by heap poisoning, thus we
6679 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006680 if (label_low != nullptr) {
6681 __ SetReorder(reordering);
6682 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006683 }
6684}
6685
Alexey Frunze15958152017-02-09 19:08:30 -08006686void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6687 Location ref,
6688 Register obj,
6689 uint32_t offset,
6690 Location temp,
6691 bool needs_null_check) {
6692 DCHECK(kEmitCompilerReadBarrier);
6693 DCHECK(kUseBakerReadBarrier);
6694
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006695 if (kBakerReadBarrierThunksEnableForFields) {
6696 // Note that we do not actually check the value of `GetIsGcMarking()`
6697 // to decide whether to mark the loaded reference or not. Instead, we
6698 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6699 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6700 // vice versa.
6701 //
6702 // We use thunks for the slow path. That thunk checks the reference
6703 // and jumps to the entrypoint if needed. If the holder is not gray,
6704 // it issues a load-load memory barrier and returns to the original
6705 // reference load.
6706 //
6707 // temp = Thread::Current()->pReadBarrierMarkReg00
6708 // // AKA &art_quick_read_barrier_mark_introspection.
6709 // if (temp != nullptr) {
6710 // temp = &field_array_thunk<holder_reg>
6711 // temp()
6712 // }
6713 // not_gray_return_address:
6714 // // If the offset is too large to fit into the lw instruction, we
6715 // // use an adjusted base register (TMP) here. This register
6716 // // receives bits 16 ... 31 of the offset before the thunk invocation
6717 // // and the thunk benefits from it.
6718 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6719 // gray_return_address:
6720
6721 DCHECK(temp.IsInvalid());
6722 bool isR6 = GetInstructionSetFeatures().IsR6();
6723 int16_t offset_low = Low16Bits(offset);
6724 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6725 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6726 bool reordering = __ SetReorder(false);
6727 const int32_t entry_point_offset =
6728 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6729 // There may have or may have not been a null check if the field offset is smaller than
6730 // the page size.
6731 // There must've been a null check in case it's actually a load from an array.
6732 // We will, however, perform an explicit null check in the thunk as it's easier to
6733 // do it than not.
6734 if (instruction->IsArrayGet()) {
6735 DCHECK(!needs_null_check);
6736 }
6737 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6738 // Loading the entrypoint does not require a load acquire since it is only changed when
6739 // threads are suspended or running a checkpoint.
6740 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6741 Register ref_reg = ref.AsRegister<Register>();
6742 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006743 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006744 if (short_offset) {
6745 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006746 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006747 __ Nop(); // In forbidden slot.
6748 __ Jialc(T9, thunk_disp);
6749 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006750 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006751 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6752 __ Jalr(T9);
6753 __ Nop(); // In delay slot.
6754 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006755 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006756 } else {
6757 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006758 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006759 __ Aui(base, obj, offset_high); // In delay slot.
6760 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006761 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006762 } else {
6763 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006764 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006765 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6766 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006767 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006768 __ Addu(base, base, obj); // In delay slot.
6769 }
6770 }
6771 // /* HeapReference<Object> */ ref = *(obj + offset)
6772 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6773 if (needs_null_check) {
6774 MaybeRecordImplicitNullCheck(instruction);
6775 }
6776 __ MaybeUnpoisonHeapReference(ref_reg);
6777 __ SetReorder(reordering);
6778 return;
6779 }
6780
Alexey Frunze15958152017-02-09 19:08:30 -08006781 // /* HeapReference<Object> */ ref = *(obj + offset)
6782 Location no_index = Location::NoLocation();
6783 ScaleFactor no_scale_factor = TIMES_1;
6784 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6785 ref,
6786 obj,
6787 offset,
6788 no_index,
6789 no_scale_factor,
6790 temp,
6791 needs_null_check);
6792}
6793
6794void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6795 Location ref,
6796 Register obj,
6797 uint32_t data_offset,
6798 Location index,
6799 Location temp,
6800 bool needs_null_check) {
6801 DCHECK(kEmitCompilerReadBarrier);
6802 DCHECK(kUseBakerReadBarrier);
6803
6804 static_assert(
6805 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6806 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006807 ScaleFactor scale_factor = TIMES_4;
6808
6809 if (kBakerReadBarrierThunksEnableForArrays) {
6810 // Note that we do not actually check the value of `GetIsGcMarking()`
6811 // to decide whether to mark the loaded reference or not. Instead, we
6812 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6813 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6814 // vice versa.
6815 //
6816 // We use thunks for the slow path. That thunk checks the reference
6817 // and jumps to the entrypoint if needed. If the holder is not gray,
6818 // it issues a load-load memory barrier and returns to the original
6819 // reference load.
6820 //
6821 // temp = Thread::Current()->pReadBarrierMarkReg00
6822 // // AKA &art_quick_read_barrier_mark_introspection.
6823 // if (temp != nullptr) {
6824 // temp = &field_array_thunk<holder_reg>
6825 // temp()
6826 // }
6827 // not_gray_return_address:
6828 // // The element address is pre-calculated in the TMP register before the
6829 // // thunk invocation and the thunk benefits from it.
6830 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6831 // gray_return_address:
6832
6833 DCHECK(temp.IsInvalid());
6834 DCHECK(index.IsValid());
6835 bool reordering = __ SetReorder(false);
6836 const int32_t entry_point_offset =
6837 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6838 // We will not do the explicit null check in the thunk as some form of a null check
6839 // must've been done earlier.
6840 DCHECK(!needs_null_check);
6841 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6842 // Loading the entrypoint does not require a load acquire since it is only changed when
6843 // threads are suspended or running a checkpoint.
6844 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6845 Register ref_reg = ref.AsRegister<Register>();
6846 Register index_reg = index.IsRegisterPair()
6847 ? index.AsRegisterPairLow<Register>()
6848 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006849 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006850 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006851 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006852 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6853 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006854 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006855 } else {
6856 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006857 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006858 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6859 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006860 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006861 __ Addu(TMP, TMP, obj); // In delay slot.
6862 }
6863 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6864 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6865 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6866 __ MaybeUnpoisonHeapReference(ref_reg);
6867 __ SetReorder(reordering);
6868 return;
6869 }
6870
Alexey Frunze15958152017-02-09 19:08:30 -08006871 // /* HeapReference<Object> */ ref =
6872 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006873 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6874 ref,
6875 obj,
6876 data_offset,
6877 index,
6878 scale_factor,
6879 temp,
6880 needs_null_check);
6881}
6882
6883void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6884 Location ref,
6885 Register obj,
6886 uint32_t offset,
6887 Location index,
6888 ScaleFactor scale_factor,
6889 Location temp,
6890 bool needs_null_check,
6891 bool always_update_field) {
6892 DCHECK(kEmitCompilerReadBarrier);
6893 DCHECK(kUseBakerReadBarrier);
6894
6895 // In slow path based read barriers, the read barrier call is
6896 // inserted after the original load. However, in fast path based
6897 // Baker's read barriers, we need to perform the load of
6898 // mirror::Object::monitor_ *before* the original reference load.
6899 // This load-load ordering is required by the read barrier.
6900 // The fast path/slow path (for Baker's algorithm) should look like:
6901 //
6902 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6903 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6904 // HeapReference<Object> ref = *src; // Original reference load.
6905 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6906 // if (is_gray) {
6907 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6908 // }
6909 //
6910 // Note: the original implementation in ReadBarrier::Barrier is
6911 // slightly more complex as it performs additional checks that we do
6912 // not do here for performance reasons.
6913
6914 Register ref_reg = ref.AsRegister<Register>();
6915 Register temp_reg = temp.AsRegister<Register>();
6916 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6917
6918 // /* int32_t */ monitor = obj->monitor_
6919 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6920 if (needs_null_check) {
6921 MaybeRecordImplicitNullCheck(instruction);
6922 }
6923 // /* LockWord */ lock_word = LockWord(monitor)
6924 static_assert(sizeof(LockWord) == sizeof(int32_t),
6925 "art::LockWord and int32_t have different sizes.");
6926
6927 __ Sync(0); // Barrier to prevent load-load reordering.
6928
6929 // The actual reference load.
6930 if (index.IsValid()) {
6931 // Load types involving an "index": ArrayGet,
6932 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6933 // intrinsics.
6934 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6935 if (index.IsConstant()) {
6936 size_t computed_offset =
6937 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6938 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6939 } else {
6940 // Handle the special case of the
6941 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6942 // intrinsics, which use a register pair as index ("long
6943 // offset"), of which only the low part contains data.
6944 Register index_reg = index.IsRegisterPair()
6945 ? index.AsRegisterPairLow<Register>()
6946 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006947 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006948 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6949 }
6950 } else {
6951 // /* HeapReference<Object> */ ref = *(obj + offset)
6952 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6953 }
6954
6955 // Object* ref = ref_addr->AsMirrorPtr()
6956 __ MaybeUnpoisonHeapReference(ref_reg);
6957
6958 // Slow path marking the object `ref` when it is gray.
6959 SlowPathCodeMIPS* slow_path;
6960 if (always_update_field) {
6961 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6962 // of the form `obj + field_offset`, where `obj` is a register and
6963 // `field_offset` is a register pair (of which only the lower half
6964 // is used). Thus `offset` and `scale_factor` above are expected
6965 // to be null in this code path.
6966 DCHECK_EQ(offset, 0u);
6967 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006968 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08006969 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6970 ref,
6971 obj,
6972 /* field_offset */ index,
6973 temp_reg);
6974 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006975 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08006976 }
6977 AddSlowPath(slow_path);
6978
6979 // if (rb_state == ReadBarrier::GrayState())
6980 // ref = ReadBarrier::Mark(ref);
6981 // Given the numeric representation, it's enough to check the low bit of the
6982 // rb_state. We do that by shifting the bit into the sign bit (31) and
6983 // performing a branch on less than zero.
6984 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6985 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6986 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6987 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6988 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6989 __ Bind(slow_path->GetExitLabel());
6990}
6991
6992void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6993 Location out,
6994 Location ref,
6995 Location obj,
6996 uint32_t offset,
6997 Location index) {
6998 DCHECK(kEmitCompilerReadBarrier);
6999
7000 // Insert a slow path based read barrier *after* the reference load.
7001 //
7002 // If heap poisoning is enabled, the unpoisoning of the loaded
7003 // reference will be carried out by the runtime within the slow
7004 // path.
7005 //
7006 // Note that `ref` currently does not get unpoisoned (when heap
7007 // poisoning is enabled), which is alright as the `ref` argument is
7008 // not used by the artReadBarrierSlow entry point.
7009 //
7010 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007011 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007012 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7013 AddSlowPath(slow_path);
7014
7015 __ B(slow_path->GetEntryLabel());
7016 __ Bind(slow_path->GetExitLabel());
7017}
7018
7019void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7020 Location out,
7021 Location ref,
7022 Location obj,
7023 uint32_t offset,
7024 Location index) {
7025 if (kEmitCompilerReadBarrier) {
7026 // Baker's read barriers shall be handled by the fast path
7027 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7028 DCHECK(!kUseBakerReadBarrier);
7029 // If heap poisoning is enabled, unpoisoning will be taken care of
7030 // by the runtime within the slow path.
7031 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7032 } else if (kPoisonHeapReferences) {
7033 __ UnpoisonHeapReference(out.AsRegister<Register>());
7034 }
7035}
7036
7037void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7038 Location out,
7039 Location root) {
7040 DCHECK(kEmitCompilerReadBarrier);
7041
7042 // Insert a slow path based read barrier *after* the GC root load.
7043 //
7044 // Note that GC roots are not affected by heap poisoning, so we do
7045 // not need to do anything special for this here.
7046 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007047 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007048 AddSlowPath(slow_path);
7049
7050 __ B(slow_path->GetEntryLabel());
7051 __ Bind(slow_path->GetExitLabel());
7052}
7053
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007054void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007055 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7056 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007057 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007058 switch (type_check_kind) {
7059 case TypeCheckKind::kExactCheck:
7060 case TypeCheckKind::kAbstractClassCheck:
7061 case TypeCheckKind::kClassHierarchyCheck:
7062 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007063 call_kind =
7064 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007065 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007066 break;
7067 case TypeCheckKind::kArrayCheck:
7068 case TypeCheckKind::kUnresolvedCheck:
7069 case TypeCheckKind::kInterfaceCheck:
7070 call_kind = LocationSummary::kCallOnSlowPath;
7071 break;
7072 }
7073
Vladimir Markoca6fff82017-10-03 14:49:14 +01007074 LocationSummary* locations =
7075 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007076 if (baker_read_barrier_slow_path) {
7077 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7078 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007079 locations->SetInAt(0, Location::RequiresRegister());
7080 locations->SetInAt(1, Location::RequiresRegister());
7081 // The output does overlap inputs.
7082 // Note that TypeCheckSlowPathMIPS uses this register too.
7083 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007084 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007085}
7086
7087void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007088 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007089 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007090 Location obj_loc = locations->InAt(0);
7091 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007092 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007093 Location out_loc = locations->Out();
7094 Register out = out_loc.AsRegister<Register>();
7095 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7096 DCHECK_LE(num_temps, 1u);
7097 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007098 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7099 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7100 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7101 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007102 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007103 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007104
7105 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007106 // Avoid this check if we know `obj` is not null.
7107 if (instruction->MustDoNullCheck()) {
7108 __ Move(out, ZERO);
7109 __ Beqz(obj, &done);
7110 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007111
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007112 switch (type_check_kind) {
7113 case TypeCheckKind::kExactCheck: {
7114 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007115 GenerateReferenceLoadTwoRegisters(instruction,
7116 out_loc,
7117 obj_loc,
7118 class_offset,
7119 maybe_temp_loc,
7120 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007121 // Classes must be equal for the instanceof to succeed.
7122 __ Xor(out, out, cls);
7123 __ Sltiu(out, out, 1);
7124 break;
7125 }
7126
7127 case TypeCheckKind::kAbstractClassCheck: {
7128 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007129 GenerateReferenceLoadTwoRegisters(instruction,
7130 out_loc,
7131 obj_loc,
7132 class_offset,
7133 maybe_temp_loc,
7134 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007135 // If the class is abstract, we eagerly fetch the super class of the
7136 // object to avoid doing a comparison we know will fail.
7137 MipsLabel loop;
7138 __ Bind(&loop);
7139 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007140 GenerateReferenceLoadOneRegister(instruction,
7141 out_loc,
7142 super_offset,
7143 maybe_temp_loc,
7144 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007145 // If `out` is null, we use it for the result, and jump to `done`.
7146 __ Beqz(out, &done);
7147 __ Bne(out, cls, &loop);
7148 __ LoadConst32(out, 1);
7149 break;
7150 }
7151
7152 case TypeCheckKind::kClassHierarchyCheck: {
7153 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007154 GenerateReferenceLoadTwoRegisters(instruction,
7155 out_loc,
7156 obj_loc,
7157 class_offset,
7158 maybe_temp_loc,
7159 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007160 // Walk over the class hierarchy to find a match.
7161 MipsLabel loop, success;
7162 __ Bind(&loop);
7163 __ Beq(out, cls, &success);
7164 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007165 GenerateReferenceLoadOneRegister(instruction,
7166 out_loc,
7167 super_offset,
7168 maybe_temp_loc,
7169 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007170 __ Bnez(out, &loop);
7171 // If `out` is null, we use it for the result, and jump to `done`.
7172 __ B(&done);
7173 __ Bind(&success);
7174 __ LoadConst32(out, 1);
7175 break;
7176 }
7177
7178 case TypeCheckKind::kArrayObjectCheck: {
7179 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007180 GenerateReferenceLoadTwoRegisters(instruction,
7181 out_loc,
7182 obj_loc,
7183 class_offset,
7184 maybe_temp_loc,
7185 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007186 // Do an exact check.
7187 MipsLabel success;
7188 __ Beq(out, cls, &success);
7189 // Otherwise, we need to check that the object's class is a non-primitive array.
7190 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007191 GenerateReferenceLoadOneRegister(instruction,
7192 out_loc,
7193 component_offset,
7194 maybe_temp_loc,
7195 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007196 // If `out` is null, we use it for the result, and jump to `done`.
7197 __ Beqz(out, &done);
7198 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7199 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7200 __ Sltiu(out, out, 1);
7201 __ B(&done);
7202 __ Bind(&success);
7203 __ LoadConst32(out, 1);
7204 break;
7205 }
7206
7207 case TypeCheckKind::kArrayCheck: {
7208 // No read barrier since the slow path will retry upon failure.
7209 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007210 GenerateReferenceLoadTwoRegisters(instruction,
7211 out_loc,
7212 obj_loc,
7213 class_offset,
7214 maybe_temp_loc,
7215 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007216 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007217 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7218 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007219 codegen_->AddSlowPath(slow_path);
7220 __ Bne(out, cls, slow_path->GetEntryLabel());
7221 __ LoadConst32(out, 1);
7222 break;
7223 }
7224
7225 case TypeCheckKind::kUnresolvedCheck:
7226 case TypeCheckKind::kInterfaceCheck: {
7227 // Note that we indeed only call on slow path, but we always go
7228 // into the slow path for the unresolved and interface check
7229 // cases.
7230 //
7231 // We cannot directly call the InstanceofNonTrivial runtime
7232 // entry point without resorting to a type checking slow path
7233 // here (i.e. by calling InvokeRuntime directly), as it would
7234 // require to assign fixed registers for the inputs of this
7235 // HInstanceOf instruction (following the runtime calling
7236 // convention), which might be cluttered by the potential first
7237 // read barrier emission at the beginning of this method.
7238 //
7239 // TODO: Introduce a new runtime entry point taking the object
7240 // to test (instead of its class) as argument, and let it deal
7241 // with the read barrier issues. This will let us refactor this
7242 // case of the `switch` code as it was previously (with a direct
7243 // call to the runtime not using a type checking slow path).
7244 // This should also be beneficial for the other cases above.
7245 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007246 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7247 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007248 codegen_->AddSlowPath(slow_path);
7249 __ B(slow_path->GetEntryLabel());
7250 break;
7251 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007252 }
7253
7254 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007255
7256 if (slow_path != nullptr) {
7257 __ Bind(slow_path->GetExitLabel());
7258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007259}
7260
7261void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007262 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007263 locations->SetOut(Location::ConstantLocation(constant));
7264}
7265
7266void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7267 // Will be generated at use site.
7268}
7269
7270void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007271 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007272 locations->SetOut(Location::ConstantLocation(constant));
7273}
7274
7275void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7276 // Will be generated at use site.
7277}
7278
7279void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7280 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7281 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7282}
7283
7284void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7285 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007286 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007287 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007288 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007289}
7290
7291void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7292 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7293 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007294 Location receiver = invoke->GetLocations()->InAt(0);
7295 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007296 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007297
7298 // Set the hidden argument.
7299 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7300 invoke->GetDexMethodIndex());
7301
7302 // temp = object->GetClass();
7303 if (receiver.IsStackSlot()) {
7304 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7305 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7306 } else {
7307 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7308 }
7309 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007310 // Instead of simply (possibly) unpoisoning `temp` here, we should
7311 // emit a read barrier for the previous class reference load.
7312 // However this is not required in practice, as this is an
7313 // intermediate/temporary reference and because the current
7314 // concurrent copying collector keeps the from-space memory
7315 // intact/accessible until the end of the marking phase (the
7316 // concurrent copying collector may not in the future).
7317 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007318 __ LoadFromOffset(kLoadWord, temp, temp,
7319 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7320 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007321 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007322 // temp = temp->GetImtEntryAt(method_offset);
7323 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7324 // T9 = temp->GetEntryPoint();
7325 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7326 // T9();
7327 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007328 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007329 DCHECK(!codegen_->IsLeafMethod());
7330 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7331}
7332
7333void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007334 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7335 if (intrinsic.TryDispatch(invoke)) {
7336 return;
7337 }
7338
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007339 HandleInvoke(invoke);
7340}
7341
7342void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007343 // Explicit clinit checks triggered by static invokes must have been pruned by
7344 // art::PrepareForRegisterAllocation.
7345 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007346
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007347 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007348 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7349 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007350
Chris Larsen701566a2015-10-27 15:29:13 -07007351 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7352 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007353 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7354 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7355 }
Chris Larsen701566a2015-10-27 15:29:13 -07007356 return;
7357 }
7358
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007359 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007360
7361 // Add the extra input register if either the dex cache array base register
7362 // or the PC-relative base register for accessing literals is needed.
7363 if (has_extra_input) {
7364 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7365 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007366}
7367
Orion Hodsonac141392017-01-13 11:53:47 +00007368void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7369 HandleInvoke(invoke);
7370}
7371
7372void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7373 codegen_->GenerateInvokePolymorphicCall(invoke);
7374}
7375
Chris Larsen701566a2015-10-27 15:29:13 -07007376static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007377 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007378 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7379 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007380 return true;
7381 }
7382 return false;
7383}
7384
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007385HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007386 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007387 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007388 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007389 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007390 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007391 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007392 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007393 case HLoadString::LoadKind::kJitTableAddress:
7394 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007395 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007396 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007397 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007398 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007399 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007400 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007401}
7402
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007403HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7404 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007405 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007406 case HLoadClass::LoadKind::kInvalid:
7407 LOG(FATAL) << "UNREACHABLE";
7408 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007409 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007410 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007411 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007412 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007413 case HLoadClass::LoadKind::kBssEntry:
7414 DCHECK(!Runtime::Current()->UseJitCompilation());
7415 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007416 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007417 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007418 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007419 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007420 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007421 break;
7422 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007423 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007424}
7425
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007426Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7427 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007428 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007429 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007430 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7431 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7432 if (!invoke->GetLocations()->Intrinsified()) {
7433 return location.AsRegister<Register>();
7434 }
7435 // For intrinsics we allow any location, so it may be on the stack.
7436 if (!location.IsRegister()) {
7437 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7438 return temp;
7439 }
7440 // For register locations, check if the register was saved. If so, get it from the stack.
7441 // Note: There is a chance that the register was saved but not overwritten, so we could
7442 // save one load. However, since this is just an intrinsic slow path we prefer this
7443 // simple and more robust approach rather that trying to determine if that's the case.
7444 SlowPathCode* slow_path = GetCurrentSlowPath();
7445 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7446 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7447 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7448 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7449 return temp;
7450 }
7451 return location.AsRegister<Register>();
7452}
7453
Vladimir Markodc151b22015-10-15 18:02:30 +01007454HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7455 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007456 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007457 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007458}
7459
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007460void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7461 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007462 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007463 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007464 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7465 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007466 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007467 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7468 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007469 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7470 : ZERO;
7471
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007472 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007473 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007474 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007475 uint32_t offset =
7476 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007477 __ LoadFromOffset(kLoadWord,
7478 temp.AsRegister<Register>(),
7479 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007480 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007481 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007482 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007483 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007484 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007485 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007486 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7487 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007488 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7489 PcRelativePatchInfo* info_low =
7490 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007491 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007492 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7493 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007494 break;
7495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007496 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7497 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7498 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007499 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007500 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007501 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007502 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7503 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007504 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007505 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7506 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007507 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007508 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007509 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7510 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7511 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007512 }
7513 }
7514
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007515 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007516 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007517 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007518 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007519 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7520 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007521 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007522 T9,
7523 callee_method.AsRegister<Register>(),
7524 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007525 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007526 // T9()
7527 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007528 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007529 break;
7530 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007531 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7532
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533 DCHECK(!IsLeafMethod());
7534}
7535
7536void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007537 // Explicit clinit checks triggered by static invokes must have been pruned by
7538 // art::PrepareForRegisterAllocation.
7539 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007540
7541 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7542 return;
7543 }
7544
7545 LocationSummary* locations = invoke->GetLocations();
7546 codegen_->GenerateStaticOrDirectCall(invoke,
7547 locations->HasTemps()
7548 ? locations->GetTemp(0)
7549 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007550}
7551
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007552void CodeGeneratorMIPS::GenerateVirtualCall(
7553 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007554 // Use the calling convention instead of the location of the receiver, as
7555 // intrinsics may have put the receiver in a different register. In the intrinsics
7556 // slow path, the arguments have been moved to the right place, so here we are
7557 // guaranteed that the receiver is the first register of the calling convention.
7558 InvokeDexCallingConvention calling_convention;
7559 Register receiver = calling_convention.GetRegisterAt(0);
7560
Chris Larsen3acee732015-11-18 13:31:08 -08007561 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007562 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7563 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007565 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007566
7567 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007568 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007569 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007570 // Instead of simply (possibly) unpoisoning `temp` here, we should
7571 // emit a read barrier for the previous class reference load.
7572 // However this is not required in practice, as this is an
7573 // intermediate/temporary reference and because the current
7574 // concurrent copying collector keeps the from-space memory
7575 // intact/accessible until the end of the marking phase (the
7576 // concurrent copying collector may not in the future).
7577 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007578 // temp = temp->GetMethodAt(method_offset);
7579 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7580 // T9 = temp->GetEntryPoint();
7581 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7582 // T9();
7583 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007584 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007585 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007586}
7587
7588void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7589 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7590 return;
7591 }
7592
7593 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007594 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007595}
7596
7597void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007598 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007599 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007600 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007601 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7602 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007603 return;
7604 }
Vladimir Marko41559982017-01-06 14:04:23 +00007605 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007606 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007607 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007608 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7609 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007610 ? LocationSummary::kCallOnSlowPath
7611 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007612 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007613 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7614 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7615 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007616 switch (load_kind) {
7617 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007618 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007619 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007620 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007621 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007622 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007623 break;
7624 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007625 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007626 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7627 codegen_->ClobberRA();
7628 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007629 break;
7630 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007631 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007632 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007633 locations->SetInAt(0, Location::RequiresRegister());
7634 break;
7635 default:
7636 break;
7637 }
7638 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007639 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7640 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7641 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007642 RegisterSet caller_saves = RegisterSet::Empty();
7643 InvokeRuntimeCallingConvention calling_convention;
7644 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7645 locations->SetCustomSlowPathCallerSaves(caller_saves);
7646 } else {
7647 // For non-Baker read barriers we have a temp-clobbering call.
7648 }
7649 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007650}
7651
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007652// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7653// move.
7654void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007655 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007656 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007657 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007658 return;
7659 }
Vladimir Marko41559982017-01-06 14:04:23 +00007660 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007661
Vladimir Marko41559982017-01-06 14:04:23 +00007662 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007663 Location out_loc = locations->Out();
7664 Register out = out_loc.AsRegister<Register>();
7665 Register base_or_current_method_reg;
7666 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007667 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007668 switch (load_kind) {
7669 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007670 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007671 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007672 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007673 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007674 base_or_current_method_reg =
7675 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007676 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007677 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007678 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007679 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7680 break;
7681 default:
7682 base_or_current_method_reg = ZERO;
7683 break;
7684 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007685
Alexey Frunze15958152017-02-09 19:08:30 -08007686 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7687 ? kWithoutReadBarrier
7688 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007689 bool generate_null_check = false;
7690 switch (load_kind) {
7691 case HLoadClass::LoadKind::kReferrersClass: {
7692 DCHECK(!cls->CanCallRuntime());
7693 DCHECK(!cls->MustGenerateClinitCheck());
7694 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7695 GenerateGcRootFieldLoad(cls,
7696 out_loc,
7697 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007698 ArtMethod::DeclaringClassOffset().Int32Value(),
7699 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007700 break;
7701 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007702 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007703 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007704 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007705 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007706 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007707 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7708 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007709 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7710 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007711 base_or_current_method_reg);
7712 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007713 break;
7714 }
7715 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007716 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007717 uint32_t address = dchecked_integral_cast<uint32_t>(
7718 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7719 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007720 if (isR6 || !has_irreducible_loops) {
7721 __ LoadLiteral(out,
7722 base_or_current_method_reg,
7723 codegen_->DeduplicateBootImageAddressLiteral(address));
7724 } else {
7725 __ LoadConst32(out, address);
7726 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 break;
7728 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007729 case HLoadClass::LoadKind::kBootImageClassTable: {
7730 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7731 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7732 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7733 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7734 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7735 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7736 out,
7737 base_or_current_method_reg);
7738 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7739 // Extract the reference from the slot data, i.e. clear the hash bits.
7740 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7741 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7742 if (masked_hash != 0) {
7743 __ Addiu(out, out, -masked_hash);
7744 }
7745 break;
7746 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007747 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007748 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7749 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007750 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7751 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007752 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007753 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007754 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007755 GenerateGcRootFieldLoad(cls,
7756 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007757 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007758 /* placeholder */ 0x5678,
7759 read_barrier_option,
7760 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007761 generate_null_check = true;
7762 break;
7763 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007764 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007765 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7766 cls->GetTypeIndex(),
7767 cls->GetClass());
7768 bool reordering = __ SetReorder(false);
7769 __ Bind(&info->high_label);
7770 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007771 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007772 GenerateGcRootFieldLoad(cls,
7773 out_loc,
7774 out,
7775 /* placeholder */ 0x5678,
7776 read_barrier_option,
7777 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007778 break;
7779 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007780 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007781 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007782 LOG(FATAL) << "UNREACHABLE";
7783 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007784 }
7785
7786 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7787 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007788 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007789 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007790 codegen_->AddSlowPath(slow_path);
7791 if (generate_null_check) {
7792 __ Beqz(out, slow_path->GetEntryLabel());
7793 }
7794 if (cls->MustGenerateClinitCheck()) {
7795 GenerateClassInitializationCheck(slow_path, out);
7796 } else {
7797 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007798 }
7799 }
7800}
7801
7802static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007803 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007804}
7805
7806void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7807 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007808 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007809 locations->SetOut(Location::RequiresRegister());
7810}
7811
7812void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7813 Register out = load->GetLocations()->Out().AsRegister<Register>();
7814 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7815}
7816
7817void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007818 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007819}
7820
7821void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7822 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7823}
7824
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007825void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007826 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007827 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007828 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007829 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007830 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007831 switch (load_kind) {
7832 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007833 case HLoadString::LoadKind::kBootImageAddress:
7834 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007835 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007836 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007837 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007838 break;
7839 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007840 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007841 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7842 codegen_->ClobberRA();
7843 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007844 break;
7845 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007846 FALLTHROUGH_INTENDED;
7847 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007848 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007849 locations->SetInAt(0, Location::RequiresRegister());
7850 break;
7851 default:
7852 break;
7853 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007854 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007855 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007856 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007857 } else {
7858 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007859 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7860 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7861 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007862 RegisterSet caller_saves = RegisterSet::Empty();
7863 InvokeRuntimeCallingConvention calling_convention;
7864 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7865 locations->SetCustomSlowPathCallerSaves(caller_saves);
7866 } else {
7867 // For non-Baker read barriers we have a temp-clobbering call.
7868 }
7869 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007870 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007871}
7872
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007873// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7874// move.
7875void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007876 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007877 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007878 Location out_loc = locations->Out();
7879 Register out = out_loc.AsRegister<Register>();
7880 Register base_or_current_method_reg;
7881 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007882 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007883 switch (load_kind) {
7884 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 case HLoadString::LoadKind::kBootImageAddress:
7886 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007887 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007888 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007889 base_or_current_method_reg =
7890 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007891 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007892 default:
7893 base_or_current_method_reg = ZERO;
7894 break;
7895 }
7896
7897 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007898 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007899 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007900 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007901 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007902 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7903 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007904 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7905 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007906 base_or_current_method_reg);
7907 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007908 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007909 }
7910 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007911 uint32_t address = dchecked_integral_cast<uint32_t>(
7912 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7913 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007914 if (isR6 || !has_irreducible_loops) {
7915 __ LoadLiteral(out,
7916 base_or_current_method_reg,
7917 codegen_->DeduplicateBootImageAddressLiteral(address));
7918 } else {
7919 __ LoadConst32(out, address);
7920 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007921 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007922 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007923 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007924 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007925 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007926 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007927 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7928 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007929 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7930 out,
7931 base_or_current_method_reg);
7932 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7933 return;
7934 }
7935 case HLoadString::LoadKind::kBssEntry: {
7936 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7937 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7938 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7939 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7940 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007941 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007942 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007943 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007944 GenerateGcRootFieldLoad(load,
7945 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007946 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007947 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007948 kCompilerReadBarrierOption,
7949 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007950 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00007951 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007952 codegen_->AddSlowPath(slow_path);
7953 __ Beqz(out, slow_path->GetEntryLabel());
7954 __ Bind(slow_path->GetExitLabel());
7955 return;
7956 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007957 case HLoadString::LoadKind::kJitTableAddress: {
7958 CodeGeneratorMIPS::JitPatchInfo* info =
7959 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7960 load->GetStringIndex(),
7961 load->GetString());
7962 bool reordering = __ SetReorder(false);
7963 __ Bind(&info->high_label);
7964 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007965 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08007966 GenerateGcRootFieldLoad(load,
7967 out_loc,
7968 out,
7969 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007970 kCompilerReadBarrierOption,
7971 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007972 return;
7973 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007974 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007975 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007976 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007977
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007978 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007979 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007980 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007981 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007982 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007983 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7984 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007985}
7986
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007987void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007988 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007989 locations->SetOut(Location::ConstantLocation(constant));
7990}
7991
7992void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7993 // Will be generated at use site.
7994}
7995
7996void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007997 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7998 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007999 InvokeRuntimeCallingConvention calling_convention;
8000 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8001}
8002
8003void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8004 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008005 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008006 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8007 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008008 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008009 }
8010 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8011}
8012
8013void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8014 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008015 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008016 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008017 case DataType::Type::kInt32:
8018 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008019 locations->SetInAt(0, Location::RequiresRegister());
8020 locations->SetInAt(1, Location::RequiresRegister());
8021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8022 break;
8023
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008024 case DataType::Type::kFloat32:
8025 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008026 locations->SetInAt(0, Location::RequiresFpuRegister());
8027 locations->SetInAt(1, Location::RequiresFpuRegister());
8028 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8029 break;
8030
8031 default:
8032 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8033 }
8034}
8035
8036void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008037 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008038 LocationSummary* locations = instruction->GetLocations();
8039 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8040
8041 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008042 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008043 Register dst = locations->Out().AsRegister<Register>();
8044 Register lhs = locations->InAt(0).AsRegister<Register>();
8045 Register rhs = locations->InAt(1).AsRegister<Register>();
8046
8047 if (isR6) {
8048 __ MulR6(dst, lhs, rhs);
8049 } else {
8050 __ MulR2(dst, lhs, rhs);
8051 }
8052 break;
8053 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008054 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008055 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8056 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8057 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8058 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8059 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8060 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8061
8062 // Extra checks to protect caused by the existance of A1_A2.
8063 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8064 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8065 DCHECK_NE(dst_high, lhs_low);
8066 DCHECK_NE(dst_high, rhs_low);
8067
8068 // A_B * C_D
8069 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8070 // dst_lo: [ low(B*D) ]
8071 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8072
8073 if (isR6) {
8074 __ MulR6(TMP, lhs_high, rhs_low);
8075 __ MulR6(dst_high, lhs_low, rhs_high);
8076 __ Addu(dst_high, dst_high, TMP);
8077 __ MuhuR6(TMP, lhs_low, rhs_low);
8078 __ Addu(dst_high, dst_high, TMP);
8079 __ MulR6(dst_low, lhs_low, rhs_low);
8080 } else {
8081 __ MulR2(TMP, lhs_high, rhs_low);
8082 __ MulR2(dst_high, lhs_low, rhs_high);
8083 __ Addu(dst_high, dst_high, TMP);
8084 __ MultuR2(lhs_low, rhs_low);
8085 __ Mfhi(TMP);
8086 __ Addu(dst_high, dst_high, TMP);
8087 __ Mflo(dst_low);
8088 }
8089 break;
8090 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008091 case DataType::Type::kFloat32:
8092 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008093 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8094 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8095 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008096 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008097 __ MulS(dst, lhs, rhs);
8098 } else {
8099 __ MulD(dst, lhs, rhs);
8100 }
8101 break;
8102 }
8103 default:
8104 LOG(FATAL) << "Unexpected mul type " << type;
8105 }
8106}
8107
8108void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8109 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008110 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008111 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008112 case DataType::Type::kInt32:
8113 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008114 locations->SetInAt(0, Location::RequiresRegister());
8115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8116 break;
8117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008118 case DataType::Type::kFloat32:
8119 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008120 locations->SetInAt(0, Location::RequiresFpuRegister());
8121 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8122 break;
8123
8124 default:
8125 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8126 }
8127}
8128
8129void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008130 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008131 LocationSummary* locations = instruction->GetLocations();
8132
8133 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008134 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008135 Register dst = locations->Out().AsRegister<Register>();
8136 Register src = locations->InAt(0).AsRegister<Register>();
8137 __ Subu(dst, ZERO, src);
8138 break;
8139 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008140 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008141 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8142 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8143 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8144 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8145 __ Subu(dst_low, ZERO, src_low);
8146 __ Sltu(TMP, ZERO, dst_low);
8147 __ Subu(dst_high, ZERO, src_high);
8148 __ Subu(dst_high, dst_high, TMP);
8149 break;
8150 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008151 case DataType::Type::kFloat32:
8152 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008153 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8154 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008155 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008156 __ NegS(dst, src);
8157 } else {
8158 __ NegD(dst, src);
8159 }
8160 break;
8161 }
8162 default:
8163 LOG(FATAL) << "Unexpected neg type " << type;
8164 }
8165}
8166
8167void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008168 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8169 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008170 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008171 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008172 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8173 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008174}
8175
8176void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008177 // Note: if heap poisoning is enabled, the entry point takes care
8178 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008179 QuickEntrypointEnum entrypoint =
8180 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8181 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008182 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008183 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008184}
8185
8186void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008187 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8188 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008189 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008190 if (instruction->IsStringAlloc()) {
8191 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8192 } else {
8193 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008194 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008195 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008196}
8197
8198void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008199 // Note: if heap poisoning is enabled, the entry point takes care
8200 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008201 if (instruction->IsStringAlloc()) {
8202 // String is allocated through StringFactory. Call NewEmptyString entry point.
8203 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008204 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008205 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8206 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8207 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008208 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008209 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8210 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008211 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008212 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008213 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008214}
8215
8216void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008217 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008218 locations->SetInAt(0, Location::RequiresRegister());
8219 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8220}
8221
8222void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008223 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008224 LocationSummary* locations = instruction->GetLocations();
8225
8226 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008227 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008228 Register dst = locations->Out().AsRegister<Register>();
8229 Register src = locations->InAt(0).AsRegister<Register>();
8230 __ Nor(dst, src, ZERO);
8231 break;
8232 }
8233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008234 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8236 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8237 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8238 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8239 __ Nor(dst_high, src_high, ZERO);
8240 __ Nor(dst_low, src_low, ZERO);
8241 break;
8242 }
8243
8244 default:
8245 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8246 }
8247}
8248
8249void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008250 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008251 locations->SetInAt(0, Location::RequiresRegister());
8252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8253}
8254
8255void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8256 LocationSummary* locations = instruction->GetLocations();
8257 __ Xori(locations->Out().AsRegister<Register>(),
8258 locations->InAt(0).AsRegister<Register>(),
8259 1);
8260}
8261
8262void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008263 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8264 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008265}
8266
Calin Juravle2ae48182016-03-16 14:05:09 +00008267void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8268 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008269 return;
8270 }
8271 Location obj = instruction->GetLocations()->InAt(0);
8272
8273 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008274 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008275}
8276
Calin Juravle2ae48182016-03-16 14:05:09 +00008277void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008278 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008279 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008280
8281 Location obj = instruction->GetLocations()->InAt(0);
8282
8283 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8284}
8285
8286void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008287 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008288}
8289
8290void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8291 HandleBinaryOp(instruction);
8292}
8293
8294void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8295 HandleBinaryOp(instruction);
8296}
8297
8298void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8299 LOG(FATAL) << "Unreachable";
8300}
8301
8302void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008303 if (instruction->GetNext()->IsSuspendCheck() &&
8304 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8305 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8306 // The back edge will generate the suspend check.
8307 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8308 }
8309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008310 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8311}
8312
8313void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008315 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8316 if (location.IsStackSlot()) {
8317 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8318 } else if (location.IsDoubleStackSlot()) {
8319 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8320 }
8321 locations->SetOut(location);
8322}
8323
8324void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8325 ATTRIBUTE_UNUSED) {
8326 // Nothing to do, the parameter is already at its location.
8327}
8328
8329void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8330 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008331 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008332 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8333}
8334
8335void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8336 ATTRIBUTE_UNUSED) {
8337 // Nothing to do, the method is already at its location.
8338}
8339
8340void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008341 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008342 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008343 locations->SetInAt(i, Location::Any());
8344 }
8345 locations->SetOut(Location::Any());
8346}
8347
8348void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8349 LOG(FATAL) << "Unreachable";
8350}
8351
8352void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008353 DataType::Type type = rem->GetResultType();
8354 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8355 ? LocationSummary::kNoCall
8356 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008357 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008358
8359 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008360 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008361 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008362 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8364 break;
8365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008366 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008367 InvokeRuntimeCallingConvention calling_convention;
8368 locations->SetInAt(0, Location::RegisterPairLocation(
8369 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8370 locations->SetInAt(1, Location::RegisterPairLocation(
8371 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8372 locations->SetOut(calling_convention.GetReturnLocation(type));
8373 break;
8374 }
8375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008376 case DataType::Type::kFloat32:
8377 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008378 InvokeRuntimeCallingConvention calling_convention;
8379 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8380 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8381 locations->SetOut(calling_convention.GetReturnLocation(type));
8382 break;
8383 }
8384
8385 default:
8386 LOG(FATAL) << "Unexpected rem type " << type;
8387 }
8388}
8389
8390void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008391 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008392
8393 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008394 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008395 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008396 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008397 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008398 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008399 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8400 break;
8401 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008402 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008403 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008404 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008405 break;
8406 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008407 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008408 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008409 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008410 break;
8411 }
8412 default:
8413 LOG(FATAL) << "Unexpected rem type " << type;
8414 }
8415}
8416
Igor Murashkind01745e2017-04-05 16:40:31 -07008417void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8418 constructor_fence->SetLocations(nullptr);
8419}
8420
8421void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8422 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8423 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8424}
8425
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008426void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8427 memory_barrier->SetLocations(nullptr);
8428}
8429
8430void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8431 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8432}
8433
8434void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008435 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008436 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008437 locations->SetInAt(0, MipsReturnLocation(return_type));
8438}
8439
8440void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8441 codegen_->GenerateFrameExit();
8442}
8443
8444void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8445 ret->SetLocations(nullptr);
8446}
8447
8448void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8449 codegen_->GenerateFrameExit();
8450}
8451
Alexey Frunze92d90602015-12-18 18:16:36 -08008452void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8453 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008454}
8455
Alexey Frunze92d90602015-12-18 18:16:36 -08008456void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8457 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008458}
8459
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8461 HandleShift(shl);
8462}
8463
8464void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8465 HandleShift(shl);
8466}
8467
8468void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8469 HandleShift(shr);
8470}
8471
8472void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8473 HandleShift(shr);
8474}
8475
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008476void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8477 HandleBinaryOp(instruction);
8478}
8479
8480void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8481 HandleBinaryOp(instruction);
8482}
8483
8484void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8485 HandleFieldGet(instruction, instruction->GetFieldInfo());
8486}
8487
8488void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8489 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8490}
8491
8492void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8493 HandleFieldSet(instruction, instruction->GetFieldInfo());
8494}
8495
8496void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008497 HandleFieldSet(instruction,
8498 instruction->GetFieldInfo(),
8499 instruction->GetDexPc(),
8500 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008501}
8502
8503void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8504 HUnresolvedInstanceFieldGet* instruction) {
8505 FieldAccessCallingConventionMIPS calling_convention;
8506 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8507 instruction->GetFieldType(),
8508 calling_convention);
8509}
8510
8511void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8512 HUnresolvedInstanceFieldGet* instruction) {
8513 FieldAccessCallingConventionMIPS calling_convention;
8514 codegen_->GenerateUnresolvedFieldAccess(instruction,
8515 instruction->GetFieldType(),
8516 instruction->GetFieldIndex(),
8517 instruction->GetDexPc(),
8518 calling_convention);
8519}
8520
8521void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8522 HUnresolvedInstanceFieldSet* instruction) {
8523 FieldAccessCallingConventionMIPS calling_convention;
8524 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8525 instruction->GetFieldType(),
8526 calling_convention);
8527}
8528
8529void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8530 HUnresolvedInstanceFieldSet* instruction) {
8531 FieldAccessCallingConventionMIPS calling_convention;
8532 codegen_->GenerateUnresolvedFieldAccess(instruction,
8533 instruction->GetFieldType(),
8534 instruction->GetFieldIndex(),
8535 instruction->GetDexPc(),
8536 calling_convention);
8537}
8538
8539void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8540 HUnresolvedStaticFieldGet* instruction) {
8541 FieldAccessCallingConventionMIPS calling_convention;
8542 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8543 instruction->GetFieldType(),
8544 calling_convention);
8545}
8546
8547void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8548 HUnresolvedStaticFieldGet* instruction) {
8549 FieldAccessCallingConventionMIPS calling_convention;
8550 codegen_->GenerateUnresolvedFieldAccess(instruction,
8551 instruction->GetFieldType(),
8552 instruction->GetFieldIndex(),
8553 instruction->GetDexPc(),
8554 calling_convention);
8555}
8556
8557void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8558 HUnresolvedStaticFieldSet* instruction) {
8559 FieldAccessCallingConventionMIPS calling_convention;
8560 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8561 instruction->GetFieldType(),
8562 calling_convention);
8563}
8564
8565void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8566 HUnresolvedStaticFieldSet* instruction) {
8567 FieldAccessCallingConventionMIPS calling_convention;
8568 codegen_->GenerateUnresolvedFieldAccess(instruction,
8569 instruction->GetFieldType(),
8570 instruction->GetFieldIndex(),
8571 instruction->GetDexPc(),
8572 calling_convention);
8573}
8574
8575void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008576 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8577 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008578 // In suspend check slow path, usually there are no caller-save registers at all.
8579 // If SIMD instructions are present, however, we force spilling all live SIMD
8580 // registers in full width (since the runtime only saves/restores lower part).
8581 locations->SetCustomSlowPathCallerSaves(
8582 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008583}
8584
8585void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8586 HBasicBlock* block = instruction->GetBlock();
8587 if (block->GetLoopInformation() != nullptr) {
8588 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8589 // The back edge will generate the suspend check.
8590 return;
8591 }
8592 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8593 // The goto will generate the suspend check.
8594 return;
8595 }
8596 GenerateSuspendCheck(instruction, nullptr);
8597}
8598
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008599void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008600 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8601 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008602 InvokeRuntimeCallingConvention calling_convention;
8603 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8604}
8605
8606void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008607 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008608 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8609}
8610
8611void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008612 DataType::Type input_type = conversion->GetInputType();
8613 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008614 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8615 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008616 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008618 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8619 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008620 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8621 }
8622
8623 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008624 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008625 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8626 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008627 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008628 }
8629
Vladimir Markoca6fff82017-10-03 14:49:14 +01008630 LocationSummary* locations =
8631 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008632
8633 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008634 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008635 locations->SetInAt(0, Location::RequiresFpuRegister());
8636 } else {
8637 locations->SetInAt(0, Location::RequiresRegister());
8638 }
8639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008640 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8642 } else {
8643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8644 }
8645 } else {
8646 InvokeRuntimeCallingConvention calling_convention;
8647
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008648 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008649 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8650 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008651 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008652 locations->SetInAt(0, Location::RegisterPairLocation(
8653 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8654 }
8655
8656 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8657 }
8658}
8659
8660void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8661 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008662 DataType::Type result_type = conversion->GetResultType();
8663 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008664 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008665 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008666
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008667 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8668 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008670 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008671 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8672 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8673 Register src = locations->InAt(0).AsRegister<Register>();
8674
Alexey Frunzea871ef12016-06-27 15:20:11 -07008675 if (dst_low != src) {
8676 __ Move(dst_low, src);
8677 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008678 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008679 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008680 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008681 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682 ? locations->InAt(0).AsRegisterPairLow<Register>()
8683 : locations->InAt(0).AsRegister<Register>();
8684
8685 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008686 case DataType::Type::kUint8:
8687 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008688 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008689 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008690 if (has_sign_extension) {
8691 __ Seb(dst, src);
8692 } else {
8693 __ Sll(dst, src, 24);
8694 __ Sra(dst, dst, 24);
8695 }
8696 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008697 case DataType::Type::kUint16:
8698 __ Andi(dst, src, 0xFFFF);
8699 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008700 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008701 if (has_sign_extension) {
8702 __ Seh(dst, src);
8703 } else {
8704 __ Sll(dst, src, 16);
8705 __ Sra(dst, dst, 16);
8706 }
8707 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008708 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008709 if (dst != src) {
8710 __ Move(dst, src);
8711 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008712 break;
8713
8714 default:
8715 LOG(FATAL) << "Unexpected type conversion from " << input_type
8716 << " to " << result_type;
8717 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008718 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8719 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008720 if (isR6) {
8721 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8722 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8723 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8724 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8725 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8726 __ Mtc1(src_low, FTMP);
8727 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008728 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008729 __ Cvtsl(dst, FTMP);
8730 } else {
8731 __ Cvtdl(dst, FTMP);
8732 }
8733 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008734 QuickEntrypointEnum entrypoint =
8735 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008736 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008737 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008738 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8739 } else {
8740 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8741 }
8742 }
8743 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 Register src = locations->InAt(0).AsRegister<Register>();
8745 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8746 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008747 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008748 __ Cvtsw(dst, FTMP);
8749 } else {
8750 __ Cvtdw(dst, FTMP);
8751 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008752 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008753 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8754 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008755
8756 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8757 // value of the output type if the input is outside of the range after the truncation or
8758 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8759 // results. This matches the desired float/double-to-int/long conversion exactly.
8760 //
8761 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8762 // value when the input is either a NaN or is outside of the range of the output type
8763 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8764 // the same result.
8765 //
8766 // The code takes care of the different behaviors by first comparing the input to the
8767 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8768 // If the input is greater than or equal to the minimum, it procedes to the truncate
8769 // instruction, which will handle such an input the same way irrespective of NAN2008.
8770 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8771 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008772 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008773 if (isR6) {
8774 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8775 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8776 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8777 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8778 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008780 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008781 __ TruncLS(FTMP, src);
8782 } else {
8783 __ TruncLD(FTMP, src);
8784 }
8785 __ Mfc1(dst_low, FTMP);
8786 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008787 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008788 QuickEntrypointEnum entrypoint =
8789 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008790 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008791 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008792 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8793 } else {
8794 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8795 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008796 }
8797 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008798 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8799 Register dst = locations->Out().AsRegister<Register>();
8800 MipsLabel truncate;
8801 MipsLabel done;
8802
Lena Djokicf4e23a82017-05-09 15:43:45 +02008803 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008804 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008805 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8806 __ LoadConst32(TMP, min_val);
8807 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008808 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008809 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8810 __ LoadConst32(TMP, High32Bits(min_val));
8811 __ Mtc1(ZERO, FTMP);
8812 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008813 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008815 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008816 __ ColeS(0, FTMP, src);
8817 } else {
8818 __ ColeD(0, FTMP, src);
8819 }
8820 __ Bc1t(0, &truncate);
8821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008822 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008823 __ CeqS(0, src, src);
8824 } else {
8825 __ CeqD(0, src, src);
8826 }
8827 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8828 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008829
8830 __ B(&done);
8831
8832 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008833 }
8834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008835 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008836 __ TruncWS(FTMP, src);
8837 } else {
8838 __ TruncWD(FTMP, src);
8839 }
8840 __ Mfc1(dst, FTMP);
8841
Lena Djokicf4e23a82017-05-09 15:43:45 +02008842 if (!isR6) {
8843 __ Bind(&done);
8844 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008845 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008846 } else if (DataType::IsFloatingPointType(result_type) &&
8847 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008848 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8849 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008850 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008851 __ Cvtsd(dst, src);
8852 } else {
8853 __ Cvtds(dst, src);
8854 }
8855 } else {
8856 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8857 << " to " << result_type;
8858 }
8859}
8860
8861void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8862 HandleShift(ushr);
8863}
8864
8865void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8866 HandleShift(ushr);
8867}
8868
8869void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8870 HandleBinaryOp(instruction);
8871}
8872
8873void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8874 HandleBinaryOp(instruction);
8875}
8876
8877void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8878 // Nothing to do, this should be removed during prepare for register allocator.
8879 LOG(FATAL) << "Unreachable";
8880}
8881
8882void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8883 // Nothing to do, this should be removed during prepare for register allocator.
8884 LOG(FATAL) << "Unreachable";
8885}
8886
8887void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008888 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008889}
8890
8891void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008892 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008893}
8894
8895void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008896 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008897}
8898
8899void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008900 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008901}
8902
8903void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008904 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008905}
8906
8907void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008908 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008909}
8910
8911void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008912 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008913}
8914
8915void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008916 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008917}
8918
8919void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008920 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008921}
8922
8923void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008924 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008925}
8926
8927void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008928 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008929}
8930
8931void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008932 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008933}
8934
8935void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008937}
8938
8939void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008941}
8942
8943void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008945}
8946
8947void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008948 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008949}
8950
8951void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008952 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008953}
8954
8955void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008956 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008957}
8958
8959void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008960 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008961}
8962
8963void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008964 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008965}
8966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008967void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8968 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008969 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008970 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008971 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
8972 uint32_t num_entries = switch_instr->GetNumEntries();
8973 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
8974 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
8975 // instruction to simulate PC-relative addressing when accessing the jump table.
8976 // NAL clobbers RA. Make sure RA is preserved.
8977 codegen_->ClobberRA();
8978 }
8979 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008980}
8981
Alexey Frunze96b66822016-09-10 02:32:44 -07008982void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8983 int32_t lower_bound,
8984 uint32_t num_entries,
8985 HBasicBlock* switch_block,
8986 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008987 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008988 Register temp_reg = TMP;
8989 __ Addiu32(temp_reg, value_reg, -lower_bound);
8990 // Jump to default if index is negative
8991 // Note: We don't check the case that index is positive while value < lower_bound, because in
8992 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8993 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8994
Alexey Frunze96b66822016-09-10 02:32:44 -07008995 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008996 // Jump to successors[0] if value == lower_bound.
8997 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8998 int32_t last_index = 0;
8999 for (; num_entries - last_index > 2; last_index += 2) {
9000 __ Addiu(temp_reg, temp_reg, -2);
9001 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9002 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9003 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9004 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9005 }
9006 if (num_entries - last_index == 2) {
9007 // The last missing case_value.
9008 __ Addiu(temp_reg, temp_reg, -1);
9009 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009010 }
9011
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009012 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009013 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009014 __ B(codegen_->GetLabelOf(default_block));
9015 }
9016}
9017
Alexey Frunze96b66822016-09-10 02:32:44 -07009018void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9019 Register constant_area,
9020 int32_t lower_bound,
9021 uint32_t num_entries,
9022 HBasicBlock* switch_block,
9023 HBasicBlock* default_block) {
9024 // Create a jump table.
9025 std::vector<MipsLabel*> labels(num_entries);
9026 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9027 for (uint32_t i = 0; i < num_entries; i++) {
9028 labels[i] = codegen_->GetLabelOf(successors[i]);
9029 }
9030 JumpTable* table = __ CreateJumpTable(std::move(labels));
9031
9032 // Is the value in range?
9033 __ Addiu32(TMP, value_reg, -lower_bound);
9034 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9035 __ Sltiu(AT, TMP, num_entries);
9036 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9037 } else {
9038 __ LoadConst32(AT, num_entries);
9039 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9040 }
9041
9042 // We are in the range of the table.
9043 // Load the target address from the jump table, indexing by the value.
9044 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009045 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009046 __ Lw(TMP, TMP, 0);
9047 // Compute the absolute target address by adding the table start address
9048 // (the table contains offsets to targets relative to its start).
9049 __ Addu(TMP, TMP, AT);
9050 // And jump.
9051 __ Jr(TMP);
9052 __ NopIfNoReordering();
9053}
9054
9055void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9056 int32_t lower_bound = switch_instr->GetStartValue();
9057 uint32_t num_entries = switch_instr->GetNumEntries();
9058 LocationSummary* locations = switch_instr->GetLocations();
9059 Register value_reg = locations->InAt(0).AsRegister<Register>();
9060 HBasicBlock* switch_block = switch_instr->GetBlock();
9061 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9062
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009063 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009064 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009065 //
9066 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9067 // to access the jump table and it is implemented by changing HPackedSwitch to
9068 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9069 // VisitMipsPackedSwitch()).
9070 //
9071 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9072 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9073 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009074 GenTableBasedPackedSwitch(value_reg,
9075 ZERO,
9076 lower_bound,
9077 num_entries,
9078 switch_block,
9079 default_block);
9080 } else {
9081 GenPackedSwitchWithCompares(value_reg,
9082 lower_bound,
9083 num_entries,
9084 switch_block,
9085 default_block);
9086 }
9087}
9088
9089void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9090 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009091 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009092 locations->SetInAt(0, Location::RequiresRegister());
9093 // Constant area pointer (HMipsComputeBaseMethodAddress).
9094 locations->SetInAt(1, Location::RequiresRegister());
9095}
9096
9097void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9098 int32_t lower_bound = switch_instr->GetStartValue();
9099 uint32_t num_entries = switch_instr->GetNumEntries();
9100 LocationSummary* locations = switch_instr->GetLocations();
9101 Register value_reg = locations->InAt(0).AsRegister<Register>();
9102 Register constant_area = locations->InAt(1).AsRegister<Register>();
9103 HBasicBlock* switch_block = switch_instr->GetBlock();
9104 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9105
9106 // This is an R2-only path. HPackedSwitch has been changed to
9107 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9108 // required to address the jump table relative to PC.
9109 GenTableBasedPackedSwitch(value_reg,
9110 constant_area,
9111 lower_bound,
9112 num_entries,
9113 switch_block,
9114 default_block);
9115}
9116
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009117void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9118 HMipsComputeBaseMethodAddress* insn) {
9119 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009120 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009121 locations->SetOut(Location::RequiresRegister());
9122}
9123
9124void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9125 HMipsComputeBaseMethodAddress* insn) {
9126 LocationSummary* locations = insn->GetLocations();
9127 Register reg = locations->Out().AsRegister<Register>();
9128
9129 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9130
9131 // Generate a dummy PC-relative call to obtain PC.
9132 __ Nal();
9133 // Grab the return address off RA.
9134 __ Move(reg, RA);
9135
9136 // Remember this offset (the obtained PC value) for later use with constant area.
9137 __ BindPcRelBaseLabel();
9138}
9139
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009140void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9141 // The trampoline uses the same calling convention as dex calling conventions,
9142 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9143 // the method_idx.
9144 HandleInvoke(invoke);
9145}
9146
9147void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9148 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9149}
9150
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009151void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009153 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009154 locations->SetInAt(0, Location::RequiresRegister());
9155 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009156}
9157
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009158void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9159 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009160 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009161 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009162 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009163 __ LoadFromOffset(kLoadWord,
9164 locations->Out().AsRegister<Register>(),
9165 locations->InAt(0).AsRegister<Register>(),
9166 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009167 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009168 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009169 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009170 __ LoadFromOffset(kLoadWord,
9171 locations->Out().AsRegister<Register>(),
9172 locations->InAt(0).AsRegister<Register>(),
9173 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009174 __ LoadFromOffset(kLoadWord,
9175 locations->Out().AsRegister<Register>(),
9176 locations->Out().AsRegister<Register>(),
9177 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009178 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009179}
9180
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009181void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9182 ATTRIBUTE_UNUSED) {
9183 LOG(FATAL) << "Unreachable";
9184}
9185
9186void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9187 ATTRIBUTE_UNUSED) {
9188 LOG(FATAL) << "Unreachable";
9189}
9190
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009191#undef __
9192#undef QUICK_ENTRY_POINT
9193
9194} // namespace mips
9195} // namespace art