blob: 1f6b214f110e0d56ca364490b89bae60cf0f51c9 [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();
Lena Djokic38530172017-11-16 11:11:50 +01001944 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001946 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 locations->SetInAt(0, Location::RequiresRegister());
1948 HInstruction* right = instruction->InputAt(1);
1949 bool can_use_imm = false;
1950 if (right->IsConstant()) {
1951 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1952 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1953 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001954 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001955 DCHECK(instruction->IsSub() || instruction->IsAdd());
1956 if (instruction->IsSub()) {
1957 imm = -imm;
1958 }
1959 if (isR6) {
1960 bool single_use = right->GetUses().HasExactlyOneElement();
1961 int16_t imm_high = High16Bits(imm);
1962 int16_t imm_low = Low16Bits(imm);
1963 if (imm_low < 0) {
1964 imm_high += 1;
1965 }
1966 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
1967 } else {
1968 can_use_imm = IsInt<16>(imm);
1969 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001970 }
1971 }
1972 if (can_use_imm)
1973 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1974 else
1975 locations->SetInAt(1, Location::RequiresRegister());
1976 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1977 break;
1978 }
1979
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001981 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001982 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 break;
1985 }
1986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001987 case DataType::Type::kFloat32:
1988 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001989 DCHECK(instruction->IsAdd() || instruction->IsSub());
1990 locations->SetInAt(0, Location::RequiresFpuRegister());
1991 locations->SetInAt(1, Location::RequiresFpuRegister());
1992 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1993 break;
1994
1995 default:
1996 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1997 }
1998}
1999
2000void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002002 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002003 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002004
2005 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002006 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002007 Register dst = locations->Out().AsRegister<Register>();
2008 Register lhs = locations->InAt(0).AsRegister<Register>();
2009 Location rhs_location = locations->InAt(1);
2010
2011 Register rhs_reg = ZERO;
2012 int32_t rhs_imm = 0;
2013 bool use_imm = rhs_location.IsConstant();
2014 if (use_imm) {
2015 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2016 } else {
2017 rhs_reg = rhs_location.AsRegister<Register>();
2018 }
2019
2020 if (instruction->IsAnd()) {
2021 if (use_imm)
2022 __ Andi(dst, lhs, rhs_imm);
2023 else
2024 __ And(dst, lhs, rhs_reg);
2025 } else if (instruction->IsOr()) {
2026 if (use_imm)
2027 __ Ori(dst, lhs, rhs_imm);
2028 else
2029 __ Or(dst, lhs, rhs_reg);
2030 } else if (instruction->IsXor()) {
2031 if (use_imm)
2032 __ Xori(dst, lhs, rhs_imm);
2033 else
2034 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002036 DCHECK(instruction->IsAdd() || instruction->IsSub());
2037 if (use_imm) {
2038 if (instruction->IsSub()) {
2039 rhs_imm = -rhs_imm;
2040 }
2041 if (IsInt<16>(rhs_imm)) {
2042 __ Addiu(dst, lhs, rhs_imm);
2043 } else {
2044 DCHECK(isR6);
2045 int16_t rhs_imm_high = High16Bits(rhs_imm);
2046 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2047 if (rhs_imm_low < 0) {
2048 rhs_imm_high += 1;
2049 }
2050 __ Aui(dst, lhs, rhs_imm_high);
2051 if (rhs_imm_low != 0) {
2052 __ Addiu(dst, dst, rhs_imm_low);
2053 }
2054 }
2055 } else if (instruction->IsAdd()) {
2056 __ Addu(dst, lhs, rhs_reg);
2057 } else {
2058 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002060 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 }
2062 break;
2063 }
2064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2067 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2068 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2069 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002070 Location rhs_location = locations->InAt(1);
2071 bool use_imm = rhs_location.IsConstant();
2072 if (!use_imm) {
2073 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2074 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2075 if (instruction->IsAnd()) {
2076 __ And(dst_low, lhs_low, rhs_low);
2077 __ And(dst_high, lhs_high, rhs_high);
2078 } else if (instruction->IsOr()) {
2079 __ Or(dst_low, lhs_low, rhs_low);
2080 __ Or(dst_high, lhs_high, rhs_high);
2081 } else if (instruction->IsXor()) {
2082 __ Xor(dst_low, lhs_low, rhs_low);
2083 __ Xor(dst_high, lhs_high, rhs_high);
2084 } else if (instruction->IsAdd()) {
2085 if (lhs_low == rhs_low) {
2086 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2087 __ Slt(TMP, lhs_low, ZERO);
2088 __ Addu(dst_low, lhs_low, rhs_low);
2089 } else {
2090 __ Addu(dst_low, lhs_low, rhs_low);
2091 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2092 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2093 }
2094 __ Addu(dst_high, lhs_high, rhs_high);
2095 __ Addu(dst_high, dst_high, TMP);
2096 } else {
2097 DCHECK(instruction->IsSub());
2098 __ Sltu(TMP, lhs_low, rhs_low);
2099 __ Subu(dst_low, lhs_low, rhs_low);
2100 __ Subu(dst_high, lhs_high, rhs_high);
2101 __ Subu(dst_high, dst_high, TMP);
2102 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002104 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2105 if (instruction->IsOr()) {
2106 uint32_t low = Low32Bits(value);
2107 uint32_t high = High32Bits(value);
2108 if (IsUint<16>(low)) {
2109 if (dst_low != lhs_low || low != 0) {
2110 __ Ori(dst_low, lhs_low, low);
2111 }
2112 } else {
2113 __ LoadConst32(TMP, low);
2114 __ Or(dst_low, lhs_low, TMP);
2115 }
2116 if (IsUint<16>(high)) {
2117 if (dst_high != lhs_high || high != 0) {
2118 __ Ori(dst_high, lhs_high, high);
2119 }
2120 } else {
2121 if (high != low) {
2122 __ LoadConst32(TMP, high);
2123 }
2124 __ Or(dst_high, lhs_high, TMP);
2125 }
2126 } else if (instruction->IsXor()) {
2127 uint32_t low = Low32Bits(value);
2128 uint32_t high = High32Bits(value);
2129 if (IsUint<16>(low)) {
2130 if (dst_low != lhs_low || low != 0) {
2131 __ Xori(dst_low, lhs_low, low);
2132 }
2133 } else {
2134 __ LoadConst32(TMP, low);
2135 __ Xor(dst_low, lhs_low, TMP);
2136 }
2137 if (IsUint<16>(high)) {
2138 if (dst_high != lhs_high || high != 0) {
2139 __ Xori(dst_high, lhs_high, high);
2140 }
2141 } else {
2142 if (high != low) {
2143 __ LoadConst32(TMP, high);
2144 }
2145 __ Xor(dst_high, lhs_high, TMP);
2146 }
2147 } else if (instruction->IsAnd()) {
2148 uint32_t low = Low32Bits(value);
2149 uint32_t high = High32Bits(value);
2150 if (IsUint<16>(low)) {
2151 __ Andi(dst_low, lhs_low, low);
2152 } else if (low != 0xFFFFFFFF) {
2153 __ LoadConst32(TMP, low);
2154 __ And(dst_low, lhs_low, TMP);
2155 } else if (dst_low != lhs_low) {
2156 __ Move(dst_low, lhs_low);
2157 }
2158 if (IsUint<16>(high)) {
2159 __ Andi(dst_high, lhs_high, high);
2160 } else if (high != 0xFFFFFFFF) {
2161 if (high != low) {
2162 __ LoadConst32(TMP, high);
2163 }
2164 __ And(dst_high, lhs_high, TMP);
2165 } else if (dst_high != lhs_high) {
2166 __ Move(dst_high, lhs_high);
2167 }
2168 } else {
2169 if (instruction->IsSub()) {
2170 value = -value;
2171 } else {
2172 DCHECK(instruction->IsAdd());
2173 }
2174 int32_t low = Low32Bits(value);
2175 int32_t high = High32Bits(value);
2176 if (IsInt<16>(low)) {
2177 if (dst_low != lhs_low || low != 0) {
2178 __ Addiu(dst_low, lhs_low, low);
2179 }
2180 if (low != 0) {
2181 __ Sltiu(AT, dst_low, low);
2182 }
2183 } else {
2184 __ LoadConst32(TMP, low);
2185 __ Addu(dst_low, lhs_low, TMP);
2186 __ Sltu(AT, dst_low, TMP);
2187 }
2188 if (IsInt<16>(high)) {
2189 if (dst_high != lhs_high || high != 0) {
2190 __ Addiu(dst_high, lhs_high, high);
2191 }
2192 } else {
2193 if (high != low) {
2194 __ LoadConst32(TMP, high);
2195 }
2196 __ Addu(dst_high, lhs_high, TMP);
2197 }
2198 if (low != 0) {
2199 __ Addu(dst_high, dst_high, AT);
2200 }
2201 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002202 }
2203 break;
2204 }
2205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002206 case DataType::Type::kFloat32:
2207 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002208 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2209 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2210 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2211 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002212 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002213 __ AddS(dst, lhs, rhs);
2214 } else {
2215 __ AddD(dst, lhs, rhs);
2216 }
2217 } else {
2218 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002220 __ SubS(dst, lhs, rhs);
2221 } else {
2222 __ SubD(dst, lhs, rhs);
2223 }
2224 }
2225 break;
2226 }
2227
2228 default:
2229 LOG(FATAL) << "Unexpected binary operation type " << type;
2230 }
2231}
2232
2233void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002234 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002235
Vladimir Markoca6fff82017-10-03 14:49:14 +01002236 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002238 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002240 locations->SetInAt(0, Location::RequiresRegister());
2241 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2242 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2243 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2247 locations->SetOut(Location::RequiresRegister());
2248 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002249 default:
2250 LOG(FATAL) << "Unexpected shift type " << type;
2251 }
2252}
2253
2254static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2255
2256void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002257 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260
2261 Location rhs_location = locations->InAt(1);
2262 bool use_imm = rhs_location.IsConstant();
2263 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2264 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002265 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002267 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002268 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2269 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002270
2271 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273 Register dst = locations->Out().AsRegister<Register>();
2274 Register lhs = locations->InAt(0).AsRegister<Register>();
2275 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002276 if (shift_value == 0) {
2277 if (dst != lhs) {
2278 __ Move(dst, lhs);
2279 }
2280 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 __ Sll(dst, lhs, shift_value);
2282 } else if (instr->IsShr()) {
2283 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002284 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002286 } else {
2287 if (has_ins_rotr) {
2288 __ Rotr(dst, lhs, shift_value);
2289 } else {
2290 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2291 __ Srl(dst, lhs, shift_value);
2292 __ Or(dst, dst, TMP);
2293 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002294 }
2295 } else {
2296 if (instr->IsShl()) {
2297 __ Sllv(dst, lhs, rhs_reg);
2298 } else if (instr->IsShr()) {
2299 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002300 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002302 } else {
2303 if (has_ins_rotr) {
2304 __ Rotrv(dst, lhs, rhs_reg);
2305 } else {
2306 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002307 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2308 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2309 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2310 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2311 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002312 __ Sllv(TMP, lhs, TMP);
2313 __ Srlv(dst, lhs, rhs_reg);
2314 __ Or(dst, dst, TMP);
2315 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316 }
2317 }
2318 break;
2319 }
2320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002322 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2323 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2324 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2325 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2326 if (use_imm) {
2327 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002328 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002330 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002331 if (instr->IsShl()) {
2332 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2333 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2334 __ Sll(dst_low, lhs_low, shift_value);
2335 } else if (instr->IsShr()) {
2336 __ Srl(dst_low, lhs_low, shift_value);
2337 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2338 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 } else if (instr->IsUShr()) {
2340 __ Srl(dst_low, lhs_low, shift_value);
2341 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2342 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002343 } else {
2344 __ Srl(dst_low, lhs_low, shift_value);
2345 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2346 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002347 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002349 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002350 if (instr->IsShl()) {
2351 __ Sll(dst_low, lhs_low, shift_value);
2352 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2353 __ Sll(dst_high, lhs_high, shift_value);
2354 __ Or(dst_high, dst_high, TMP);
2355 } else if (instr->IsShr()) {
2356 __ Sra(dst_high, lhs_high, shift_value);
2357 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2358 __ Srl(dst_low, lhs_low, shift_value);
2359 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002360 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002361 __ Srl(dst_high, lhs_high, shift_value);
2362 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2363 __ Srl(dst_low, lhs_low, shift_value);
2364 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002365 } else {
2366 __ Srl(TMP, lhs_low, shift_value);
2367 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2368 __ Or(dst_low, dst_low, TMP);
2369 __ Srl(TMP, lhs_high, shift_value);
2370 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2371 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002372 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 }
2374 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002375 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002377 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 __ Move(dst_low, ZERO);
2379 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002380 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002383 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002384 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002385 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002386 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002387 // 64-bit rotation by 32 is just a swap.
2388 __ Move(dst_low, lhs_high);
2389 __ Move(dst_high, lhs_low);
2390 } else {
2391 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002392 __ Srl(dst_low, lhs_high, shift_value_high);
2393 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2394 __ Srl(dst_high, lhs_low, shift_value_high);
2395 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002396 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002397 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2398 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002399 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002400 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2401 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002402 __ Or(dst_high, dst_high, TMP);
2403 }
2404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 }
2406 }
2407 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002408 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002409 MipsLabel done;
2410 if (instr->IsShl()) {
2411 __ Sllv(dst_low, lhs_low, rhs_reg);
2412 __ Nor(AT, ZERO, rhs_reg);
2413 __ Srl(TMP, lhs_low, 1);
2414 __ Srlv(TMP, TMP, AT);
2415 __ Sllv(dst_high, lhs_high, rhs_reg);
2416 __ Or(dst_high, dst_high, TMP);
2417 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002418 if (isR6) {
2419 __ Beqzc(TMP, &done, /* is_bare */ true);
2420 __ Move(dst_high, dst_low);
2421 __ Move(dst_low, ZERO);
2422 } else {
2423 __ Movn(dst_high, dst_low, TMP);
2424 __ Movn(dst_low, ZERO, TMP);
2425 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426 } else if (instr->IsShr()) {
2427 __ Srav(dst_high, lhs_high, rhs_reg);
2428 __ Nor(AT, ZERO, rhs_reg);
2429 __ Sll(TMP, lhs_high, 1);
2430 __ Sllv(TMP, TMP, AT);
2431 __ Srlv(dst_low, lhs_low, rhs_reg);
2432 __ Or(dst_low, dst_low, TMP);
2433 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002434 if (isR6) {
2435 __ Beqzc(TMP, &done, /* is_bare */ true);
2436 __ Move(dst_low, dst_high);
2437 __ Sra(dst_high, dst_high, 31);
2438 } else {
2439 __ Sra(AT, dst_high, 31);
2440 __ Movn(dst_low, dst_high, TMP);
2441 __ Movn(dst_high, AT, TMP);
2442 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002443 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002444 __ Srlv(dst_high, lhs_high, rhs_reg);
2445 __ Nor(AT, ZERO, rhs_reg);
2446 __ Sll(TMP, lhs_high, 1);
2447 __ Sllv(TMP, TMP, AT);
2448 __ Srlv(dst_low, lhs_low, rhs_reg);
2449 __ Or(dst_low, dst_low, TMP);
2450 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002451 if (isR6) {
2452 __ Beqzc(TMP, &done, /* is_bare */ true);
2453 __ Move(dst_low, dst_high);
2454 __ Move(dst_high, ZERO);
2455 } else {
2456 __ Movn(dst_low, dst_high, TMP);
2457 __ Movn(dst_high, ZERO, TMP);
2458 }
2459 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002460 __ Nor(AT, ZERO, rhs_reg);
2461 __ Srlv(TMP, lhs_low, rhs_reg);
2462 __ Sll(dst_low, lhs_high, 1);
2463 __ Sllv(dst_low, dst_low, AT);
2464 __ Or(dst_low, dst_low, TMP);
2465 __ Srlv(TMP, lhs_high, rhs_reg);
2466 __ Sll(dst_high, lhs_low, 1);
2467 __ Sllv(dst_high, dst_high, AT);
2468 __ Or(dst_high, dst_high, TMP);
2469 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002470 if (isR6) {
2471 __ Beqzc(TMP, &done, /* is_bare */ true);
2472 __ Move(TMP, dst_high);
2473 __ Move(dst_high, dst_low);
2474 __ Move(dst_low, TMP);
2475 } else {
2476 __ Movn(AT, dst_high, TMP);
2477 __ Movn(dst_high, dst_low, TMP);
2478 __ Movn(dst_low, AT, TMP);
2479 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002480 }
2481 __ Bind(&done);
2482 }
2483 break;
2484 }
2485
2486 default:
2487 LOG(FATAL) << "Unexpected shift operation type " << type;
2488 }
2489}
2490
2491void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2492 HandleBinaryOp(instruction);
2493}
2494
2495void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2496 HandleBinaryOp(instruction);
2497}
2498
2499void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2500 HandleBinaryOp(instruction);
2501}
2502
2503void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2504 HandleBinaryOp(instruction);
2505}
2506
2507void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002509 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002511 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002512 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2513 object_array_get_with_read_barrier
2514 ? LocationSummary::kCallOnSlowPath
2515 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002516 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2517 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2518 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002519 locations->SetInAt(0, Location::RequiresRegister());
2520 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002522 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2523 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002524 // The output overlaps in the case of an object array get with
2525 // read barriers enabled: we do not want the move to overwrite the
2526 // array's location, as we need it to emit the read barrier.
2527 locations->SetOut(Location::RequiresRegister(),
2528 object_array_get_with_read_barrier
2529 ? Location::kOutputOverlap
2530 : Location::kNoOutputOverlap);
2531 }
2532 // We need a temporary register for the read barrier marking slow
2533 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2534 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002535 bool temp_needed = instruction->GetIndex()->IsConstant()
2536 ? !kBakerReadBarrierThunksEnableForFields
2537 : !kBakerReadBarrierThunksEnableForArrays;
2538 if (temp_needed) {
2539 locations->AddTemp(Location::RequiresRegister());
2540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002541 }
2542}
2543
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002544static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2545 auto null_checker = [codegen, instruction]() {
2546 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002547 };
2548 return null_checker;
2549}
2550
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2552 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002553 Location obj_loc = locations->InAt(0);
2554 Register obj = obj_loc.AsRegister<Register>();
2555 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002557 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002558 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002561 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2562 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002563 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002564 case DataType::Type::kBool:
2565 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002566 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002567 if (index.IsConstant()) {
2568 size_t offset =
2569 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002570 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571 } else {
2572 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002573 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 }
2575 break;
2576 }
2577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002579 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580 if (index.IsConstant()) {
2581 size_t offset =
2582 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002583 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584 } else {
2585 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002586 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002587 }
2588 break;
2589 }
2590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002592 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002593 if (maybe_compressed_char_at) {
2594 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2595 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2596 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2597 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2598 "Expecting 0=compressed, 1=uncompressed");
2599 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002601 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2602 if (maybe_compressed_char_at) {
2603 MipsLabel uncompressed_load, done;
2604 __ Bnez(TMP, &uncompressed_load);
2605 __ LoadFromOffset(kLoadUnsignedByte,
2606 out,
2607 obj,
2608 data_offset + (const_index << TIMES_1));
2609 __ B(&done);
2610 __ Bind(&uncompressed_load);
2611 __ LoadFromOffset(kLoadUnsignedHalfword,
2612 out,
2613 obj,
2614 data_offset + (const_index << TIMES_2));
2615 __ Bind(&done);
2616 } else {
2617 __ LoadFromOffset(kLoadUnsignedHalfword,
2618 out,
2619 obj,
2620 data_offset + (const_index << TIMES_2),
2621 null_checker);
2622 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002623 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002624 Register index_reg = index.AsRegister<Register>();
2625 if (maybe_compressed_char_at) {
2626 MipsLabel uncompressed_load, done;
2627 __ Bnez(TMP, &uncompressed_load);
2628 __ Addu(TMP, obj, index_reg);
2629 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2630 __ B(&done);
2631 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002632 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002633 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2634 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002635 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2636 __ Addu(TMP, index_reg, obj);
2637 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002638 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002639 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002640 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2641 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002642 }
2643 break;
2644 }
2645
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002646 case DataType::Type::kInt16: {
2647 Register out = out_loc.AsRegister<Register>();
2648 if (index.IsConstant()) {
2649 size_t offset =
2650 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2651 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002652 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2653 __ Addu(TMP, index.AsRegister<Register>(), obj);
2654 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002655 } else {
2656 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2657 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2658 }
2659 break;
2660 }
2661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002664 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002665 if (index.IsConstant()) {
2666 size_t offset =
2667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002668 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002669 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2670 __ Addu(TMP, index.AsRegister<Register>(), obj);
2671 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002673 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002674 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 }
2676 break;
2677 }
2678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002680 static_assert(
2681 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2682 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2683 // /* HeapReference<Object> */ out =
2684 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2685 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002686 bool temp_needed = index.IsConstant()
2687 ? !kBakerReadBarrierThunksEnableForFields
2688 : !kBakerReadBarrierThunksEnableForArrays;
2689 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002690 // Note that a potential implicit null check is handled in this
2691 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002692 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2693 if (index.IsConstant()) {
2694 // Array load with a constant index can be treated as a field load.
2695 size_t offset =
2696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2697 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2698 out_loc,
2699 obj,
2700 offset,
2701 temp,
2702 /* needs_null_check */ false);
2703 } else {
2704 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2705 out_loc,
2706 obj,
2707 data_offset,
2708 index,
2709 temp,
2710 /* needs_null_check */ false);
2711 }
Alexey Frunze15958152017-02-09 19:08:30 -08002712 } else {
2713 Register out = out_loc.AsRegister<Register>();
2714 if (index.IsConstant()) {
2715 size_t offset =
2716 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2717 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2718 // If read barriers are enabled, emit read barriers other than
2719 // Baker's using a slow path (and also unpoison the loaded
2720 // reference, if heap poisoning is enabled).
2721 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2722 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002723 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002724 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2725 // If read barriers are enabled, emit read barriers other than
2726 // Baker's using a slow path (and also unpoison the loaded
2727 // reference, if heap poisoning is enabled).
2728 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2729 out_loc,
2730 out_loc,
2731 obj_loc,
2732 data_offset,
2733 index);
2734 }
2735 }
2736 break;
2737 }
2738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002740 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002741 if (index.IsConstant()) {
2742 size_t offset =
2743 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002744 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002745 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2746 __ Addu(TMP, index.AsRegister<Register>(), obj);
2747 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002748 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002749 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002750 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002751 }
2752 break;
2753 }
2754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002756 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002757 if (index.IsConstant()) {
2758 size_t offset =
2759 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002760 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002761 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2762 __ Addu(TMP, index.AsRegister<Register>(), obj);
2763 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002764 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002765 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002766 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002767 }
2768 break;
2769 }
2770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002772 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002773 if (index.IsConstant()) {
2774 size_t offset =
2775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002776 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002777 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2778 __ Addu(TMP, index.AsRegister<Register>(), obj);
2779 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002780 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002781 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002782 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002783 }
2784 break;
2785 }
2786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2789 UNREACHABLE();
2790 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791}
2792
2793void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002794 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 locations->SetInAt(0, Location::RequiresRegister());
2796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2797}
2798
2799void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2800 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002801 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802 Register obj = locations->InAt(0).AsRegister<Register>();
2803 Register out = locations->Out().AsRegister<Register>();
2804 __ LoadFromOffset(kLoadWord, out, obj, offset);
2805 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002806 // Mask out compression flag from String's array length.
2807 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2808 __ Srl(out, out, 1u);
2809 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810}
2811
Alexey Frunzef58b2482016-09-02 22:14:06 -07002812Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2813 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2814 ? Location::ConstantLocation(instruction->AsConstant())
2815 : Location::RequiresRegister();
2816}
2817
2818Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2819 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2820 // We can store a non-zero float or double constant without first loading it into the FPU,
2821 // but we should only prefer this if the constant has a single use.
2822 if (instruction->IsConstant() &&
2823 (instruction->AsConstant()->IsZeroBitPattern() ||
2824 instruction->GetUses().HasExactlyOneElement())) {
2825 return Location::ConstantLocation(instruction->AsConstant());
2826 // Otherwise fall through and require an FPU register for the constant.
2827 }
2828 return Location::RequiresFpuRegister();
2829}
2830
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002831void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002833
2834 bool needs_write_barrier =
2835 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2836 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2837
Vladimir Markoca6fff82017-10-03 14:49:14 +01002838 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002840 may_need_runtime_call_for_type_check ?
2841 LocationSummary::kCallOnSlowPath :
2842 LocationSummary::kNoCall);
2843
2844 locations->SetInAt(0, Location::RequiresRegister());
2845 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002847 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002848 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002849 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2850 }
2851 if (needs_write_barrier) {
2852 // Temporary register for the write barrier.
2853 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002854 }
2855}
2856
2857void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2858 LocationSummary* locations = instruction->GetLocations();
2859 Register obj = locations->InAt(0).AsRegister<Register>();
2860 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002861 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002863 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864 bool needs_write_barrier =
2865 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002866 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002867 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868
2869 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002871 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002873 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002874 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002875 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002876 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002877 __ Addu(base_reg, obj, index.AsRegister<Register>());
2878 }
2879 if (value_location.IsConstant()) {
2880 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2881 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2882 } else {
2883 Register value = value_location.AsRegister<Register>();
2884 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002885 }
2886 break;
2887 }
2888
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002889 case DataType::Type::kUint16:
2890 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002892 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002893 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002894 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2895 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002896 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002897 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002898 }
2899 if (value_location.IsConstant()) {
2900 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2901 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2902 } else {
2903 Register value = value_location.AsRegister<Register>();
2904 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905 }
2906 break;
2907 }
2908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002909 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002910 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2911 if (index.IsConstant()) {
2912 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002913 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2914 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002915 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002916 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002917 }
2918 if (value_location.IsConstant()) {
2919 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2920 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2921 } else {
2922 Register value = value_location.AsRegister<Register>();
2923 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2924 }
2925 break;
2926 }
2927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002929 if (value_location.IsConstant()) {
2930 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002932 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002933 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002935 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002936 }
Alexey Frunze15958152017-02-09 19:08:30 -08002937 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2938 DCHECK_EQ(value, 0);
2939 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2940 DCHECK(!needs_write_barrier);
2941 DCHECK(!may_need_runtime_call_for_type_check);
2942 break;
2943 }
2944
2945 DCHECK(needs_write_barrier);
2946 Register value = value_location.AsRegister<Register>();
2947 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2948 Register temp2 = TMP; // Doesn't need to survive slow path.
2949 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2950 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2951 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2952 MipsLabel done;
2953 SlowPathCodeMIPS* slow_path = nullptr;
2954
2955 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002956 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002957 codegen_->AddSlowPath(slow_path);
2958 if (instruction->GetValueCanBeNull()) {
2959 MipsLabel non_zero;
2960 __ Bnez(value, &non_zero);
2961 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2962 if (index.IsConstant()) {
2963 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002964 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2965 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002966 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002967 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002968 }
Alexey Frunze15958152017-02-09 19:08:30 -08002969 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2970 __ B(&done);
2971 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002972 }
Alexey Frunze15958152017-02-09 19:08:30 -08002973
2974 // Note that when read barriers are enabled, the type checks
2975 // are performed without read barriers. This is fine, even in
2976 // the case where a class object is in the from-space after
2977 // the flip, as a comparison involving such a type would not
2978 // produce a false positive; it may of course produce a false
2979 // negative, in which case we would take the ArraySet slow
2980 // path.
2981
2982 // /* HeapReference<Class> */ temp1 = obj->klass_
2983 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2984 __ MaybeUnpoisonHeapReference(temp1);
2985
2986 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2987 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2988 // /* HeapReference<Class> */ temp2 = value->klass_
2989 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2990 // If heap poisoning is enabled, no need to unpoison `temp1`
2991 // nor `temp2`, as we are comparing two poisoned references.
2992
2993 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2994 MipsLabel do_put;
2995 __ Beq(temp1, temp2, &do_put);
2996 // If heap poisoning is enabled, the `temp1` reference has
2997 // not been unpoisoned yet; unpoison it now.
2998 __ MaybeUnpoisonHeapReference(temp1);
2999
3000 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3001 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3002 // If heap poisoning is enabled, no need to unpoison
3003 // `temp1`, as we are comparing against null below.
3004 __ Bnez(temp1, slow_path->GetEntryLabel());
3005 __ Bind(&do_put);
3006 } else {
3007 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3008 }
3009 }
3010
3011 Register source = value;
3012 if (kPoisonHeapReferences) {
3013 // Note that in the case where `value` is a null reference,
3014 // we do not enter this block, as a null reference does not
3015 // need poisoning.
3016 __ Move(temp1, value);
3017 __ PoisonHeapReference(temp1);
3018 source = temp1;
3019 }
3020
3021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3022 if (index.IsConstant()) {
3023 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003024 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003025 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003026 }
3027 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3028
3029 if (!may_need_runtime_call_for_type_check) {
3030 codegen_->MaybeRecordImplicitNullCheck(instruction);
3031 }
3032
3033 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3034
3035 if (done.IsLinked()) {
3036 __ Bind(&done);
3037 }
3038
3039 if (slow_path != nullptr) {
3040 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003041 }
3042 break;
3043 }
3044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003047 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003048 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003049 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3050 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003051 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003052 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003053 }
3054 if (value_location.IsConstant()) {
3055 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3056 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3057 } else {
3058 Register value = value_location.AsRegisterPairLow<Register>();
3059 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003060 }
3061 break;
3062 }
3063
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003065 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003066 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003067 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003068 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3069 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003070 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003071 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003072 }
3073 if (value_location.IsConstant()) {
3074 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3075 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3076 } else {
3077 FRegister value = value_location.AsFpuRegister<FRegister>();
3078 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003079 }
3080 break;
3081 }
3082
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003083 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003085 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003086 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003087 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3088 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003090 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003091 }
3092 if (value_location.IsConstant()) {
3093 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3094 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3095 } else {
3096 FRegister value = value_location.AsFpuRegister<FRegister>();
3097 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003098 }
3099 break;
3100 }
3101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003102 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003103 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3104 UNREACHABLE();
3105 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003106}
3107
Lena Djokica2901602017-09-21 13:50:52 +02003108void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3109 HIntermediateArrayAddressIndex* instruction) {
3110 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003111 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003112
3113 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3114
3115 locations->SetInAt(0, Location::RequiresRegister());
3116 locations->SetInAt(1, Location::ConstantLocation(shift));
3117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3118}
3119
3120void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3121 HIntermediateArrayAddressIndex* instruction) {
3122 LocationSummary* locations = instruction->GetLocations();
3123 Register index_reg = locations->InAt(0).AsRegister<Register>();
3124 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3125 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3126}
3127
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003128void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003129 RegisterSet caller_saves = RegisterSet::Empty();
3130 InvokeRuntimeCallingConvention calling_convention;
3131 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3132 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3133 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003134
3135 HInstruction* index = instruction->InputAt(0);
3136 HInstruction* length = instruction->InputAt(1);
3137
3138 bool const_index = false;
3139 bool const_length = false;
3140
3141 if (index->IsConstant()) {
3142 if (length->IsConstant()) {
3143 const_index = true;
3144 const_length = true;
3145 } else {
3146 int32_t index_value = index->AsIntConstant()->GetValue();
3147 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3148 const_index = true;
3149 }
3150 }
3151 } else if (length->IsConstant()) {
3152 int32_t length_value = length->AsIntConstant()->GetValue();
3153 if (IsUint<15>(length_value)) {
3154 const_length = true;
3155 }
3156 }
3157
3158 locations->SetInAt(0, const_index
3159 ? Location::ConstantLocation(index->AsConstant())
3160 : Location::RequiresRegister());
3161 locations->SetInAt(1, const_length
3162 ? Location::ConstantLocation(length->AsConstant())
3163 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003164}
3165
3166void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3167 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003168 Location index_loc = locations->InAt(0);
3169 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003170
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003171 if (length_loc.IsConstant()) {
3172 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3173 if (index_loc.IsConstant()) {
3174 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3175 if (index < 0 || index >= length) {
3176 BoundsCheckSlowPathMIPS* slow_path =
3177 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3178 codegen_->AddSlowPath(slow_path);
3179 __ B(slow_path->GetEntryLabel());
3180 } else {
3181 // Nothing to be done.
3182 }
3183 return;
3184 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003185
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003186 BoundsCheckSlowPathMIPS* slow_path =
3187 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3188 codegen_->AddSlowPath(slow_path);
3189 Register index = index_loc.AsRegister<Register>();
3190 if (length == 0) {
3191 __ B(slow_path->GetEntryLabel());
3192 } else if (length == 1) {
3193 __ Bnez(index, slow_path->GetEntryLabel());
3194 } else {
3195 DCHECK(IsUint<15>(length)) << length;
3196 __ Sltiu(TMP, index, length);
3197 __ Beqz(TMP, slow_path->GetEntryLabel());
3198 }
3199 } else {
3200 Register length = length_loc.AsRegister<Register>();
3201 BoundsCheckSlowPathMIPS* slow_path =
3202 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3203 codegen_->AddSlowPath(slow_path);
3204 if (index_loc.IsConstant()) {
3205 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3206 if (index < 0) {
3207 __ B(slow_path->GetEntryLabel());
3208 } else if (index == 0) {
3209 __ Blez(length, slow_path->GetEntryLabel());
3210 } else {
3211 DCHECK(IsInt<16>(index + 1)) << index;
3212 __ Sltiu(TMP, length, index + 1);
3213 __ Bnez(TMP, slow_path->GetEntryLabel());
3214 }
3215 } else {
3216 Register index = index_loc.AsRegister<Register>();
3217 __ Bgeu(index, length, slow_path->GetEntryLabel());
3218 }
3219 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003220}
3221
Alexey Frunze15958152017-02-09 19:08:30 -08003222// Temp is used for read barrier.
3223static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3224 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003225 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003226 (kUseBakerReadBarrier ||
3227 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3228 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3229 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3230 return 1;
3231 }
3232 return 0;
3233}
3234
3235// Extra temp is used for read barrier.
3236static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3237 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3238}
3239
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003241 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3242 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3243
3244 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3245 switch (type_check_kind) {
3246 case TypeCheckKind::kExactCheck:
3247 case TypeCheckKind::kAbstractClassCheck:
3248 case TypeCheckKind::kClassHierarchyCheck:
3249 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003250 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003251 ? LocationSummary::kCallOnSlowPath
3252 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3253 break;
3254 case TypeCheckKind::kArrayCheck:
3255 case TypeCheckKind::kUnresolvedCheck:
3256 case TypeCheckKind::kInterfaceCheck:
3257 call_kind = LocationSummary::kCallOnSlowPath;
3258 break;
3259 }
3260
Vladimir Markoca6fff82017-10-03 14:49:14 +01003261 LocationSummary* locations =
3262 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003263 locations->SetInAt(0, Location::RequiresRegister());
3264 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003265 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003266}
3267
3268void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003269 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003270 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003271 Location obj_loc = locations->InAt(0);
3272 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003273 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003274 Location temp_loc = locations->GetTemp(0);
3275 Register temp = temp_loc.AsRegister<Register>();
3276 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3277 DCHECK_LE(num_temps, 2u);
3278 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003279 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3280 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3281 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3282 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3283 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3284 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3285 const uint32_t object_array_data_offset =
3286 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3287 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003288
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003289 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3290 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3291 // read barriers is done for performance and code size reasons.
3292 bool is_type_check_slow_path_fatal = false;
3293 if (!kEmitCompilerReadBarrier) {
3294 is_type_check_slow_path_fatal =
3295 (type_check_kind == TypeCheckKind::kExactCheck ||
3296 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3297 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3298 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3299 !instruction->CanThrowIntoCatchBlock();
3300 }
3301 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003302 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3303 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003304 codegen_->AddSlowPath(slow_path);
3305
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003306 // Avoid this check if we know `obj` is not null.
3307 if (instruction->MustDoNullCheck()) {
3308 __ Beqz(obj, &done);
3309 }
3310
3311 switch (type_check_kind) {
3312 case TypeCheckKind::kExactCheck:
3313 case TypeCheckKind::kArrayCheck: {
3314 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003315 GenerateReferenceLoadTwoRegisters(instruction,
3316 temp_loc,
3317 obj_loc,
3318 class_offset,
3319 maybe_temp2_loc,
3320 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003321 // Jump to slow path for throwing the exception or doing a
3322 // more involved array check.
3323 __ Bne(temp, cls, slow_path->GetEntryLabel());
3324 break;
3325 }
3326
3327 case TypeCheckKind::kAbstractClassCheck: {
3328 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003329 GenerateReferenceLoadTwoRegisters(instruction,
3330 temp_loc,
3331 obj_loc,
3332 class_offset,
3333 maybe_temp2_loc,
3334 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003335 // If the class is abstract, we eagerly fetch the super class of the
3336 // object to avoid doing a comparison we know will fail.
3337 MipsLabel loop;
3338 __ Bind(&loop);
3339 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003340 GenerateReferenceLoadOneRegister(instruction,
3341 temp_loc,
3342 super_offset,
3343 maybe_temp2_loc,
3344 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003345 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3346 // exception.
3347 __ Beqz(temp, slow_path->GetEntryLabel());
3348 // Otherwise, compare the classes.
3349 __ Bne(temp, cls, &loop);
3350 break;
3351 }
3352
3353 case TypeCheckKind::kClassHierarchyCheck: {
3354 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003355 GenerateReferenceLoadTwoRegisters(instruction,
3356 temp_loc,
3357 obj_loc,
3358 class_offset,
3359 maybe_temp2_loc,
3360 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003361 // Walk over the class hierarchy to find a match.
3362 MipsLabel loop;
3363 __ Bind(&loop);
3364 __ Beq(temp, cls, &done);
3365 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003366 GenerateReferenceLoadOneRegister(instruction,
3367 temp_loc,
3368 super_offset,
3369 maybe_temp2_loc,
3370 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003371 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3372 // exception. Otherwise, jump to the beginning of the loop.
3373 __ Bnez(temp, &loop);
3374 __ B(slow_path->GetEntryLabel());
3375 break;
3376 }
3377
3378 case TypeCheckKind::kArrayObjectCheck: {
3379 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003380 GenerateReferenceLoadTwoRegisters(instruction,
3381 temp_loc,
3382 obj_loc,
3383 class_offset,
3384 maybe_temp2_loc,
3385 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003386 // Do an exact check.
3387 __ Beq(temp, cls, &done);
3388 // Otherwise, we need to check that the object's class is a non-primitive array.
3389 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003390 GenerateReferenceLoadOneRegister(instruction,
3391 temp_loc,
3392 component_offset,
3393 maybe_temp2_loc,
3394 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003395 // If the component type is null, jump to the slow path to throw the exception.
3396 __ Beqz(temp, slow_path->GetEntryLabel());
3397 // Otherwise, the object is indeed an array, further check that this component
3398 // type is not a primitive type.
3399 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3400 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3401 __ Bnez(temp, slow_path->GetEntryLabel());
3402 break;
3403 }
3404
3405 case TypeCheckKind::kUnresolvedCheck:
3406 // We always go into the type check slow path for the unresolved check case.
3407 // We cannot directly call the CheckCast runtime entry point
3408 // without resorting to a type checking slow path here (i.e. by
3409 // calling InvokeRuntime directly), as it would require to
3410 // assign fixed registers for the inputs of this HInstanceOf
3411 // instruction (following the runtime calling convention), which
3412 // might be cluttered by the potential first read barrier
3413 // emission at the beginning of this method.
3414 __ B(slow_path->GetEntryLabel());
3415 break;
3416
3417 case TypeCheckKind::kInterfaceCheck: {
3418 // Avoid read barriers to improve performance of the fast path. We can not get false
3419 // positives by doing this.
3420 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003421 GenerateReferenceLoadTwoRegisters(instruction,
3422 temp_loc,
3423 obj_loc,
3424 class_offset,
3425 maybe_temp2_loc,
3426 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003427 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003428 GenerateReferenceLoadTwoRegisters(instruction,
3429 temp_loc,
3430 temp_loc,
3431 iftable_offset,
3432 maybe_temp2_loc,
3433 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003434 // Iftable is never null.
3435 __ Lw(TMP, temp, array_length_offset);
3436 // Loop through the iftable and check if any class matches.
3437 MipsLabel loop;
3438 __ Bind(&loop);
3439 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3440 __ Beqz(TMP, slow_path->GetEntryLabel());
3441 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3442 __ MaybeUnpoisonHeapReference(AT);
3443 // Go to next interface.
3444 __ Addiu(TMP, TMP, -2);
3445 // Compare the classes and continue the loop if they do not match.
3446 __ Bne(AT, cls, &loop);
3447 break;
3448 }
3449 }
3450
3451 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003452 __ Bind(slow_path->GetExitLabel());
3453}
3454
3455void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3456 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003457 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003458 locations->SetInAt(0, Location::RequiresRegister());
3459 if (check->HasUses()) {
3460 locations->SetOut(Location::SameAsFirstInput());
3461 }
3462}
3463
3464void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3465 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003466 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467 check->GetLoadClass(),
3468 check,
3469 check->GetDexPc(),
3470 true);
3471 codegen_->AddSlowPath(slow_path);
3472 GenerateClassInitializationCheck(slow_path,
3473 check->GetLocations()->InAt(0).AsRegister<Register>());
3474}
3475
3476void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003477 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003478
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003479 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003480 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003481
3482 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003483 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003484 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003485 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003487 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003488 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003489 locations->SetInAt(0, Location::RequiresRegister());
3490 locations->SetInAt(1, Location::RequiresRegister());
3491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3492 break;
3493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003494 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003495 locations->SetInAt(0, Location::RequiresRegister());
3496 locations->SetInAt(1, Location::RequiresRegister());
3497 // Output overlaps because it is written before doing the low comparison.
3498 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3499 break;
3500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kFloat32:
3502 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003503 locations->SetInAt(0, Location::RequiresFpuRegister());
3504 locations->SetInAt(1, Location::RequiresFpuRegister());
3505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003506 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507
3508 default:
3509 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3510 }
3511}
3512
3513void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3514 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003515 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003517 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003518
3519 // 0 if: left == right
3520 // 1 if: left > right
3521 // -1 if: left < right
3522 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003524 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003526 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003527 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003529 Register lhs = locations->InAt(0).AsRegister<Register>();
3530 Register rhs = locations->InAt(1).AsRegister<Register>();
3531 __ Slt(TMP, lhs, rhs);
3532 __ Slt(res, rhs, lhs);
3533 __ Subu(res, res, TMP);
3534 break;
3535 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003536 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003537 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003538 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3539 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3540 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3541 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3542 // TODO: more efficient (direct) comparison with a constant.
3543 __ Slt(TMP, lhs_high, rhs_high);
3544 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3545 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3546 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3547 __ Sltu(TMP, lhs_low, rhs_low);
3548 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3549 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3550 __ Bind(&done);
3551 break;
3552 }
3553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003555 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003556 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3557 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3558 MipsLabel done;
3559 if (isR6) {
3560 __ CmpEqS(FTMP, lhs, rhs);
3561 __ LoadConst32(res, 0);
3562 __ Bc1nez(FTMP, &done);
3563 if (gt_bias) {
3564 __ CmpLtS(FTMP, lhs, rhs);
3565 __ LoadConst32(res, -1);
3566 __ Bc1nez(FTMP, &done);
3567 __ LoadConst32(res, 1);
3568 } else {
3569 __ CmpLtS(FTMP, rhs, lhs);
3570 __ LoadConst32(res, 1);
3571 __ Bc1nez(FTMP, &done);
3572 __ LoadConst32(res, -1);
3573 }
3574 } else {
3575 if (gt_bias) {
3576 __ ColtS(0, lhs, rhs);
3577 __ LoadConst32(res, -1);
3578 __ Bc1t(0, &done);
3579 __ CeqS(0, lhs, rhs);
3580 __ LoadConst32(res, 1);
3581 __ Movt(res, ZERO, 0);
3582 } else {
3583 __ ColtS(0, rhs, lhs);
3584 __ LoadConst32(res, 1);
3585 __ Bc1t(0, &done);
3586 __ CeqS(0, lhs, rhs);
3587 __ LoadConst32(res, -1);
3588 __ Movt(res, ZERO, 0);
3589 }
3590 }
3591 __ Bind(&done);
3592 break;
3593 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003595 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003596 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3597 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3598 MipsLabel done;
3599 if (isR6) {
3600 __ CmpEqD(FTMP, lhs, rhs);
3601 __ LoadConst32(res, 0);
3602 __ Bc1nez(FTMP, &done);
3603 if (gt_bias) {
3604 __ CmpLtD(FTMP, lhs, rhs);
3605 __ LoadConst32(res, -1);
3606 __ Bc1nez(FTMP, &done);
3607 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003608 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003609 __ CmpLtD(FTMP, rhs, lhs);
3610 __ LoadConst32(res, 1);
3611 __ Bc1nez(FTMP, &done);
3612 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613 }
3614 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003615 if (gt_bias) {
3616 __ ColtD(0, lhs, rhs);
3617 __ LoadConst32(res, -1);
3618 __ Bc1t(0, &done);
3619 __ CeqD(0, lhs, rhs);
3620 __ LoadConst32(res, 1);
3621 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003622 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003623 __ ColtD(0, rhs, lhs);
3624 __ LoadConst32(res, 1);
3625 __ Bc1t(0, &done);
3626 __ CeqD(0, lhs, rhs);
3627 __ LoadConst32(res, -1);
3628 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003629 }
3630 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003631 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003632 break;
3633 }
3634
3635 default:
3636 LOG(FATAL) << "Unimplemented compare type " << in_type;
3637 }
3638}
3639
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003640void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003641 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003642 switch (instruction->InputAt(0)->GetType()) {
3643 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003644 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003645 locations->SetInAt(0, Location::RequiresRegister());
3646 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3647 break;
3648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kFloat32:
3650 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003651 locations->SetInAt(0, Location::RequiresFpuRegister());
3652 locations->SetInAt(1, Location::RequiresFpuRegister());
3653 break;
3654 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003655 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3657 }
3658}
3659
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003660void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003661 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003662 return;
3663 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003665 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003667
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003668 switch (type) {
3669 default:
3670 // Integer case.
3671 GenerateIntCompare(instruction->GetCondition(), locations);
3672 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003675 GenerateLongCompare(instruction->GetCondition(), locations);
3676 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kFloat32:
3679 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003680 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3681 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682 }
3683}
3684
Alexey Frunze7e99e052015-11-24 19:28:01 -08003685void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3686 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003688
3689 LocationSummary* locations = instruction->GetLocations();
3690 Location second = locations->InAt(1);
3691 DCHECK(second.IsConstant());
3692
3693 Register out = locations->Out().AsRegister<Register>();
3694 Register dividend = locations->InAt(0).AsRegister<Register>();
3695 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3696 DCHECK(imm == 1 || imm == -1);
3697
3698 if (instruction->IsRem()) {
3699 __ Move(out, ZERO);
3700 } else {
3701 if (imm == -1) {
3702 __ Subu(out, ZERO, dividend);
3703 } else if (out != dividend) {
3704 __ Move(out, dividend);
3705 }
3706 }
3707}
3708
3709void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3710 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003712
3713 LocationSummary* locations = instruction->GetLocations();
3714 Location second = locations->InAt(1);
3715 DCHECK(second.IsConstant());
3716
3717 Register out = locations->Out().AsRegister<Register>();
3718 Register dividend = locations->InAt(0).AsRegister<Register>();
3719 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003720 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003721 int ctz_imm = CTZ(abs_imm);
3722
3723 if (instruction->IsDiv()) {
3724 if (ctz_imm == 1) {
3725 // Fast path for division by +/-2, which is very common.
3726 __ Srl(TMP, dividend, 31);
3727 } else {
3728 __ Sra(TMP, dividend, 31);
3729 __ Srl(TMP, TMP, 32 - ctz_imm);
3730 }
3731 __ Addu(out, dividend, TMP);
3732 __ Sra(out, out, ctz_imm);
3733 if (imm < 0) {
3734 __ Subu(out, ZERO, out);
3735 }
3736 } else {
3737 if (ctz_imm == 1) {
3738 // Fast path for modulo +/-2, which is very common.
3739 __ Sra(TMP, dividend, 31);
3740 __ Subu(out, dividend, TMP);
3741 __ Andi(out, out, 1);
3742 __ Addu(out, out, TMP);
3743 } else {
3744 __ Sra(TMP, dividend, 31);
3745 __ Srl(TMP, TMP, 32 - ctz_imm);
3746 __ Addu(out, dividend, TMP);
3747 if (IsUint<16>(abs_imm - 1)) {
3748 __ Andi(out, out, abs_imm - 1);
3749 } else {
3750 __ Sll(out, out, 32 - ctz_imm);
3751 __ Srl(out, out, 32 - ctz_imm);
3752 }
3753 __ Subu(out, out, TMP);
3754 }
3755 }
3756}
3757
3758void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3759 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003761
3762 LocationSummary* locations = instruction->GetLocations();
3763 Location second = locations->InAt(1);
3764 DCHECK(second.IsConstant());
3765
3766 Register out = locations->Out().AsRegister<Register>();
3767 Register dividend = locations->InAt(0).AsRegister<Register>();
3768 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3769
3770 int64_t magic;
3771 int shift;
3772 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3773
3774 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3775
3776 __ LoadConst32(TMP, magic);
3777 if (isR6) {
3778 __ MuhR6(TMP, dividend, TMP);
3779 } else {
3780 __ MultR2(dividend, TMP);
3781 __ Mfhi(TMP);
3782 }
3783 if (imm > 0 && magic < 0) {
3784 __ Addu(TMP, TMP, dividend);
3785 } else if (imm < 0 && magic > 0) {
3786 __ Subu(TMP, TMP, dividend);
3787 }
3788
3789 if (shift != 0) {
3790 __ Sra(TMP, TMP, shift);
3791 }
3792
3793 if (instruction->IsDiv()) {
3794 __ Sra(out, TMP, 31);
3795 __ Subu(out, TMP, out);
3796 } else {
3797 __ Sra(AT, TMP, 31);
3798 __ Subu(AT, TMP, AT);
3799 __ LoadConst32(TMP, imm);
3800 if (isR6) {
3801 __ MulR6(TMP, AT, TMP);
3802 } else {
3803 __ MulR2(TMP, AT, TMP);
3804 }
3805 __ Subu(out, dividend, TMP);
3806 }
3807}
3808
3809void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3810 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003812
3813 LocationSummary* locations = instruction->GetLocations();
3814 Register out = locations->Out().AsRegister<Register>();
3815 Location second = locations->InAt(1);
3816
3817 if (second.IsConstant()) {
3818 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3819 if (imm == 0) {
3820 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3821 } else if (imm == 1 || imm == -1) {
3822 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003823 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003824 DivRemByPowerOfTwo(instruction);
3825 } else {
3826 DCHECK(imm <= -2 || imm >= 2);
3827 GenerateDivRemWithAnyConstant(instruction);
3828 }
3829 } else {
3830 Register dividend = locations->InAt(0).AsRegister<Register>();
3831 Register divisor = second.AsRegister<Register>();
3832 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3833 if (instruction->IsDiv()) {
3834 if (isR6) {
3835 __ DivR6(out, dividend, divisor);
3836 } else {
3837 __ DivR2(out, dividend, divisor);
3838 }
3839 } else {
3840 if (isR6) {
3841 __ ModR6(out, dividend, divisor);
3842 } else {
3843 __ ModR2(out, dividend, divisor);
3844 }
3845 }
3846 }
3847}
3848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 DataType::Type type = div->GetResultType();
3851 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003852 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 : LocationSummary::kNoCall;
3854
Vladimir Markoca6fff82017-10-03 14:49:14 +01003855 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856
3857 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003859 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003860 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3862 break;
3863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003865 InvokeRuntimeCallingConvention calling_convention;
3866 locations->SetInAt(0, Location::RegisterPairLocation(
3867 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3868 locations->SetInAt(1, Location::RegisterPairLocation(
3869 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3870 locations->SetOut(calling_convention.GetReturnLocation(type));
3871 break;
3872 }
3873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kFloat32:
3875 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876 locations->SetInAt(0, Location::RequiresFpuRegister());
3877 locations->SetInAt(1, Location::RequiresFpuRegister());
3878 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3879 break;
3880
3881 default:
3882 LOG(FATAL) << "Unexpected div type " << type;
3883 }
3884}
3885
3886void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003887 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003888 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003889
3890 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003892 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003893 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003894 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003895 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003896 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3897 break;
3898 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kFloat32:
3900 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003901 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3902 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3903 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003905 __ DivS(dst, lhs, rhs);
3906 } else {
3907 __ DivD(dst, lhs, rhs);
3908 }
3909 break;
3910 }
3911 default:
3912 LOG(FATAL) << "Unexpected div type " << type;
3913 }
3914}
3915
3916void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003917 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003918 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003919}
3920
3921void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003922 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003923 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003924 codegen_->AddSlowPath(slow_path);
3925 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003926 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003927
3928 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003930 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003931 case DataType::Type::kInt8:
3932 case DataType::Type::kUint16:
3933 case DataType::Type::kInt16:
3934 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003935 if (value.IsConstant()) {
3936 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3937 __ B(slow_path->GetEntryLabel());
3938 } else {
3939 // A division by a non-null constant is valid. We don't need to perform
3940 // any check, so simply fall through.
3941 }
3942 } else {
3943 DCHECK(value.IsRegister()) << value;
3944 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3945 }
3946 break;
3947 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 if (value.IsConstant()) {
3950 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3951 __ B(slow_path->GetEntryLabel());
3952 } else {
3953 // A division by a non-null constant is valid. We don't need to perform
3954 // any check, so simply fall through.
3955 }
3956 } else {
3957 DCHECK(value.IsRegisterPair()) << value;
3958 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3959 __ Beqz(TMP, slow_path->GetEntryLabel());
3960 }
3961 break;
3962 }
3963 default:
3964 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3965 }
3966}
3967
3968void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3969 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003970 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003971 locations->SetOut(Location::ConstantLocation(constant));
3972}
3973
3974void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3975 // Will be generated at use site.
3976}
3977
3978void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3979 exit->SetLocations(nullptr);
3980}
3981
3982void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3983}
3984
3985void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3986 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003987 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003988 locations->SetOut(Location::ConstantLocation(constant));
3989}
3990
3991void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3992 // Will be generated at use site.
3993}
3994
3995void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3996 got->SetLocations(nullptr);
3997}
3998
3999void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
4000 DCHECK(!successor->IsExitBlock());
4001 HBasicBlock* block = got->GetBlock();
4002 HInstruction* previous = got->GetPrevious();
4003 HLoopInformation* info = block->GetLoopInformation();
4004
4005 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004006 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4007 return;
4008 }
4009 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4010 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4011 }
4012 if (!codegen_->GoesToNextBlock(block, successor)) {
4013 __ B(codegen_->GetLabelOf(successor));
4014 }
4015}
4016
4017void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4018 HandleGoto(got, got->GetSuccessor());
4019}
4020
4021void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4022 try_boundary->SetLocations(nullptr);
4023}
4024
4025void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4026 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4027 if (!successor->IsExitBlock()) {
4028 HandleGoto(try_boundary, successor);
4029 }
4030}
4031
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004032void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4033 LocationSummary* locations) {
4034 Register dst = locations->Out().AsRegister<Register>();
4035 Register lhs = locations->InAt(0).AsRegister<Register>();
4036 Location rhs_location = locations->InAt(1);
4037 Register rhs_reg = ZERO;
4038 int64_t rhs_imm = 0;
4039 bool use_imm = rhs_location.IsConstant();
4040 if (use_imm) {
4041 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4042 } else {
4043 rhs_reg = rhs_location.AsRegister<Register>();
4044 }
4045
4046 switch (cond) {
4047 case kCondEQ:
4048 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004049 if (use_imm && IsInt<16>(-rhs_imm)) {
4050 if (rhs_imm == 0) {
4051 if (cond == kCondEQ) {
4052 __ Sltiu(dst, lhs, 1);
4053 } else {
4054 __ Sltu(dst, ZERO, lhs);
4055 }
4056 } else {
4057 __ Addiu(dst, lhs, -rhs_imm);
4058 if (cond == kCondEQ) {
4059 __ Sltiu(dst, dst, 1);
4060 } else {
4061 __ Sltu(dst, ZERO, dst);
4062 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004063 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004064 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004065 if (use_imm && IsUint<16>(rhs_imm)) {
4066 __ Xori(dst, lhs, rhs_imm);
4067 } else {
4068 if (use_imm) {
4069 rhs_reg = TMP;
4070 __ LoadConst32(rhs_reg, rhs_imm);
4071 }
4072 __ Xor(dst, lhs, rhs_reg);
4073 }
4074 if (cond == kCondEQ) {
4075 __ Sltiu(dst, dst, 1);
4076 } else {
4077 __ Sltu(dst, ZERO, dst);
4078 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004079 }
4080 break;
4081
4082 case kCondLT:
4083 case kCondGE:
4084 if (use_imm && IsInt<16>(rhs_imm)) {
4085 __ Slti(dst, lhs, rhs_imm);
4086 } else {
4087 if (use_imm) {
4088 rhs_reg = TMP;
4089 __ LoadConst32(rhs_reg, rhs_imm);
4090 }
4091 __ Slt(dst, lhs, rhs_reg);
4092 }
4093 if (cond == kCondGE) {
4094 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4095 // only the slt instruction but no sge.
4096 __ Xori(dst, dst, 1);
4097 }
4098 break;
4099
4100 case kCondLE:
4101 case kCondGT:
4102 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4103 // Simulate lhs <= rhs via lhs < rhs + 1.
4104 __ Slti(dst, lhs, rhs_imm + 1);
4105 if (cond == kCondGT) {
4106 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4107 // only the slti instruction but no sgti.
4108 __ Xori(dst, dst, 1);
4109 }
4110 } else {
4111 if (use_imm) {
4112 rhs_reg = TMP;
4113 __ LoadConst32(rhs_reg, rhs_imm);
4114 }
4115 __ Slt(dst, rhs_reg, lhs);
4116 if (cond == kCondLE) {
4117 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4118 // only the slt instruction but no sle.
4119 __ Xori(dst, dst, 1);
4120 }
4121 }
4122 break;
4123
4124 case kCondB:
4125 case kCondAE:
4126 if (use_imm && IsInt<16>(rhs_imm)) {
4127 // Sltiu sign-extends its 16-bit immediate operand before
4128 // the comparison and thus lets us compare directly with
4129 // unsigned values in the ranges [0, 0x7fff] and
4130 // [0xffff8000, 0xffffffff].
4131 __ Sltiu(dst, lhs, rhs_imm);
4132 } else {
4133 if (use_imm) {
4134 rhs_reg = TMP;
4135 __ LoadConst32(rhs_reg, rhs_imm);
4136 }
4137 __ Sltu(dst, lhs, rhs_reg);
4138 }
4139 if (cond == kCondAE) {
4140 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4141 // only the sltu instruction but no sgeu.
4142 __ Xori(dst, dst, 1);
4143 }
4144 break;
4145
4146 case kCondBE:
4147 case kCondA:
4148 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4149 // Simulate lhs <= rhs via lhs < rhs + 1.
4150 // Note that this only works if rhs + 1 does not overflow
4151 // to 0, hence the check above.
4152 // Sltiu sign-extends its 16-bit immediate operand before
4153 // the comparison and thus lets us compare directly with
4154 // unsigned values in the ranges [0, 0x7fff] and
4155 // [0xffff8000, 0xffffffff].
4156 __ Sltiu(dst, lhs, rhs_imm + 1);
4157 if (cond == kCondA) {
4158 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4159 // only the sltiu instruction but no sgtiu.
4160 __ Xori(dst, dst, 1);
4161 }
4162 } else {
4163 if (use_imm) {
4164 rhs_reg = TMP;
4165 __ LoadConst32(rhs_reg, rhs_imm);
4166 }
4167 __ Sltu(dst, rhs_reg, lhs);
4168 if (cond == kCondBE) {
4169 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4170 // only the sltu instruction but no sleu.
4171 __ Xori(dst, dst, 1);
4172 }
4173 }
4174 break;
4175 }
4176}
4177
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004178bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4179 LocationSummary* input_locations,
4180 Register dst) {
4181 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4182 Location rhs_location = input_locations->InAt(1);
4183 Register rhs_reg = ZERO;
4184 int64_t rhs_imm = 0;
4185 bool use_imm = rhs_location.IsConstant();
4186 if (use_imm) {
4187 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4188 } else {
4189 rhs_reg = rhs_location.AsRegister<Register>();
4190 }
4191
4192 switch (cond) {
4193 case kCondEQ:
4194 case kCondNE:
4195 if (use_imm && IsInt<16>(-rhs_imm)) {
4196 __ Addiu(dst, lhs, -rhs_imm);
4197 } else if (use_imm && IsUint<16>(rhs_imm)) {
4198 __ Xori(dst, lhs, rhs_imm);
4199 } else {
4200 if (use_imm) {
4201 rhs_reg = TMP;
4202 __ LoadConst32(rhs_reg, rhs_imm);
4203 }
4204 __ Xor(dst, lhs, rhs_reg);
4205 }
4206 return (cond == kCondEQ);
4207
4208 case kCondLT:
4209 case kCondGE:
4210 if (use_imm && IsInt<16>(rhs_imm)) {
4211 __ Slti(dst, lhs, rhs_imm);
4212 } else {
4213 if (use_imm) {
4214 rhs_reg = TMP;
4215 __ LoadConst32(rhs_reg, rhs_imm);
4216 }
4217 __ Slt(dst, lhs, rhs_reg);
4218 }
4219 return (cond == kCondGE);
4220
4221 case kCondLE:
4222 case kCondGT:
4223 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4224 // Simulate lhs <= rhs via lhs < rhs + 1.
4225 __ Slti(dst, lhs, rhs_imm + 1);
4226 return (cond == kCondGT);
4227 } else {
4228 if (use_imm) {
4229 rhs_reg = TMP;
4230 __ LoadConst32(rhs_reg, rhs_imm);
4231 }
4232 __ Slt(dst, rhs_reg, lhs);
4233 return (cond == kCondLE);
4234 }
4235
4236 case kCondB:
4237 case kCondAE:
4238 if (use_imm && IsInt<16>(rhs_imm)) {
4239 // Sltiu sign-extends its 16-bit immediate operand before
4240 // the comparison and thus lets us compare directly with
4241 // unsigned values in the ranges [0, 0x7fff] and
4242 // [0xffff8000, 0xffffffff].
4243 __ Sltiu(dst, lhs, rhs_imm);
4244 } else {
4245 if (use_imm) {
4246 rhs_reg = TMP;
4247 __ LoadConst32(rhs_reg, rhs_imm);
4248 }
4249 __ Sltu(dst, lhs, rhs_reg);
4250 }
4251 return (cond == kCondAE);
4252
4253 case kCondBE:
4254 case kCondA:
4255 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4256 // Simulate lhs <= rhs via lhs < rhs + 1.
4257 // Note that this only works if rhs + 1 does not overflow
4258 // to 0, hence the check above.
4259 // Sltiu sign-extends its 16-bit immediate operand before
4260 // the comparison and thus lets us compare directly with
4261 // unsigned values in the ranges [0, 0x7fff] and
4262 // [0xffff8000, 0xffffffff].
4263 __ Sltiu(dst, lhs, rhs_imm + 1);
4264 return (cond == kCondA);
4265 } else {
4266 if (use_imm) {
4267 rhs_reg = TMP;
4268 __ LoadConst32(rhs_reg, rhs_imm);
4269 }
4270 __ Sltu(dst, rhs_reg, lhs);
4271 return (cond == kCondBE);
4272 }
4273 }
4274}
4275
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004276void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4277 LocationSummary* locations,
4278 MipsLabel* label) {
4279 Register lhs = locations->InAt(0).AsRegister<Register>();
4280 Location rhs_location = locations->InAt(1);
4281 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004282 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004283 bool use_imm = rhs_location.IsConstant();
4284 if (use_imm) {
4285 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4286 } else {
4287 rhs_reg = rhs_location.AsRegister<Register>();
4288 }
4289
4290 if (use_imm && rhs_imm == 0) {
4291 switch (cond) {
4292 case kCondEQ:
4293 case kCondBE: // <= 0 if zero
4294 __ Beqz(lhs, label);
4295 break;
4296 case kCondNE:
4297 case kCondA: // > 0 if non-zero
4298 __ Bnez(lhs, label);
4299 break;
4300 case kCondLT:
4301 __ Bltz(lhs, label);
4302 break;
4303 case kCondGE:
4304 __ Bgez(lhs, label);
4305 break;
4306 case kCondLE:
4307 __ Blez(lhs, label);
4308 break;
4309 case kCondGT:
4310 __ Bgtz(lhs, label);
4311 break;
4312 case kCondB: // always false
4313 break;
4314 case kCondAE: // always true
4315 __ B(label);
4316 break;
4317 }
4318 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004319 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4320 if (isR6 || !use_imm) {
4321 if (use_imm) {
4322 rhs_reg = TMP;
4323 __ LoadConst32(rhs_reg, rhs_imm);
4324 }
4325 switch (cond) {
4326 case kCondEQ:
4327 __ Beq(lhs, rhs_reg, label);
4328 break;
4329 case kCondNE:
4330 __ Bne(lhs, rhs_reg, label);
4331 break;
4332 case kCondLT:
4333 __ Blt(lhs, rhs_reg, label);
4334 break;
4335 case kCondGE:
4336 __ Bge(lhs, rhs_reg, label);
4337 break;
4338 case kCondLE:
4339 __ Bge(rhs_reg, lhs, label);
4340 break;
4341 case kCondGT:
4342 __ Blt(rhs_reg, lhs, label);
4343 break;
4344 case kCondB:
4345 __ Bltu(lhs, rhs_reg, label);
4346 break;
4347 case kCondAE:
4348 __ Bgeu(lhs, rhs_reg, label);
4349 break;
4350 case kCondBE:
4351 __ Bgeu(rhs_reg, lhs, label);
4352 break;
4353 case kCondA:
4354 __ Bltu(rhs_reg, lhs, label);
4355 break;
4356 }
4357 } else {
4358 // Special cases for more efficient comparison with constants on R2.
4359 switch (cond) {
4360 case kCondEQ:
4361 __ LoadConst32(TMP, rhs_imm);
4362 __ Beq(lhs, TMP, label);
4363 break;
4364 case kCondNE:
4365 __ LoadConst32(TMP, rhs_imm);
4366 __ Bne(lhs, TMP, label);
4367 break;
4368 case kCondLT:
4369 if (IsInt<16>(rhs_imm)) {
4370 __ Slti(TMP, lhs, rhs_imm);
4371 __ Bnez(TMP, label);
4372 } else {
4373 __ LoadConst32(TMP, rhs_imm);
4374 __ Blt(lhs, TMP, label);
4375 }
4376 break;
4377 case kCondGE:
4378 if (IsInt<16>(rhs_imm)) {
4379 __ Slti(TMP, lhs, rhs_imm);
4380 __ Beqz(TMP, label);
4381 } else {
4382 __ LoadConst32(TMP, rhs_imm);
4383 __ Bge(lhs, TMP, label);
4384 }
4385 break;
4386 case kCondLE:
4387 if (IsInt<16>(rhs_imm + 1)) {
4388 // Simulate lhs <= rhs via lhs < rhs + 1.
4389 __ Slti(TMP, lhs, rhs_imm + 1);
4390 __ Bnez(TMP, label);
4391 } else {
4392 __ LoadConst32(TMP, rhs_imm);
4393 __ Bge(TMP, lhs, label);
4394 }
4395 break;
4396 case kCondGT:
4397 if (IsInt<16>(rhs_imm + 1)) {
4398 // Simulate lhs > rhs via !(lhs < rhs + 1).
4399 __ Slti(TMP, lhs, rhs_imm + 1);
4400 __ Beqz(TMP, label);
4401 } else {
4402 __ LoadConst32(TMP, rhs_imm);
4403 __ Blt(TMP, lhs, label);
4404 }
4405 break;
4406 case kCondB:
4407 if (IsInt<16>(rhs_imm)) {
4408 __ Sltiu(TMP, lhs, rhs_imm);
4409 __ Bnez(TMP, label);
4410 } else {
4411 __ LoadConst32(TMP, rhs_imm);
4412 __ Bltu(lhs, TMP, label);
4413 }
4414 break;
4415 case kCondAE:
4416 if (IsInt<16>(rhs_imm)) {
4417 __ Sltiu(TMP, lhs, rhs_imm);
4418 __ Beqz(TMP, label);
4419 } else {
4420 __ LoadConst32(TMP, rhs_imm);
4421 __ Bgeu(lhs, TMP, label);
4422 }
4423 break;
4424 case kCondBE:
4425 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4426 // Simulate lhs <= rhs via lhs < rhs + 1.
4427 // Note that this only works if rhs + 1 does not overflow
4428 // to 0, hence the check above.
4429 __ Sltiu(TMP, lhs, rhs_imm + 1);
4430 __ Bnez(TMP, label);
4431 } else {
4432 __ LoadConst32(TMP, rhs_imm);
4433 __ Bgeu(TMP, lhs, label);
4434 }
4435 break;
4436 case kCondA:
4437 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4438 // Simulate lhs > rhs via !(lhs < rhs + 1).
4439 // Note that this only works if rhs + 1 does not overflow
4440 // to 0, hence the check above.
4441 __ Sltiu(TMP, lhs, rhs_imm + 1);
4442 __ Beqz(TMP, label);
4443 } else {
4444 __ LoadConst32(TMP, rhs_imm);
4445 __ Bltu(TMP, lhs, label);
4446 }
4447 break;
4448 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004449 }
4450 }
4451}
4452
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004453void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4454 LocationSummary* locations) {
4455 Register dst = locations->Out().AsRegister<Register>();
4456 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4457 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4458 Location rhs_location = locations->InAt(1);
4459 Register rhs_high = ZERO;
4460 Register rhs_low = ZERO;
4461 int64_t imm = 0;
4462 uint32_t imm_high = 0;
4463 uint32_t imm_low = 0;
4464 bool use_imm = rhs_location.IsConstant();
4465 if (use_imm) {
4466 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4467 imm_high = High32Bits(imm);
4468 imm_low = Low32Bits(imm);
4469 } else {
4470 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4471 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4472 }
4473 if (use_imm && imm == 0) {
4474 switch (cond) {
4475 case kCondEQ:
4476 case kCondBE: // <= 0 if zero
4477 __ Or(dst, lhs_high, lhs_low);
4478 __ Sltiu(dst, dst, 1);
4479 break;
4480 case kCondNE:
4481 case kCondA: // > 0 if non-zero
4482 __ Or(dst, lhs_high, lhs_low);
4483 __ Sltu(dst, ZERO, dst);
4484 break;
4485 case kCondLT:
4486 __ Slt(dst, lhs_high, ZERO);
4487 break;
4488 case kCondGE:
4489 __ Slt(dst, lhs_high, ZERO);
4490 __ Xori(dst, dst, 1);
4491 break;
4492 case kCondLE:
4493 __ Or(TMP, lhs_high, lhs_low);
4494 __ Sra(AT, lhs_high, 31);
4495 __ Sltu(dst, AT, TMP);
4496 __ Xori(dst, dst, 1);
4497 break;
4498 case kCondGT:
4499 __ Or(TMP, lhs_high, lhs_low);
4500 __ Sra(AT, lhs_high, 31);
4501 __ Sltu(dst, AT, TMP);
4502 break;
4503 case kCondB: // always false
4504 __ Andi(dst, dst, 0);
4505 break;
4506 case kCondAE: // always true
4507 __ Ori(dst, ZERO, 1);
4508 break;
4509 }
4510 } else if (use_imm) {
4511 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4512 switch (cond) {
4513 case kCondEQ:
4514 __ LoadConst32(TMP, imm_high);
4515 __ Xor(TMP, TMP, lhs_high);
4516 __ LoadConst32(AT, imm_low);
4517 __ Xor(AT, AT, lhs_low);
4518 __ Or(dst, TMP, AT);
4519 __ Sltiu(dst, dst, 1);
4520 break;
4521 case kCondNE:
4522 __ LoadConst32(TMP, imm_high);
4523 __ Xor(TMP, TMP, lhs_high);
4524 __ LoadConst32(AT, imm_low);
4525 __ Xor(AT, AT, lhs_low);
4526 __ Or(dst, TMP, AT);
4527 __ Sltu(dst, ZERO, dst);
4528 break;
4529 case kCondLT:
4530 case kCondGE:
4531 if (dst == lhs_low) {
4532 __ LoadConst32(TMP, imm_low);
4533 __ Sltu(dst, lhs_low, TMP);
4534 }
4535 __ LoadConst32(TMP, imm_high);
4536 __ Slt(AT, lhs_high, TMP);
4537 __ Slt(TMP, TMP, lhs_high);
4538 if (dst != lhs_low) {
4539 __ LoadConst32(dst, imm_low);
4540 __ Sltu(dst, lhs_low, dst);
4541 }
4542 __ Slt(dst, TMP, dst);
4543 __ Or(dst, dst, AT);
4544 if (cond == kCondGE) {
4545 __ Xori(dst, dst, 1);
4546 }
4547 break;
4548 case kCondGT:
4549 case kCondLE:
4550 if (dst == lhs_low) {
4551 __ LoadConst32(TMP, imm_low);
4552 __ Sltu(dst, TMP, lhs_low);
4553 }
4554 __ LoadConst32(TMP, imm_high);
4555 __ Slt(AT, TMP, lhs_high);
4556 __ Slt(TMP, lhs_high, TMP);
4557 if (dst != lhs_low) {
4558 __ LoadConst32(dst, imm_low);
4559 __ Sltu(dst, dst, lhs_low);
4560 }
4561 __ Slt(dst, TMP, dst);
4562 __ Or(dst, dst, AT);
4563 if (cond == kCondLE) {
4564 __ Xori(dst, dst, 1);
4565 }
4566 break;
4567 case kCondB:
4568 case kCondAE:
4569 if (dst == lhs_low) {
4570 __ LoadConst32(TMP, imm_low);
4571 __ Sltu(dst, lhs_low, TMP);
4572 }
4573 __ LoadConst32(TMP, imm_high);
4574 __ Sltu(AT, lhs_high, TMP);
4575 __ Sltu(TMP, TMP, lhs_high);
4576 if (dst != lhs_low) {
4577 __ LoadConst32(dst, imm_low);
4578 __ Sltu(dst, lhs_low, dst);
4579 }
4580 __ Slt(dst, TMP, dst);
4581 __ Or(dst, dst, AT);
4582 if (cond == kCondAE) {
4583 __ Xori(dst, dst, 1);
4584 }
4585 break;
4586 case kCondA:
4587 case kCondBE:
4588 if (dst == lhs_low) {
4589 __ LoadConst32(TMP, imm_low);
4590 __ Sltu(dst, TMP, lhs_low);
4591 }
4592 __ LoadConst32(TMP, imm_high);
4593 __ Sltu(AT, TMP, lhs_high);
4594 __ Sltu(TMP, lhs_high, TMP);
4595 if (dst != lhs_low) {
4596 __ LoadConst32(dst, imm_low);
4597 __ Sltu(dst, dst, lhs_low);
4598 }
4599 __ Slt(dst, TMP, dst);
4600 __ Or(dst, dst, AT);
4601 if (cond == kCondBE) {
4602 __ Xori(dst, dst, 1);
4603 }
4604 break;
4605 }
4606 } else {
4607 switch (cond) {
4608 case kCondEQ:
4609 __ Xor(TMP, lhs_high, rhs_high);
4610 __ Xor(AT, lhs_low, rhs_low);
4611 __ Or(dst, TMP, AT);
4612 __ Sltiu(dst, dst, 1);
4613 break;
4614 case kCondNE:
4615 __ Xor(TMP, lhs_high, rhs_high);
4616 __ Xor(AT, lhs_low, rhs_low);
4617 __ Or(dst, TMP, AT);
4618 __ Sltu(dst, ZERO, dst);
4619 break;
4620 case kCondLT:
4621 case kCondGE:
4622 __ Slt(TMP, rhs_high, lhs_high);
4623 __ Sltu(AT, lhs_low, rhs_low);
4624 __ Slt(TMP, TMP, AT);
4625 __ Slt(AT, lhs_high, rhs_high);
4626 __ Or(dst, AT, TMP);
4627 if (cond == kCondGE) {
4628 __ Xori(dst, dst, 1);
4629 }
4630 break;
4631 case kCondGT:
4632 case kCondLE:
4633 __ Slt(TMP, lhs_high, rhs_high);
4634 __ Sltu(AT, rhs_low, lhs_low);
4635 __ Slt(TMP, TMP, AT);
4636 __ Slt(AT, rhs_high, lhs_high);
4637 __ Or(dst, AT, TMP);
4638 if (cond == kCondLE) {
4639 __ Xori(dst, dst, 1);
4640 }
4641 break;
4642 case kCondB:
4643 case kCondAE:
4644 __ Sltu(TMP, rhs_high, lhs_high);
4645 __ Sltu(AT, lhs_low, rhs_low);
4646 __ Slt(TMP, TMP, AT);
4647 __ Sltu(AT, lhs_high, rhs_high);
4648 __ Or(dst, AT, TMP);
4649 if (cond == kCondAE) {
4650 __ Xori(dst, dst, 1);
4651 }
4652 break;
4653 case kCondA:
4654 case kCondBE:
4655 __ Sltu(TMP, lhs_high, rhs_high);
4656 __ Sltu(AT, rhs_low, lhs_low);
4657 __ Slt(TMP, TMP, AT);
4658 __ Sltu(AT, rhs_high, lhs_high);
4659 __ Or(dst, AT, TMP);
4660 if (cond == kCondBE) {
4661 __ Xori(dst, dst, 1);
4662 }
4663 break;
4664 }
4665 }
4666}
4667
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004668void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4669 LocationSummary* locations,
4670 MipsLabel* label) {
4671 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4672 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4673 Location rhs_location = locations->InAt(1);
4674 Register rhs_high = ZERO;
4675 Register rhs_low = ZERO;
4676 int64_t imm = 0;
4677 uint32_t imm_high = 0;
4678 uint32_t imm_low = 0;
4679 bool use_imm = rhs_location.IsConstant();
4680 if (use_imm) {
4681 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4682 imm_high = High32Bits(imm);
4683 imm_low = Low32Bits(imm);
4684 } else {
4685 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4686 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4687 }
4688
4689 if (use_imm && imm == 0) {
4690 switch (cond) {
4691 case kCondEQ:
4692 case kCondBE: // <= 0 if zero
4693 __ Or(TMP, lhs_high, lhs_low);
4694 __ Beqz(TMP, label);
4695 break;
4696 case kCondNE:
4697 case kCondA: // > 0 if non-zero
4698 __ Or(TMP, lhs_high, lhs_low);
4699 __ Bnez(TMP, label);
4700 break;
4701 case kCondLT:
4702 __ Bltz(lhs_high, label);
4703 break;
4704 case kCondGE:
4705 __ Bgez(lhs_high, label);
4706 break;
4707 case kCondLE:
4708 __ Or(TMP, lhs_high, lhs_low);
4709 __ Sra(AT, lhs_high, 31);
4710 __ Bgeu(AT, TMP, label);
4711 break;
4712 case kCondGT:
4713 __ Or(TMP, lhs_high, lhs_low);
4714 __ Sra(AT, lhs_high, 31);
4715 __ Bltu(AT, TMP, label);
4716 break;
4717 case kCondB: // always false
4718 break;
4719 case kCondAE: // always true
4720 __ B(label);
4721 break;
4722 }
4723 } else if (use_imm) {
4724 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4725 switch (cond) {
4726 case kCondEQ:
4727 __ LoadConst32(TMP, imm_high);
4728 __ Xor(TMP, TMP, lhs_high);
4729 __ LoadConst32(AT, imm_low);
4730 __ Xor(AT, AT, lhs_low);
4731 __ Or(TMP, TMP, AT);
4732 __ Beqz(TMP, label);
4733 break;
4734 case kCondNE:
4735 __ LoadConst32(TMP, imm_high);
4736 __ Xor(TMP, TMP, lhs_high);
4737 __ LoadConst32(AT, imm_low);
4738 __ Xor(AT, AT, lhs_low);
4739 __ Or(TMP, TMP, AT);
4740 __ Bnez(TMP, label);
4741 break;
4742 case kCondLT:
4743 __ LoadConst32(TMP, imm_high);
4744 __ Blt(lhs_high, TMP, label);
4745 __ Slt(TMP, TMP, lhs_high);
4746 __ LoadConst32(AT, imm_low);
4747 __ Sltu(AT, lhs_low, AT);
4748 __ Blt(TMP, AT, label);
4749 break;
4750 case kCondGE:
4751 __ LoadConst32(TMP, imm_high);
4752 __ Blt(TMP, lhs_high, label);
4753 __ Slt(TMP, lhs_high, TMP);
4754 __ LoadConst32(AT, imm_low);
4755 __ Sltu(AT, lhs_low, AT);
4756 __ Or(TMP, TMP, AT);
4757 __ Beqz(TMP, label);
4758 break;
4759 case kCondLE:
4760 __ LoadConst32(TMP, imm_high);
4761 __ Blt(lhs_high, TMP, label);
4762 __ Slt(TMP, TMP, lhs_high);
4763 __ LoadConst32(AT, imm_low);
4764 __ Sltu(AT, AT, lhs_low);
4765 __ Or(TMP, TMP, AT);
4766 __ Beqz(TMP, label);
4767 break;
4768 case kCondGT:
4769 __ LoadConst32(TMP, imm_high);
4770 __ Blt(TMP, lhs_high, label);
4771 __ Slt(TMP, lhs_high, TMP);
4772 __ LoadConst32(AT, imm_low);
4773 __ Sltu(AT, AT, lhs_low);
4774 __ Blt(TMP, AT, label);
4775 break;
4776 case kCondB:
4777 __ LoadConst32(TMP, imm_high);
4778 __ Bltu(lhs_high, TMP, label);
4779 __ Sltu(TMP, TMP, lhs_high);
4780 __ LoadConst32(AT, imm_low);
4781 __ Sltu(AT, lhs_low, AT);
4782 __ Blt(TMP, AT, label);
4783 break;
4784 case kCondAE:
4785 __ LoadConst32(TMP, imm_high);
4786 __ Bltu(TMP, lhs_high, label);
4787 __ Sltu(TMP, lhs_high, TMP);
4788 __ LoadConst32(AT, imm_low);
4789 __ Sltu(AT, lhs_low, AT);
4790 __ Or(TMP, TMP, AT);
4791 __ Beqz(TMP, label);
4792 break;
4793 case kCondBE:
4794 __ LoadConst32(TMP, imm_high);
4795 __ Bltu(lhs_high, TMP, label);
4796 __ Sltu(TMP, TMP, lhs_high);
4797 __ LoadConst32(AT, imm_low);
4798 __ Sltu(AT, AT, lhs_low);
4799 __ Or(TMP, TMP, AT);
4800 __ Beqz(TMP, label);
4801 break;
4802 case kCondA:
4803 __ LoadConst32(TMP, imm_high);
4804 __ Bltu(TMP, lhs_high, label);
4805 __ Sltu(TMP, lhs_high, TMP);
4806 __ LoadConst32(AT, imm_low);
4807 __ Sltu(AT, AT, lhs_low);
4808 __ Blt(TMP, AT, label);
4809 break;
4810 }
4811 } else {
4812 switch (cond) {
4813 case kCondEQ:
4814 __ Xor(TMP, lhs_high, rhs_high);
4815 __ Xor(AT, lhs_low, rhs_low);
4816 __ Or(TMP, TMP, AT);
4817 __ Beqz(TMP, label);
4818 break;
4819 case kCondNE:
4820 __ Xor(TMP, lhs_high, rhs_high);
4821 __ Xor(AT, lhs_low, rhs_low);
4822 __ Or(TMP, TMP, AT);
4823 __ Bnez(TMP, label);
4824 break;
4825 case kCondLT:
4826 __ Blt(lhs_high, rhs_high, label);
4827 __ Slt(TMP, rhs_high, lhs_high);
4828 __ Sltu(AT, lhs_low, rhs_low);
4829 __ Blt(TMP, AT, label);
4830 break;
4831 case kCondGE:
4832 __ Blt(rhs_high, lhs_high, label);
4833 __ Slt(TMP, lhs_high, rhs_high);
4834 __ Sltu(AT, lhs_low, rhs_low);
4835 __ Or(TMP, TMP, AT);
4836 __ Beqz(TMP, label);
4837 break;
4838 case kCondLE:
4839 __ Blt(lhs_high, rhs_high, label);
4840 __ Slt(TMP, rhs_high, lhs_high);
4841 __ Sltu(AT, rhs_low, lhs_low);
4842 __ Or(TMP, TMP, AT);
4843 __ Beqz(TMP, label);
4844 break;
4845 case kCondGT:
4846 __ Blt(rhs_high, lhs_high, label);
4847 __ Slt(TMP, lhs_high, rhs_high);
4848 __ Sltu(AT, rhs_low, lhs_low);
4849 __ Blt(TMP, AT, label);
4850 break;
4851 case kCondB:
4852 __ Bltu(lhs_high, rhs_high, label);
4853 __ Sltu(TMP, rhs_high, lhs_high);
4854 __ Sltu(AT, lhs_low, rhs_low);
4855 __ Blt(TMP, AT, label);
4856 break;
4857 case kCondAE:
4858 __ Bltu(rhs_high, lhs_high, label);
4859 __ Sltu(TMP, lhs_high, rhs_high);
4860 __ Sltu(AT, lhs_low, rhs_low);
4861 __ Or(TMP, TMP, AT);
4862 __ Beqz(TMP, label);
4863 break;
4864 case kCondBE:
4865 __ Bltu(lhs_high, rhs_high, label);
4866 __ Sltu(TMP, rhs_high, lhs_high);
4867 __ Sltu(AT, rhs_low, lhs_low);
4868 __ Or(TMP, TMP, AT);
4869 __ Beqz(TMP, label);
4870 break;
4871 case kCondA:
4872 __ Bltu(rhs_high, lhs_high, label);
4873 __ Sltu(TMP, lhs_high, rhs_high);
4874 __ Sltu(AT, rhs_low, lhs_low);
4875 __ Blt(TMP, AT, label);
4876 break;
4877 }
4878 }
4879}
4880
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004881void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4882 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004884 LocationSummary* locations) {
4885 Register dst = locations->Out().AsRegister<Register>();
4886 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4887 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4888 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004889 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004890 if (isR6) {
4891 switch (cond) {
4892 case kCondEQ:
4893 __ CmpEqS(FTMP, lhs, rhs);
4894 __ Mfc1(dst, FTMP);
4895 __ Andi(dst, dst, 1);
4896 break;
4897 case kCondNE:
4898 __ CmpEqS(FTMP, lhs, rhs);
4899 __ Mfc1(dst, FTMP);
4900 __ Addiu(dst, dst, 1);
4901 break;
4902 case kCondLT:
4903 if (gt_bias) {
4904 __ CmpLtS(FTMP, lhs, rhs);
4905 } else {
4906 __ CmpUltS(FTMP, lhs, rhs);
4907 }
4908 __ Mfc1(dst, FTMP);
4909 __ Andi(dst, dst, 1);
4910 break;
4911 case kCondLE:
4912 if (gt_bias) {
4913 __ CmpLeS(FTMP, lhs, rhs);
4914 } else {
4915 __ CmpUleS(FTMP, lhs, rhs);
4916 }
4917 __ Mfc1(dst, FTMP);
4918 __ Andi(dst, dst, 1);
4919 break;
4920 case kCondGT:
4921 if (gt_bias) {
4922 __ CmpUltS(FTMP, rhs, lhs);
4923 } else {
4924 __ CmpLtS(FTMP, rhs, lhs);
4925 }
4926 __ Mfc1(dst, FTMP);
4927 __ Andi(dst, dst, 1);
4928 break;
4929 case kCondGE:
4930 if (gt_bias) {
4931 __ CmpUleS(FTMP, rhs, lhs);
4932 } else {
4933 __ CmpLeS(FTMP, rhs, lhs);
4934 }
4935 __ Mfc1(dst, FTMP);
4936 __ Andi(dst, dst, 1);
4937 break;
4938 default:
4939 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4940 UNREACHABLE();
4941 }
4942 } else {
4943 switch (cond) {
4944 case kCondEQ:
4945 __ CeqS(0, lhs, rhs);
4946 __ LoadConst32(dst, 1);
4947 __ Movf(dst, ZERO, 0);
4948 break;
4949 case kCondNE:
4950 __ CeqS(0, lhs, rhs);
4951 __ LoadConst32(dst, 1);
4952 __ Movt(dst, ZERO, 0);
4953 break;
4954 case kCondLT:
4955 if (gt_bias) {
4956 __ ColtS(0, lhs, rhs);
4957 } else {
4958 __ CultS(0, lhs, rhs);
4959 }
4960 __ LoadConst32(dst, 1);
4961 __ Movf(dst, ZERO, 0);
4962 break;
4963 case kCondLE:
4964 if (gt_bias) {
4965 __ ColeS(0, lhs, rhs);
4966 } else {
4967 __ CuleS(0, lhs, rhs);
4968 }
4969 __ LoadConst32(dst, 1);
4970 __ Movf(dst, ZERO, 0);
4971 break;
4972 case kCondGT:
4973 if (gt_bias) {
4974 __ CultS(0, rhs, lhs);
4975 } else {
4976 __ ColtS(0, rhs, lhs);
4977 }
4978 __ LoadConst32(dst, 1);
4979 __ Movf(dst, ZERO, 0);
4980 break;
4981 case kCondGE:
4982 if (gt_bias) {
4983 __ CuleS(0, rhs, lhs);
4984 } else {
4985 __ ColeS(0, rhs, lhs);
4986 }
4987 __ LoadConst32(dst, 1);
4988 __ Movf(dst, ZERO, 0);
4989 break;
4990 default:
4991 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4992 UNREACHABLE();
4993 }
4994 }
4995 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004996 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004997 if (isR6) {
4998 switch (cond) {
4999 case kCondEQ:
5000 __ CmpEqD(FTMP, lhs, rhs);
5001 __ Mfc1(dst, FTMP);
5002 __ Andi(dst, dst, 1);
5003 break;
5004 case kCondNE:
5005 __ CmpEqD(FTMP, lhs, rhs);
5006 __ Mfc1(dst, FTMP);
5007 __ Addiu(dst, dst, 1);
5008 break;
5009 case kCondLT:
5010 if (gt_bias) {
5011 __ CmpLtD(FTMP, lhs, rhs);
5012 } else {
5013 __ CmpUltD(FTMP, lhs, rhs);
5014 }
5015 __ Mfc1(dst, FTMP);
5016 __ Andi(dst, dst, 1);
5017 break;
5018 case kCondLE:
5019 if (gt_bias) {
5020 __ CmpLeD(FTMP, lhs, rhs);
5021 } else {
5022 __ CmpUleD(FTMP, lhs, rhs);
5023 }
5024 __ Mfc1(dst, FTMP);
5025 __ Andi(dst, dst, 1);
5026 break;
5027 case kCondGT:
5028 if (gt_bias) {
5029 __ CmpUltD(FTMP, rhs, lhs);
5030 } else {
5031 __ CmpLtD(FTMP, rhs, lhs);
5032 }
5033 __ Mfc1(dst, FTMP);
5034 __ Andi(dst, dst, 1);
5035 break;
5036 case kCondGE:
5037 if (gt_bias) {
5038 __ CmpUleD(FTMP, rhs, lhs);
5039 } else {
5040 __ CmpLeD(FTMP, rhs, lhs);
5041 }
5042 __ Mfc1(dst, FTMP);
5043 __ Andi(dst, dst, 1);
5044 break;
5045 default:
5046 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5047 UNREACHABLE();
5048 }
5049 } else {
5050 switch (cond) {
5051 case kCondEQ:
5052 __ CeqD(0, lhs, rhs);
5053 __ LoadConst32(dst, 1);
5054 __ Movf(dst, ZERO, 0);
5055 break;
5056 case kCondNE:
5057 __ CeqD(0, lhs, rhs);
5058 __ LoadConst32(dst, 1);
5059 __ Movt(dst, ZERO, 0);
5060 break;
5061 case kCondLT:
5062 if (gt_bias) {
5063 __ ColtD(0, lhs, rhs);
5064 } else {
5065 __ CultD(0, lhs, rhs);
5066 }
5067 __ LoadConst32(dst, 1);
5068 __ Movf(dst, ZERO, 0);
5069 break;
5070 case kCondLE:
5071 if (gt_bias) {
5072 __ ColeD(0, lhs, rhs);
5073 } else {
5074 __ CuleD(0, lhs, rhs);
5075 }
5076 __ LoadConst32(dst, 1);
5077 __ Movf(dst, ZERO, 0);
5078 break;
5079 case kCondGT:
5080 if (gt_bias) {
5081 __ CultD(0, rhs, lhs);
5082 } else {
5083 __ ColtD(0, rhs, lhs);
5084 }
5085 __ LoadConst32(dst, 1);
5086 __ Movf(dst, ZERO, 0);
5087 break;
5088 case kCondGE:
5089 if (gt_bias) {
5090 __ CuleD(0, rhs, lhs);
5091 } else {
5092 __ ColeD(0, rhs, lhs);
5093 }
5094 __ LoadConst32(dst, 1);
5095 __ Movf(dst, ZERO, 0);
5096 break;
5097 default:
5098 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5099 UNREACHABLE();
5100 }
5101 }
5102 }
5103}
5104
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005105bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5106 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005107 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005108 LocationSummary* input_locations,
5109 int cc) {
5110 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5111 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5112 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005113 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005114 switch (cond) {
5115 case kCondEQ:
5116 __ CeqS(cc, lhs, rhs);
5117 return false;
5118 case kCondNE:
5119 __ CeqS(cc, lhs, rhs);
5120 return true;
5121 case kCondLT:
5122 if (gt_bias) {
5123 __ ColtS(cc, lhs, rhs);
5124 } else {
5125 __ CultS(cc, lhs, rhs);
5126 }
5127 return false;
5128 case kCondLE:
5129 if (gt_bias) {
5130 __ ColeS(cc, lhs, rhs);
5131 } else {
5132 __ CuleS(cc, lhs, rhs);
5133 }
5134 return false;
5135 case kCondGT:
5136 if (gt_bias) {
5137 __ CultS(cc, rhs, lhs);
5138 } else {
5139 __ ColtS(cc, rhs, lhs);
5140 }
5141 return false;
5142 case kCondGE:
5143 if (gt_bias) {
5144 __ CuleS(cc, rhs, lhs);
5145 } else {
5146 __ ColeS(cc, rhs, lhs);
5147 }
5148 return false;
5149 default:
5150 LOG(FATAL) << "Unexpected non-floating-point condition";
5151 UNREACHABLE();
5152 }
5153 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005154 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005155 switch (cond) {
5156 case kCondEQ:
5157 __ CeqD(cc, lhs, rhs);
5158 return false;
5159 case kCondNE:
5160 __ CeqD(cc, lhs, rhs);
5161 return true;
5162 case kCondLT:
5163 if (gt_bias) {
5164 __ ColtD(cc, lhs, rhs);
5165 } else {
5166 __ CultD(cc, lhs, rhs);
5167 }
5168 return false;
5169 case kCondLE:
5170 if (gt_bias) {
5171 __ ColeD(cc, lhs, rhs);
5172 } else {
5173 __ CuleD(cc, lhs, rhs);
5174 }
5175 return false;
5176 case kCondGT:
5177 if (gt_bias) {
5178 __ CultD(cc, rhs, lhs);
5179 } else {
5180 __ ColtD(cc, rhs, lhs);
5181 }
5182 return false;
5183 case kCondGE:
5184 if (gt_bias) {
5185 __ CuleD(cc, rhs, lhs);
5186 } else {
5187 __ ColeD(cc, rhs, lhs);
5188 }
5189 return false;
5190 default:
5191 LOG(FATAL) << "Unexpected non-floating-point condition";
5192 UNREACHABLE();
5193 }
5194 }
5195}
5196
5197bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5198 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005200 LocationSummary* input_locations,
5201 FRegister dst) {
5202 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5203 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5204 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005206 switch (cond) {
5207 case kCondEQ:
5208 __ CmpEqS(dst, lhs, rhs);
5209 return false;
5210 case kCondNE:
5211 __ CmpEqS(dst, lhs, rhs);
5212 return true;
5213 case kCondLT:
5214 if (gt_bias) {
5215 __ CmpLtS(dst, lhs, rhs);
5216 } else {
5217 __ CmpUltS(dst, lhs, rhs);
5218 }
5219 return false;
5220 case kCondLE:
5221 if (gt_bias) {
5222 __ CmpLeS(dst, lhs, rhs);
5223 } else {
5224 __ CmpUleS(dst, lhs, rhs);
5225 }
5226 return false;
5227 case kCondGT:
5228 if (gt_bias) {
5229 __ CmpUltS(dst, rhs, lhs);
5230 } else {
5231 __ CmpLtS(dst, rhs, lhs);
5232 }
5233 return false;
5234 case kCondGE:
5235 if (gt_bias) {
5236 __ CmpUleS(dst, rhs, lhs);
5237 } else {
5238 __ CmpLeS(dst, rhs, lhs);
5239 }
5240 return false;
5241 default:
5242 LOG(FATAL) << "Unexpected non-floating-point condition";
5243 UNREACHABLE();
5244 }
5245 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005246 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005247 switch (cond) {
5248 case kCondEQ:
5249 __ CmpEqD(dst, lhs, rhs);
5250 return false;
5251 case kCondNE:
5252 __ CmpEqD(dst, lhs, rhs);
5253 return true;
5254 case kCondLT:
5255 if (gt_bias) {
5256 __ CmpLtD(dst, lhs, rhs);
5257 } else {
5258 __ CmpUltD(dst, lhs, rhs);
5259 }
5260 return false;
5261 case kCondLE:
5262 if (gt_bias) {
5263 __ CmpLeD(dst, lhs, rhs);
5264 } else {
5265 __ CmpUleD(dst, lhs, rhs);
5266 }
5267 return false;
5268 case kCondGT:
5269 if (gt_bias) {
5270 __ CmpUltD(dst, rhs, lhs);
5271 } else {
5272 __ CmpLtD(dst, rhs, lhs);
5273 }
5274 return false;
5275 case kCondGE:
5276 if (gt_bias) {
5277 __ CmpUleD(dst, rhs, lhs);
5278 } else {
5279 __ CmpLeD(dst, rhs, lhs);
5280 }
5281 return false;
5282 default:
5283 LOG(FATAL) << "Unexpected non-floating-point condition";
5284 UNREACHABLE();
5285 }
5286 }
5287}
5288
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005289void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5290 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005292 LocationSummary* locations,
5293 MipsLabel* label) {
5294 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5295 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5296 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005298 if (isR6) {
5299 switch (cond) {
5300 case kCondEQ:
5301 __ CmpEqS(FTMP, lhs, rhs);
5302 __ Bc1nez(FTMP, label);
5303 break;
5304 case kCondNE:
5305 __ CmpEqS(FTMP, lhs, rhs);
5306 __ Bc1eqz(FTMP, label);
5307 break;
5308 case kCondLT:
5309 if (gt_bias) {
5310 __ CmpLtS(FTMP, lhs, rhs);
5311 } else {
5312 __ CmpUltS(FTMP, lhs, rhs);
5313 }
5314 __ Bc1nez(FTMP, label);
5315 break;
5316 case kCondLE:
5317 if (gt_bias) {
5318 __ CmpLeS(FTMP, lhs, rhs);
5319 } else {
5320 __ CmpUleS(FTMP, lhs, rhs);
5321 }
5322 __ Bc1nez(FTMP, label);
5323 break;
5324 case kCondGT:
5325 if (gt_bias) {
5326 __ CmpUltS(FTMP, rhs, lhs);
5327 } else {
5328 __ CmpLtS(FTMP, rhs, lhs);
5329 }
5330 __ Bc1nez(FTMP, label);
5331 break;
5332 case kCondGE:
5333 if (gt_bias) {
5334 __ CmpUleS(FTMP, rhs, lhs);
5335 } else {
5336 __ CmpLeS(FTMP, rhs, lhs);
5337 }
5338 __ Bc1nez(FTMP, label);
5339 break;
5340 default:
5341 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005342 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005343 }
5344 } else {
5345 switch (cond) {
5346 case kCondEQ:
5347 __ CeqS(0, lhs, rhs);
5348 __ Bc1t(0, label);
5349 break;
5350 case kCondNE:
5351 __ CeqS(0, lhs, rhs);
5352 __ Bc1f(0, label);
5353 break;
5354 case kCondLT:
5355 if (gt_bias) {
5356 __ ColtS(0, lhs, rhs);
5357 } else {
5358 __ CultS(0, lhs, rhs);
5359 }
5360 __ Bc1t(0, label);
5361 break;
5362 case kCondLE:
5363 if (gt_bias) {
5364 __ ColeS(0, lhs, rhs);
5365 } else {
5366 __ CuleS(0, lhs, rhs);
5367 }
5368 __ Bc1t(0, label);
5369 break;
5370 case kCondGT:
5371 if (gt_bias) {
5372 __ CultS(0, rhs, lhs);
5373 } else {
5374 __ ColtS(0, rhs, lhs);
5375 }
5376 __ Bc1t(0, label);
5377 break;
5378 case kCondGE:
5379 if (gt_bias) {
5380 __ CuleS(0, rhs, lhs);
5381 } else {
5382 __ ColeS(0, rhs, lhs);
5383 }
5384 __ Bc1t(0, label);
5385 break;
5386 default:
5387 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005388 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005389 }
5390 }
5391 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005393 if (isR6) {
5394 switch (cond) {
5395 case kCondEQ:
5396 __ CmpEqD(FTMP, lhs, rhs);
5397 __ Bc1nez(FTMP, label);
5398 break;
5399 case kCondNE:
5400 __ CmpEqD(FTMP, lhs, rhs);
5401 __ Bc1eqz(FTMP, label);
5402 break;
5403 case kCondLT:
5404 if (gt_bias) {
5405 __ CmpLtD(FTMP, lhs, rhs);
5406 } else {
5407 __ CmpUltD(FTMP, lhs, rhs);
5408 }
5409 __ Bc1nez(FTMP, label);
5410 break;
5411 case kCondLE:
5412 if (gt_bias) {
5413 __ CmpLeD(FTMP, lhs, rhs);
5414 } else {
5415 __ CmpUleD(FTMP, lhs, rhs);
5416 }
5417 __ Bc1nez(FTMP, label);
5418 break;
5419 case kCondGT:
5420 if (gt_bias) {
5421 __ CmpUltD(FTMP, rhs, lhs);
5422 } else {
5423 __ CmpLtD(FTMP, rhs, lhs);
5424 }
5425 __ Bc1nez(FTMP, label);
5426 break;
5427 case kCondGE:
5428 if (gt_bias) {
5429 __ CmpUleD(FTMP, rhs, lhs);
5430 } else {
5431 __ CmpLeD(FTMP, rhs, lhs);
5432 }
5433 __ Bc1nez(FTMP, label);
5434 break;
5435 default:
5436 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005437 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005438 }
5439 } else {
5440 switch (cond) {
5441 case kCondEQ:
5442 __ CeqD(0, lhs, rhs);
5443 __ Bc1t(0, label);
5444 break;
5445 case kCondNE:
5446 __ CeqD(0, lhs, rhs);
5447 __ Bc1f(0, label);
5448 break;
5449 case kCondLT:
5450 if (gt_bias) {
5451 __ ColtD(0, lhs, rhs);
5452 } else {
5453 __ CultD(0, lhs, rhs);
5454 }
5455 __ Bc1t(0, label);
5456 break;
5457 case kCondLE:
5458 if (gt_bias) {
5459 __ ColeD(0, lhs, rhs);
5460 } else {
5461 __ CuleD(0, lhs, rhs);
5462 }
5463 __ Bc1t(0, label);
5464 break;
5465 case kCondGT:
5466 if (gt_bias) {
5467 __ CultD(0, rhs, lhs);
5468 } else {
5469 __ ColtD(0, rhs, lhs);
5470 }
5471 __ Bc1t(0, label);
5472 break;
5473 case kCondGE:
5474 if (gt_bias) {
5475 __ CuleD(0, rhs, lhs);
5476 } else {
5477 __ ColeD(0, rhs, lhs);
5478 }
5479 __ Bc1t(0, label);
5480 break;
5481 default:
5482 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005483 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005484 }
5485 }
5486 }
5487}
5488
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005489void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005490 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005491 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005492 MipsLabel* false_target) {
5493 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005494
David Brazdil0debae72015-11-12 18:37:00 +00005495 if (true_target == nullptr && false_target == nullptr) {
5496 // Nothing to do. The code always falls through.
5497 return;
5498 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005499 // Constant condition, statically compared against "true" (integer value 1).
5500 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005501 if (true_target != nullptr) {
5502 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005503 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005504 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005505 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005506 if (false_target != nullptr) {
5507 __ B(false_target);
5508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005509 }
David Brazdil0debae72015-11-12 18:37:00 +00005510 return;
5511 }
5512
5513 // The following code generates these patterns:
5514 // (1) true_target == nullptr && false_target != nullptr
5515 // - opposite condition true => branch to false_target
5516 // (2) true_target != nullptr && false_target == nullptr
5517 // - condition true => branch to true_target
5518 // (3) true_target != nullptr && false_target != nullptr
5519 // - condition true => branch to true_target
5520 // - branch to false_target
5521 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005522 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005523 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005524 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005525 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005526 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5527 } else {
5528 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5529 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005530 } else {
5531 // The condition instruction has not been materialized, use its inputs as
5532 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005533 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005535 LocationSummary* locations = cond->GetLocations();
5536 IfCondition if_cond = condition->GetCondition();
5537 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005538
David Brazdil0debae72015-11-12 18:37:00 +00005539 if (true_target == nullptr) {
5540 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005541 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005542 }
5543
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005544 switch (type) {
5545 default:
5546 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5547 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005549 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5550 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 case DataType::Type::kFloat32:
5552 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005553 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5554 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005555 }
5556 }
David Brazdil0debae72015-11-12 18:37:00 +00005557
5558 // If neither branch falls through (case 3), the conditional branch to `true_target`
5559 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5560 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561 __ B(false_target);
5562 }
5563}
5564
5565void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005566 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005567 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005568 locations->SetInAt(0, Location::RequiresRegister());
5569 }
5570}
5571
5572void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005573 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5574 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5575 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5576 nullptr : codegen_->GetLabelOf(true_successor);
5577 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5578 nullptr : codegen_->GetLabelOf(false_successor);
5579 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580}
5581
5582void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005583 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005584 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005585 InvokeRuntimeCallingConvention calling_convention;
5586 RegisterSet caller_saves = RegisterSet::Empty();
5587 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5588 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005589 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005590 locations->SetInAt(0, Location::RequiresRegister());
5591 }
5592}
5593
5594void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005595 SlowPathCodeMIPS* slow_path =
5596 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005597 GenerateTestAndBranch(deoptimize,
5598 /* condition_input_index */ 0,
5599 slow_path->GetEntryLabel(),
5600 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005601}
5602
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005603// This function returns true if a conditional move can be generated for HSelect.
5604// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5605// branches and regular moves.
5606//
5607// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5608//
5609// While determining feasibility of a conditional move and setting inputs/outputs
5610// are two distinct tasks, this function does both because they share quite a bit
5611// of common logic.
5612static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5613 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5614 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5615 HCondition* condition = cond->AsCondition();
5616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005617 DataType::Type cond_type =
5618 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5619 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005620
5621 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5622 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5623 bool is_true_value_zero_constant =
5624 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5625 bool is_false_value_zero_constant =
5626 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5627
5628 bool can_move_conditionally = false;
5629 bool use_const_for_false_in = false;
5630 bool use_const_for_true_in = false;
5631
5632 if (!cond->IsConstant()) {
5633 switch (cond_type) {
5634 default:
5635 switch (dst_type) {
5636 default:
5637 // Moving int on int condition.
5638 if (is_r6) {
5639 if (is_true_value_zero_constant) {
5640 // seleqz out_reg, false_reg, cond_reg
5641 can_move_conditionally = true;
5642 use_const_for_true_in = true;
5643 } else if (is_false_value_zero_constant) {
5644 // selnez out_reg, true_reg, cond_reg
5645 can_move_conditionally = true;
5646 use_const_for_false_in = true;
5647 } else if (materialized) {
5648 // Not materializing unmaterialized int conditions
5649 // to keep the instruction count low.
5650 // selnez AT, true_reg, cond_reg
5651 // seleqz TMP, false_reg, cond_reg
5652 // or out_reg, AT, TMP
5653 can_move_conditionally = true;
5654 }
5655 } else {
5656 // movn out_reg, true_reg/ZERO, cond_reg
5657 can_move_conditionally = true;
5658 use_const_for_true_in = is_true_value_zero_constant;
5659 }
5660 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005662 // Moving long on int condition.
5663 if (is_r6) {
5664 if (is_true_value_zero_constant) {
5665 // seleqz out_reg_lo, false_reg_lo, cond_reg
5666 // seleqz out_reg_hi, false_reg_hi, cond_reg
5667 can_move_conditionally = true;
5668 use_const_for_true_in = true;
5669 } else if (is_false_value_zero_constant) {
5670 // selnez out_reg_lo, true_reg_lo, cond_reg
5671 // selnez out_reg_hi, true_reg_hi, cond_reg
5672 can_move_conditionally = true;
5673 use_const_for_false_in = true;
5674 }
5675 // Other long conditional moves would generate 6+ instructions,
5676 // which is too many.
5677 } else {
5678 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5679 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5680 can_move_conditionally = true;
5681 use_const_for_true_in = is_true_value_zero_constant;
5682 }
5683 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005684 case DataType::Type::kFloat32:
5685 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005686 // Moving float/double on int condition.
5687 if (is_r6) {
5688 if (materialized) {
5689 // Not materializing unmaterialized int conditions
5690 // to keep the instruction count low.
5691 can_move_conditionally = true;
5692 if (is_true_value_zero_constant) {
5693 // sltu TMP, ZERO, cond_reg
5694 // mtc1 TMP, temp_cond_reg
5695 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5696 use_const_for_true_in = true;
5697 } else if (is_false_value_zero_constant) {
5698 // sltu TMP, ZERO, cond_reg
5699 // mtc1 TMP, temp_cond_reg
5700 // selnez.fmt out_reg, true_reg, temp_cond_reg
5701 use_const_for_false_in = true;
5702 } else {
5703 // sltu TMP, ZERO, cond_reg
5704 // mtc1 TMP, temp_cond_reg
5705 // sel.fmt temp_cond_reg, false_reg, true_reg
5706 // mov.fmt out_reg, temp_cond_reg
5707 }
5708 }
5709 } else {
5710 // movn.fmt out_reg, true_reg, cond_reg
5711 can_move_conditionally = true;
5712 }
5713 break;
5714 }
5715 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005717 // We don't materialize long comparison now
5718 // and use conditional branches instead.
5719 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kFloat32:
5721 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005722 switch (dst_type) {
5723 default:
5724 // Moving int on float/double condition.
5725 if (is_r6) {
5726 if (is_true_value_zero_constant) {
5727 // mfc1 TMP, temp_cond_reg
5728 // seleqz out_reg, false_reg, TMP
5729 can_move_conditionally = true;
5730 use_const_for_true_in = true;
5731 } else if (is_false_value_zero_constant) {
5732 // mfc1 TMP, temp_cond_reg
5733 // selnez out_reg, true_reg, TMP
5734 can_move_conditionally = true;
5735 use_const_for_false_in = true;
5736 } else {
5737 // mfc1 TMP, temp_cond_reg
5738 // selnez AT, true_reg, TMP
5739 // seleqz TMP, false_reg, TMP
5740 // or out_reg, AT, TMP
5741 can_move_conditionally = true;
5742 }
5743 } else {
5744 // movt out_reg, true_reg/ZERO, cc
5745 can_move_conditionally = true;
5746 use_const_for_true_in = is_true_value_zero_constant;
5747 }
5748 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005750 // Moving long on float/double condition.
5751 if (is_r6) {
5752 if (is_true_value_zero_constant) {
5753 // mfc1 TMP, temp_cond_reg
5754 // seleqz out_reg_lo, false_reg_lo, TMP
5755 // seleqz out_reg_hi, false_reg_hi, TMP
5756 can_move_conditionally = true;
5757 use_const_for_true_in = true;
5758 } else if (is_false_value_zero_constant) {
5759 // mfc1 TMP, temp_cond_reg
5760 // selnez out_reg_lo, true_reg_lo, TMP
5761 // selnez out_reg_hi, true_reg_hi, TMP
5762 can_move_conditionally = true;
5763 use_const_for_false_in = true;
5764 }
5765 // Other long conditional moves would generate 6+ instructions,
5766 // which is too many.
5767 } else {
5768 // movt out_reg_lo, true_reg_lo/ZERO, cc
5769 // movt out_reg_hi, true_reg_hi/ZERO, cc
5770 can_move_conditionally = true;
5771 use_const_for_true_in = is_true_value_zero_constant;
5772 }
5773 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 case DataType::Type::kFloat32:
5775 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005776 // Moving float/double on float/double condition.
5777 if (is_r6) {
5778 can_move_conditionally = true;
5779 if (is_true_value_zero_constant) {
5780 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5781 use_const_for_true_in = true;
5782 } else if (is_false_value_zero_constant) {
5783 // selnez.fmt out_reg, true_reg, temp_cond_reg
5784 use_const_for_false_in = true;
5785 } else {
5786 // sel.fmt temp_cond_reg, false_reg, true_reg
5787 // mov.fmt out_reg, temp_cond_reg
5788 }
5789 } else {
5790 // movt.fmt out_reg, true_reg, cc
5791 can_move_conditionally = true;
5792 }
5793 break;
5794 }
5795 break;
5796 }
5797 }
5798
5799 if (can_move_conditionally) {
5800 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5801 } else {
5802 DCHECK(!use_const_for_false_in);
5803 DCHECK(!use_const_for_true_in);
5804 }
5805
5806 if (locations_to_set != nullptr) {
5807 if (use_const_for_false_in) {
5808 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5809 } else {
5810 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005811 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005812 ? Location::RequiresFpuRegister()
5813 : Location::RequiresRegister());
5814 }
5815 if (use_const_for_true_in) {
5816 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5817 } else {
5818 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005820 ? Location::RequiresFpuRegister()
5821 : Location::RequiresRegister());
5822 }
5823 if (materialized) {
5824 locations_to_set->SetInAt(2, Location::RequiresRegister());
5825 }
5826 // On R6 we don't require the output to be the same as the
5827 // first input for conditional moves unlike on R2.
5828 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5829 if (is_out_same_as_first_in) {
5830 locations_to_set->SetOut(Location::SameAsFirstInput());
5831 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005833 ? Location::RequiresFpuRegister()
5834 : Location::RequiresRegister());
5835 }
5836 }
5837
5838 return can_move_conditionally;
5839}
5840
5841void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5842 LocationSummary* locations = select->GetLocations();
5843 Location dst = locations->Out();
5844 Location src = locations->InAt(1);
5845 Register src_reg = ZERO;
5846 Register src_reg_high = ZERO;
5847 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5848 Register cond_reg = TMP;
5849 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005851 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005852 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005853
5854 if (IsBooleanValueOrMaterializedCondition(cond)) {
5855 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5856 } else {
5857 HCondition* condition = cond->AsCondition();
5858 LocationSummary* cond_locations = cond->GetLocations();
5859 IfCondition if_cond = condition->GetCondition();
5860 cond_type = condition->InputAt(0)->GetType();
5861 switch (cond_type) {
5862 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005864 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5865 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 case DataType::Type::kFloat32:
5867 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005868 cond_inverted = MaterializeFpCompareR2(if_cond,
5869 condition->IsGtBias(),
5870 cond_type,
5871 cond_locations,
5872 cond_cc);
5873 break;
5874 }
5875 }
5876
5877 DCHECK(dst.Equals(locations->InAt(0)));
5878 if (src.IsRegister()) {
5879 src_reg = src.AsRegister<Register>();
5880 } else if (src.IsRegisterPair()) {
5881 src_reg = src.AsRegisterPairLow<Register>();
5882 src_reg_high = src.AsRegisterPairHigh<Register>();
5883 } else if (src.IsConstant()) {
5884 DCHECK(src.GetConstant()->IsZeroBitPattern());
5885 }
5886
5887 switch (cond_type) {
5888 default:
5889 switch (dst_type) {
5890 default:
5891 if (cond_inverted) {
5892 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5893 } else {
5894 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5895 }
5896 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005897 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005898 if (cond_inverted) {
5899 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5900 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5901 } else {
5902 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5903 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5904 }
5905 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005906 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005907 if (cond_inverted) {
5908 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5909 } else {
5910 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5911 }
5912 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005914 if (cond_inverted) {
5915 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5916 } else {
5917 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5918 }
5919 break;
5920 }
5921 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005922 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005923 LOG(FATAL) << "Unreachable";
5924 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005925 case DataType::Type::kFloat32:
5926 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005927 switch (dst_type) {
5928 default:
5929 if (cond_inverted) {
5930 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5931 } else {
5932 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5933 }
5934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005936 if (cond_inverted) {
5937 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5938 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5939 } else {
5940 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5941 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5942 }
5943 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005945 if (cond_inverted) {
5946 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5947 } else {
5948 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5949 }
5950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005952 if (cond_inverted) {
5953 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5954 } else {
5955 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5956 }
5957 break;
5958 }
5959 break;
5960 }
5961}
5962
5963void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5964 LocationSummary* locations = select->GetLocations();
5965 Location dst = locations->Out();
5966 Location false_src = locations->InAt(0);
5967 Location true_src = locations->InAt(1);
5968 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5969 Register cond_reg = TMP;
5970 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005972 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005973 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005974
5975 if (IsBooleanValueOrMaterializedCondition(cond)) {
5976 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5977 } else {
5978 HCondition* condition = cond->AsCondition();
5979 LocationSummary* cond_locations = cond->GetLocations();
5980 IfCondition if_cond = condition->GetCondition();
5981 cond_type = condition->InputAt(0)->GetType();
5982 switch (cond_type) {
5983 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005985 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5986 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 case DataType::Type::kFloat32:
5988 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005989 cond_inverted = MaterializeFpCompareR6(if_cond,
5990 condition->IsGtBias(),
5991 cond_type,
5992 cond_locations,
5993 fcond_reg);
5994 break;
5995 }
5996 }
5997
5998 if (true_src.IsConstant()) {
5999 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6000 }
6001 if (false_src.IsConstant()) {
6002 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6003 }
6004
6005 switch (dst_type) {
6006 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006007 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006008 __ Mfc1(cond_reg, fcond_reg);
6009 }
6010 if (true_src.IsConstant()) {
6011 if (cond_inverted) {
6012 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6013 } else {
6014 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6015 }
6016 } else if (false_src.IsConstant()) {
6017 if (cond_inverted) {
6018 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6019 } else {
6020 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6021 }
6022 } else {
6023 DCHECK_NE(cond_reg, AT);
6024 if (cond_inverted) {
6025 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6026 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6027 } else {
6028 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6029 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6030 }
6031 __ Or(dst.AsRegister<Register>(), AT, TMP);
6032 }
6033 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 case DataType::Type::kInt64: {
6035 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006036 __ Mfc1(cond_reg, fcond_reg);
6037 }
6038 Register dst_lo = dst.AsRegisterPairLow<Register>();
6039 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6040 if (true_src.IsConstant()) {
6041 Register src_lo = false_src.AsRegisterPairLow<Register>();
6042 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6043 if (cond_inverted) {
6044 __ Selnez(dst_lo, src_lo, cond_reg);
6045 __ Selnez(dst_hi, src_hi, cond_reg);
6046 } else {
6047 __ Seleqz(dst_lo, src_lo, cond_reg);
6048 __ Seleqz(dst_hi, src_hi, cond_reg);
6049 }
6050 } else {
6051 DCHECK(false_src.IsConstant());
6052 Register src_lo = true_src.AsRegisterPairLow<Register>();
6053 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6054 if (cond_inverted) {
6055 __ Seleqz(dst_lo, src_lo, cond_reg);
6056 __ Seleqz(dst_hi, src_hi, cond_reg);
6057 } else {
6058 __ Selnez(dst_lo, src_lo, cond_reg);
6059 __ Selnez(dst_hi, src_hi, cond_reg);
6060 }
6061 }
6062 break;
6063 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006064 case DataType::Type::kFloat32: {
6065 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006066 // sel*.fmt tests bit 0 of the condition register, account for that.
6067 __ Sltu(TMP, ZERO, cond_reg);
6068 __ Mtc1(TMP, fcond_reg);
6069 }
6070 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6071 if (true_src.IsConstant()) {
6072 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6073 if (cond_inverted) {
6074 __ SelnezS(dst_reg, src_reg, fcond_reg);
6075 } else {
6076 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6077 }
6078 } else if (false_src.IsConstant()) {
6079 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6080 if (cond_inverted) {
6081 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6082 } else {
6083 __ SelnezS(dst_reg, src_reg, fcond_reg);
6084 }
6085 } else {
6086 if (cond_inverted) {
6087 __ SelS(fcond_reg,
6088 true_src.AsFpuRegister<FRegister>(),
6089 false_src.AsFpuRegister<FRegister>());
6090 } else {
6091 __ SelS(fcond_reg,
6092 false_src.AsFpuRegister<FRegister>(),
6093 true_src.AsFpuRegister<FRegister>());
6094 }
6095 __ MovS(dst_reg, fcond_reg);
6096 }
6097 break;
6098 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006099 case DataType::Type::kFloat64: {
6100 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006101 // sel*.fmt tests bit 0 of the condition register, account for that.
6102 __ Sltu(TMP, ZERO, cond_reg);
6103 __ Mtc1(TMP, fcond_reg);
6104 }
6105 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6106 if (true_src.IsConstant()) {
6107 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6108 if (cond_inverted) {
6109 __ SelnezD(dst_reg, src_reg, fcond_reg);
6110 } else {
6111 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6112 }
6113 } else if (false_src.IsConstant()) {
6114 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6115 if (cond_inverted) {
6116 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6117 } else {
6118 __ SelnezD(dst_reg, src_reg, fcond_reg);
6119 }
6120 } else {
6121 if (cond_inverted) {
6122 __ SelD(fcond_reg,
6123 true_src.AsFpuRegister<FRegister>(),
6124 false_src.AsFpuRegister<FRegister>());
6125 } else {
6126 __ SelD(fcond_reg,
6127 false_src.AsFpuRegister<FRegister>(),
6128 true_src.AsFpuRegister<FRegister>());
6129 }
6130 __ MovD(dst_reg, fcond_reg);
6131 }
6132 break;
6133 }
6134 }
6135}
6136
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006137void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006138 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006139 LocationSummary(flag, LocationSummary::kNoCall);
6140 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006141}
6142
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006143void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6144 __ LoadFromOffset(kLoadWord,
6145 flag->GetLocations()->Out().AsRegister<Register>(),
6146 SP,
6147 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006148}
6149
David Brazdil74eb1b22015-12-14 11:44:01 +00006150void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006151 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006152 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006153}
6154
6155void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006156 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6157 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6158 if (is_r6) {
6159 GenConditionalMoveR6(select);
6160 } else {
6161 GenConditionalMoveR2(select);
6162 }
6163 } else {
6164 LocationSummary* locations = select->GetLocations();
6165 MipsLabel false_target;
6166 GenerateTestAndBranch(select,
6167 /* condition_input_index */ 2,
6168 /* true_target */ nullptr,
6169 &false_target);
6170 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6171 __ Bind(&false_target);
6172 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006173}
6174
David Srbecky0cf44932015-12-09 14:09:59 +00006175void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006176 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006177}
6178
David Srbeckyd28f4a02016-03-14 17:14:24 +00006179void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6180 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006181}
6182
6183void CodeGeneratorMIPS::GenerateNop() {
6184 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006185}
6186
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006187void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 DataType::Type field_type = field_info.GetFieldType();
6189 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006190 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006191 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006193 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006194 instruction,
6195 generate_volatile
6196 ? LocationSummary::kCallOnMainOnly
6197 : (object_field_get_with_read_barrier
6198 ? LocationSummary::kCallOnSlowPath
6199 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006200
Alexey Frunzec61c0762017-04-10 13:54:23 -07006201 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6203 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006204 locations->SetInAt(0, Location::RequiresRegister());
6205 if (generate_volatile) {
6206 InvokeRuntimeCallingConvention calling_convention;
6207 // need A0 to hold base + offset
6208 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006209 if (field_type == DataType::Type::kInt64) {
6210 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006211 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006212 // Use Location::Any() to prevent situations when running out of available fp registers.
6213 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006216 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6217 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6218 }
6219 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 locations->SetOut(Location::RequiresFpuRegister());
6222 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006223 // The output overlaps in the case of an object field get with
6224 // read barriers enabled: we do not want the move to overwrite the
6225 // object's location, as we need it to emit the read barrier.
6226 locations->SetOut(Location::RequiresRegister(),
6227 object_field_get_with_read_barrier
6228 ? Location::kOutputOverlap
6229 : Location::kNoOutputOverlap);
6230 }
6231 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6232 // We need a temporary register for the read barrier marking slow
6233 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006234 if (!kBakerReadBarrierThunksEnableForFields) {
6235 locations->AddTemp(Location::RequiresRegister());
6236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006237 }
6238 }
6239}
6240
6241void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6242 const FieldInfo& field_info,
6243 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006244 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6245 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006246 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006247 Location obj_loc = locations->InAt(0);
6248 Register obj = obj_loc.AsRegister<Register>();
6249 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250 LoadOperandType load_type = kLoadUnsignedByte;
6251 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006252 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006253 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006254
6255 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006256 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006257 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006258 load_type = kLoadUnsignedByte;
6259 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006261 load_type = kLoadSignedByte;
6262 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006264 load_type = kLoadUnsignedHalfword;
6265 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006266 case DataType::Type::kInt16:
6267 load_type = kLoadSignedHalfword;
6268 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 case DataType::Type::kInt32:
6270 case DataType::Type::kFloat32:
6271 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006272 load_type = kLoadWord;
6273 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006274 case DataType::Type::kInt64:
6275 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276 load_type = kLoadDoubleword;
6277 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006279 LOG(FATAL) << "Unreachable type " << type;
6280 UNREACHABLE();
6281 }
6282
6283 if (is_volatile && load_type == kLoadDoubleword) {
6284 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006285 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006287 __ LoadFromOffset(kLoadWord,
6288 ZERO,
6289 locations->GetTemp(0).AsRegister<Register>(),
6290 0,
6291 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006292 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006295 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006296 if (dst_loc.IsFpuRegister()) {
6297 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006298 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006299 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006300 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006301 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006302 __ StoreToOffset(kStoreWord,
6303 locations->GetTemp(1).AsRegister<Register>(),
6304 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006305 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006306 __ StoreToOffset(kStoreWord,
6307 locations->GetTemp(2).AsRegister<Register>(),
6308 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006309 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006310 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006311 }
6312 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006313 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006314 // /* HeapReference<Object> */ dst = *(obj + offset)
6315 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006316 Location temp_loc =
6317 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006318 // Note that a potential implicit null check is handled in this
6319 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6320 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6321 dst_loc,
6322 obj,
6323 offset,
6324 temp_loc,
6325 /* needs_null_check */ true);
6326 if (is_volatile) {
6327 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6328 }
6329 } else {
6330 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6331 if (is_volatile) {
6332 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6333 }
6334 // If read barriers are enabled, emit read barriers other than
6335 // Baker's using a slow path (and also unpoison the loaded
6336 // reference, if heap poisoning is enabled).
6337 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6338 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006340 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006342 DCHECK(dst_loc.IsRegisterPair());
6343 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006344 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006345 DCHECK(dst_loc.IsRegister());
6346 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006347 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006348 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006349 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006350 DCHECK(dst_loc.IsFpuRegister());
6351 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006353 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006354 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006355 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006356 }
6357 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006358 }
6359
Alexey Frunze15958152017-02-09 19:08:30 -08006360 // Memory barriers, in the case of references, are handled in the
6361 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006363 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6364 }
6365}
6366
6367void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 DataType::Type field_type = field_info.GetFieldType();
6369 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006371 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006372 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006373
6374 locations->SetInAt(0, Location::RequiresRegister());
6375 if (generate_volatile) {
6376 InvokeRuntimeCallingConvention calling_convention;
6377 // need A0 to hold base + offset
6378 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 locations->SetInAt(1, Location::RegisterPairLocation(
6381 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6382 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006383 // Use Location::Any() to prevent situations when running out of available fp registers.
6384 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 // Pass FP parameters in core registers.
6386 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6387 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6388 }
6389 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006391 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006393 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 }
6395 }
6396}
6397
6398void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6399 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006400 uint32_t dex_pc,
6401 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006403 LocationSummary* locations = instruction->GetLocations();
6404 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006405 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006406 StoreOperandType store_type = kStoreByte;
6407 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006408 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006409 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006410 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006411
6412 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006414 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006415 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006416 store_type = kStoreByte;
6417 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006418 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006419 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420 store_type = kStoreHalfword;
6421 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006422 case DataType::Type::kInt32:
6423 case DataType::Type::kFloat32:
6424 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425 store_type = kStoreWord;
6426 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006427 case DataType::Type::kInt64:
6428 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006429 store_type = kStoreDoubleword;
6430 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006431 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006432 LOG(FATAL) << "Unreachable type " << type;
6433 UNREACHABLE();
6434 }
6435
6436 if (is_volatile) {
6437 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6438 }
6439
6440 if (is_volatile && store_type == kStoreDoubleword) {
6441 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006442 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006443 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006444 __ LoadFromOffset(kLoadWord,
6445 ZERO,
6446 locations->GetTemp(0).AsRegister<Register>(),
6447 0,
6448 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006451 if (value_location.IsFpuRegister()) {
6452 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6453 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006454 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006455 value_location.AsFpuRegister<FRegister>());
6456 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006457 __ LoadFromOffset(kLoadWord,
6458 locations->GetTemp(1).AsRegister<Register>(),
6459 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006460 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006461 __ LoadFromOffset(kLoadWord,
6462 locations->GetTemp(2).AsRegister<Register>(),
6463 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006464 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006465 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006466 DCHECK(value_location.IsConstant());
6467 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6468 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006469 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6470 locations->GetTemp(1).AsRegister<Register>(),
6471 value);
6472 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006473 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006474 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006475 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6476 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006477 if (value_location.IsConstant()) {
6478 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6479 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006481 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006482 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006483 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006484 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006485 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006486 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006487 if (kPoisonHeapReferences && needs_write_barrier) {
6488 // Note that in the case where `value` is a null reference,
6489 // we do not enter this block, as a null reference does not
6490 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006491 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006492 __ PoisonHeapReference(TMP, src);
6493 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6494 } else {
6495 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6496 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006497 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006498 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006500 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006501 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006502 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006503 }
6504 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006505 }
6506
Alexey Frunzec061de12017-02-14 13:27:23 -08006507 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006508 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006509 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006510 }
6511
6512 if (is_volatile) {
6513 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6514 }
6515}
6516
6517void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6518 HandleFieldGet(instruction, instruction->GetFieldInfo());
6519}
6520
6521void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6522 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6523}
6524
6525void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6526 HandleFieldSet(instruction, instruction->GetFieldInfo());
6527}
6528
6529void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006530 HandleFieldSet(instruction,
6531 instruction->GetFieldInfo(),
6532 instruction->GetDexPc(),
6533 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534}
6535
Alexey Frunze15958152017-02-09 19:08:30 -08006536void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6537 HInstruction* instruction,
6538 Location out,
6539 uint32_t offset,
6540 Location maybe_temp,
6541 ReadBarrierOption read_barrier_option) {
6542 Register out_reg = out.AsRegister<Register>();
6543 if (read_barrier_option == kWithReadBarrier) {
6544 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006545 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6546 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6547 }
Alexey Frunze15958152017-02-09 19:08:30 -08006548 if (kUseBakerReadBarrier) {
6549 // Load with fast path based Baker's read barrier.
6550 // /* HeapReference<Object> */ out = *(out + offset)
6551 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6552 out,
6553 out_reg,
6554 offset,
6555 maybe_temp,
6556 /* needs_null_check */ false);
6557 } else {
6558 // Load with slow path based read barrier.
6559 // Save the value of `out` into `maybe_temp` before overwriting it
6560 // in the following move operation, as we will need it for the
6561 // read barrier below.
6562 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6563 // /* HeapReference<Object> */ out = *(out + offset)
6564 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6565 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6566 }
6567 } else {
6568 // Plain load with no read barrier.
6569 // /* HeapReference<Object> */ out = *(out + offset)
6570 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6571 __ MaybeUnpoisonHeapReference(out_reg);
6572 }
6573}
6574
6575void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6576 HInstruction* instruction,
6577 Location out,
6578 Location obj,
6579 uint32_t offset,
6580 Location maybe_temp,
6581 ReadBarrierOption read_barrier_option) {
6582 Register out_reg = out.AsRegister<Register>();
6583 Register obj_reg = obj.AsRegister<Register>();
6584 if (read_barrier_option == kWithReadBarrier) {
6585 CHECK(kEmitCompilerReadBarrier);
6586 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006587 if (!kBakerReadBarrierThunksEnableForFields) {
6588 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6589 }
Alexey Frunze15958152017-02-09 19:08:30 -08006590 // Load with fast path based Baker's read barrier.
6591 // /* HeapReference<Object> */ out = *(obj + offset)
6592 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6593 out,
6594 obj_reg,
6595 offset,
6596 maybe_temp,
6597 /* needs_null_check */ false);
6598 } else {
6599 // Load with slow path based read barrier.
6600 // /* HeapReference<Object> */ out = *(obj + offset)
6601 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6602 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6603 }
6604 } else {
6605 // Plain load with no read barrier.
6606 // /* HeapReference<Object> */ out = *(obj + offset)
6607 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6608 __ MaybeUnpoisonHeapReference(out_reg);
6609 }
6610}
6611
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006612static inline int GetBakerMarkThunkNumber(Register reg) {
6613 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6614 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6615 return reg - V0;
6616 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6617 return 14 + (reg - S2);
6618 } else if (reg == FP) { // One more.
6619 return 20;
6620 }
6621 LOG(FATAL) << "Unexpected register " << reg;
6622 UNREACHABLE();
6623}
6624
6625static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6626 int num = GetBakerMarkThunkNumber(reg) +
6627 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6628 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6629}
6630
6631static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6632 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6633 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6634}
6635
Alexey Frunze15958152017-02-09 19:08:30 -08006636void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6637 Location root,
6638 Register obj,
6639 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006640 ReadBarrierOption read_barrier_option,
6641 MipsLabel* label_low) {
6642 bool reordering;
6643 if (label_low != nullptr) {
6644 DCHECK_EQ(offset, 0x5678u);
6645 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006646 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006647 if (read_barrier_option == kWithReadBarrier) {
6648 DCHECK(kEmitCompilerReadBarrier);
6649 if (kUseBakerReadBarrier) {
6650 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6651 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006652 if (kBakerReadBarrierThunksEnableForGcRoots) {
6653 // Note that we do not actually check the value of `GetIsGcMarking()`
6654 // to decide whether to mark the loaded GC root or not. Instead, we
6655 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6656 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6657 // vice versa.
6658 //
6659 // We use thunks for the slow path. That thunk checks the reference
6660 // and jumps to the entrypoint if needed.
6661 //
6662 // temp = Thread::Current()->pReadBarrierMarkReg00
6663 // // AKA &art_quick_read_barrier_mark_introspection.
6664 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6665 // if (temp != nullptr) {
6666 // temp = &gc_root_thunk<root_reg>
6667 // root = temp(root)
6668 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006669
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006670 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6671 const int32_t entry_point_offset =
6672 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6673 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6674 int16_t offset_low = Low16Bits(offset);
6675 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6676 // extension in lw.
6677 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6678 Register base = short_offset ? obj : TMP;
6679 // Loading the entrypoint does not require a load acquire since it is only changed when
6680 // threads are suspended or running a checkpoint.
6681 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6682 reordering = __ SetReorder(false);
6683 if (!short_offset) {
6684 DCHECK(!label_low);
6685 __ AddUpper(base, obj, offset_high);
6686 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006687 MipsLabel skip_call;
6688 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006689 if (label_low != nullptr) {
6690 DCHECK(short_offset);
6691 __ Bind(label_low);
6692 }
6693 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6694 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6695 // in delay slot.
6696 if (isR6) {
6697 __ Jialc(T9, thunk_disp);
6698 } else {
6699 __ Addiu(T9, T9, thunk_disp);
6700 __ Jalr(T9);
6701 __ Nop();
6702 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006703 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006704 __ SetReorder(reordering);
6705 } else {
6706 // Note that we do not actually check the value of `GetIsGcMarking()`
6707 // to decide whether to mark the loaded GC root or not. Instead, we
6708 // load into `temp` (T9) the read barrier mark entry point corresponding
6709 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6710 // is false, and vice versa.
6711 //
6712 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6713 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6714 // if (temp != null) {
6715 // root = temp(root)
6716 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006717
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006718 if (label_low != nullptr) {
6719 reordering = __ SetReorder(false);
6720 __ Bind(label_low);
6721 }
6722 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6723 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6724 if (label_low != nullptr) {
6725 __ SetReorder(reordering);
6726 }
6727 static_assert(
6728 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6729 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6730 "have different sizes.");
6731 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6732 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6733 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006734
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006735 // Slow path marking the GC root `root`.
6736 Location temp = Location::RegisterLocation(T9);
6737 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006738 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006739 instruction,
6740 root,
6741 /*entrypoint*/ temp);
6742 codegen_->AddSlowPath(slow_path);
6743
6744 const int32_t entry_point_offset =
6745 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6746 // Loading the entrypoint does not require a load acquire since it is only changed when
6747 // threads are suspended or running a checkpoint.
6748 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6749 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6750 __ Bind(slow_path->GetExitLabel());
6751 }
Alexey Frunze15958152017-02-09 19:08:30 -08006752 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006753 if (label_low != nullptr) {
6754 reordering = __ SetReorder(false);
6755 __ Bind(label_low);
6756 }
Alexey Frunze15958152017-02-09 19:08:30 -08006757 // GC root loaded through a slow path for read barriers other
6758 // than Baker's.
6759 // /* GcRoot<mirror::Object>* */ root = obj + offset
6760 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006761 if (label_low != nullptr) {
6762 __ SetReorder(reordering);
6763 }
Alexey Frunze15958152017-02-09 19:08:30 -08006764 // /* mirror::Object* */ root = root->Read()
6765 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6766 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006767 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006768 if (label_low != nullptr) {
6769 reordering = __ SetReorder(false);
6770 __ Bind(label_low);
6771 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006772 // Plain GC root load with no read barrier.
6773 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6774 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6775 // Note that GC roots are not affected by heap poisoning, thus we
6776 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006777 if (label_low != nullptr) {
6778 __ SetReorder(reordering);
6779 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006780 }
6781}
6782
Alexey Frunze15958152017-02-09 19:08:30 -08006783void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6784 Location ref,
6785 Register obj,
6786 uint32_t offset,
6787 Location temp,
6788 bool needs_null_check) {
6789 DCHECK(kEmitCompilerReadBarrier);
6790 DCHECK(kUseBakerReadBarrier);
6791
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006792 if (kBakerReadBarrierThunksEnableForFields) {
6793 // Note that we do not actually check the value of `GetIsGcMarking()`
6794 // to decide whether to mark the loaded reference or not. Instead, we
6795 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6796 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6797 // vice versa.
6798 //
6799 // We use thunks for the slow path. That thunk checks the reference
6800 // and jumps to the entrypoint if needed. If the holder is not gray,
6801 // it issues a load-load memory barrier and returns to the original
6802 // reference load.
6803 //
6804 // temp = Thread::Current()->pReadBarrierMarkReg00
6805 // // AKA &art_quick_read_barrier_mark_introspection.
6806 // if (temp != nullptr) {
6807 // temp = &field_array_thunk<holder_reg>
6808 // temp()
6809 // }
6810 // not_gray_return_address:
6811 // // If the offset is too large to fit into the lw instruction, we
6812 // // use an adjusted base register (TMP) here. This register
6813 // // receives bits 16 ... 31 of the offset before the thunk invocation
6814 // // and the thunk benefits from it.
6815 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6816 // gray_return_address:
6817
6818 DCHECK(temp.IsInvalid());
6819 bool isR6 = GetInstructionSetFeatures().IsR6();
6820 int16_t offset_low = Low16Bits(offset);
6821 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6822 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6823 bool reordering = __ SetReorder(false);
6824 const int32_t entry_point_offset =
6825 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6826 // There may have or may have not been a null check if the field offset is smaller than
6827 // the page size.
6828 // There must've been a null check in case it's actually a load from an array.
6829 // We will, however, perform an explicit null check in the thunk as it's easier to
6830 // do it than not.
6831 if (instruction->IsArrayGet()) {
6832 DCHECK(!needs_null_check);
6833 }
6834 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6835 // Loading the entrypoint does not require a load acquire since it is only changed when
6836 // threads are suspended or running a checkpoint.
6837 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6838 Register ref_reg = ref.AsRegister<Register>();
6839 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006840 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006841 if (short_offset) {
6842 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006843 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006844 __ Nop(); // In forbidden slot.
6845 __ Jialc(T9, thunk_disp);
6846 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006847 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006848 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6849 __ Jalr(T9);
6850 __ Nop(); // In delay slot.
6851 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006852 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006853 } else {
6854 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006855 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006856 __ Aui(base, obj, offset_high); // In delay slot.
6857 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006858 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006859 } else {
6860 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006861 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006862 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6863 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006864 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006865 __ Addu(base, base, obj); // In delay slot.
6866 }
6867 }
6868 // /* HeapReference<Object> */ ref = *(obj + offset)
6869 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6870 if (needs_null_check) {
6871 MaybeRecordImplicitNullCheck(instruction);
6872 }
6873 __ MaybeUnpoisonHeapReference(ref_reg);
6874 __ SetReorder(reordering);
6875 return;
6876 }
6877
Alexey Frunze15958152017-02-09 19:08:30 -08006878 // /* HeapReference<Object> */ ref = *(obj + offset)
6879 Location no_index = Location::NoLocation();
6880 ScaleFactor no_scale_factor = TIMES_1;
6881 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6882 ref,
6883 obj,
6884 offset,
6885 no_index,
6886 no_scale_factor,
6887 temp,
6888 needs_null_check);
6889}
6890
6891void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6892 Location ref,
6893 Register obj,
6894 uint32_t data_offset,
6895 Location index,
6896 Location temp,
6897 bool needs_null_check) {
6898 DCHECK(kEmitCompilerReadBarrier);
6899 DCHECK(kUseBakerReadBarrier);
6900
6901 static_assert(
6902 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6903 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904 ScaleFactor scale_factor = TIMES_4;
6905
6906 if (kBakerReadBarrierThunksEnableForArrays) {
6907 // Note that we do not actually check the value of `GetIsGcMarking()`
6908 // to decide whether to mark the loaded reference or not. Instead, we
6909 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6910 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6911 // vice versa.
6912 //
6913 // We use thunks for the slow path. That thunk checks the reference
6914 // and jumps to the entrypoint if needed. If the holder is not gray,
6915 // it issues a load-load memory barrier and returns to the original
6916 // reference load.
6917 //
6918 // temp = Thread::Current()->pReadBarrierMarkReg00
6919 // // AKA &art_quick_read_barrier_mark_introspection.
6920 // if (temp != nullptr) {
6921 // temp = &field_array_thunk<holder_reg>
6922 // temp()
6923 // }
6924 // not_gray_return_address:
6925 // // The element address is pre-calculated in the TMP register before the
6926 // // thunk invocation and the thunk benefits from it.
6927 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6928 // gray_return_address:
6929
6930 DCHECK(temp.IsInvalid());
6931 DCHECK(index.IsValid());
6932 bool reordering = __ SetReorder(false);
6933 const int32_t entry_point_offset =
6934 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6935 // We will not do the explicit null check in the thunk as some form of a null check
6936 // must've been done earlier.
6937 DCHECK(!needs_null_check);
6938 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6939 // Loading the entrypoint does not require a load acquire since it is only changed when
6940 // threads are suspended or running a checkpoint.
6941 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6942 Register ref_reg = ref.AsRegister<Register>();
6943 Register index_reg = index.IsRegisterPair()
6944 ? index.AsRegisterPairLow<Register>()
6945 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006946 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006947 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006948 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006949 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6950 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006951 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006952 } else {
6953 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006954 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006955 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6956 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006957 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006958 __ Addu(TMP, TMP, obj); // In delay slot.
6959 }
6960 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6961 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6962 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6963 __ MaybeUnpoisonHeapReference(ref_reg);
6964 __ SetReorder(reordering);
6965 return;
6966 }
6967
Alexey Frunze15958152017-02-09 19:08:30 -08006968 // /* HeapReference<Object> */ ref =
6969 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006970 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6971 ref,
6972 obj,
6973 data_offset,
6974 index,
6975 scale_factor,
6976 temp,
6977 needs_null_check);
6978}
6979
6980void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6981 Location ref,
6982 Register obj,
6983 uint32_t offset,
6984 Location index,
6985 ScaleFactor scale_factor,
6986 Location temp,
6987 bool needs_null_check,
6988 bool always_update_field) {
6989 DCHECK(kEmitCompilerReadBarrier);
6990 DCHECK(kUseBakerReadBarrier);
6991
6992 // In slow path based read barriers, the read barrier call is
6993 // inserted after the original load. However, in fast path based
6994 // Baker's read barriers, we need to perform the load of
6995 // mirror::Object::monitor_ *before* the original reference load.
6996 // This load-load ordering is required by the read barrier.
6997 // The fast path/slow path (for Baker's algorithm) should look like:
6998 //
6999 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7000 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7001 // HeapReference<Object> ref = *src; // Original reference load.
7002 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7003 // if (is_gray) {
7004 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7005 // }
7006 //
7007 // Note: the original implementation in ReadBarrier::Barrier is
7008 // slightly more complex as it performs additional checks that we do
7009 // not do here for performance reasons.
7010
7011 Register ref_reg = ref.AsRegister<Register>();
7012 Register temp_reg = temp.AsRegister<Register>();
7013 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7014
7015 // /* int32_t */ monitor = obj->monitor_
7016 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7017 if (needs_null_check) {
7018 MaybeRecordImplicitNullCheck(instruction);
7019 }
7020 // /* LockWord */ lock_word = LockWord(monitor)
7021 static_assert(sizeof(LockWord) == sizeof(int32_t),
7022 "art::LockWord and int32_t have different sizes.");
7023
7024 __ Sync(0); // Barrier to prevent load-load reordering.
7025
7026 // The actual reference load.
7027 if (index.IsValid()) {
7028 // Load types involving an "index": ArrayGet,
7029 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7030 // intrinsics.
7031 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7032 if (index.IsConstant()) {
7033 size_t computed_offset =
7034 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7035 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7036 } else {
7037 // Handle the special case of the
7038 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7039 // intrinsics, which use a register pair as index ("long
7040 // offset"), of which only the low part contains data.
7041 Register index_reg = index.IsRegisterPair()
7042 ? index.AsRegisterPairLow<Register>()
7043 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007044 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007045 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7046 }
7047 } else {
7048 // /* HeapReference<Object> */ ref = *(obj + offset)
7049 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7050 }
7051
7052 // Object* ref = ref_addr->AsMirrorPtr()
7053 __ MaybeUnpoisonHeapReference(ref_reg);
7054
7055 // Slow path marking the object `ref` when it is gray.
7056 SlowPathCodeMIPS* slow_path;
7057 if (always_update_field) {
7058 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7059 // of the form `obj + field_offset`, where `obj` is a register and
7060 // `field_offset` is a register pair (of which only the lower half
7061 // is used). Thus `offset` and `scale_factor` above are expected
7062 // to be null in this code path.
7063 DCHECK_EQ(offset, 0u);
7064 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007065 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007066 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7067 ref,
7068 obj,
7069 /* field_offset */ index,
7070 temp_reg);
7071 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007072 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007073 }
7074 AddSlowPath(slow_path);
7075
7076 // if (rb_state == ReadBarrier::GrayState())
7077 // ref = ReadBarrier::Mark(ref);
7078 // Given the numeric representation, it's enough to check the low bit of the
7079 // rb_state. We do that by shifting the bit into the sign bit (31) and
7080 // performing a branch on less than zero.
7081 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7082 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7083 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7084 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7085 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7086 __ Bind(slow_path->GetExitLabel());
7087}
7088
7089void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7090 Location out,
7091 Location ref,
7092 Location obj,
7093 uint32_t offset,
7094 Location index) {
7095 DCHECK(kEmitCompilerReadBarrier);
7096
7097 // Insert a slow path based read barrier *after* the reference load.
7098 //
7099 // If heap poisoning is enabled, the unpoisoning of the loaded
7100 // reference will be carried out by the runtime within the slow
7101 // path.
7102 //
7103 // Note that `ref` currently does not get unpoisoned (when heap
7104 // poisoning is enabled), which is alright as the `ref` argument is
7105 // not used by the artReadBarrierSlow entry point.
7106 //
7107 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007108 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007109 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7110 AddSlowPath(slow_path);
7111
7112 __ B(slow_path->GetEntryLabel());
7113 __ Bind(slow_path->GetExitLabel());
7114}
7115
7116void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7117 Location out,
7118 Location ref,
7119 Location obj,
7120 uint32_t offset,
7121 Location index) {
7122 if (kEmitCompilerReadBarrier) {
7123 // Baker's read barriers shall be handled by the fast path
7124 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7125 DCHECK(!kUseBakerReadBarrier);
7126 // If heap poisoning is enabled, unpoisoning will be taken care of
7127 // by the runtime within the slow path.
7128 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7129 } else if (kPoisonHeapReferences) {
7130 __ UnpoisonHeapReference(out.AsRegister<Register>());
7131 }
7132}
7133
7134void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7135 Location out,
7136 Location root) {
7137 DCHECK(kEmitCompilerReadBarrier);
7138
7139 // Insert a slow path based read barrier *after* the GC root load.
7140 //
7141 // Note that GC roots are not affected by heap poisoning, so we do
7142 // not need to do anything special for this here.
7143 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007144 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007145 AddSlowPath(slow_path);
7146
7147 __ B(slow_path->GetEntryLabel());
7148 __ Bind(slow_path->GetExitLabel());
7149}
7150
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007151void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007152 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7153 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007154 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007155 switch (type_check_kind) {
7156 case TypeCheckKind::kExactCheck:
7157 case TypeCheckKind::kAbstractClassCheck:
7158 case TypeCheckKind::kClassHierarchyCheck:
7159 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007160 call_kind =
7161 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007162 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007163 break;
7164 case TypeCheckKind::kArrayCheck:
7165 case TypeCheckKind::kUnresolvedCheck:
7166 case TypeCheckKind::kInterfaceCheck:
7167 call_kind = LocationSummary::kCallOnSlowPath;
7168 break;
7169 }
7170
Vladimir Markoca6fff82017-10-03 14:49:14 +01007171 LocationSummary* locations =
7172 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007173 if (baker_read_barrier_slow_path) {
7174 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7175 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176 locations->SetInAt(0, Location::RequiresRegister());
7177 locations->SetInAt(1, Location::RequiresRegister());
7178 // The output does overlap inputs.
7179 // Note that TypeCheckSlowPathMIPS uses this register too.
7180 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007181 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007182}
7183
7184void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007185 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007186 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007187 Location obj_loc = locations->InAt(0);
7188 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007189 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007190 Location out_loc = locations->Out();
7191 Register out = out_loc.AsRegister<Register>();
7192 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7193 DCHECK_LE(num_temps, 1u);
7194 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007195 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7196 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7197 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7198 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007200 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007201
7202 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007203 // Avoid this check if we know `obj` is not null.
7204 if (instruction->MustDoNullCheck()) {
7205 __ Move(out, ZERO);
7206 __ Beqz(obj, &done);
7207 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007208
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007209 switch (type_check_kind) {
7210 case TypeCheckKind::kExactCheck: {
7211 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007212 GenerateReferenceLoadTwoRegisters(instruction,
7213 out_loc,
7214 obj_loc,
7215 class_offset,
7216 maybe_temp_loc,
7217 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007218 // Classes must be equal for the instanceof to succeed.
7219 __ Xor(out, out, cls);
7220 __ Sltiu(out, out, 1);
7221 break;
7222 }
7223
7224 case TypeCheckKind::kAbstractClassCheck: {
7225 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007226 GenerateReferenceLoadTwoRegisters(instruction,
7227 out_loc,
7228 obj_loc,
7229 class_offset,
7230 maybe_temp_loc,
7231 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007232 // If the class is abstract, we eagerly fetch the super class of the
7233 // object to avoid doing a comparison we know will fail.
7234 MipsLabel loop;
7235 __ Bind(&loop);
7236 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007237 GenerateReferenceLoadOneRegister(instruction,
7238 out_loc,
7239 super_offset,
7240 maybe_temp_loc,
7241 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007242 // If `out` is null, we use it for the result, and jump to `done`.
7243 __ Beqz(out, &done);
7244 __ Bne(out, cls, &loop);
7245 __ LoadConst32(out, 1);
7246 break;
7247 }
7248
7249 case TypeCheckKind::kClassHierarchyCheck: {
7250 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007251 GenerateReferenceLoadTwoRegisters(instruction,
7252 out_loc,
7253 obj_loc,
7254 class_offset,
7255 maybe_temp_loc,
7256 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007257 // Walk over the class hierarchy to find a match.
7258 MipsLabel loop, success;
7259 __ Bind(&loop);
7260 __ Beq(out, cls, &success);
7261 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007262 GenerateReferenceLoadOneRegister(instruction,
7263 out_loc,
7264 super_offset,
7265 maybe_temp_loc,
7266 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007267 __ Bnez(out, &loop);
7268 // If `out` is null, we use it for the result, and jump to `done`.
7269 __ B(&done);
7270 __ Bind(&success);
7271 __ LoadConst32(out, 1);
7272 break;
7273 }
7274
7275 case TypeCheckKind::kArrayObjectCheck: {
7276 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007277 GenerateReferenceLoadTwoRegisters(instruction,
7278 out_loc,
7279 obj_loc,
7280 class_offset,
7281 maybe_temp_loc,
7282 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007283 // Do an exact check.
7284 MipsLabel success;
7285 __ Beq(out, cls, &success);
7286 // Otherwise, we need to check that the object's class is a non-primitive array.
7287 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007288 GenerateReferenceLoadOneRegister(instruction,
7289 out_loc,
7290 component_offset,
7291 maybe_temp_loc,
7292 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007293 // If `out` is null, we use it for the result, and jump to `done`.
7294 __ Beqz(out, &done);
7295 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7296 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7297 __ Sltiu(out, out, 1);
7298 __ B(&done);
7299 __ Bind(&success);
7300 __ LoadConst32(out, 1);
7301 break;
7302 }
7303
7304 case TypeCheckKind::kArrayCheck: {
7305 // No read barrier since the slow path will retry upon failure.
7306 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007307 GenerateReferenceLoadTwoRegisters(instruction,
7308 out_loc,
7309 obj_loc,
7310 class_offset,
7311 maybe_temp_loc,
7312 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007313 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007314 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7315 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007316 codegen_->AddSlowPath(slow_path);
7317 __ Bne(out, cls, slow_path->GetEntryLabel());
7318 __ LoadConst32(out, 1);
7319 break;
7320 }
7321
7322 case TypeCheckKind::kUnresolvedCheck:
7323 case TypeCheckKind::kInterfaceCheck: {
7324 // Note that we indeed only call on slow path, but we always go
7325 // into the slow path for the unresolved and interface check
7326 // cases.
7327 //
7328 // We cannot directly call the InstanceofNonTrivial runtime
7329 // entry point without resorting to a type checking slow path
7330 // here (i.e. by calling InvokeRuntime directly), as it would
7331 // require to assign fixed registers for the inputs of this
7332 // HInstanceOf instruction (following the runtime calling
7333 // convention), which might be cluttered by the potential first
7334 // read barrier emission at the beginning of this method.
7335 //
7336 // TODO: Introduce a new runtime entry point taking the object
7337 // to test (instead of its class) as argument, and let it deal
7338 // with the read barrier issues. This will let us refactor this
7339 // case of the `switch` code as it was previously (with a direct
7340 // call to the runtime not using a type checking slow path).
7341 // This should also be beneficial for the other cases above.
7342 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007343 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7344 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007345 codegen_->AddSlowPath(slow_path);
7346 __ B(slow_path->GetEntryLabel());
7347 break;
7348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007349 }
7350
7351 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007352
7353 if (slow_path != nullptr) {
7354 __ Bind(slow_path->GetExitLabel());
7355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007356}
7357
7358void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007359 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007360 locations->SetOut(Location::ConstantLocation(constant));
7361}
7362
7363void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7364 // Will be generated at use site.
7365}
7366
7367void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007368 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007369 locations->SetOut(Location::ConstantLocation(constant));
7370}
7371
7372void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7373 // Will be generated at use site.
7374}
7375
7376void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7377 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7378 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7379}
7380
7381void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7382 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007383 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007384 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007385 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007386}
7387
7388void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7389 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7390 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007391 Location receiver = invoke->GetLocations()->InAt(0);
7392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007393 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007394
7395 // Set the hidden argument.
7396 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7397 invoke->GetDexMethodIndex());
7398
7399 // temp = object->GetClass();
7400 if (receiver.IsStackSlot()) {
7401 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7402 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7403 } else {
7404 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7405 }
7406 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007407 // Instead of simply (possibly) unpoisoning `temp` here, we should
7408 // emit a read barrier for the previous class reference load.
7409 // However this is not required in practice, as this is an
7410 // intermediate/temporary reference and because the current
7411 // concurrent copying collector keeps the from-space memory
7412 // intact/accessible until the end of the marking phase (the
7413 // concurrent copying collector may not in the future).
7414 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007415 __ LoadFromOffset(kLoadWord, temp, temp,
7416 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7417 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007418 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007419 // temp = temp->GetImtEntryAt(method_offset);
7420 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7421 // T9 = temp->GetEntryPoint();
7422 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7423 // T9();
7424 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007425 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007426 DCHECK(!codegen_->IsLeafMethod());
7427 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7428}
7429
7430void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007431 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7432 if (intrinsic.TryDispatch(invoke)) {
7433 return;
7434 }
7435
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007436 HandleInvoke(invoke);
7437}
7438
7439void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007440 // Explicit clinit checks triggered by static invokes must have been pruned by
7441 // art::PrepareForRegisterAllocation.
7442 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007443
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007444 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007445 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7446 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007447
Chris Larsen701566a2015-10-27 15:29:13 -07007448 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7449 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007450 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7451 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7452 }
Chris Larsen701566a2015-10-27 15:29:13 -07007453 return;
7454 }
7455
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007456 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007457
7458 // Add the extra input register if either the dex cache array base register
7459 // or the PC-relative base register for accessing literals is needed.
7460 if (has_extra_input) {
7461 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7462 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007463}
7464
Orion Hodsonac141392017-01-13 11:53:47 +00007465void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7466 HandleInvoke(invoke);
7467}
7468
7469void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7470 codegen_->GenerateInvokePolymorphicCall(invoke);
7471}
7472
Chris Larsen701566a2015-10-27 15:29:13 -07007473static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007474 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007475 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7476 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007477 return true;
7478 }
7479 return false;
7480}
7481
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007482HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007483 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007484 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007485 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007486 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007487 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007488 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007489 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007490 case HLoadString::LoadKind::kJitTableAddress:
7491 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007492 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007493 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007494 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007495 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007496 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007497 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007498}
7499
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007500HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7501 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007502 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007503 case HLoadClass::LoadKind::kInvalid:
7504 LOG(FATAL) << "UNREACHABLE";
7505 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007506 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007507 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007508 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007509 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007510 case HLoadClass::LoadKind::kBssEntry:
7511 DCHECK(!Runtime::Current()->UseJitCompilation());
7512 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007513 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007514 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007515 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007516 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007517 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007518 break;
7519 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007520 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007521}
7522
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007523Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7524 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007525 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007526 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007527 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7528 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7529 if (!invoke->GetLocations()->Intrinsified()) {
7530 return location.AsRegister<Register>();
7531 }
7532 // For intrinsics we allow any location, so it may be on the stack.
7533 if (!location.IsRegister()) {
7534 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7535 return temp;
7536 }
7537 // For register locations, check if the register was saved. If so, get it from the stack.
7538 // Note: There is a chance that the register was saved but not overwritten, so we could
7539 // save one load. However, since this is just an intrinsic slow path we prefer this
7540 // simple and more robust approach rather that trying to determine if that's the case.
7541 SlowPathCode* slow_path = GetCurrentSlowPath();
7542 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7543 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7544 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7545 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7546 return temp;
7547 }
7548 return location.AsRegister<Register>();
7549}
7550
Vladimir Markodc151b22015-10-15 18:02:30 +01007551HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7552 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007553 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007554 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007555}
7556
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007557void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7558 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007559 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007560 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007561 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7562 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007563 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007564 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7565 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007566 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7567 : ZERO;
7568
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007569 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007570 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007572 uint32_t offset =
7573 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007574 __ LoadFromOffset(kLoadWord,
7575 temp.AsRegister<Register>(),
7576 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007577 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007578 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007579 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007580 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007581 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007582 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007583 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7584 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007585 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7586 PcRelativePatchInfo* info_low =
7587 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007588 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007589 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7590 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007591 break;
7592 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007593 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7594 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7595 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007596 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007597 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007598 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007599 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7600 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007601 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007602 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7603 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007604 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007605 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007606 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7607 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7608 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 }
7610 }
7611
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007612 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007613 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007614 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007615 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007616 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7617 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007618 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619 T9,
7620 callee_method.AsRegister<Register>(),
7621 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007622 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007623 // T9()
7624 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007625 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007626 break;
7627 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007628 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7629
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007630 DCHECK(!IsLeafMethod());
7631}
7632
7633void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007634 // Explicit clinit checks triggered by static invokes must have been pruned by
7635 // art::PrepareForRegisterAllocation.
7636 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007637
7638 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7639 return;
7640 }
7641
7642 LocationSummary* locations = invoke->GetLocations();
7643 codegen_->GenerateStaticOrDirectCall(invoke,
7644 locations->HasTemps()
7645 ? locations->GetTemp(0)
7646 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007647}
7648
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007649void CodeGeneratorMIPS::GenerateVirtualCall(
7650 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007651 // Use the calling convention instead of the location of the receiver, as
7652 // intrinsics may have put the receiver in a different register. In the intrinsics
7653 // slow path, the arguments have been moved to the right place, so here we are
7654 // guaranteed that the receiver is the first register of the calling convention.
7655 InvokeDexCallingConvention calling_convention;
7656 Register receiver = calling_convention.GetRegisterAt(0);
7657
Chris Larsen3acee732015-11-18 13:31:08 -08007658 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007659 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7660 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7661 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007662 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007663
7664 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007665 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007666 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007667 // Instead of simply (possibly) unpoisoning `temp` here, we should
7668 // emit a read barrier for the previous class reference load.
7669 // However this is not required in practice, as this is an
7670 // intermediate/temporary reference and because the current
7671 // concurrent copying collector keeps the from-space memory
7672 // intact/accessible until the end of the marking phase (the
7673 // concurrent copying collector may not in the future).
7674 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007675 // temp = temp->GetMethodAt(method_offset);
7676 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7677 // T9 = temp->GetEntryPoint();
7678 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7679 // T9();
7680 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007681 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007682 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007683}
7684
7685void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7686 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7687 return;
7688 }
7689
7690 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007691 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007692}
7693
7694void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007695 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007696 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007697 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007698 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7699 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007700 return;
7701 }
Vladimir Marko41559982017-01-06 14:04:23 +00007702 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007703 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007704 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007705 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7706 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007707 ? LocationSummary::kCallOnSlowPath
7708 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007709 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007710 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7711 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7712 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007713 switch (load_kind) {
7714 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007715 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007716 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007717 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007718 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007719 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007720 break;
7721 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007722 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007723 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7724 codegen_->ClobberRA();
7725 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007726 break;
7727 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007728 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007729 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007730 locations->SetInAt(0, Location::RequiresRegister());
7731 break;
7732 default:
7733 break;
7734 }
7735 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007736 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7737 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7738 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007739 RegisterSet caller_saves = RegisterSet::Empty();
7740 InvokeRuntimeCallingConvention calling_convention;
7741 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7742 locations->SetCustomSlowPathCallerSaves(caller_saves);
7743 } else {
7744 // For non-Baker read barriers we have a temp-clobbering call.
7745 }
7746 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007747}
7748
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007749// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7750// move.
7751void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007752 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007753 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007754 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007755 return;
7756 }
Vladimir Marko41559982017-01-06 14:04:23 +00007757 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007758
Vladimir Marko41559982017-01-06 14:04:23 +00007759 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 Location out_loc = locations->Out();
7761 Register out = out_loc.AsRegister<Register>();
7762 Register base_or_current_method_reg;
7763 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007764 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007765 switch (load_kind) {
7766 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007767 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007768 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007769 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007770 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007771 base_or_current_method_reg =
7772 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007773 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007774 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007775 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007776 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7777 break;
7778 default:
7779 base_or_current_method_reg = ZERO;
7780 break;
7781 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007782
Alexey Frunze15958152017-02-09 19:08:30 -08007783 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7784 ? kWithoutReadBarrier
7785 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007786 bool generate_null_check = false;
7787 switch (load_kind) {
7788 case HLoadClass::LoadKind::kReferrersClass: {
7789 DCHECK(!cls->CanCallRuntime());
7790 DCHECK(!cls->MustGenerateClinitCheck());
7791 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7792 GenerateGcRootFieldLoad(cls,
7793 out_loc,
7794 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007795 ArtMethod::DeclaringClassOffset().Int32Value(),
7796 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007797 break;
7798 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007799 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007800 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007801 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007802 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007803 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007804 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7805 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007806 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7807 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007808 base_or_current_method_reg);
7809 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007810 break;
7811 }
7812 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007813 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007814 uint32_t address = dchecked_integral_cast<uint32_t>(
7815 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7816 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007817 if (isR6 || !has_irreducible_loops) {
7818 __ LoadLiteral(out,
7819 base_or_current_method_reg,
7820 codegen_->DeduplicateBootImageAddressLiteral(address));
7821 } else {
7822 __ LoadConst32(out, address);
7823 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007824 break;
7825 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007826 case HLoadClass::LoadKind::kBootImageClassTable: {
7827 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7828 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7829 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7830 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7831 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7832 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7833 out,
7834 base_or_current_method_reg);
7835 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7836 // Extract the reference from the slot data, i.e. clear the hash bits.
7837 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7838 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7839 if (masked_hash != 0) {
7840 __ Addiu(out, out, -masked_hash);
7841 }
7842 break;
7843 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007844 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007845 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7846 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007847 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7848 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007849 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007850 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007851 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007852 GenerateGcRootFieldLoad(cls,
7853 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007854 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007855 /* placeholder */ 0x5678,
7856 read_barrier_option,
7857 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007858 generate_null_check = true;
7859 break;
7860 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007861 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007862 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7863 cls->GetTypeIndex(),
7864 cls->GetClass());
7865 bool reordering = __ SetReorder(false);
7866 __ Bind(&info->high_label);
7867 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007868 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007869 GenerateGcRootFieldLoad(cls,
7870 out_loc,
7871 out,
7872 /* placeholder */ 0x5678,
7873 read_barrier_option,
7874 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007875 break;
7876 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007877 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007878 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007879 LOG(FATAL) << "UNREACHABLE";
7880 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007881 }
7882
7883 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7884 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007885 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007886 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007887 codegen_->AddSlowPath(slow_path);
7888 if (generate_null_check) {
7889 __ Beqz(out, slow_path->GetEntryLabel());
7890 }
7891 if (cls->MustGenerateClinitCheck()) {
7892 GenerateClassInitializationCheck(slow_path, out);
7893 } else {
7894 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007895 }
7896 }
7897}
7898
7899static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007900 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007901}
7902
7903void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7904 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007905 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007906 locations->SetOut(Location::RequiresRegister());
7907}
7908
7909void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7910 Register out = load->GetLocations()->Out().AsRegister<Register>();
7911 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7912}
7913
7914void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007915 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007916}
7917
7918void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7919 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7920}
7921
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007922void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007923 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007924 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007925 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007926 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007927 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007928 switch (load_kind) {
7929 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007930 case HLoadString::LoadKind::kBootImageAddress:
7931 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007932 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007933 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007934 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007935 break;
7936 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007937 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007938 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7939 codegen_->ClobberRA();
7940 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007941 break;
7942 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007943 FALLTHROUGH_INTENDED;
7944 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007945 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007946 locations->SetInAt(0, Location::RequiresRegister());
7947 break;
7948 default:
7949 break;
7950 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007951 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007952 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007953 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007954 } else {
7955 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007956 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7957 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7958 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007959 RegisterSet caller_saves = RegisterSet::Empty();
7960 InvokeRuntimeCallingConvention calling_convention;
7961 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7962 locations->SetCustomSlowPathCallerSaves(caller_saves);
7963 } else {
7964 // For non-Baker read barriers we have a temp-clobbering call.
7965 }
7966 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007967 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007968}
7969
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007970// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7971// move.
7972void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007973 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007974 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007975 Location out_loc = locations->Out();
7976 Register out = out_loc.AsRegister<Register>();
7977 Register base_or_current_method_reg;
7978 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007979 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007980 switch (load_kind) {
7981 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007982 case HLoadString::LoadKind::kBootImageAddress:
7983 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007984 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007985 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007986 base_or_current_method_reg =
7987 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007988 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007989 default:
7990 base_or_current_method_reg = ZERO;
7991 break;
7992 }
7993
7994 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007995 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007996 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007997 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007998 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007999 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8000 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008001 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8002 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008003 base_or_current_method_reg);
8004 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008005 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008006 }
8007 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008008 uint32_t address = dchecked_integral_cast<uint32_t>(
8009 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8010 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008011 if (isR6 || !has_irreducible_loops) {
8012 __ LoadLiteral(out,
8013 base_or_current_method_reg,
8014 codegen_->DeduplicateBootImageAddressLiteral(address));
8015 } else {
8016 __ LoadConst32(out, address);
8017 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008018 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008019 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008020 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008021 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008022 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008023 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008024 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8025 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008026 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8027 out,
8028 base_or_current_method_reg);
8029 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8030 return;
8031 }
8032 case HLoadString::LoadKind::kBssEntry: {
8033 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8034 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8035 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8036 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8037 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008038 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008039 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008040 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008041 GenerateGcRootFieldLoad(load,
8042 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008043 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008044 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008045 kCompilerReadBarrierOption,
8046 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008047 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008048 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008049 codegen_->AddSlowPath(slow_path);
8050 __ Beqz(out, slow_path->GetEntryLabel());
8051 __ Bind(slow_path->GetExitLabel());
8052 return;
8053 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008054 case HLoadString::LoadKind::kJitTableAddress: {
8055 CodeGeneratorMIPS::JitPatchInfo* info =
8056 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8057 load->GetStringIndex(),
8058 load->GetString());
8059 bool reordering = __ SetReorder(false);
8060 __ Bind(&info->high_label);
8061 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008062 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008063 GenerateGcRootFieldLoad(load,
8064 out_loc,
8065 out,
8066 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008067 kCompilerReadBarrierOption,
8068 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008069 return;
8070 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008071 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008072 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008073 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008074
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008075 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008076 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008077 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008078 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008079 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008080 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8081 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008082}
8083
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008084void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008085 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008086 locations->SetOut(Location::ConstantLocation(constant));
8087}
8088
8089void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8090 // Will be generated at use site.
8091}
8092
8093void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008094 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8095 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008096 InvokeRuntimeCallingConvention calling_convention;
8097 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8098}
8099
8100void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8101 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008102 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008103 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8104 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008105 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008106 }
8107 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8108}
8109
8110void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8111 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008112 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008113 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008114 case DataType::Type::kInt32:
8115 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008116 locations->SetInAt(0, Location::RequiresRegister());
8117 locations->SetInAt(1, Location::RequiresRegister());
8118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8119 break;
8120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008121 case DataType::Type::kFloat32:
8122 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008123 locations->SetInAt(0, Location::RequiresFpuRegister());
8124 locations->SetInAt(1, Location::RequiresFpuRegister());
8125 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8126 break;
8127
8128 default:
8129 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8130 }
8131}
8132
8133void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008134 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008135 LocationSummary* locations = instruction->GetLocations();
8136 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8137
8138 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008139 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008140 Register dst = locations->Out().AsRegister<Register>();
8141 Register lhs = locations->InAt(0).AsRegister<Register>();
8142 Register rhs = locations->InAt(1).AsRegister<Register>();
8143
8144 if (isR6) {
8145 __ MulR6(dst, lhs, rhs);
8146 } else {
8147 __ MulR2(dst, lhs, rhs);
8148 }
8149 break;
8150 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008151 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008152 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8153 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8154 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8155 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8156 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8157 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8158
8159 // Extra checks to protect caused by the existance of A1_A2.
8160 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8161 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8162 DCHECK_NE(dst_high, lhs_low);
8163 DCHECK_NE(dst_high, rhs_low);
8164
8165 // A_B * C_D
8166 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8167 // dst_lo: [ low(B*D) ]
8168 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8169
8170 if (isR6) {
8171 __ MulR6(TMP, lhs_high, rhs_low);
8172 __ MulR6(dst_high, lhs_low, rhs_high);
8173 __ Addu(dst_high, dst_high, TMP);
8174 __ MuhuR6(TMP, lhs_low, rhs_low);
8175 __ Addu(dst_high, dst_high, TMP);
8176 __ MulR6(dst_low, lhs_low, rhs_low);
8177 } else {
8178 __ MulR2(TMP, lhs_high, rhs_low);
8179 __ MulR2(dst_high, lhs_low, rhs_high);
8180 __ Addu(dst_high, dst_high, TMP);
8181 __ MultuR2(lhs_low, rhs_low);
8182 __ Mfhi(TMP);
8183 __ Addu(dst_high, dst_high, TMP);
8184 __ Mflo(dst_low);
8185 }
8186 break;
8187 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008188 case DataType::Type::kFloat32:
8189 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008190 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8191 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8192 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008193 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008194 __ MulS(dst, lhs, rhs);
8195 } else {
8196 __ MulD(dst, lhs, rhs);
8197 }
8198 break;
8199 }
8200 default:
8201 LOG(FATAL) << "Unexpected mul type " << type;
8202 }
8203}
8204
8205void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008207 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008208 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008209 case DataType::Type::kInt32:
8210 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008211 locations->SetInAt(0, Location::RequiresRegister());
8212 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8213 break;
8214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008215 case DataType::Type::kFloat32:
8216 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008217 locations->SetInAt(0, Location::RequiresFpuRegister());
8218 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8219 break;
8220
8221 default:
8222 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8223 }
8224}
8225
8226void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008227 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008228 LocationSummary* locations = instruction->GetLocations();
8229
8230 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008231 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008232 Register dst = locations->Out().AsRegister<Register>();
8233 Register src = locations->InAt(0).AsRegister<Register>();
8234 __ Subu(dst, ZERO, src);
8235 break;
8236 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008237 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008238 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8239 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8240 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8241 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8242 __ Subu(dst_low, ZERO, src_low);
8243 __ Sltu(TMP, ZERO, dst_low);
8244 __ Subu(dst_high, ZERO, src_high);
8245 __ Subu(dst_high, dst_high, TMP);
8246 break;
8247 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008248 case DataType::Type::kFloat32:
8249 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008250 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8251 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008252 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008253 __ NegS(dst, src);
8254 } else {
8255 __ NegD(dst, src);
8256 }
8257 break;
8258 }
8259 default:
8260 LOG(FATAL) << "Unexpected neg type " << type;
8261 }
8262}
8263
8264void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008265 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8266 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008267 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008268 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008269 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8270 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008271}
8272
8273void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008274 // Note: if heap poisoning is enabled, the entry point takes care
8275 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008276 QuickEntrypointEnum entrypoint =
8277 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8278 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008279 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008280 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008281}
8282
8283void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008284 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8285 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008286 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008287 if (instruction->IsStringAlloc()) {
8288 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8289 } else {
8290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008291 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008292 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008293}
8294
8295void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008296 // Note: if heap poisoning is enabled, the entry point takes care
8297 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008298 if (instruction->IsStringAlloc()) {
8299 // String is allocated through StringFactory. Call NewEmptyString entry point.
8300 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008301 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008302 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8303 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8304 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008305 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008306 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8307 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008308 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008309 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008310 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008311}
8312
8313void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008315 locations->SetInAt(0, Location::RequiresRegister());
8316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8317}
8318
8319void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008320 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008321 LocationSummary* locations = instruction->GetLocations();
8322
8323 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008324 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008325 Register dst = locations->Out().AsRegister<Register>();
8326 Register src = locations->InAt(0).AsRegister<Register>();
8327 __ Nor(dst, src, ZERO);
8328 break;
8329 }
8330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008331 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008332 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8333 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8334 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8335 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8336 __ Nor(dst_high, src_high, ZERO);
8337 __ Nor(dst_low, src_low, ZERO);
8338 break;
8339 }
8340
8341 default:
8342 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8343 }
8344}
8345
8346void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008347 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008348 locations->SetInAt(0, Location::RequiresRegister());
8349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8350}
8351
8352void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8353 LocationSummary* locations = instruction->GetLocations();
8354 __ Xori(locations->Out().AsRegister<Register>(),
8355 locations->InAt(0).AsRegister<Register>(),
8356 1);
8357}
8358
8359void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008360 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8361 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008362}
8363
Calin Juravle2ae48182016-03-16 14:05:09 +00008364void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8365 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 return;
8367 }
8368 Location obj = instruction->GetLocations()->InAt(0);
8369
8370 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008371 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008372}
8373
Calin Juravle2ae48182016-03-16 14:05:09 +00008374void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008375 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008376 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377
8378 Location obj = instruction->GetLocations()->InAt(0);
8379
8380 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8381}
8382
8383void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008384 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008385}
8386
8387void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8388 HandleBinaryOp(instruction);
8389}
8390
8391void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8392 HandleBinaryOp(instruction);
8393}
8394
8395void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8396 LOG(FATAL) << "Unreachable";
8397}
8398
8399void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008400 if (instruction->GetNext()->IsSuspendCheck() &&
8401 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8402 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8403 // The back edge will generate the suspend check.
8404 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8405 }
8406
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008407 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8408}
8409
8410void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008411 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008412 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8413 if (location.IsStackSlot()) {
8414 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8415 } else if (location.IsDoubleStackSlot()) {
8416 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8417 }
8418 locations->SetOut(location);
8419}
8420
8421void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8422 ATTRIBUTE_UNUSED) {
8423 // Nothing to do, the parameter is already at its location.
8424}
8425
8426void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8427 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008428 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008429 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8430}
8431
8432void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8433 ATTRIBUTE_UNUSED) {
8434 // Nothing to do, the method is already at its location.
8435}
8436
8437void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008438 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008439 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008440 locations->SetInAt(i, Location::Any());
8441 }
8442 locations->SetOut(Location::Any());
8443}
8444
8445void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8446 LOG(FATAL) << "Unreachable";
8447}
8448
8449void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008450 DataType::Type type = rem->GetResultType();
8451 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8452 ? LocationSummary::kNoCall
8453 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008454 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008455
8456 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008457 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008458 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008459 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8461 break;
8462
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008463 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008464 InvokeRuntimeCallingConvention calling_convention;
8465 locations->SetInAt(0, Location::RegisterPairLocation(
8466 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8467 locations->SetInAt(1, Location::RegisterPairLocation(
8468 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8469 locations->SetOut(calling_convention.GetReturnLocation(type));
8470 break;
8471 }
8472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008473 case DataType::Type::kFloat32:
8474 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475 InvokeRuntimeCallingConvention calling_convention;
8476 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8477 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8478 locations->SetOut(calling_convention.GetReturnLocation(type));
8479 break;
8480 }
8481
8482 default:
8483 LOG(FATAL) << "Unexpected rem type " << type;
8484 }
8485}
8486
8487void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008488 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008489
8490 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008491 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008492 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008493 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008495 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8497 break;
8498 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008499 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008500 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008501 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008502 break;
8503 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008504 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008505 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008506 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008507 break;
8508 }
8509 default:
8510 LOG(FATAL) << "Unexpected rem type " << type;
8511 }
8512}
8513
Igor Murashkind01745e2017-04-05 16:40:31 -07008514void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8515 constructor_fence->SetLocations(nullptr);
8516}
8517
8518void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8519 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8520 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8521}
8522
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008523void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8524 memory_barrier->SetLocations(nullptr);
8525}
8526
8527void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8528 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8529}
8530
8531void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008532 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008533 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008534 locations->SetInAt(0, MipsReturnLocation(return_type));
8535}
8536
8537void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8538 codegen_->GenerateFrameExit();
8539}
8540
8541void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8542 ret->SetLocations(nullptr);
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8546 codegen_->GenerateFrameExit();
8547}
8548
Alexey Frunze92d90602015-12-18 18:16:36 -08008549void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8550 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008551}
8552
Alexey Frunze92d90602015-12-18 18:16:36 -08008553void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8554 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008555}
8556
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008557void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8558 HandleShift(shl);
8559}
8560
8561void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8562 HandleShift(shl);
8563}
8564
8565void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8566 HandleShift(shr);
8567}
8568
8569void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8570 HandleShift(shr);
8571}
8572
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008573void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8574 HandleBinaryOp(instruction);
8575}
8576
8577void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8578 HandleBinaryOp(instruction);
8579}
8580
8581void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8582 HandleFieldGet(instruction, instruction->GetFieldInfo());
8583}
8584
8585void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8586 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8587}
8588
8589void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8590 HandleFieldSet(instruction, instruction->GetFieldInfo());
8591}
8592
8593void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008594 HandleFieldSet(instruction,
8595 instruction->GetFieldInfo(),
8596 instruction->GetDexPc(),
8597 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008598}
8599
8600void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8601 HUnresolvedInstanceFieldGet* instruction) {
8602 FieldAccessCallingConventionMIPS calling_convention;
8603 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8604 instruction->GetFieldType(),
8605 calling_convention);
8606}
8607
8608void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8609 HUnresolvedInstanceFieldGet* instruction) {
8610 FieldAccessCallingConventionMIPS calling_convention;
8611 codegen_->GenerateUnresolvedFieldAccess(instruction,
8612 instruction->GetFieldType(),
8613 instruction->GetFieldIndex(),
8614 instruction->GetDexPc(),
8615 calling_convention);
8616}
8617
8618void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8619 HUnresolvedInstanceFieldSet* instruction) {
8620 FieldAccessCallingConventionMIPS calling_convention;
8621 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8622 instruction->GetFieldType(),
8623 calling_convention);
8624}
8625
8626void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8627 HUnresolvedInstanceFieldSet* instruction) {
8628 FieldAccessCallingConventionMIPS calling_convention;
8629 codegen_->GenerateUnresolvedFieldAccess(instruction,
8630 instruction->GetFieldType(),
8631 instruction->GetFieldIndex(),
8632 instruction->GetDexPc(),
8633 calling_convention);
8634}
8635
8636void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8637 HUnresolvedStaticFieldGet* instruction) {
8638 FieldAccessCallingConventionMIPS calling_convention;
8639 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8640 instruction->GetFieldType(),
8641 calling_convention);
8642}
8643
8644void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8645 HUnresolvedStaticFieldGet* instruction) {
8646 FieldAccessCallingConventionMIPS calling_convention;
8647 codegen_->GenerateUnresolvedFieldAccess(instruction,
8648 instruction->GetFieldType(),
8649 instruction->GetFieldIndex(),
8650 instruction->GetDexPc(),
8651 calling_convention);
8652}
8653
8654void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8655 HUnresolvedStaticFieldSet* instruction) {
8656 FieldAccessCallingConventionMIPS calling_convention;
8657 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8658 instruction->GetFieldType(),
8659 calling_convention);
8660}
8661
8662void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8663 HUnresolvedStaticFieldSet* instruction) {
8664 FieldAccessCallingConventionMIPS calling_convention;
8665 codegen_->GenerateUnresolvedFieldAccess(instruction,
8666 instruction->GetFieldType(),
8667 instruction->GetFieldIndex(),
8668 instruction->GetDexPc(),
8669 calling_convention);
8670}
8671
8672void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008673 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8674 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008675 // In suspend check slow path, usually there are no caller-save registers at all.
8676 // If SIMD instructions are present, however, we force spilling all live SIMD
8677 // registers in full width (since the runtime only saves/restores lower part).
8678 locations->SetCustomSlowPathCallerSaves(
8679 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008680}
8681
8682void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8683 HBasicBlock* block = instruction->GetBlock();
8684 if (block->GetLoopInformation() != nullptr) {
8685 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8686 // The back edge will generate the suspend check.
8687 return;
8688 }
8689 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8690 // The goto will generate the suspend check.
8691 return;
8692 }
8693 GenerateSuspendCheck(instruction, nullptr);
8694}
8695
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008696void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008697 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8698 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008699 InvokeRuntimeCallingConvention calling_convention;
8700 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8701}
8702
8703void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008704 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008705 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8706}
8707
8708void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008709 DataType::Type input_type = conversion->GetInputType();
8710 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008711 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8712 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008713 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008715 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8716 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008717 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8718 }
8719
8720 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008721 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008722 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8723 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008724 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725 }
8726
Vladimir Markoca6fff82017-10-03 14:49:14 +01008727 LocationSummary* locations =
8728 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008729
8730 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008731 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008732 locations->SetInAt(0, Location::RequiresFpuRegister());
8733 } else {
8734 locations->SetInAt(0, Location::RequiresRegister());
8735 }
8736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008737 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008738 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8739 } else {
8740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8741 }
8742 } else {
8743 InvokeRuntimeCallingConvention calling_convention;
8744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008745 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8747 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008748 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008749 locations->SetInAt(0, Location::RegisterPairLocation(
8750 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8751 }
8752
8753 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8754 }
8755}
8756
8757void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8758 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008759 DataType::Type result_type = conversion->GetResultType();
8760 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008761 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008762 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008763
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008764 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8765 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008766
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008767 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008768 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8769 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8770 Register src = locations->InAt(0).AsRegister<Register>();
8771
Alexey Frunzea871ef12016-06-27 15:20:11 -07008772 if (dst_low != src) {
8773 __ Move(dst_low, src);
8774 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008775 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008776 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008777 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008778 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008779 ? locations->InAt(0).AsRegisterPairLow<Register>()
8780 : locations->InAt(0).AsRegister<Register>();
8781
8782 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008783 case DataType::Type::kUint8:
8784 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008785 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008786 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008787 if (has_sign_extension) {
8788 __ Seb(dst, src);
8789 } else {
8790 __ Sll(dst, src, 24);
8791 __ Sra(dst, dst, 24);
8792 }
8793 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008794 case DataType::Type::kUint16:
8795 __ Andi(dst, src, 0xFFFF);
8796 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008797 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008798 if (has_sign_extension) {
8799 __ Seh(dst, src);
8800 } else {
8801 __ Sll(dst, src, 16);
8802 __ Sra(dst, dst, 16);
8803 }
8804 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008805 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008806 if (dst != src) {
8807 __ Move(dst, src);
8808 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008809 break;
8810
8811 default:
8812 LOG(FATAL) << "Unexpected type conversion from " << input_type
8813 << " to " << result_type;
8814 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008815 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8816 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008817 if (isR6) {
8818 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8819 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8820 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8821 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8822 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8823 __ Mtc1(src_low, FTMP);
8824 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008825 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008826 __ Cvtsl(dst, FTMP);
8827 } else {
8828 __ Cvtdl(dst, FTMP);
8829 }
8830 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008831 QuickEntrypointEnum entrypoint =
8832 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008833 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008834 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008835 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8836 } else {
8837 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8838 }
8839 }
8840 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008841 Register src = locations->InAt(0).AsRegister<Register>();
8842 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8843 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008844 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008845 __ Cvtsw(dst, FTMP);
8846 } else {
8847 __ Cvtdw(dst, FTMP);
8848 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008849 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008850 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8851 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008852
8853 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8854 // value of the output type if the input is outside of the range after the truncation or
8855 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8856 // results. This matches the desired float/double-to-int/long conversion exactly.
8857 //
8858 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8859 // value when the input is either a NaN or is outside of the range of the output type
8860 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8861 // the same result.
8862 //
8863 // The code takes care of the different behaviors by first comparing the input to the
8864 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8865 // If the input is greater than or equal to the minimum, it procedes to the truncate
8866 // instruction, which will handle such an input the same way irrespective of NAN2008.
8867 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8868 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008869 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008870 if (isR6) {
8871 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8872 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8873 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8874 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8875 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008877 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008878 __ TruncLS(FTMP, src);
8879 } else {
8880 __ TruncLD(FTMP, src);
8881 }
8882 __ Mfc1(dst_low, FTMP);
8883 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008884 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008885 QuickEntrypointEnum entrypoint =
8886 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008887 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008888 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008889 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8890 } else {
8891 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8892 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008893 }
8894 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008895 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8896 Register dst = locations->Out().AsRegister<Register>();
8897 MipsLabel truncate;
8898 MipsLabel done;
8899
Lena Djokicf4e23a82017-05-09 15:43:45 +02008900 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008901 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008902 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8903 __ LoadConst32(TMP, min_val);
8904 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008905 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008906 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8907 __ LoadConst32(TMP, High32Bits(min_val));
8908 __ Mtc1(ZERO, FTMP);
8909 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008910 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008911
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008912 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008913 __ ColeS(0, FTMP, src);
8914 } else {
8915 __ ColeD(0, FTMP, src);
8916 }
8917 __ Bc1t(0, &truncate);
8918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008919 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008920 __ CeqS(0, src, src);
8921 } else {
8922 __ CeqD(0, src, src);
8923 }
8924 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8925 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008926
8927 __ B(&done);
8928
8929 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008930 }
8931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008932 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008933 __ TruncWS(FTMP, src);
8934 } else {
8935 __ TruncWD(FTMP, src);
8936 }
8937 __ Mfc1(dst, FTMP);
8938
Lena Djokicf4e23a82017-05-09 15:43:45 +02008939 if (!isR6) {
8940 __ Bind(&done);
8941 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008942 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008943 } else if (DataType::IsFloatingPointType(result_type) &&
8944 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008945 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8946 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008947 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008948 __ Cvtsd(dst, src);
8949 } else {
8950 __ Cvtds(dst, src);
8951 }
8952 } else {
8953 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8954 << " to " << result_type;
8955 }
8956}
8957
8958void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8959 HandleShift(ushr);
8960}
8961
8962void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8963 HandleShift(ushr);
8964}
8965
8966void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8967 HandleBinaryOp(instruction);
8968}
8969
8970void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8971 HandleBinaryOp(instruction);
8972}
8973
8974void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8975 // Nothing to do, this should be removed during prepare for register allocator.
8976 LOG(FATAL) << "Unreachable";
8977}
8978
8979void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8980 // Nothing to do, this should be removed during prepare for register allocator.
8981 LOG(FATAL) << "Unreachable";
8982}
8983
8984void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008985 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008986}
8987
8988void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008989 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008990}
8991
8992void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008993 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008994}
8995
8996void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008997 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008998}
8999
9000void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009001 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009002}
9003
9004void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009005 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009006}
9007
9008void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009009 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009010}
9011
9012void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009013 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009014}
9015
9016void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009017 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009018}
9019
9020void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009021 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009022}
9023
9024void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009025 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009026}
9027
9028void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009029 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009030}
9031
9032void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009033 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009034}
9035
9036void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009037 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009038}
9039
9040void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009041 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009042}
9043
9044void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009045 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009046}
9047
9048void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009049 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009050}
9051
9052void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009053 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009054}
9055
9056void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009057 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009058}
9059
9060void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009061 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009062}
9063
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009064void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9065 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009066 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009067 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009068 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9069 uint32_t num_entries = switch_instr->GetNumEntries();
9070 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9071 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9072 // instruction to simulate PC-relative addressing when accessing the jump table.
9073 // NAL clobbers RA. Make sure RA is preserved.
9074 codegen_->ClobberRA();
9075 }
9076 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009077}
9078
Alexey Frunze96b66822016-09-10 02:32:44 -07009079void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9080 int32_t lower_bound,
9081 uint32_t num_entries,
9082 HBasicBlock* switch_block,
9083 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009084 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009085 Register temp_reg = TMP;
9086 __ Addiu32(temp_reg, value_reg, -lower_bound);
9087 // Jump to default if index is negative
9088 // Note: We don't check the case that index is positive while value < lower_bound, because in
9089 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9090 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9091
Alexey Frunze96b66822016-09-10 02:32:44 -07009092 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009093 // Jump to successors[0] if value == lower_bound.
9094 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9095 int32_t last_index = 0;
9096 for (; num_entries - last_index > 2; last_index += 2) {
9097 __ Addiu(temp_reg, temp_reg, -2);
9098 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9099 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9100 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9101 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9102 }
9103 if (num_entries - last_index == 2) {
9104 // The last missing case_value.
9105 __ Addiu(temp_reg, temp_reg, -1);
9106 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009107 }
9108
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009109 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009110 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009111 __ B(codegen_->GetLabelOf(default_block));
9112 }
9113}
9114
Alexey Frunze96b66822016-09-10 02:32:44 -07009115void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9116 Register constant_area,
9117 int32_t lower_bound,
9118 uint32_t num_entries,
9119 HBasicBlock* switch_block,
9120 HBasicBlock* default_block) {
9121 // Create a jump table.
9122 std::vector<MipsLabel*> labels(num_entries);
9123 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9124 for (uint32_t i = 0; i < num_entries; i++) {
9125 labels[i] = codegen_->GetLabelOf(successors[i]);
9126 }
9127 JumpTable* table = __ CreateJumpTable(std::move(labels));
9128
9129 // Is the value in range?
9130 __ Addiu32(TMP, value_reg, -lower_bound);
9131 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9132 __ Sltiu(AT, TMP, num_entries);
9133 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9134 } else {
9135 __ LoadConst32(AT, num_entries);
9136 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9137 }
9138
9139 // We are in the range of the table.
9140 // Load the target address from the jump table, indexing by the value.
9141 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009142 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009143 __ Lw(TMP, TMP, 0);
9144 // Compute the absolute target address by adding the table start address
9145 // (the table contains offsets to targets relative to its start).
9146 __ Addu(TMP, TMP, AT);
9147 // And jump.
9148 __ Jr(TMP);
9149 __ NopIfNoReordering();
9150}
9151
9152void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9153 int32_t lower_bound = switch_instr->GetStartValue();
9154 uint32_t num_entries = switch_instr->GetNumEntries();
9155 LocationSummary* locations = switch_instr->GetLocations();
9156 Register value_reg = locations->InAt(0).AsRegister<Register>();
9157 HBasicBlock* switch_block = switch_instr->GetBlock();
9158 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9159
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009160 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009161 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009162 //
9163 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9164 // to access the jump table and it is implemented by changing HPackedSwitch to
9165 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9166 // VisitMipsPackedSwitch()).
9167 //
9168 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9169 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9170 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009171 GenTableBasedPackedSwitch(value_reg,
9172 ZERO,
9173 lower_bound,
9174 num_entries,
9175 switch_block,
9176 default_block);
9177 } else {
9178 GenPackedSwitchWithCompares(value_reg,
9179 lower_bound,
9180 num_entries,
9181 switch_block,
9182 default_block);
9183 }
9184}
9185
9186void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009188 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009189 locations->SetInAt(0, Location::RequiresRegister());
9190 // Constant area pointer (HMipsComputeBaseMethodAddress).
9191 locations->SetInAt(1, Location::RequiresRegister());
9192}
9193
9194void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9195 int32_t lower_bound = switch_instr->GetStartValue();
9196 uint32_t num_entries = switch_instr->GetNumEntries();
9197 LocationSummary* locations = switch_instr->GetLocations();
9198 Register value_reg = locations->InAt(0).AsRegister<Register>();
9199 Register constant_area = locations->InAt(1).AsRegister<Register>();
9200 HBasicBlock* switch_block = switch_instr->GetBlock();
9201 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9202
9203 // This is an R2-only path. HPackedSwitch has been changed to
9204 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9205 // required to address the jump table relative to PC.
9206 GenTableBasedPackedSwitch(value_reg,
9207 constant_area,
9208 lower_bound,
9209 num_entries,
9210 switch_block,
9211 default_block);
9212}
9213
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009214void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9215 HMipsComputeBaseMethodAddress* insn) {
9216 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009217 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009218 locations->SetOut(Location::RequiresRegister());
9219}
9220
9221void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9222 HMipsComputeBaseMethodAddress* insn) {
9223 LocationSummary* locations = insn->GetLocations();
9224 Register reg = locations->Out().AsRegister<Register>();
9225
9226 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9227
9228 // Generate a dummy PC-relative call to obtain PC.
9229 __ Nal();
9230 // Grab the return address off RA.
9231 __ Move(reg, RA);
9232
9233 // Remember this offset (the obtained PC value) for later use with constant area.
9234 __ BindPcRelBaseLabel();
9235}
9236
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009237void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9238 // The trampoline uses the same calling convention as dex calling conventions,
9239 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9240 // the method_idx.
9241 HandleInvoke(invoke);
9242}
9243
9244void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9245 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9246}
9247
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009248void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9249 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009250 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009251 locations->SetInAt(0, Location::RequiresRegister());
9252 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009253}
9254
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009255void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9256 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009257 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009258 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009259 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009260 __ LoadFromOffset(kLoadWord,
9261 locations->Out().AsRegister<Register>(),
9262 locations->InAt(0).AsRegister<Register>(),
9263 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009264 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009265 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009266 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009267 __ LoadFromOffset(kLoadWord,
9268 locations->Out().AsRegister<Register>(),
9269 locations->InAt(0).AsRegister<Register>(),
9270 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009271 __ LoadFromOffset(kLoadWord,
9272 locations->Out().AsRegister<Register>(),
9273 locations->Out().AsRegister<Register>(),
9274 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009275 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009276}
9277
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009278void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9279 ATTRIBUTE_UNUSED) {
9280 LOG(FATAL) << "Unreachable";
9281}
9282
9283void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9284 ATTRIBUTE_UNUSED) {
9285 LOG(FATAL) << "Unreachable";
9286}
9287
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009288#undef __
9289#undef QUICK_ENTRY_POINT
9290
9291} // namespace mips
9292} // namespace art